/** * @fileoverview Generator Key System for SKMTC Core * * This module provides a comprehensive type-safe key system for identifying and * organizing generated content within the SKMTC pipeline. Generator keys provide * unique identification for operations, models, and other generated artifacts, * ensuring proper organization and preventing collisions. * * The key system uses branded types to ensure type safety and provides utilities * for creating, parsing, and manipulating keys throughout the generation process. * * ## Key Features * * - **Type Safety**: Branded types prevent mixing different kinds of keys * - **Unique Identification**: Composite keys ensure global uniqueness * - **Parsing Utilities**: Safe extraction of components from composite keys * - **Generator Organization**: Clear mapping between generators and their artifacts * - **Reference Integration**: Seamless integration with OpenAPI reference system * * @example Creating an OAS operation generator key * ```typescript * import { toOasOperationGeneratorKey, fromGeneratorKey } from '@skmtc/core/GeneratorKeys'; * * const key = toOasOperationGeneratorKey({ * generatorId: 'api-client', * path: '/users', * method: 'get' * }); * * const parsed = fromGeneratorKey(key); * if (parsed.type === 'oasOperation') { * console.log(parsed); // { type: 'oasOperation', generatorId: 'api-client', path: '/users', method: 'get' } * } * ``` * * @example Creating a GraphQL operation generator key * ```typescript * import { toGqlOperationGeneratorKey, fromGeneratorKey } from '@skmtc/core/GeneratorKeys'; * * const key = toGqlOperationGeneratorKey({ * generatorId: 'graphql-client', * rootKind: 'query', * fieldName: 'getUser' * }); * * const parsed = fromGeneratorKey(key); * if (parsed.type === 'gqlOperation') { * console.log(parsed); // { type: 'gqlOperation', generatorId: 'graphql-client', rootKind: 'query', fieldName: 'getUser' } * } * ``` * * @example Working with model keys * ```typescript * import { toModelGeneratorKey } from '@skmtc/core/GeneratorKeys'; * import type { RefName } from '@skmtc/core/RefName'; * * const modelKey = toModelGeneratorKey({ * generatorId: 'typescript-models', * refName: 'User' as RefName, * variant: 'main' * }); * ``` * * @module GeneratorKeys */ import "../_dnt.polyfills.js"; import type { OasOperation } from '../oas/operation/Operation.js'; import type { OasWebhook } from '../oas/webhook/Webhook.js'; import type { GqlOperation, GqlRootKind } from '../gql/operation/GqlOperation.js'; import type { Brand } from '../types/Brand.js'; import type { RefName } from '../types/RefName.js'; import { type Method } from '../types/Method.js'; /** * Template literal type for OAS operation generator keys before branding. * Format: `generatorId|path|method|variant` * (e.g., 'api-client|/users|get|main', 'forms|/quotes|patch|customer'). * * The trailing `variant` segment carries the operation-variant axis * (see {@link Variant}). For variants-unaware generators it is always * `'main'`; for variants-aware generators it carries the per-call * variant name. */ export type NakedOasOperationGeneratorKey = `${string}|${string}|${Method}|${string}`; /** * Template literal type for GraphQL operation generator keys before branding. * Format: `generatorId|rootKind|fieldName|variant` * (e.g., 'graphql-client|query|getUser|main', * 'graphql-client|mutation|createPost|customer'). * * The trailing `variant` segment carries the operation-variant axis * (see {@link Variant}). */ export type NakedGqlOperationGeneratorKey = `${string}|${GqlRootKind}|${string}|${string}`; /** * Template literal type for model generator keys before branding. * Format: `generatorId|refName|variant` * (e.g., 'typescript-models|User|main', * 'zod-schemas|Customer|coercive'). * * The trailing `variant` segment carries the model-variant axis * (see {@link Variant}). For variants-unaware generators it is always * `'main'`; for variants-aware generators it carries the per-call * variant name. The shape mirrors the operation key so the integrity * check in `ModelDriver.affirmDefinition` catches a variants-aware * generator that forgot to fold `variant` into its `toIdentifier`. */ export type NakedModelGeneratorKey = `${string}|${string}|${string}`; /** * Template literal type for webhook generator keys before branding. * Format: `generatorId|webhook|name|method|variant` * (e.g., 'webhook-handlers|webhook|newPet|post|main'). * * The literal `webhook` segment in position 2 distinguishes webhook keys * (5 segments) from operation keys (`generatorId|path|method|variant`, 4 * segments), so a webhook named `users` never collides with a path * `/users` in the key space or the `(name, exportPath)` cache. The * trailing `variant` segment carries the variant axis (see {@link Variant}). */ export type NakedWebhookGeneratorKey = `${string}|webhook|${string}|${Method}|${string}`; /** * Raw string format for container generator keys: * `generatorId|container|group|name|variant`. * * A container is a place, not a subject — many subjects insert members into * one — so its key cannot name a subject the way an operation's does. It * names the GROUP those subjects share instead: the tag they carry, the * module they belong to, whatever the generator collects them by. Every * member of the group computes the same one, and the `generatorId` segment * is what the integrity check compares, so a second generator claiming that * declaration fails rather than silently inheriting it. * * `name` distinguishes containers within one group — a tag may produce a * service interface and a controller class. The literal `container` segment * in position 2 keeps the key disjoint from the 5-segment webhook key, whose * own discriminator is `webhook`. */ export type NakedContainerGeneratorKey = `${string}|container|${string}|${string}|${string}`; /** * Branded type for OAS operation generator keys. * * Uniquely identifies a generator processing a specific OpenAPI operation — * encodes generator ID, API path, and HTTP method. See * {@link GqlOperationGeneratorKey} for the GraphQL counterpart. */ export type OasOperationGeneratorKey = Brand; /** * Branded type for GraphQL operation generator keys. * * Sibling to {@link OasOperationGeneratorKey} for the GraphQL protocol. The key * encodes the generator ID, the root type (`query` / `mutation` / * `subscription`), and the root field name. */ export type GqlOperationGeneratorKey = Brand; /** * Branded type for model generator keys. * * Model generator keys uniquely identify generators that process * OpenAPI schema models. The key encodes the generator ID and * the schema reference name. */ export type ModelGeneratorKey = Brand; /** * Branded type for webhook generator keys. * * Sibling to {@link OasOperationGeneratorKey} for the OpenAPI 3.1 webhook * subject. The literal `webhook` discriminator segment keeps it from * colliding with operation keys in the `(name, exportPath)` cache and the * key space. */ export type WebhookGeneratorKey = Brand; /** * Branded type for container generator keys — the key a definition that * holds members carries. See {@link NakedContainerGeneratorKey}. */ export type ContainerGeneratorKey = Brand; /** * Branded type for generator-only keys. * * Generator-only keys identify generators that don't process specific * operations or models, but generate global artifacts like configuration * files, base classes, or utility modules. */ export type GeneratorOnlyKey = Brand; /** * Union type of all possible generator key types. * * Generator keys serve as unique identifiers for different types of * code generators in the SKMTC system. They enable tracking, caching, * and dependency management between generated artifacts. * * @example * ```typescript * // OAS operation generator key * const opKey: GeneratorKey = toOasOperationGeneratorKey({ * generatorId: 'api-client', * path: '/users/{id}', * method: 'get' * }); * * // GraphQL operation generator key * const gqlKey: GeneratorKey = toGqlOperationGeneratorKey({ * generatorId: 'graphql-client', * rootKind: 'query', * fieldName: 'getUser' * }); * * // Model generator key * const modelKey: GeneratorKey = toModelGeneratorKey({ * generatorId: 'typescript-types', * refName: 'User', * variant: 'main' * }); * * // Generator-only key * const globalKey: GeneratorKey = toGeneratorOnlyKey({ * generatorId: 'api-config' * }); * ``` */ export type GeneratorKey = OasOperationGeneratorKey | WebhookGeneratorKey | GqlOperationGeneratorKey | ModelGeneratorKey | ContainerGeneratorKey | GeneratorOnlyKey; /** * Arguments for {@link toOasOperationGeneratorKey}. * * Can specify operation details directly or provide an OasOperation * object from which the path and method will be extracted. The * `variant` segment is always required so the resulting key * disambiguates per-variant Definitions (see {@link Variant}). */ type ToOasOperationGeneratorKeyArgs = { /** Unique identifier for the generator */ generatorId: string; /** API path (e.g., '/users/{id}') */ path: string; /** HTTP method */ method: Method; /** Operation variant name — omitted means `'main'` */ variant?: string; } | { /** Unique identifier for the generator */ generatorId: string; /** OpenAPI operation object containing path and method */ operation: OasOperation; /** Operation variant name — omitted means `'main'` */ variant?: string; }; /** * Creates an OAS operation generator key from generator ID and operation details. * * Uniquely identifies a generator processing a specific API operation. The * key format is: `generatorId|path|method`. See * {@link toGqlOperationGeneratorKey} for the GraphQL counterpart. * * @param args - Key construction arguments * @returns A branded OasOperationGeneratorKey * * @example With explicit path and method * ```typescript * const key = toOasOperationGeneratorKey({ * generatorId: 'api-client', * path: '/users/{id}', * method: 'get' * }); * // Result: 'api-client|/users/{id}|get' (branded) * ``` * * @example With OasOperation object * ```typescript * const operation = new OasOperation({ * path: '/posts', * method: 'post', * // ... other operation details * }); * * const key = toOasOperationGeneratorKey({ * generatorId: 'rest-client', * operation * }); * // Result: 'rest-client|/posts|post' (branded) * ``` */ export declare const toOasOperationGeneratorKey: ({ generatorId, variant, ...rest }: ToOasOperationGeneratorKeyArgs) => OasOperationGeneratorKey; /** * Arguments for {@link toWebhookGeneratorKey}. * * Can specify webhook details directly or provide an {@link OasWebhook} * object from which the name and method will be extracted. The `variant` * segment is always required so the resulting key disambiguates * per-variant Definitions (see {@link Variant}). */ type ToWebhookGeneratorKeyArgs = { /** Unique identifier for the generator */ generatorId: string; /** Webhook name (the `webhooks` map key — NOT a URL path) */ name: string; /** HTTP method */ method: Method; /** Webhook variant name — omitted means `'main'` */ variant?: string; } | { /** Unique identifier for the generator */ generatorId: string; /** OpenAPI webhook object containing name and method */ webhook: OasWebhook; /** Webhook variant name — omitted means `'main'` */ variant?: string; }; /** * Creates a webhook generator key from generator ID and webhook details. * * Sibling to {@link toOasOperationGeneratorKey} for the 3.1 webhook subject. * Format: `generatorId|webhook|name|method|variant`. The literal `webhook` * segment keeps webhook keys from colliding with operation keys. */ export declare const toWebhookGeneratorKey: ({ generatorId, variant, ...rest }: ToWebhookGeneratorKeyArgs) => WebhookGeneratorKey; /** * Arguments for {@link toContainerGeneratorKey}. */ export type ToContainerGeneratorKeyArgs = { /** The generator that owns the container. */ generatorId: string; /** The group its members share — a tag, a module, a service. */ group: string; /** The container's identifier name, distinguishing it within the group. */ name: string; /** Variant the container belongs to (see {@link Variant}). */ variant?: string; }; /** * Build the key for a definition that holds members. * * Every member of a group computes the same key for the container it inserts * into, because the group is a function of the subject the same way an * identifier name is. Deriving it from the group rather than from where the * container happens to land keeps the key stable when a generator changes * its file or naming policy, and keeps it saying what the definition IS * rather than where it sits. */ export declare const toContainerGeneratorKey: ({ generatorId, group, name, variant }: ToContainerGeneratorKeyArgs) => ContainerGeneratorKey; /** * Arguments for {@link toGqlOperationGeneratorKey}. * * Can specify operation details directly or provide a {@link GqlOperation} * object from which the root type and field name will be extracted. The * `variant` segment is always required (see {@link Variant}). */ type ToGqlOperationGeneratorKeyArgs = { /** Unique identifier for the generator */ generatorId: string; /** GraphQL root type */ rootKind: GqlRootKind; /** Root field name */ fieldName: string; /** Operation variant name — omitted means `'main'` */ variant?: string; } | { /** Unique identifier for the generator */ generatorId: string; /** GraphQL operation object */ operation: GqlOperation; /** Operation variant name — omitted means `'main'` */ variant?: string; }; /** * Creates a GraphQL operation generator key. * * Sibling to {@link toOasOperationGeneratorKey} for the GraphQL protocol. Format: * `generatorId|rootKind|fieldName|variant`. */ export declare const toGqlOperationGeneratorKey: ({ generatorId, variant, ...rest }: ToGqlOperationGeneratorKeyArgs) => GqlOperationGeneratorKey; /** * Arguments for {@link toModelGeneratorKey}. * * The `variant` segment is always required so the resulting key * disambiguates per-variant Definitions (see {@link Variant}). */ type ToModelGeneratorKeyArgs = { /** Unique identifier for the generator */ generatorId: string; /** Reference name of the schema model */ refName: RefName; /** Model variant name — omitted means `'main'` */ variant?: string; }; /** * Creates a model generator key from generator ID, schema reference name, * and variant. * * Model generator keys uniquely identify generators processing specific * OpenAPI schema models for a given variant. The key format is: * `generatorId|refName|variant`. * * @param args - Model generator key arguments * @returns A branded ModelGeneratorKey * * @example * ```typescript * const key = toModelGeneratorKey({ * generatorId: 'typescript-interfaces', * refName: 'User', * variant: 'main' * }); * // Result: 'typescript-interfaces|User|main' (branded) * * const coercive = toModelGeneratorKey({ * generatorId: 'zod-schemas', * refName: 'CreateUserRequest', * variant: 'coercive' * }); * // Result: 'zod-schemas|CreateUserRequest|coercive' (branded) * ``` */ export declare const toModelGeneratorKey: ({ generatorId, refName, variant }: ToModelGeneratorKeyArgs) => ModelGeneratorKey; /** * Arguments for {@link toGeneratorOnlyKey}. */ type ToGeneratorOnlyKeyArgs = { /** Unique identifier for the generator */ generatorId: string; }; /** * Creates a generator-only key for global/utility generators. * * Generator-only keys identify generators that produce artifacts not tied * to specific operations or models, such as configuration files, base classes, * utilities, or documentation. * * @param args - Generator-only key arguments * @returns A branded GeneratorOnlyKey * * @example * ```typescript * const configKey = toGeneratorOnlyKey({ * generatorId: 'api-config' * }); * // Result: 'api-config' (branded) * * const utilsKey = toGeneratorOnlyKey({ * generatorId: 'common-utilities' * }); * // Result: 'common-utilities' (branded) * ``` */ export declare const toGeneratorOnlyKey: ({ generatorId }: ToGeneratorOnlyKeyArgs) => GeneratorOnlyKey; /** * Type guard to check if a value is a valid GeneratorKey. * * This function validates that the argument is one of the three * generator key types: operation, model, or generator-only. * * @param arg - Value to check * @returns True if the value is a valid GeneratorKey * * @example * ```typescript * const key = 'api-client|/users|get'; * * if (isGeneratorKey(key)) { * // key is now typed as GeneratorKey * const generatorId = toGeneratorId(key); * console.log(generatorId); // 'api-client' * } * ``` */ export declare const isGeneratorKey: (arg: unknown) => arg is GeneratorKey; /** * Type guard to check if a value is a valid OasOperationGeneratorKey. * * Validates that the argument is a string with the correct format: * `generatorId|path|method|variant` where each part is non-empty, path * starts with `/` and method is a valid HTTP method. * * @param arg - Value to check * @returns True if the value is a valid OasOperationGeneratorKey * * @example * ```typescript * const key = 'api-client|/users/{id}|get'; * * if (isOasOperationGeneratorKey(key)) { * // key is now typed as OasOperationGeneratorKey * const obj = fromGeneratorKey(key); * console.log(obj.type); // 'oasOperation' * console.log(obj.generatorId); // 'api-client' * console.log(obj.path); // '/users/{id}' * console.log(obj.method); // 'get' * } * ``` */ export declare const isOasOperationGeneratorKey: (arg: unknown) => arg is OasOperationGeneratorKey; /** * Type guard to check if a value is a valid {@link WebhookGeneratorKey}. * * Validates the format `generatorId|webhook|name|method|variant`: five * pipe-delimited segments with a literal `webhook` discriminator in * position 2, a valid HTTP method, and non-empty `generatorId` / `name` / * `variant`. The 5-segment shape (and the `webhook` literal) is what keeps * it disjoint from the 4-segment operation key. */ export declare const isWebhookGeneratorKey: (arg: unknown) => arg is WebhookGeneratorKey; /** * Type guard to check if a value is a valid {@link ContainerGeneratorKey}. * * Validates `generatorId|container|group|name|variant`: five * pipe-delimited segments with a literal `container` discriminator in * position 2. That literal is what keeps it disjoint from the webhook key, * which is also five segments but carries `webhook` in the same position. */ export declare const isContainerGeneratorKey: (arg: unknown) => arg is ContainerGeneratorKey; /** * Type guard to check if a value is a valid {@link GqlOperationGeneratorKey}. * * Validates that the argument is a string with the format * `generatorId|rootKind|fieldName`, with `rootKind` constrained to a * GraphQL root operation type (`query` / `mutation` / `subscription`). */ export declare const isGqlOperationGeneratorKey: (arg: unknown) => arg is GqlOperationGeneratorKey; /** * Type guard to check if a value is a valid ModelGeneratorKey. * * Validates that the argument is a string with the correct format: * `generatorId|refName` where both parts are non-empty strings. * * @param arg - Value to check * @returns True if the value is a valid ModelGeneratorKey * * @example * ```typescript * const key = 'zod-schemas|User|main'; * * if (isModelGeneratorKey(key)) { * // key is now typed as ModelGeneratorKey * const obj = fromGeneratorKey(key); * console.log(obj.type); // 'model' * console.log(obj.generatorId); // 'zod-schemas' * console.log(obj.refName); // 'User' * console.log(obj.variant); // 'main' * } * ``` */ export declare const isModelGeneratorKey: (arg: unknown) => arg is ModelGeneratorKey; /** * Type guard to check if a value is a valid GeneratorOnlyKey. * * Validates that the argument is a non-empty string. Generator-only keys * are simple strings containing just the generator ID. * * @param arg - Value to check * @returns True if the value is a valid GeneratorOnlyKey * * @example * ```typescript * const key = 'api-config'; * * if (isGeneratorOnlyKey(key)) { * // key is now typed as GeneratorOnlyKey * const obj = fromGeneratorKey(key); * console.log(obj.type); // 'generator-only' * console.log(obj.generatorId); // 'api-config' * } * ``` */ export declare const isGeneratorOnlyKey: (arg: unknown) => arg is GeneratorOnlyKey; /** * Extracts the generator ID from any type of GeneratorKey. * * This utility function parses the generator key to extract just the * generator identifier, regardless of the key type. For operation and * model keys, it extracts the first part before the pipe. For generator-only * keys, it returns the entire key since it's just the generator ID. * * @param generatorKey - Any type of generator key * @returns The generator ID string * * @example * ```typescript * const opKey = toOasOperationGeneratorKey({ * generatorId: 'api-client', * path: '/users', * method: 'get' * }); * console.log(toGeneratorId(opKey)); // 'api-client' * * const modelKey = toModelGeneratorKey({ * generatorId: 'typescript-types', * refName: 'User', * variant: 'main' * }); * console.log(toGeneratorId(modelKey)); // 'typescript-types' * * const globalKey = toGeneratorOnlyKey({ * generatorId: 'utilities' * }); * console.log(toGeneratorId(globalKey)); // 'utilities' * ``` */ export declare const toGeneratorId: (generatorKey: GeneratorKey) => string; /** * Object representation of a parsed GeneratorKey. * * This discriminated union type represents the parsed components of any * generator key, making it easier to work with key data in a structured way. * The `type` field discriminates between the three key types. */ export type GeneratorKeyObject = { /** Discriminator for OAS operation generator keys */ type: 'oasOperation'; /** Generator identifier */ generatorId: string; /** API path */ path: string; /** HTTP method */ method: Method; /** Operation variant name */ variant: string; } | { /** Discriminator for webhook generator keys */ type: 'webhook'; /** Generator identifier */ generatorId: string; /** Webhook name (the `webhooks` map key — NOT a URL path) */ name: string; /** HTTP method */ method: Method; /** Webhook variant name */ variant: string; } | { /** Discriminator for container generator keys */ type: 'container'; /** Generator identifier */ generatorId: string; /** The group the container's members share */ group: string; /** The container's identifier name */ name: string; /** Container variant name */ variant: string; } | { /** Discriminator for GraphQL operation generator keys */ type: 'gqlOperation'; /** Generator identifier */ generatorId: string; /** GraphQL root type */ rootKind: GqlRootKind; /** Root field name */ fieldName: string; /** Operation variant name */ variant: string; } | { /** Discriminator for model generator keys */ type: 'model'; /** Generator identifier */ generatorId: string; /** Schema reference name */ refName: string; /** Model variant name */ variant: string; } | { /** Discriminator for generator-only keys */ type: 'generator-only'; /** Generator identifier */ generatorId: string; }; /** * Parses a GeneratorKey into its structured object representation. * * This function decomposes any generator key into a structured object * with discriminated union types, making it easier to work with the * key components in a type-safe manner. * * @param generatorKey - Any type of generator key to parse * @returns Parsed generator key object with discriminated type * * @example Operation key parsing * ```typescript * const opKey = 'api-client|/users/{id}|get' as OasOperationGeneratorKey; * const parsed = fromGeneratorKey(opKey); * * if (parsed.type === 'oasOperation') { * console.log(parsed.generatorId); // 'api-client' * console.log(parsed.path); // '/users/{id}' * console.log(parsed.method); // 'get' * } * ``` * * @example Model key parsing * ```typescript * const modelKey = 'zod-schemas|User|main' as ModelGeneratorKey; * const parsed = fromGeneratorKey(modelKey); * * if (parsed.type === 'model') { * console.log(parsed.generatorId); // 'zod-schemas' * console.log(parsed.refName); // 'User' * console.log(parsed.variant); // 'main' * } * ``` * * @example Generator-only key parsing * ```typescript * const globalKey = 'utilities' as GeneratorOnlyKey; * const parsed = fromGeneratorKey(globalKey); * * if (parsed.type === 'generator-only') { * console.log(parsed.generatorId); // 'utilities' * } * ``` */ export declare const fromGeneratorKey: (generatorKey: GeneratorKey) => GeneratorKeyObject; export {};