Directus SDK Typegen

Generate TypeScript types for your Directus SDK dynamically. Split between the package that generates the types based on you (using typescript compiler API) and a cli
Usage as a cli
Using the snapshot.yaml
generated by cli to get the snapshot
npx @ikerin/directus-typegen-cli snapshot --input snapshot.yaml --output src/directus-schema.ts
You can also install the script with its dependencies locally to run it with your package manager of choice:
# With yarn
yarn add --dev @ikerin/directus-typegen-cli
yarn directus-typegen-cli snapshot --input snapshot.yaml --output src/directus-schema.ts
# With bun
bun add --dev @ikerin/directus-typegen-cli
bun run directus-typegen-cli snapshot --input snapshot.yaml --output src/directus-schema.ts
You can utilize both stdin and stdout if this needs to be a part of a pipeline
cat snapshot.yaml | bun run directus-typegen-cli snapshot | bun run prettier --stdin-filepath directus-schema.ts > src/directus-schema.ts
You can also query a live server to get your types, which will take into account the user's permissions.
yarn directus-typegen-cli server --url http://localhost:8055 --user 'admin@example.com:admin-password' --output src/directus-schema.ts
More detailed docs can be found in the @ikerin/directus-typegen-cli package folder
Using the library
You can integrate the library (without the CLI dependencies) in you extensions.
bun add @ikerin/directus-typegen
simple.ts
import { generateTypesFromSnapshot } from '@ikerin/directus-typegen';
import { readFileSync } from 'fs';
import { join } from 'path';
// Get the contents of the snapshot
// We are using JSON for simplicity, usually you will use YAML
const snapshotPath = join(import.meta.dirname, 'snapshot.json');
const snapshot = JSON.parse(readFileSync(snapshotPath, 'utf-8'));
// This is the core of the operation
const types = generateTypesFromSnapshot(snapshot);
console.log(types);
More explanations in the package itself @ikerin/directus-typegen