Skip to content

Commit

Permalink
fix(core): add non readonly only inside implementation file when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Apr 11, 2023
1 parent d9f3b3d commit 3be6a54
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
11 changes: 2 additions & 9 deletions packages/core/src/getters/res-req-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,10 @@ export const getResReqTypes = (
const isFormUrlEncoded =
formUrlEncodedContentTypes.includes(contentType);

const imports = [
...resolvedValue.imports,
...(resolvedValue.hasReadonlyProps
? [{ name: 'NonReadonly' }]
: []),
];

if ((!isFormData && !isFormUrlEncoded) || !propName) {
return {
...resolvedValue,
imports,
imports: resolvedValue.imports,
contentType,
};
}
Expand All @@ -182,7 +175,7 @@ export const getResReqTypes = (

return {
...resolvedValue,
imports,
imports: resolvedValue.imports,
formData,
formUrlEncoded,
contentType,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/writers/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const writeSchemas = async ({
.sort()
.join('\n');

const fileContent = `${header}\n${exports}\n${getOrvalGeneratedTypes()}`;
const fileContent = `${header}\n${exports}`;

await fs.writeFile(schemaFilePath, fileContent);
} catch (e) {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/writers/single-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
upath,
} from '../utils';
import { generateTarget } from './target';
import { getOrvalGeneratedTypes } from './types';

export const writeSingleMode = async ({
builder,
Expand Down Expand Up @@ -90,6 +91,11 @@ export const writeSingleMode = async ({
data += generateMutatorImports({ mutators: formUrlEncoded });
}

if (implementation.includes('NonReadonly<')) {
data += getOrvalGeneratedTypes();
data += '\n';
}

if (!output.schemas && needSchema) {
data += generateModelsInline(builder.schemas);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/writers/split-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
upath,
} from '../utils';
import { generateTarget } from './target';
import { getOrvalGeneratedTypes } from './types';

export const writeSplitMode = async ({
builder,
Expand Down Expand Up @@ -102,6 +103,10 @@ export const writeSplitMode = async ({
});
}

if (implementation.includes('NonReadonly<')) {
implementationData += getOrvalGeneratedTypes();
}

implementationData += `\n${implementation}`;
mswData += `\n${implementationMSW}`;

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/writers/split-tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
upath,
} from '../utils';
import { generateTargetForTags } from './target-tags';
import { getOrvalGeneratedTypes } from './types';

export const writeSplitTagsMode = async ({
builder,
Expand Down Expand Up @@ -109,6 +110,11 @@ export const writeSplitTagsMode = async ({
});
}

if (implementation.includes('NonReadonly<')) {
implementationData += getOrvalGeneratedTypes();
implementationData += '\n';
}

implementationData += `\n${implementation}`;
mswData += `\n${implementationMSW}`;

Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/writers/tags-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
upath,
} from '../utils';
import { generateTargetForTags } from './target-tags';
import { getOrvalGeneratedTypes } from './types';

export const writeTagsMode = async ({
builder,
Expand Down Expand Up @@ -104,6 +105,12 @@ export const writeTagsMode = async ({
}

data += '\n\n';

if (implementation.includes('NonReadonly<')) {
data += getOrvalGeneratedTypes();
data += '\n';
}

data += implementation;

if (output.mock) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/writers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type UnionToIntersection<U> =
type DistributeReadOnlyOverUnions<T> = T extends any ? NonReadonly<T> : never;
type Writable<T> = Pick<T, WritableKeys<T>>;
export type NonReadonly<T> = [T] extends [UnionToIntersection<T>] ? {
type NonReadonly<T> = [T] extends [UnionToIntersection<T>] ? {
[P in keyof Writable<T>]: T[P] extends object
? NonReadonly<NonNullable<T[P]>>
: T[P];
Expand Down

1 comment on commit 3be6a54

@vercel
Copy link

@vercel vercel bot commented on 3be6a54 Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.