typescript

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 8 Imported by: 0

README

typescript

TypeScript client code generator for Melange.

Responsibility

Generates type-safe TypeScript code from OpenFGA schemas, producing object type constants, relation constants, factory functions, and the names of the installed accessible functions.

Architecture Role

Registered in the generator registry as "typescript". Invoked by the CLI via melange generate client --runtime typescript.

Generated Output

The generator produces four TypeScript files:

types.ts

Contains type constants and union types:

  • ObjectTypes - Constant object with PascalCase keys mapping to object type strings
  • Relations - Constant object with PascalCase keys mapping to relation strings
  • ObjectType - Union type of all valid object types
  • Relation - Union type of all valid relations

Uses TypeScript's as const for type safety.

schema.ts

Contains factory functions and wildcard constructors:

  • Factory functions (camelCase) - e.g., user(id), repository(id)
  • Wildcard constructors (any + PascalCase) - e.g., anyUser(), anyRepository()

All functions return MelangeObject from the @pthm/melange runtime package.

sql.ts

Contains the exact names of the installed accessible functions of the selected relations, truncated names included:

  • AccessibleFunctions - { [type]: { [relation]: name } } as const
  • AccessibleByParentFunctions - { [type]: { [relation]: { [link]: name } } } as const, the parent-key forms

Keys are the schema's names, quoted when they are not identifiers. A map is {} as const when its function family is off.

index.ts

Re-exports all types and functions for clean imports.

Example Output

Given a schema with user and repository types:

// types.ts
export const ObjectTypes = {
  User: "user",
  Repository: "repository",
} as const;

export type ObjectType = (typeof ObjectTypes)[keyof typeof ObjectTypes];

// schema.ts
export function user(id: string): MelangeObject {
  return { type: ObjectTypes.User, id };
}

export function anyUser(): MelangeObject {
  return { type: ObjectTypes.User, id: '*' };
}

// index.ts
export { ObjectTypes, Relations } from './types.js';
export type { ObjectType, Relation } from './types.js';
export * from './schema.js';
export { AccessibleFunctions, AccessibleByParentFunctions } from './sql.js';

Configuration

Supports standard clientgen.Config options:

  • Exposure - The SQL API exposure the database is migrated with; decides the accessible names and, unless Relations is set, the relations
  • Relations - Relation patterns for the Relations constants
  • RelationFilter - Legacy prefix filter applied after them (e.g., "can_")
  • Version - Melange version for header comment
  • SourcePath - Schema file path for header comment

The Package and IDType config fields are not used (TypeScript doesn't have packages, IDs are always strings).

Naming Conventions

  • Object type constants: PascalCase (User, PullRequest)
  • Relation constants: PascalCase (CanRead, Owner)
  • Factory functions: camelCase (user, pullRequest)
  • Wildcard functions: any + PascalCase (anyUser, anyPullRequest)

Usage

# Generate to a directory
melange generate client --runtime typescript --schema schema.fga --output src/authz/

# Generate only permission relations
melange generate client --runtime typescript --schema schema.fga --output src/authz/ --include-relation 'can_*'

clients/typescript/test/fixtures/clientgen holds this generator's output for test/testutil/testdata/schema.fga, which the TypeScript unit tests type-check with tsc. TestGenerator_Fixtures keeps it current; regenerate it with just update-golden.

Documentation

Overview

Package typescript implements the TypeScript client code generator for melange.

This generator produces type-safe TypeScript code from authorization schemas, including object type constants, relation constants, factory functions, and the names of the installed accessible functions.

Generated code uses the @pthm/melange runtime package for type definitions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct{}

Generator implements clientgen.Generator for TypeScript.

func (*Generator) DefaultConfig

func (g *Generator) DefaultConfig() *clientgen.Config

DefaultConfig returns default configuration for TypeScript code generation.

func (*Generator) Generate

func (g *Generator) Generate(types []schema.TypeDefinition, cfg *clientgen.Config) (map[string][]byte, error)

Generate produces TypeScript client code from the given type definitions.

Returns a multi-file map with keys: "types.ts", "schema.ts", "sql.ts", "index.ts".

Generated code includes:

  • types.ts: ObjectType constants for every type, Relation constants for the relations the config selects (see clientgen.Config), and union types
  • schema.ts: Factory functions and wildcard constructors
  • sql.ts: AccessibleFunctions and AccessibleByParentFunctions, the names of the installed accessible functions of the selected relations, keyed by type, relation and link
  • index.ts: Re-exports for clean imports

It fails when two schema names map to one key or function name, or a function name is not a valid identifier or is a name the files import or export by name.

func (*Generator) Name

func (g *Generator) Name() string

Name returns "typescript" as the runtime identifier.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL