UNPKG

@kubb/plugin-swr

Version:

SWR hooks generator plugin for Kubb, creating type-safe data fetching hooks from OpenAPI specifications for React and Next.js applications.

470 lines (469 loc) 12.4 kB
import { i as __name, n as PluginSwr } from "./types-wR9RZTnh.js"; import { HttpMethod, Oas, Operation, SchemaObject, contentType } from "@kubb/oas"; import { FabricReactNode } from "@kubb/react-fabric/types"; import { BaseGenerator, Config, FileMetaBase, Group, KubbEvents, Output, Plugin, PluginFactoryOptions, PluginManager, ResolveNameParams } from "@kubb/core"; import { Fabric } from "@kubb/react-fabric"; import { AsyncEventEmitter } from "@kubb/core/utils"; import { KubbFile } from "@kubb/fabric-core/types"; //#region ../plugin-oas/src/types.d.ts type GetOasOptions = { validate?: boolean; }; type Context$2 = { getOas(options?: GetOasOptions): Promise<Oas>; getBaseURL(): Promise<string | undefined>; }; declare global { namespace Kubb { interface PluginContext extends Context$2 {} } } /** * `propertyName` is the ref name + resolved with the nameResolver * @example import { Pet } from './Pet' * * `originalName` is the original name used(in PascalCase), only used to remove duplicates * * `pluginKey` can be used to override the current plugin being used, handy when you want to import a type/schema out of another plugin * @example import a type(plugin-ts) for a mock file(swagger-faker) */ type Ref = { propertyName: string; originalName: string; path: KubbFile.Path; pluginKey?: Plugin['key']; }; type Refs = Record<string, Ref>; type OperationSchema = { /** * Converted name, contains already `PathParams`, `QueryParams`, ... */ name: string; schema: SchemaObject; operation?: Operation; /** * OperationName in PascalCase, only being used in OperationGenerator */ operationName: string; description?: string; statusCode?: number; keys?: string[]; keysToOmit?: string[]; withData?: boolean; }; type OperationSchemas = { pathParams?: OperationSchema & { keysToOmit?: never; }; queryParams?: OperationSchema & { keysToOmit?: never; }; headerParams?: OperationSchema & { keysToOmit?: never; }; request?: OperationSchema; response: OperationSchema; responses: Array<OperationSchema>; statusCodes?: Array<OperationSchema>; errors?: Array<OperationSchema>; }; type ByTag = { type: 'tag'; pattern: string | RegExp; }; type ByOperationId = { type: 'operationId'; pattern: string | RegExp; }; type ByPath = { type: 'path'; pattern: string | RegExp; }; type ByMethod = { type: 'method'; pattern: HttpMethod | RegExp; }; type BySchemaName = { type: 'schemaName'; pattern: string | RegExp; }; type ByContentType = { type: 'contentType'; pattern: string | RegExp; }; type Exclude = ByTag | ByOperationId | ByPath | ByMethod | ByContentType; type Include = ByTag | ByOperationId | ByPath | ByMethod | ByContentType; type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | BySchemaName | ByContentType) & { options: Partial<TOptions>; }; //#endregion //#region ../plugin-oas/src/OperationGenerator.d.ts type Context$1<TOptions, TPluginOptions extends PluginFactoryOptions> = { fabric: Fabric; oas: Oas; exclude: Array<Exclude> | undefined; include: Array<Include> | undefined; override: Array<Override<TOptions>> | undefined; contentType: contentType | undefined; pluginManager: PluginManager; events?: AsyncEventEmitter<KubbEvents>; /** * Current plugin */ plugin: Plugin<TPluginOptions>; mode: KubbFile.Mode; UNSTABLE_NAMING?: true; }; declare class OperationGenerator<TPluginOptions extends PluginFactoryOptions = PluginFactoryOptions, TFileMeta extends FileMetaBase = FileMetaBase> extends BaseGenerator<TPluginOptions['resolvedOptions'], Context$1<TPluginOptions['resolvedOptions'], TPluginOptions>> { #private; getOptions(operation: Operation, method: HttpMethod): Partial<TPluginOptions['resolvedOptions']>; getSchemas(operation: Operation, { resolveName }?: { resolveName?: (name: string) => string; }): OperationSchemas; getOperations(): Promise<Array<{ path: string; method: HttpMethod; operation: Operation; }>>; build(...generators: Array<Generator<TPluginOptions>>): Promise<Array<KubbFile.File<TFileMeta>>>; } //#endregion //#region ../plugin-oas/src/SchemaMapper.d.ts type SchemaKeywordMapper = { object: { keyword: 'object'; args: { properties: { [x: string]: Schema[]; }; additionalProperties: Schema[]; patternProperties?: Record<string, Schema[]>; strict?: boolean; }; }; url: { keyword: 'url'; }; readOnly: { keyword: 'readOnly'; }; writeOnly: { keyword: 'writeOnly'; }; uuid: { keyword: 'uuid'; }; email: { keyword: 'email'; }; firstName: { keyword: 'firstName'; }; lastName: { keyword: 'lastName'; }; phone: { keyword: 'phone'; }; password: { keyword: 'password'; }; date: { keyword: 'date'; args: { type?: 'date' | 'string'; }; }; time: { keyword: 'time'; args: { type?: 'date' | 'string'; }; }; datetime: { keyword: 'datetime'; args: { offset?: boolean; local?: boolean; }; }; tuple: { keyword: 'tuple'; args: { items: Schema[]; min?: number; max?: number; rest?: Schema; }; }; array: { keyword: 'array'; args: { items: Schema[]; min?: number; max?: number; unique?: boolean; }; }; enum: { keyword: 'enum'; args: { name: string; typeName: string; asConst: boolean; items: Array<{ name: string | number; format: 'string' | 'number' | 'boolean'; value?: string | number | boolean; }>; }; }; and: { keyword: 'and'; args: Schema[]; }; const: { keyword: 'const'; args: { name: string | number; format: 'string' | 'number' | 'boolean'; value?: string | number | boolean; }; }; union: { keyword: 'union'; args: Schema[]; }; ref: { keyword: 'ref'; args: { name: string; $ref: string; /** * Full qualified path. */ path: KubbFile.Path; /** * When true `File.Import` is used. * When false a reference is used inside the current file. */ isImportable: boolean; }; }; matches: { keyword: 'matches'; args?: string; }; boolean: { keyword: 'boolean'; }; default: { keyword: 'default'; args: string | number | boolean; }; string: { keyword: 'string'; }; integer: { keyword: 'integer'; }; bigint: { keyword: 'bigint'; }; number: { keyword: 'number'; }; max: { keyword: 'max'; args: number; }; min: { keyword: 'min'; args: number; }; exclusiveMaximum: { keyword: 'exclusiveMaximum'; args: number; }; exclusiveMinimum: { keyword: 'exclusiveMinimum'; args: number; }; describe: { keyword: 'describe'; args: string; }; example: { keyword: 'example'; args: string; }; deprecated: { keyword: 'deprecated'; }; optional: { keyword: 'optional'; }; undefined: { keyword: 'undefined'; }; nullish: { keyword: 'nullish'; }; nullable: { keyword: 'nullable'; }; null: { keyword: 'null'; }; any: { keyword: 'any'; }; unknown: { keyword: 'unknown'; }; void: { keyword: 'void'; }; blob: { keyword: 'blob'; }; schema: { keyword: 'schema'; args: { type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object'; format?: string; }; }; name: { keyword: 'name'; args: string; }; catchall: { keyword: 'catchall'; }; interface: { keyword: 'interface'; }; }; type Schema = { keyword: string; } | SchemaKeywordMapper[keyof SchemaKeywordMapper]; //#endregion //#region ../plugin-oas/src/SchemaGenerator.d.ts type Context<TOptions, TPluginOptions extends PluginFactoryOptions> = { fabric: Fabric; oas: Oas; pluginManager: PluginManager; events?: AsyncEventEmitter<KubbEvents>; /** * Current plugin */ plugin: Plugin<TPluginOptions>; mode: KubbFile.Mode; include?: Array<'schemas' | 'responses' | 'requestBodies'>; override: Array<Override<TOptions>> | undefined; contentType?: contentType; output?: string; }; type SchemaGeneratorOptions = { dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'; integerType?: 'number' | 'bigint'; unknownType: 'any' | 'unknown' | 'void'; emptySchemaType: 'any' | 'unknown' | 'void'; enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal' | 'inlineLiteral'; enumSuffix?: string; /** * @deprecated Will be removed in v5. Use `collisionDetection: true` instead to prevent enum name collisions. * When `collisionDetection` is enabled, the rootName-based approach eliminates the need for numeric suffixes. * @internal */ usedEnumNames?: Record<string, number>; mapper?: Record<string, string>; typed?: boolean; transformers: { /** * Customize the names based on the type that is provided by the plugin. */ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string; /** * Receive schema and name(propertyName) and return FakerMeta array * TODO TODO add docs * @beta */ schema?: (schemaProps: SchemaProps$1, defaultSchemas: Schema[]) => Array<Schema> | undefined; }; }; type SchemaProps$1 = { schema: SchemaObject | null; name: string | null; parentName: string | null; rootName?: string | null; }; declare class SchemaGenerator<TOptions extends SchemaGeneratorOptions = SchemaGeneratorOptions, TPluginOptions extends PluginFactoryOptions = PluginFactoryOptions, TFileMeta extends FileMetaBase = FileMetaBase> extends BaseGenerator<TOptions, Context<TOptions, TPluginOptions>> { #private; refs: Refs; /** * Creates a type node from a given schema. * Delegates to getBaseTypeFromSchema internally and * optionally adds a union with null. */ parse(props: SchemaProps$1): Schema[]; static deepSearch<T extends keyof SchemaKeywordMapper>(tree: Schema[] | undefined, keyword: T): Array<SchemaKeywordMapper[T]>; static find<T extends keyof SchemaKeywordMapper>(tree: Schema[] | undefined, keyword: T): SchemaKeywordMapper[T] | undefined; static combineObjects(tree: Schema[] | undefined): Schema[]; build(...generators: Array<Generator<TPluginOptions>>): Promise<Array<KubbFile.File<TFileMeta>>>; } //#endregion //#region ../plugin-oas/src/generators/createGenerator.d.ts type CoreGenerator<TOptions extends PluginFactoryOptions> = { name: string; type: 'core'; operations: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>; operation: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>; schema: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>; }; //#endregion //#region ../plugin-oas/src/generators/types.d.ts type OperationsProps<TOptions extends PluginFactoryOptions> = { config: Config; generator: Omit<OperationGenerator<TOptions>, 'build'>; plugin: Plugin<TOptions>; operations: Array<Operation>; }; type OperationProps<TOptions extends PluginFactoryOptions> = { config: Config; generator: Omit<OperationGenerator<TOptions>, 'build'>; plugin: Plugin<TOptions>; operation: Operation; }; type SchemaProps<TOptions extends PluginFactoryOptions> = { config: Config; generator: Omit<SchemaGenerator<SchemaGeneratorOptions, TOptions>, 'build'>; plugin: Plugin<TOptions>; schema: { name: string; tree: Array<Schema>; value: SchemaObject; }; }; type Generator<TOptions extends PluginFactoryOptions> = CoreGenerator<TOptions> | ReactGenerator<TOptions>; //#endregion //#region ../plugin-oas/src/generators/createReactGenerator.d.ts type ReactGenerator<TOptions extends PluginFactoryOptions> = { name: string; type: 'react'; Operations: (props: OperationsProps<TOptions>) => FabricReactNode; Operation: (props: OperationProps<TOptions>) => FabricReactNode; Schema: (props: SchemaProps<TOptions>) => FabricReactNode; }; //#endregion //#region src/generators/mutationGenerator.d.ts declare const mutationGenerator: ReactGenerator<PluginSwr>; //#endregion //#region src/generators/queryGenerator.d.ts declare const queryGenerator: ReactGenerator<PluginSwr>; //#endregion export { mutationGenerator, queryGenerator }; //# sourceMappingURL=generators.d.ts.map