Directus SDK Types Generator
Opinionated CLI tool and library to generate TypeScript types from Directus collections to use with the Directus SDK.
Generates the individual interfaces for the Directus collections and a Schema type that collects all the interfaces together for use with the Directus SDK.
Example
Generate the types from the CLI.
npx directus-sdk-typegen -u http://localhost:8055 -t your-token-here -o ./my-schema.ts// my-schema.ts
export interface Event {
/** @required */
id: string;
status?: 'draft' | 'active' | 'past';
sort?: number | null;
user_created?: DirectusUser | string | null;
date_created?: string | null;
user_updated?: DirectusUser | string | null;
date_updated?: string | null;
/** @description Name of the event. @required */
title: string;slug: string; / @description When does this event start? @required / startat: string; / @description When does this event end? / endat?: string | null; / @description Short description of the event. Used in SEO meta description. / description?: string | null; featuredimage?: DirectusFile | string | null; timezone?: string | null; location?: 'whereby' | 'youtube' | 'other' | null; locationurl?: string | null; locationyoutube?: string | null; recordingurl?: string | null; content?: string | null; locationwhereby?: any | null; customregistrationquestions?: EventCustomRegistrationQuestion[] | string[]; emails?: EventEmail[] | string[]; feedback?: EventFeedback[] | string[]; registrants?: EventRegistration[] | string[]; hosts?: EventHost[] | string[]; }
export interface Schema { events: Event[] }
Then use the generated types with the Directus SDK.
import type { Schema } from './my-schema';
import { createDirectus, rest } from '@directus/sdk';
const directus = createDirectus<Schema('http://localhost:8055').with(rest())
## Installation
You can run the CLI tool using `npx` or install it globally.
npx directus-sdk-typegen -u http://localhost:8055 -t your-token-here -o ./my-schema.ts
or
npm install -g directus-sdk-typegen