57 lines
1.6 KiB
TypeScript
Executable File
57 lines
1.6 KiB
TypeScript
Executable File
import Typesense from 'typesense';
|
|
import { ref } from 'vue';
|
|
|
|
const documentData = ref({});
|
|
const error = ref("");
|
|
|
|
// Initialize the Typesense client
|
|
const client = new Typesense.Client({
|
|
'nodes': [{
|
|
'host': 'searchts.carpa.com', // For Typesense Cloud use xxx.a1.typesense.net
|
|
'port': 443, // For Typesense Cloud use 443
|
|
'protocol': 'https' // For Typesense Cloud use https
|
|
}],
|
|
'apiKey': '3KrmYlcirARCxG4AZPV5bnJgQD0qtoW0',
|
|
'connectionTimeoutSeconds': 2
|
|
});
|
|
|
|
export async function fetchDocumentBySlug( collectionName:string, slug: string ){
|
|
error.value = "";
|
|
documentData.value = {};
|
|
|
|
const nSlug = parseInt(slug);
|
|
|
|
if( Number.isFinite( nSlug ) ){
|
|
return fetchDocument( collectionName, slug );
|
|
}
|
|
|
|
try {
|
|
// Call the retrieve a document API directly
|
|
const doc = await client.collections(collectionName).documents().search({
|
|
'q': slug,
|
|
'query_by': 'slug'
|
|
})
|
|
|
|
//documentData.value = doc.hits?.[0]?.document;
|
|
return doc.hits?.[0]?.document;
|
|
|
|
} catch (err) {
|
|
console.error("Error fetching document:", err);
|
|
error.value = "Failed to fetch document.";
|
|
}
|
|
}
|
|
|
|
export async function fetchDocument( collectionName:string, documentId:string) {
|
|
error.value = "";
|
|
documentData.value = {};
|
|
try {
|
|
// Call the retrieve a document API directly
|
|
const doc = await client.collections(collectionName).documents(documentId).retrieve();
|
|
//documentData.value = doc;
|
|
|
|
return doc;
|
|
} catch (err) {
|
|
console.error("Error fetching document:", err);
|
|
error.value = "Failed to fetch document.";
|
|
}
|
|
}; |