Skip to content

Commit 02161de

Browse files
authoredJan 27, 2024
feat(msw): separated mock handler and made it reusable (#1182)
* feat(msw): separate mock handler and aggregate function definition in `split-tags` and `tags` modes * feat: support `split` mode
1 parent ee5334f commit 02161de

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed
 

‎packages/core/src/types.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ export type GeneratorTargetFull = {
526526
implementationMock: {
527527
function: string;
528528
handler: string;
529+
handlerName: string;
529530
};
530531
importsMock: GeneratorImport[];
531532
mutators?: GeneratorMutator[];
@@ -538,7 +539,11 @@ export type GeneratorTargetFull = {
538539
export type GeneratorOperation = {
539540
imports: GeneratorImport[];
540541
implementation: string;
541-
implementationMock: { function: string; handler: string };
542+
implementationMock: {
543+
function: string;
544+
handler: string;
545+
handlerName: string;
546+
};
542547
importsMock: GeneratorImport[];
543548
tags: string[];
544549
mutator?: GeneratorMutator;

‎packages/core/src/writers/target-tags.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const generateTargetTags = (
3939
implementationMock: {
4040
function: operation.implementationMock.function,
4141
handler: operation.implementationMock.handler,
42+
handlerName: ' ' + operation.implementationMock.handlerName,
4243
},
4344
};
4445

@@ -57,6 +58,10 @@ const generateTargetTags = (
5758
handler:
5859
currentOperation.implementationMock.handler +
5960
operation.implementationMock.handler,
61+
handlerName:
62+
currentOperation.implementationMock.handlerName +
63+
',\n ' +
64+
operation.implementationMock.handlerName,
6065
},
6166
mutators: operation.mutator
6267
? [...(currentOperation.mutators ?? []), operation.mutator]
@@ -150,9 +155,11 @@ export const generateTargetForTags = (
150155
implementationMock: {
151156
function: target.implementationMock.function,
152157
handler:
153-
header.implementationMock +
154158
target.implementationMock.handler +
159+
header.implementationMock +
160+
target.implementationMock.handlerName +
155161
footer.implementationMock,
162+
handlerName: target.implementationMock.handlerName,
156163
},
157164
imports: target.imports,
158165
importsMock: target.importsMock,

‎packages/core/src/writers/target.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export const generateTarget = (
3030
acc.implementation += operation.implementation + '\n';
3131
acc.implementationMock.function += operation.implementationMock.function;
3232
acc.implementationMock.handler += operation.implementationMock.handler;
33+
34+
const handlerNameSeparator = acc.implementationMock.handlerName.length
35+
? ',\n '
36+
: ' ';
37+
acc.implementationMock.handlerName +=
38+
handlerNameSeparator + operation.implementationMock.handlerName;
39+
3340
if (operation.mutator) {
3441
acc.mutators.push(operation.mutator);
3542
}
@@ -70,9 +77,12 @@ export const generateTarget = (
7077
titles,
7178
output: options,
7279
});
80+
7381
acc.implementation = header.implementation + acc.implementation;
7482
acc.implementationMock.handler =
75-
header.implementationMock + acc.implementationMock.handler;
83+
acc.implementationMock.handler +
84+
header.implementationMock +
85+
acc.implementationMock.handlerName;
7686

7787
const footer = builder.footer({
7888
outputClient: options?.client,
@@ -93,6 +103,7 @@ export const generateTarget = (
93103
implementationMock: {
94104
function: '',
95105
handler: '',
106+
handlerName: '',
96107
},
97108
importsMock: [],
98109
mutators: [],

‎packages/mock/src/msw/index.ts

+24-22
Original file line numberDiff line numberDiff line change
@@ -76,34 +76,36 @@ export const generateMSW = (
7676

7777
const functionName = `get${pascal(operationId)}Mock`;
7878

79+
const handlerName = `get${pascal(operationId)}MockHandler`;
80+
81+
const handlerImplementation = `
82+
export const ${handlerName} = http.${verb}('${route}', async () => {
83+
await delay(${getDelay(override, !isFunction(mock) ? mock : undefined)});
84+
return new HttpResponse(${
85+
value && value !== 'undefined'
86+
? isTextPlain
87+
? `${functionName}()`
88+
: `JSON.stringify(${functionName}())`
89+
: null
90+
},
91+
{
92+
status: 200,
93+
headers: {
94+
'Content-Type': '${isTextPlain ? 'text/plain' : 'application/json'}',
95+
}
96+
}
97+
)
98+
})
99+
`;
100+
79101
return {
80102
implementation: {
81103
function:
82104
value && value !== 'undefined'
83105
? `export const ${functionName} = () => (${value})\n\n`
84106
: '',
85-
handler: `http.${verb}('${route}', async () => {
86-
await delay(${getDelay(
87-
override,
88-
!isFunction(mock) ? mock : undefined,
89-
)});
90-
return new HttpResponse(${
91-
value && value !== 'undefined'
92-
? isTextPlain
93-
? `${functionName}()`
94-
: `JSON.stringify(${functionName}())`
95-
: null
96-
},
97-
{
98-
status: 200,
99-
headers: {
100-
'Content-Type': '${
101-
isTextPlain ? 'text/plain' : 'application/json'
102-
}',
103-
}
104-
}
105-
)
106-
}),`,
107+
handlerName: handlerName,
108+
handler: handlerImplementation,
107109
},
108110
imports,
109111
};

‎packages/orval/src/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export const generateClientFooter: GeneratorClientFooter = ({
121121
if (!footer) {
122122
return {
123123
implementation: '',
124-
implementationMock: `]\n`,
124+
implementationMock: `\n]\n`,
125125
};
126126
}
127127

0 commit comments

Comments
 (0)
Please sign in to comment.