import "../../_dnt.polyfills.js"; import type { SchemaPath } from '../../types/SchemaPath.js'; import type { OasRefData } from './ref-types.js'; import type { OasSchema, ToJsonSchemaOptions } from '../schema/Schema.js'; import type { OasResponse } from '../response/Response.js'; import type { OasParameter } from '../parameter/Parameter.js'; import type { OasExample } from '../example/Example.js'; import type { OasRequestBody } from '../requestBody/RequestBody.js'; import type { OasHeader } from '../header/Header.js'; import type { OasLink } from '../link/Link.js'; import type { SkmtcParsedDocument } from '../../types/SkmtcDocument.js'; import type { RefName } from '../../types/RefName.js'; import type { OpenAPIV3 } from 'openapi-types'; import type { OasSecurityScheme } from '../securitySchemes/SecurityScheme.js'; import { OasBase } from '../../types/OasBase.js'; import { type SiblingKeywords } from '../schema/siblings.js'; import type { ParseContextType } from '../../context/parseTypes.js'; /** * What a reference reads from the parse context when it is made: the document * it resolves through, and the trail it records. `clone` passes them itself. */ type RefContext = Pick; /** * Field data for creating OAS reference objects: the reference, its use-site * flags, and the other keywords next to it in a one-member `allOf` * (`{ allOf: [{ $ref }], minLength: 3, description: '…' }`), parsed without * the target's type (see {@link SiblingKeywords}). {@link OasRef.resolve} * applies the keywords to a copy of the target, so the shared target never * changes, and the reference stays lazy, so a self-reference still ends. * * @template T - The type of component being referenced (e.g., 'schema', 'response') */ export type RefFields = SiblingKeywords & { refType: T; $ref: string; }; /** * Represents an OpenAPI reference ($ref) in the SKMTC OAS processing system. * * The `OasRef` class handles OpenAPI JSON Reference Objects that point to reusable * components within the same document. It provides type-safe reference resolution * with support for chained references and circular reference detection. * * ## Key Features * * - **Type Safety**: Generic parameter ensures resolved types match the reference type * - **Lazy Resolution**: References are resolved on-demand, not during construction * - **Chain Resolution**: Handles references that point to other references * - **Loop Detection**: A chain of references that loops names nothing ({@link OasRef.toLoop}) * - **Type Validation**: Ensures resolved objects match expected reference types * * @template T - The type of component this reference points to * * @example Basic reference resolution * ```typescript * import { OasRef } from '@skmtc/core'; * * // Reference to a schema component * const userRef = new OasRef<'schema'>({ * refType: 'schema', * $ref: '#/components/schemas/User' * }, document); * * // Resolve the reference * const userSchema = userRef.resolve(); * console.log(userSchema.properties); // Access resolved schema properties * ``` * * @example Working with different reference types * ```typescript * // Schema reference * const schemaRef = new OasRef<'schema'>({ * refType: 'schema', * $ref: '#/components/schemas/Product' * }, document); * * // Response reference * const responseRef = new OasRef<'response'>({ * refType: 'response', * $ref: '#/components/responses/ErrorResponse' * }, document); * * // Parameter reference * const paramRef = new OasRef<'parameter'>({ * refType: 'parameter', * $ref: '#/components/parameters/PageSize' * }, document); * ``` * * @example Reference checking and conditional resolution * ```typescript * function processSchemaOrRef(schema: OasSchema | OasRef<'schema'>) { * if (schema.isRef()) { * // Handle reference * const refName = schema.toRefName(); * console.log(`Processing reference: ${refName}`); * * // Resolve only when needed * const resolved = schema.resolve(); * return processed(resolved); * } else { * // Handle direct schema * return process(schema); * } * } * ``` * * @example Chained reference handling * ```typescript * // References can point to other references * const chainedRef = new OasRef<'schema'>({ * refType: 'schema', * $ref: '#/components/schemas/AliasToUser' * }, document); * * // resolve() automatically follows the chain * const finalSchema = chainedRef.resolve(); // Follows chain to final schema * * // resolveOnce() resolves only one step * const oneStep = chainedRef.resolveOnce(); // May still be a reference * ``` */ export declare class OasRef extends OasBase { #private; /** OAS type identifier */ oasType: 'ref'; /** Type identifier */ type: 'ref'; /** The kind of component the reference points to. */ refType: T; /** The JSON Reference: `#/components//`. */ $ref: string; /** * Use-site nullability (see {@link SiblingKeywords.nullable}). Assigned * even when `undefined`, so `'nullable' in ref` is always true and the * value-function nullable read picks it up uniformly. */ nullable: SiblingKeywords['nullable']; /** Use-site `readOnly` (see {@link SiblingKeywords.readOnly}). */ readOnly: SiblingKeywords['readOnly']; /** Use-site `writeOnly` (see {@link SiblingKeywords.writeOnly}). */ writeOnly: SiblingKeywords['writeOnly']; /** The `type` written next to this reference (see {@link SiblingKeywords._type}). */ _type: SiblingKeywords['_type']; /** The `title` next to this reference (see {@link SiblingKeywords}). */ title: SiblingKeywords['title']; /** The `description` next to this reference (see {@link SiblingKeywords}). */ description: SiblingKeywords['description']; /** The `deprecated` next to this reference (see {@link SiblingKeywords}). */ deprecated: SiblingKeywords['deprecated']; /** The `example` next to this reference (see {@link SiblingKeywords}). */ example: SiblingKeywords['example']; /** The `default` next to this reference (see {@link SiblingKeywords}). */ default: SiblingKeywords['default']; /** The `enum` next to this reference (see {@link SiblingKeywords}). */ enums: SiblingKeywords['enums']; /** The extensions (`x-*`) next to this reference (see {@link SiblingKeywords}). */ extensionFields: SiblingKeywords['extensionFields']; /** The `format` next to this reference (see {@link SiblingKeywords}). */ format: SiblingKeywords['format']; /** The `minLength` next to this reference (see {@link SiblingKeywords}). */ minLength: SiblingKeywords['minLength']; /** The `maxLength` next to this reference (see {@link SiblingKeywords}). */ maxLength: SiblingKeywords['maxLength']; /** The `pattern` next to this reference (see {@link SiblingKeywords}). */ pattern: SiblingKeywords['pattern']; /** The `multipleOf` next to this reference (see {@link SiblingKeywords}). */ multipleOf: SiblingKeywords['multipleOf']; /** The `minimum` next to this reference (see {@link SiblingKeywords}). */ minimum: SiblingKeywords['minimum']; /** The `maximum` next to this reference (see {@link SiblingKeywords}). */ maximum: SiblingKeywords['maximum']; /** The `exclusiveMinimum` next to this reference (see {@link SiblingKeywords}). */ exclusiveMinimum: SiblingKeywords['exclusiveMinimum']; /** The `exclusiveMaximum` next to this reference (see {@link SiblingKeywords}). */ exclusiveMaximum: SiblingKeywords['exclusiveMaximum']; /** The `minItems` next to this reference (see {@link SiblingKeywords}). */ minItems: SiblingKeywords['minItems']; /** The `maxItems` next to this reference (see {@link SiblingKeywords}). */ maxItems: SiblingKeywords['maxItems']; /** The `uniqueItems` next to this reference (see {@link SiblingKeywords}). */ uniqueItems: SiblingKeywords['uniqueItems']; /** The `properties` next to this reference (see {@link SiblingKeywords}). */ properties: SiblingKeywords['properties']; /** The `required` next to this reference (see {@link SiblingKeywords}). */ required: SiblingKeywords['required']; /** The `additionalProperties` next to this reference (see {@link SiblingKeywords}). */ additionalProperties: SiblingKeywords['additionalProperties']; /** The `minProperties` next to this reference (see {@link SiblingKeywords}). */ minProperties: SiblingKeywords['minProperties']; /** The `maxProperties` next to this reference (see {@link SiblingKeywords}). */ maxProperties: SiblingKeywords['maxProperties']; /** * Creates a new OAS reference instance. * * @param fields - Reference field data including refType and $ref * @param context - The parse context, or what `clone` passes in its place: * the discriminated document containing the referenced component (for * OAS, refs resolve through the document's components; for GQL, through * the document's registry, as GQL only ever creates schema refs) and the * current trail. */ constructor(fields: RefFields, context: RefContext); /** * Type guard to check if this instance is a reference. * * @returns Always true for OasRef instances */ isRef(): this is OasRef; /** * Recursively resolves this reference to its final target component. * * Follows the chain of references until it reaches a component that is * not a reference. A schema reference returns the * schema at this site: when the keywords next to it, or next to a * reference on the chain ({@link SiblingKeywords}), change the target, a * copy of the target with them applied. When they change nothing, the * shared target itself. `resolveOnce()` applies no siblings. * * The result is shared, so do not modify it: the shared target is the * model every reference to it sees, and a copy is built once, kept on * this reference, and returned on every call. * * @returns The resolved component * @throws {DanglingRefError} when a reference on the chain names nothing, * or the chain loops ({@link toLoop}), so it never reaches a component. * The parser keeps either one only when `system.danglingRefs` lists its kind. * @throws Error when a reference on the chain names a component of another * kind (see {@link resolveOnce}) */ resolve(): ResolvedRef; /** * The references that form the loop this reference's chain comes back to, * starting with the first one it comes back to, or `undefined` when the * chain reaches a component. `A` a `$ref` to `B`, and `B` to `A`, form a * loop: from `A` the chain goes to `B`, then to `A` again, and never * reaches a component. A reference that leads into a loop is not part of it. * * @throws what {@link resolveOnce} throws, at any step of the chain */ toLoop(): OasRef[] | undefined; /** * Resolves this reference one level. Dispatches on the document's * protocol — OAS reads from `document.components.`; GQL * reads from `document.registry.schemas`. * * @returns Either the resolved component or another reference in the chain * @throws {DanglingRefError} when the reference names nothing in the * document (kept when `client.json` `system.danglingRefs` lists its kind), * including a `$ref` with no name, such as `#/components/schemas/` * @throws Error when it names a component of another kind, such as a * `requestBody` ref to `#/components/schemas/…` */ resolveOnce(): OasRef | ResolvedRef; /** * The component name the reference looks up. * * @throws {DanglingRefError} when the `$ref` has no name, such as * `#/components/schemas/`: it names nothing */ toRefName(): RefName; /** * Narrows this reference to a schema reference. * * @returns True when this reference points at a schema component. */ isSchemaRef(): this is OasRef<'schema'>; /** * Navigate an {@link SchemaPath} starting from this reference, resolving it * to descend through. See {@link traverseSchema}. * * Available on every `OasRef` so `.traverse()` works on the common * `OasSchema | OasRef<'schema'>` value (an object property, array `items`). * Schema paths only describe schemas for now, so it throws for non-schema * refs (response/parameter/…); {@link isSchemaRef} narrows `this`, keeping the * delegation cast-free. * * @returns The schema at the path (may be an unresolved `$ref`). * @throws Error when called on a non-schema ref. */ traverse(path: SchemaPath): OasSchema | OasRef<'schema'>; /** * Whether this reference adds anything at its site: a use-site flag * (`nullable`, `readOnly`, `writeOnly`) or another keyword next to it (see * {@link RefFields}). {@link resolve} then returns a copy of the target with * them applied, not the shared target. */ hasSiblings(): boolean; /** * A copy of this reference with what `ref` adds at its site merged in: its * flags, and its keywords over this reference's own * ({@link mergeSiblingKeywords}). It points at the same target. Each schema * class's `clone` does the same for a schema, so one step along a chain of * references can carry a site's keywords (`resolveSchemaRefOnce`). This * reference does not change. */ clone(ref?: OasRef<'schema'>): OasRef; /** * Whether the keywords next to this reference change which values it * accepts (`minLength` next to a string, `properties` next to an object), * not only how it is described (`description`, `deprecated`, `x-*`). No * shared model accepts the same values as such a site, so a generator * applies these keywords where it renders the reference by name. See * {@link changesValues}. */ hasValueSiblings(): boolean; /** * The keywords next to this reference that {@link resolve} leaves out, by * their names in the document: ones the target's type does not take * (`minLength` next to an array), a `type` that disagrees, and values that * do not fit. The parser cannot tell them while the reference is lazy, so a * generator logs them when it renders the site. See {@link toSkippedSiblings}. */ toSkippedSiblings(): string[]; /** * Whether the value is `readOnly` at this site, as Forge reads it: on this * reference (`{ allOf: [{ $ref }], readOnly: true }`), or on what it points * to, through any chain of references. A `readOnly: false` does not undo * another's `true`. `false` for a reference to anything but a schema. In a * loop ({@link toLoop}), which reaches no schema, only the references count. */ isReadOnly(): boolean; /** * Whether the value is `writeOnly` at this site: on this reference, or on * what it points to, as {@link isReadOnly} reads `readOnly`. */ isWriteOnly(): boolean; /** * Returns the discriminated parsed document this ref resolves * through. OAS variant carries the parent `OasDocument`; GQL variant * carries the parent `GqlDocument` (whose registry holds the * schemas). */ get document(): SkmtcParsedDocument; /** * The reference as JSON Schema. With `resolve`, the schema at this site, * with the keywords next to the reference applied. Without it, only * `{ $ref }`: the keywords next to the reference are not written yet * (skmtc/skmtc#338). */ toJsonSchema({ resolve }: ToJsonSchemaOptions): OpenAPIV3.ReferenceObject | ResolvedRefJsonType; /** `{ $ref }` only, as {@link toJsonSchema} without `resolve` (skmtc/skmtc#338). */ toJSON(): object; } /** * Type representing the JSON schema result from resolving a reference. * * @template T - The type of component being referenced */ export type ResolvedRefJsonType = ReturnType['toJsonSchema']>; /** * Union type of all OAS component types that can be referenced. * * Includes all OpenAPI component types that support $ref resolution. */ export type OasComponentType = OasSchema | OasResponse | OasParameter | OasExample | OasRequestBody | OasHeader | OasSecurityScheme | OasLink; /** * Type representing a resolved reference to a specific component type. * * @template T - The type of component being referenced (e.g., 'schema', 'response') */ export type ResolvedRef = Extract; export {};