Skip to content

Commit 3be6a54

Browse files
committedApr 11, 2023
fix(core): add non readonly only inside implementation file when needed
1 parent d9f3b3d commit 3be6a54

File tree

7 files changed

+28
-11
lines changed

7 files changed

+28
-11
lines changed
 

‎packages/core/src/getters/res-req-types.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,10 @@ export const getResReqTypes = (
148148
const isFormUrlEncoded =
149149
formUrlEncodedContentTypes.includes(contentType);
150150

151-
const imports = [
152-
...resolvedValue.imports,
153-
...(resolvedValue.hasReadonlyProps
154-
? [{ name: 'NonReadonly' }]
155-
: []),
156-
];
157-
158151
if ((!isFormData && !isFormUrlEncoded) || !propName) {
159152
return {
160153
...resolvedValue,
161-
imports,
154+
imports: resolvedValue.imports,
162155
contentType,
163156
};
164157
}
@@ -182,7 +175,7 @@ export const getResReqTypes = (
182175

183176
return {
184177
...resolvedValue,
185-
imports,
178+
imports: resolvedValue.imports,
186179
formData,
187180
formUrlEncoded,
188181
contentType,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export const writeSchemas = async ({
130130
.sort()
131131
.join('\n');
132132

133-
const fileContent = `${header}\n${exports}\n${getOrvalGeneratedTypes()}`;
133+
const fileContent = `${header}\n${exports}`;
134134

135135
await fs.writeFile(schemaFilePath, fileContent);
136136
} catch (e) {

‎packages/core/src/writers/single-mode.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
upath,
99
} from '../utils';
1010
import { generateTarget } from './target';
11+
import { getOrvalGeneratedTypes } from './types';
1112

1213
export const writeSingleMode = async ({
1314
builder,
@@ -90,6 +91,11 @@ export const writeSingleMode = async ({
9091
data += generateMutatorImports({ mutators: formUrlEncoded });
9192
}
9293

94+
if (implementation.includes('NonReadonly<')) {
95+
data += getOrvalGeneratedTypes();
96+
data += '\n';
97+
}
98+
9399
if (!output.schemas && needSchema) {
94100
data += generateModelsInline(builder.schemas);
95101
}

‎packages/core/src/writers/split-mode.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
upath,
99
} from '../utils';
1010
import { generateTarget } from './target';
11+
import { getOrvalGeneratedTypes } from './types';
1112

1213
export const writeSplitMode = async ({
1314
builder,
@@ -102,6 +103,10 @@ export const writeSplitMode = async ({
102103
});
103104
}
104105

106+
if (implementation.includes('NonReadonly<')) {
107+
implementationData += getOrvalGeneratedTypes();
108+
}
109+
105110
implementationData += `\n${implementation}`;
106111
mswData += `\n${implementationMSW}`;
107112

‎packages/core/src/writers/split-tags-mode.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
upath,
99
} from '../utils';
1010
import { generateTargetForTags } from './target-tags';
11+
import { getOrvalGeneratedTypes } from './types';
1112

1213
export const writeSplitTagsMode = async ({
1314
builder,
@@ -109,6 +110,11 @@ export const writeSplitTagsMode = async ({
109110
});
110111
}
111112

113+
if (implementation.includes('NonReadonly<')) {
114+
implementationData += getOrvalGeneratedTypes();
115+
implementationData += '\n';
116+
}
117+
112118
implementationData += `\n${implementation}`;
113119
mswData += `\n${implementationMSW}`;
114120

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

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
upath,
1010
} from '../utils';
1111
import { generateTargetForTags } from './target-tags';
12+
import { getOrvalGeneratedTypes } from './types';
1213

1314
export const writeTagsMode = async ({
1415
builder,
@@ -104,6 +105,12 @@ export const writeTagsMode = async ({
104105
}
105106

106107
data += '\n\n';
108+
109+
if (implementation.includes('NonReadonly<')) {
110+
data += getOrvalGeneratedTypes();
111+
data += '\n';
112+
}
113+
107114
data += implementation;
108115

109116
if (output.mock) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type UnionToIntersection<U> =
1919
type DistributeReadOnlyOverUnions<T> = T extends any ? NonReadonly<T> : never;
2020
2121
type Writable<T> = Pick<T, WritableKeys<T>>;
22-
export type NonReadonly<T> = [T] extends [UnionToIntersection<T>] ? {
22+
type NonReadonly<T> = [T] extends [UnionToIntersection<T>] ? {
2323
[P in keyof Writable<T>]: T[P] extends object
2424
? NonReadonly<NonNullable<T[P]>>
2525
: T[P];

1 commit comments

Comments
 (1)

vercel[bot] commented on Apr 11, 2023

@vercel[bot]
Please sign in to comment.