typescript

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 6 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, and factory 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 three 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.

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';

Configuration

Supports standard clientgen.Config options:

  • RelationFilter - Prefix filter for relations (e.g., "can_" to generate only permissions)
  • 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/ --filter can_

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, and factory 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", "index.ts".

Generated code includes:

  • types.ts: ObjectType/Relation constants and union types
  • schema.ts: Factory functions and wildcard constructors
  • index.ts: Re-exports for clean imports

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