Skip to content

Commit

Permalink
fix: revert orval-labs#1171 to not specify return type in msw
Browse files Browse the repository at this point in the history
  • Loading branch information
soartec-lab committed Feb 5, 2024
1 parent cd0a56e commit c29ffec
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/writers/split-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ export const writeSplitMode = async ({
});
mockData += builder.importsMock({
implementation: implementationMock,
imports: [{ exports: imports, dependency: relativeSchemasPath }],
imports: [
{
exports: importsMock,
dependency: relativeSchemasPath,
},
],
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/writers/split-tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ export const writeSplitTagsMode = async ({
});
mockData += builder.importsMock({
implementation: implementationMock,
imports: importsForBuilder,
imports: [
{
exports: importsMock,
dependency: relativeSchemasPath,
},
],
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/writers/tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,18 @@ export const writeTagsMode = async ({
? upath.relativeSafe(dirname, getFileInfo(output.schemas).dirname)
: './' + filename + '.schemas';

const importsForBuilder = [
{
exports: imports.filter(
(imp) =>
!importsMock.some((impMock) => imp.name === impMock.name),
),
dependency: schemasPathRelative,
},
];

data += builder.imports({
client: output.client,
implementation,
imports: importsForBuilder,
imports: [
{
exports: imports.filter(
(imp) =>
!importsMock.some((impMock) => imp.name === impMock.name),
),
dependency: schemasPathRelative,
},
],
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand All @@ -76,7 +74,9 @@ export const writeTagsMode = async ({
if (output.mock) {
data += builder.importsMock({
implementation: implementationMock,
imports: importsForBuilder,
imports: [
{ exports: importsMock, dependency: schemasPathRelative },
],
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
7 changes: 3 additions & 4 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,18 @@ export const generateMSW = (
const isTextPlain = response.contentTypes.includes('text/plain');
const isReturnHttpResponse = value && value !== 'undefined';

const returnType = response.definition.success;
const functionName = `get${pascal(operationId)}Mock`;
const handlerName = `get${pascal(operationId)}MockHandler`;

const handlerImplementation = `
export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse?: ${returnType}` : ''}) => {
export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrideResponse: any = {}` : ''}) => {
return http.${verb}('${route}', async () => {
await delay(${getDelay(override, !isFunction(mock) ? mock : undefined)});
return new HttpResponse(${
isReturnHttpResponse
? isTextPlain
? `${functionName}()`
: `JSON.stringify(overrideResponse ? overrideResponse : ${functionName}())`
: `JSON.stringify(overrideResponse.length ? overrideResponse : ${functionName}())`
: null
},
{
Expand All @@ -104,7 +103,7 @@ export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrid
return {
implementation: {
function: isReturnHttpResponse
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''}): ${returnType} => (${value})\n\n`
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''}) => (${value})\n\n`
: '',
handlerName: handlerName,
handler: handlerImplementation,
Expand Down

0 comments on commit c29ffec

Please sign in to comment.