diff --git a/packages/mock/src/msw/index.ts b/packages/mock/src/msw/index.ts index 2c12c87eb..1a8ddf610 100644 --- a/packages/mock/src/msw/index.ts +++ b/packages/mock/src/msw/index.ts @@ -6,6 +6,7 @@ import { GeneratorImport, GeneratorOptions, GeneratorVerbOptions, + GlobalMockOptions, isFunction, isObject, pascal, @@ -15,20 +16,34 @@ import { getDelay } from '../delay'; import { getRouteMSW, overrideVarName } from '../faker/getters'; import { getMockDefinition, getMockOptionsDataOverride } from './mocks'; -const getMSWDependencies = (locale?: string): GeneratorDependency[] => [ - { - exports: [ - { name: 'http', values: true }, - { name: 'HttpResponse', values: true }, - { name: 'delay', values: true }, - ], - dependency: 'msw', - }, - { - exports: [{ name: 'faker', values: true }], - dependency: locale ? `@faker-js/faker/locale/${locale}` : '@faker-js/faker', - }, -]; +const getMSWDependencies = ( + options?: GlobalMockOptions, +): GeneratorDependency[] => { + const hasDelay = options?.delay !== false; + const locale = options?.locale; + + const exports = [ + { name: 'http', values: true }, + { name: 'HttpResponse', values: true }, + ]; + + if (hasDelay) { + exports.push({ name: 'delay', values: true }); + } + + return [ + { + exports, + dependency: 'msw', + }, + { + exports: [{ name: 'faker', values: true }], + dependency: locale + ? `@faker-js/faker/locale/${locale}` + : '@faker-js/faker', + }, + ]; +}; export const generateMSWImports: GenerateMockImports = ({ implementation, @@ -40,7 +55,7 @@ export const generateMSWImports: GenerateMockImports = ({ }) => { return generateDependencyImports( implementation, - [...getMSWDependencies(options?.locale), ...imports], + [...getMSWDependencies(options), ...imports], specsName, hasSchemaDir, isAllowSyntheticDefaultImports, @@ -112,7 +127,6 @@ const generateDefinition = ( : mockImplementations; const delay = getDelay(override, !isFunction(mock) ? mock : undefined); - const isHandlerOverridden = isReturnHttpResponse && !isTextPlain; const infoParam = 'info'; const handlerImplementation = ` export const ${handlerName} = (overrideResponse?: ${returnType} | ((${infoParam}: Parameters[1]>[0]) => Promise<${returnType}> | ${returnType})) => { diff --git a/tests/configs/mock.config.ts b/tests/configs/mock.config.ts index a48af5712..e185d6da9 100644 --- a/tests/configs/mock.config.ts +++ b/tests/configs/mock.config.ts @@ -29,6 +29,19 @@ export default defineConfig({ target: '../specifications/petstore.yaml', }, }, + endpointsNamedDelay: { + output: { + schemas: '../generated/mock/endpoints-named-delay/model', + target: '../generated/mock/endpoints-named-delay/endpoints.ts', + mock: { + type: 'msw', + delay: false, + }, + }, + input: { + target: '../specifications/endpoints-named-delay.yaml', + }, + }, petstoreEachHttpStatus: { output: { target: '../generated/mock/petstore-each-http-status/endpoints.ts', diff --git a/tests/specifications/endpoints-named-delay.yaml b/tests/specifications/endpoints-named-delay.yaml new file mode 100644 index 000000000..aae6dcb0a --- /dev/null +++ b/tests/specifications/endpoints-named-delay.yaml @@ -0,0 +1,26 @@ +openapi: '3.0.0' +info: + version: 1.0.0 + title: Swagger Delay in name + license: + name: MIT +servers: + - url: http://petstore.swagger.io/v1 +paths: + /delay-in-name: + get: + summary: Delay in name + description: If delay in orval config is set to false, do not import delay from msw + operationId: delayInName + tags: + - test + parameters: + - name: limit + in: query + description: How many items to return at one time (max 100) + required: false + schema: + type: string + responses: + '204': + description: Ok