@kubb/plugin-msw
Version:
Mock Service Worker (MSW) handlers generator plugin for Kubb, creating API mocks from OpenAPI specifications for frontend development and testing.
517 lines (516 loc) • 13.7 kB
TypeScript
import { _ as contentType, a as Output, c as ResolveNameParams, d as KubbEvents, f as BaseGenerator, g as SchemaObject, h as Operation, i as Group, m as HttpMethod, n as PluginManager, o as Plugin, p as Oas, r as Config, s as PluginFactoryOptions, t as FileMetaBase, u as AsyncEventEmitter } from "./index-BVhRy10l.js";
import { Fabric } from "@kubb/react-fabric";
import { KubbNode } from "@kubb/react-fabric/types";
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 {}
}
}
type ResolvePathOptions = {
pluginKey?: Plugin['key'];
group?: {
tag?: string;
path?: string;
};
type?: ResolveNameParams['type'];
};
/**
* `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;
};
declare class OperationGenerator<TPluginOptions extends PluginFactoryOptions = PluginFactoryOptions, TFileMeta extends FileMetaBase = FileMetaBase> extends BaseGenerator<TPluginOptions['resolvedOptions'], Context$1<TPluginOptions['resolvedOptions'], TPluginOptions>> {
#private;
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` will be used.
* When false a reference will be 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';
};
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';
unknownType: 'any' | 'unknown' | 'void';
emptySchemaType: 'any' | 'unknown' | 'void';
enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal';
enumSuffix?: string;
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(propertName) 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;
};
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>) => KubbNode;
Operation: (props: OperationProps<TOptions>) => KubbNode;
Schema: (props: SchemaProps<TOptions>) => KubbNode;
};
//#endregion
//#region src/types.d.ts
type Options = {
/**
* Specify the export location for the files and define the behavior of the output
* @default { path: 'mocks', barrelType: 'named' }
*/
output?: Output<Oas>;
/**
* Define which contentType should be used.
* By default, the first JSON valid mediaType will be used
*/
contentType?: contentType;
baseURL?: string;
/**
* Group the MSW mocks based on the provided name.
*/
group?: Group;
/**
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
*/
exclude?: Array<Exclude>;
/**
* Array containing include parameters to include tags/operations/methods/paths.
*/
include?: Array<Include>;
/**
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
*/
override?: Array<Override<ResolvedOptions>>;
transformers?: {
/**
* Customize the names based on the type that is provided by the plugin.
*/
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
};
/**
* Create `handlers.ts` file with all handlers grouped by methods.
* @default false
*/
handlers?: boolean;
/**
* Which parser should be used before returning the data to the `Response` of MSW.
* - `'faker'` will use `@kubb/plugin-faker` to generate the data for the response
* - `'data'` will use your custom data to generate the data for the response
* @default 'data'
*/
parser?: 'data' | 'faker';
/**
* Define some generators next to the msw generators
*/
generators?: Array<Generator<PluginMsw>>;
};
type ResolvedOptions = {
output: Output<Oas>;
group: Options['group'];
parser: NonNullable<Options['parser']>;
baseURL: Options['baseURL'] | undefined;
};
type PluginMsw = PluginFactoryOptions<'plugin-msw', Options, ResolvedOptions, never, ResolvePathOptions>;
//#endregion
export { PluginMsw as n, ReactGenerator as r, Options as t };
//# sourceMappingURL=types-CU7bNIp4.d.ts.map