Skip to content

Commit

Permalink
fix(msw): do not import delay if not needed (#1643)
Browse files Browse the repository at this point in the history
* do not import delay if not needed

* fixup! do not import delay if not needed

* fixup! fixup! do not import delay if not needed

* fixup! fixup! fixup! do not import delay if not needed
  • Loading branch information
karlismelderis-mckinsey authored Sep 27, 2024
1 parent b37b2b3 commit a752a3e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 16 deletions.
46 changes: 30 additions & 16 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GeneratorImport,
GeneratorOptions,
GeneratorVerbOptions,
GlobalMockOptions,
isFunction,
isObject,
pascal,
Expand All @@ -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,
Expand All @@ -40,7 +55,7 @@ export const generateMSWImports: GenerateMockImports = ({
}) => {
return generateDependencyImports(
implementation,
[...getMSWDependencies(options?.locale), ...imports],
[...getMSWDependencies(options), ...imports],
specsName,
hasSchemaDir,
isAllowSyntheticDefaultImports,
Expand Down Expand Up @@ -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<Parameters<typeof http.${verb}>[1]>[0]) => Promise<${returnType}> | ${returnType})) => {
Expand Down
13 changes: 13 additions & 0 deletions tests/configs/mock.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
26 changes: 26 additions & 0 deletions tests/specifications/endpoints-named-delay.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a752a3e

Please sign in to comment.