@honorestjs/contract
Version:
Contract-first API definitions for HonorestJS - Framework-agnostic contract definitions with full type safety
86 lines • 2.49 kB
TypeScript
import type { Endpoint } from './endpoint';
/**
* Contract definition structure
* Defines a group of related API endpoints (typically for a resource)
*/
export interface ContractDefinition<TEndpoints extends Record<string, Endpoint> = Record<string, Endpoint>> {
/**
* Contract name (used for identification)
* Should be unique within an app
* @example 'users' | 'posts' | 'auth'
*/
name: string;
/**
* Base path for all endpoints in this contract
* Endpoints paths are relative to this base path
* @example '/users' | '/api/posts' | '/auth'
*/
path: string;
/**
* API version for this contract
* Can be overridden at the app level
* @example 1 | 2
*/
version?: number;
/**
* Endpoint definitions for this contract
* Maps endpoint names to their definitions
* @example { getUser: endpoint({...}), createUser: endpoint({...}) }
*/
endpoints: TEndpoints;
/**
* Contract description
* Used for documentation generation
*/
description?: string;
/**
* Tags for grouping contracts
* Used to organize contracts in documentation
* @example ['users', 'admin']
*/
tags?: string[];
}
/**
* Branded contract type - a definition with a runtime marker
*/
export type Contract<T extends ContractDefinition = ContractDefinition> = T & {
readonly __contract: unique symbol;
};
/**
* Creates a contract definition
*
* A contract represents a collection of related API endpoints,
* typically for a single resource or domain concept.
*
* @param definition - The contract specification
* @returns A contract object with type inference helpers
*
* @example
* ```typescript
* const UsersContract = defineContract({
* name: 'users',
* path: '/users',
* description: 'User management endpoints',
* endpoints: {
* getUser: endpoint({
* method: 'GET',
* path: '/:id',
* params: z.object({ id: z.string().uuid() }),
* output: UserSchema
* }),
* createUser: endpoint({
* method: 'POST',
* path: '/',
* body: CreateUserSchema,
* output: UserSchema
* })
* }
* })
* ```
*/
export declare function defineContract<const T extends ContractDefinition>(definition: T): Contract<T>;
/**
* Type guard to check if a value is a contract
*/
export declare function isContract(value: unknown): value is Contract;
//# sourceMappingURL=contract.d.ts.map