Skip to content

Commit

Permalink
feat: enhance overrideResponse assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
DevSDK committed Feb 5, 2024
1 parent 84a9ac7 commit 6535b03
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
24 changes: 24 additions & 0 deletions packages/mock/src/createOverrideVariable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { overrideVarName } from './faker/getters';
import { MockSchemaObject } from './types';

export function createOverrideVariable(path?: string) {
let index = 0;

if (!path) {
return overrideVarName;
}

return (
overrideVarName +
path
?.replace('#.', '')
.split('.')
.reduce((acc, key) => {
if (key !== '[]') {
return acc + `?.['${key}']`;
} else {
return acc + `?.[index_${index++}]`;
}
}, '')
);
}
2 changes: 0 additions & 2 deletions packages/mock/src/faker/getters/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ export const getMockObject = ({
})
.filter(Boolean);

properyScalars.push(`...${overrideVarName}`);

value += properyScalars.join(', ');
value +=
!combine ||
Expand Down
7 changes: 4 additions & 3 deletions packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { MockDefinition, MockSchemaObject } from '../../types';
import { getMockObject } from './object';
import { DEFAULT_FORMAT_MOCK } from '../constants';
import { createOverrideVariable } from '../../createOverrideVariable';

export const getMockScalar = ({
item,
Expand Down Expand Up @@ -195,10 +196,10 @@ export const getMockScalar = ({

return {
value:
`Array.from({ length: faker.number.int({ ` +
`Array.from({ length: Math.max(${createOverrideVariable(item.path)}?.length ?? 0, faker.number.int({ ` +
`min: ${mockOptions?.arrayMin}, ` +
`max: ${mockOptions?.arrayMax} }) ` +
`}, (_, i) => i + 1).map(() => (${mapValue}))`,
`max: ${mockOptions?.arrayMax} })) ` +
`}, (_, i) => i + 1).map((_, index_${item.path?.match(/\[\]/g)?.length ?? 0}) => (${mapValue}))`,
imports: resolvedImports,
name: item.name,
};
Expand Down
11 changes: 8 additions & 3 deletions packages/mock/src/msw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const getMSWDependencies = (locale?: string): GeneratorDependency[] => [
exports: [{ name: 'faker', values: true }],
dependency: locale ? `@faker-js/faker/locale/${locale}` : '@faker-js/faker',
},
{
exports: [{ name: 'merge', default: true, values: true }],
dependency: 'lodash/merge',
},
];

export const generateMSWImports: GenerateMockImports = ({
Expand Down Expand Up @@ -103,9 +107,10 @@ export const ${handlerName} = (${isReturnHttpResponse && !isTextPlain ? `overrid

return {
implementation: {
function: isReturnHttpResponse
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''}): ${returnType} => (${value})\n\n`
: '',
function:
value && value !== 'undefined'
? `export const ${functionName} = (${isResponseOverridable ? `overrideResponse: any = {}` : ''}): ${returnType} => ${isResponseOverridable ? `merge((${value}), overrideResponse)` : `(${value}) as any`}\n\n`
: '',
handlerName: handlerName,
handler: handlerImplementation,
},
Expand Down

0 comments on commit 6535b03

Please sign in to comment.