0.8.6 • Published 9 months ago

directus-typeforge v0.8.6

Weekly downloads
-
License
-
Repository
-
Last release
9 months ago

Directus TypeForge

Directus TypeForge generates TypeScript definitions for Directus collections from an OpenAPI schema or a live Directus server. It supports both custom and system collections, providing accurate types for use with the Directus TypeScript SDK.

This tool is a fork and rewrite of elierotenberg/directus-typescript-gen.

View project on NPM | View project on GitHub

Features

  • Dynamic Generation: Get types from a static schema file or an active Directus instance
  • System Collections: Automatically handle Directus system collections with proper type definitions
  • Authentication Options: Support for email/password or bearer token authentication
  • Relationships: Represent collection relationships with proper TypeScript interfaces
  • Type Consistency: Generate singular type names (e.g., Event for events collection)
  • Special Field Types: Support for Directus-specific field types like datetime, JSON, and CSV
  • Customizable Output: Control the type name generation and references

Installation

Using npx

npx directus-typeforge [options]

Local Install

pnpm add -D directus-typeforge
npx directus-typeforge [options]

Global Install

pnpm add -g directus-typeforge
directus-typeforge [options]

Available Options

OptionAliasDescriptionDefault
--specFile-iPath to OpenAPI spec file-
--host-hDirectus host URL-
--email-eEmail for authentication-
--password-pPassword for authentication-
--token-tAdmin bearer token for authentication-
--outFile-oOutput file for TypeScript types-
--typeName-nRoot type nameApiCollections
--useTypeReferences-rUse interface references for relation typestrue
--useTypes-uUse 'type' instead of 'interface'false

only disable --useTypeReferences for very specific debugging reasons, it will make all of your relational types break.

Usage Examples

# From a Spec File
npx directus-typeforge -i directus.oas.json > schema.d.ts

# From a Live Server with Email/Password
npx directus-typeforge --host https://example.com --email user@example.com --password pass123 --outFile schema.d.ts

# From a Live Server with Admin Token
npx directus-typeforge --host https://example.com --token your-static-token --outFile schema.d.ts

# Custom Root Type Name
npx directus-typeforge -i directus.oas.json --typeName MySchema > schema.d.ts

# Generate using 'type' instead of 'interface'
npx directus-typeforge -i ./directus.oas.json -u -o ./types/schema.d.ts

Expected Output

export interface Event {
  id: string;
  title?: string;
  start_date?: string;
  event_registrations?: string[] | EventRegistration[];
}

export interface EventRegistration {
  id: string;
  event?: string | Event;
  user?: string | CustomDirectusUser;
}

export interface Ticket {
  id: string;
  date_created?: string;
  date_updated?: string;
  title?: string;
  event?: string | Event;
}

// system collections with custom fields
export interface DirectusUser {
  id: string;
  stripe_customer_id?: string;
  verification_token?: string;
  verification_url?: string;
}

export interface ApiCollections {
  events: Event[];
  tickets: Ticket[];
  directus_users: DirectusUser[];
}

Integration with Directus SDK

Use the generated types directly with the Directus SDK for stronger type-checking and autocompletion:

import type { ApiCollections } from "$lib/types/directus/api-collection";
import { DIRECTUS_URL } from "$env/static/private";
import { createDirectus, rest } from "@directus/sdk";

export const initDirectus = () => {
  return createDirectus<ApiCollections>(DIRECTUS_URL).with(rest());
};

How It Works

TypeForge analyzes your Directus schema through the OpenAPI specification, extracting collection information, field definitions, and relationship mappings:

  1. Schema Reading: Fetches the schema from a file or live Directus instance
  2. Type Name Generation: Creates appropriate interface names for collections
  3. Relationship Analysis: Identifies and properly types all relations
  4. Property Generation: Maps API fields to TypeScript types with proper nullability
  5. System Collection Handling: Identifies and processes Directus system collections
  6. Root Type Creation: Generates a root type that includes all collections

Caveats

  • System Collections: System collections are generated with standard names like DirectusUser and include ID fields plus any custom, user-created fields. Only system collections with custom fields are included in the output.
  • JSON Repeaters: JSON repeaters are typed as unknown since there's no standardized structure information in the OpenAPI schema.
  • Complex Field Types: Some specialized Directus field types are typed with string literals (e.g., 'datetime', 'json', 'csv') to match the Directus SDK's expected types.
  • Special Types: Certain system types like permissions and settings use unknown for complex nested objects where the structure is variable.

License

MIT

0.8.5

9 months ago

0.8.4

9 months ago

0.8.6

9 months ago

0.8.1

9 months ago

0.8.0

9 months ago

0.8.3

9 months ago

0.8.2

9 months ago

0.7.10

10 months ago

0.7.9

10 months ago

0.7.6

10 months ago

0.7.5

10 months ago

0.7.8

10 months ago

0.7.7

10 months ago

0.7.2

10 months ago

0.7.4

10 months ago

0.7.3

10 months ago

0.7.1

12 months ago

0.7.0

12 months ago

0.6.2

12 months ago

0.6.1

12 months ago

0.6.0

12 months ago

0.5.2

12 months ago

0.5.1

12 months ago

0.5.0

1 year ago