Skip to content

Commit

Permalink
fix(msw): if no success response apply a default 200 status
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Jun 7, 2024
1 parent e3e680f commit e236c5e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ const generateDefinition = (
const isTextPlain = contentTypes.includes('text/plain');
const isReturnHttpResponse = value && value !== 'undefined';

const getResponseMockFunctionName = `${getResponseMockFunctionNameBase}${pascal(name)}`;
const getResponseMockFunctionName = `${getResponseMockFunctionNameBase}${pascal(
name,
)}`;
const handlerName = `${handlerNameBase}${pascal(name)}`;

const addedSplitMockImplementations = splitMockImplementations.slice(
Expand All @@ -102,15 +104,25 @@ const generateDefinition = (
: '';

const mockImplementation = isReturnHttpResponse
? `${mockImplementations}export const ${getResponseMockFunctionName} = (${isResponseOverridable ? `overrideResponse: Partial< ${returnType} > = {}` : ''})${mockData ? '' : `: ${returnType}`} => (${value})\n\n`
? `${mockImplementations}export const ${getResponseMockFunctionName} = (${
isResponseOverridable
? `overrideResponse: Partial< ${returnType} > = {}`
: ''
})${mockData ? '' : `: ${returnType}`} => (${value})\n\n`
: mockImplementations;

const delay = getDelay(override, !isFunction(mock) ? mock : undefined);
const isHandlerOverridden = isReturnHttpResponse && !isTextPlain;
const infoParam = isHandlerOverridden ? 'info' : '';
const handlerImplementation = `
export const ${handlerName} = (${isHandlerOverridden ? `overrideResponse?: ${returnType} | ((${infoParam}: Parameters<Parameters<typeof http.${verb}>[1]>[0]) => Promise<${returnType}> | ${returnType})` : ''}) => {
return http.${verb}('${route}', ${(isReturnHttpResponse && !isTextPlain) || delay !== false ? 'async' : ''} (${infoParam}) => {${
export const ${handlerName} = (${
isHandlerOverridden
? `overrideResponse?: ${returnType} | ((${infoParam}: Parameters<Parameters<typeof http.${verb}>[1]>[0]) => Promise<${returnType}> | ${returnType})`
: ''
}) => {
return http.${verb}('${route}', ${
(isReturnHttpResponse && !isTextPlain) || delay !== false ? 'async' : ''
} (${infoParam}) => {${
delay !== false
? `await delay(${isFunction(delay) ? `(${delay})()` : delay});`
: ''
Expand Down Expand Up @@ -183,7 +195,7 @@ export const generateMSW = (
generatorVerbOptions,
generatorOptions,
response.definition.success,
response.types.success[0].key,
response.types.success[0]?.key ?? '200',
response.imports,
response.types.success,
response.contentTypes,
Expand Down

0 comments on commit e236c5e

Please sign in to comment.