UNPKG

@goatlab/typesense

Version:

Modern TypeScript wrapper for Typesense search engine API

91 lines 3.56 kB
"use strict"; // Example demonstrating TypeScript type inference for Typesense // npx vitest run ./src/tests/type-inference-example.ts Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const index_1 = require("../index"); (0, vitest_1.describe)('Type Inference Example', () => { (0, vitest_1.it)('should demonstrate proper type inference', () => { // Define a strongly-typed collection const ProductCollection = (0, index_1.defineCollection)({ name: 'products', fields: [ { name: 'id', type: 'string' }, { name: 'title', type: 'string' }, { name: 'description', type: 'string', optional: true }, { name: 'price', type: 'float' }, { name: 'inStock', type: 'bool' }, { name: 'tags', type: 'string[]', optional: true }, ], }); // These should compile without errors const _validDoc1 = { title: 'Laptop', price: 999.99, inStock: true, }; const _validDoc2 = { title: 'Gaming Laptop', description: 'High-performance laptop', price: 1999.99, inStock: true, tags: ['gaming', 'performance'], }; // Create typed API const api = (0, index_1.createSchemaTypedApi)(ProductCollection)({ prefixUrl: 'http://localhost:8108', token: 'xyz', }); // Verify API structure (0, vitest_1.expect)(api).toBeDefined(); (0, vitest_1.expect)(api.documents).toBeDefined(); (0, vitest_1.expect)(typeof api.documents.insert).toBe('function'); // The following would be compile errors if uncommented: // ❌ Missing required field // const invalidDoc1: ProductDoc = { // price: 99.99, // inStock: true // } // ❌ Wrong type for price // const invalidDoc2: ProductDoc = { // title: 'Product', // price: '99.99', // should be number // inStock: true // } // ❌ Wrong type for optional field // const invalidDoc3: ProductDoc = { // title: 'Product', // price: 99.99, // inStock: true, // tags: 'single-tag' // should be string[] // } }); (0, vitest_1.it)('should handle complex field types', () => { const EventCollection = (0, index_1.defineCollection)({ name: 'events', fields: [ { name: 'id', type: 'string' }, { name: 'name', type: 'string' }, { name: 'timestamp', type: 'int64' }, { name: 'location', type: 'geopoint' }, { name: 'attendees', type: 'int32[]', optional: true }, { name: 'metadata', type: 'object', optional: true }, ], }); const validEvent = { name: 'Tech Conference', timestamp: Date.now(), location: [37.7749, -122.4194], }; const validEventWithOptionals = { name: 'Meetup', timestamp: Date.now(), location: [40.7128, -74.006], attendees: [10, 20, 30], metadata: { organizer: 'John', venue: 'Tech Hub' }, }; (0, vitest_1.expect)(validEvent).toBeDefined(); (0, vitest_1.expect)(validEventWithOptionals).toBeDefined(); }); }); //# sourceMappingURL=type-inference-example.js.map