Skip to content

Commit

Permalink
feat(msw): define the return value type in the msw mock function. (o…
Browse files Browse the repository at this point in the history
…rval-labs#1171)

* feat: possible to overwrite value when creating an `object` with `msw` mock

* chore: changed determination of orverridability

* feat: specify return type in `msw` mock values generation function
  • Loading branch information
soartec-lab authored Jan 28, 2024
1 parent c7ccb65 commit 99cc5a6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
7 changes: 1 addition & 6 deletions packages/core/src/writers/split-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ export const writeSplitMode = async ({
});
mockData += builder.importsMock({
implementation: implementationMock,
imports: [
{
exports: importsMock,
dependency: relativeSchemasPath,
},
],
imports: [{ exports: imports, dependency: relativeSchemasPath }],
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/writers/split-tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ export const writeSplitTagsMode = async ({
});
mockData += builder.importsMock({
implementation: implementationMock,
imports: [
{
exports: importsMock,
dependency: relativeSchemasPath,
},
],
imports: importsForBuilder,
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,18 +50,20 @@ 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: [
{
exports: imports.filter(
(imp) =>
!importsMock.some((impMock) => imp.name === impMock.name),
),
dependency: schemasPathRelative,
},
],
imports: importsForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand All @@ -74,9 +76,7 @@ export const writeTagsMode = async ({
if (output.mock) {
data += builder.importsMock({
implementation: implementationMock,
imports: [
{ exports: importsMock, dependency: schemasPathRelative },
],
imports: importsForBuilder,
specsName,
hasSchemaDir: !!output.schemas,
isAllowSyntheticDefaultImports,
Expand Down
4 changes: 3 additions & 1 deletion packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export const generateMSW = (

const isResponseOverridable = value.includes(overrideVarName);

const returnType = response.definition.success;

const isTextPlain = response.contentTypes.includes('text/plain');

const functionName = `get${pascal(operationId)}Mock`;
Expand Down Expand Up @@ -104,7 +106,7 @@ export const ${handlerName} = http.${verb}('${route}', async () => {
implementation: {
function:
value && value !== 'undefined'
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse?: any` : ''}) => (${value})\n\n`
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse?: any` : ''}): ${returnType} => (${value})\n\n`
: '',
handlerName: handlerName,
handler: handlerImplementation,
Expand Down

0 comments on commit 99cc5a6

Please sign in to comment.