Use with openapi-ts
Here’s how you can use the openapi-ts library with the undb
platform in a Node.js environment to generate TypeScript types and create an API client for SvelteKit.
Step 1: Install Dependencies
First, install the necessary dependencies:
npm install openapi-typescript openapi-fetch
Step 2: Fetch the OpenAPI Schema and Generate TypeScript Types
Create a script (generate-types.js
) that fetches the OpenAPI schema from the undb platform and generates the corresponding TypeScript types:
import { writeFile } from 'fs/promises'import fetch from 'node-fetch'import openapiTS, { astToString } from 'openapi-typescript'
// Replace with your environment variables or configurationconst UNDB_API_KEY = process.env.UNDB_API_KEYconst PUBLIC_UNDB_HOST = process.env.PUBLIC_UNDB_HOSTconst UNDB_TEMPLATE_BASE_NAME = process.env.UNDB_TEMPLATE_BASE_NAMEconst UNDB_TEMPLATE_TABLE_NAME = process.env.UNDB_TEMPLATE_TABLE_NAME
async function generateTypes() { const response = await fetch( `${PUBLIC_UNDB_HOST}/api/bases/${UNDB_TEMPLATE_BASE_NAME}/tables/${UNDB_TEMPLATE_TABLE_NAME}/openapi.json`, { headers: { 'X-Undb-Api-Token': UNDB_API_KEY, }, } )
if (!response.ok) { throw new Error('Failed to fetch OpenAPI schema') }
const data = await response.json() const ast = await openapiTS(data) const content = astToString(ast)
await writeFile(`./src/lib/api/${UNDB_TEMPLATE_TABLE_NAME}.d.ts`, content) console.log('TypeScript types generated successfully')}
generateTypes().catch(console.error)
Step 3: Run the Type Generation Script
You can run this script using Node.js:
node generate-types.js
This will generate the TypeScript types in the ./src/lib/api/templates.d.ts
file.
Step 4: Use the Generated Types in Your SvelteKit Project
In your SvelteKit project, you can use the generated types and set up an API client as follows:
import { UNDB_API_KEY } from '$env/static/private'import { PUBLIC_UNDB_HOST } from '$env/static/public'import createClient from 'openapi-fetch'import type { components, paths } from './templates' // Importing generated types
const headers = new Headers()headers.set('X-Undb-Api-Token', UNDB_API_KEY)headers.set('User-Agent', 'undb-sveltekit-template')
export const templateClient = createClient<paths>({ baseUrl: PUBLIC_UNDB_HOST + '/api', headers,})
export type Template = components['schemas']['Record']
Step 5: Make API Requests Using the Client
Now, you can use the templateClient
to interact with your undb API. Here’s an example of how to fetch records:
// Example usage in a SvelteKit load functionconst templates = await templateClient.GET('/bases/templates/tables/templates/records', { fetch, cache: 'no-cache',})
if (templates.error) { console.error(templates.error)} else { console.log(templates.data)}
Summary
- Generate Types: The script fetches the OpenAPI spec from undb and generates TypeScript types.
- Create API Client: The generated types are used to create a type-safe API client.
- Use in SvelteKit: The API client can be used in SvelteKit to make requests with proper type checking.
This approach ensures that your API interactions are type-safe and well-integrated into your SvelteKit project.