@newmo/graphql-fake-server 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -2
- package/README.md +0 -1
- package/dist/esm/bin.d.ts +3 -0
- package/dist/esm/bin.d.ts.map +1 -0
- package/dist/esm/bin.js +13 -0
- package/dist/esm/bin.js.map +1 -0
- package/dist/esm/cli.d.ts +13 -1
- package/dist/esm/cli.d.ts.map +1 -1
- package/dist/esm/cli.js +76 -74
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/index.d.ts +20 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +64 -10
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/logger.d.ts +14 -0
- package/dist/esm/logger.d.ts.map +1 -0
- package/dist/esm/logger.js +23 -0
- package/dist/esm/logger.js.map +1 -0
- package/package.json +80 -95
- package/src/bin.ts +13 -0
- package/src/cli.ts +84 -85
- package/src/index.ts +85 -13
- package/src/logger.ts +30 -0
- package/dist/esm/code-generator.d.ts +0 -4
- package/dist/esm/code-generator.d.ts.map +0 -1
- package/dist/esm/code-generator.js +0 -75
- package/dist/esm/code-generator.js.map +0 -1
- package/dist/esm/config.d.ts +0 -46
- package/dist/esm/config.d.ts.map +0 -1
- package/dist/esm/config.js +0 -57
- package/dist/esm/config.js.map +0 -1
- package/dist/esm/schema-scanner.d.ts +0 -32
- package/dist/esm/schema-scanner.d.ts.map +0 -1
- package/dist/esm/schema-scanner.js +0 -265
- package/dist/esm/schema-scanner.js.map +0 -1
- package/src/code-generator.ts +0 -82
- package/src/config.ts +0 -96
- package/src/schema-scanner.ts +0 -348
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
import { transformComment } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
-
import { Kind, } from 'graphql';
|
|
3
|
-
// The fork of https://github.com/dotansimha/graphql-code-generator/blob/e1dc75f3c598bf7f83138ca533619716fc73f823/packages/plugins/typescript/resolvers/src/visitor.ts#L85-L91
|
|
4
|
-
// The fork of https://github.com/dotansimha/graphql-code-generator/blob/ba84a3a2758d94dac27fcfbb1bafdf3ed7c32929/packages/plugins/other/visitor-plugin-common/src/base-visitor.ts#L422
|
|
5
|
-
function convertName(node, config) {
|
|
6
|
-
let convertedName = '';
|
|
7
|
-
convertedName += config.typesPrefix;
|
|
8
|
-
convertedName += config.convert(node);
|
|
9
|
-
convertedName += config.typesSuffix;
|
|
10
|
-
return convertedName;
|
|
11
|
-
}
|
|
12
|
-
const createIDFactory = () => {
|
|
13
|
-
const idMap = new Map();
|
|
14
|
-
return (name) => {
|
|
15
|
-
const count = idMap.get(name) ?? 0;
|
|
16
|
-
idMap.set(name, count + 1);
|
|
17
|
-
return `${name}${count}`;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
const parseTypeNodeStructure = (node) => {
|
|
21
|
-
if (node.kind === Kind.NON_NULL_TYPE) {
|
|
22
|
-
return parseTypeNodeStructure(node.type);
|
|
23
|
-
}
|
|
24
|
-
else if (node.kind === Kind.LIST_TYPE) {
|
|
25
|
-
return `array`;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
// string, number, boolean, null
|
|
29
|
-
if (node.name.value === "String") {
|
|
30
|
-
return "string";
|
|
31
|
-
}
|
|
32
|
-
if (node.name.value === "Int") {
|
|
33
|
-
return "number";
|
|
34
|
-
}
|
|
35
|
-
if (node.name.value === "Float") {
|
|
36
|
-
return "number";
|
|
37
|
-
}
|
|
38
|
-
if (node.name.value === "Boolean") {
|
|
39
|
-
return "boolean";
|
|
40
|
-
}
|
|
41
|
-
if (node.name.value === "ID") {
|
|
42
|
-
return "string";
|
|
43
|
-
}
|
|
44
|
-
return `object`;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
function valueOf(value) {
|
|
48
|
-
// object
|
|
49
|
-
if (value.kind === Kind.OBJECT) {
|
|
50
|
-
return value.fields.reduce((acc, field) => {
|
|
51
|
-
// @ts-expect-error TODO: nesting type
|
|
52
|
-
acc[field.name.value] = valueOf(field.value);
|
|
53
|
-
return acc;
|
|
54
|
-
}, {});
|
|
55
|
-
}
|
|
56
|
-
// list
|
|
57
|
-
if (value.kind === Kind.LIST) {
|
|
58
|
-
return value.values.map(v => {
|
|
59
|
-
return valueOf(v);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
// null
|
|
63
|
-
if (value.kind === Kind.NULL) {
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
// string
|
|
67
|
-
if (value.kind === Kind.STRING) {
|
|
68
|
-
return value.value;
|
|
69
|
-
}
|
|
70
|
-
// enum
|
|
71
|
-
if (value.kind === Kind.ENUM) {
|
|
72
|
-
return value.value;
|
|
73
|
-
}
|
|
74
|
-
// int
|
|
75
|
-
if (value.kind === Kind.INT) {
|
|
76
|
-
return Number.parseInt(value.value, 10);
|
|
77
|
-
}
|
|
78
|
-
// float
|
|
79
|
-
if (value.kind === Kind.FLOAT) {
|
|
80
|
-
return Number.parseFloat(value.value);
|
|
81
|
-
}
|
|
82
|
-
// boolean
|
|
83
|
-
if (value.kind === Kind.BOOLEAN) {
|
|
84
|
-
return value.value;
|
|
85
|
-
}
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
87
|
-
throw new Error(`Unknown kind of value ${value}`);
|
|
88
|
-
}
|
|
89
|
-
const typeToFunction = (type, config, idFactory) => {
|
|
90
|
-
switch (type) {
|
|
91
|
-
case "String":
|
|
92
|
-
return `"${config.defaultValues.String}"`;
|
|
93
|
-
case "Int":
|
|
94
|
-
return `${config.defaultValues.Int}`;
|
|
95
|
-
case "Float":
|
|
96
|
-
return `${config.defaultValues.Float}`;
|
|
97
|
-
case "Boolean":
|
|
98
|
-
return `${config.defaultValues.Boolean ? "true" : "false"}`;
|
|
99
|
-
case "ID":
|
|
100
|
-
return `"${idFactory(config.defaultValues.ID)}"`;
|
|
101
|
-
default:
|
|
102
|
-
// reference to the object
|
|
103
|
-
return `${type}`;
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
const typeToFunctionWithArray = (type, config, idFactory) => {
|
|
107
|
-
return `Array.from({ length: ${config.defaultValues.listLength} }).map(() => ${typeToFunction(type, config, idFactory)})`;
|
|
108
|
-
};
|
|
109
|
-
// NamedType/ListType handling
|
|
110
|
-
const nodeToExpression = ({ currentNode, isArray = false, config, idFactory }) => {
|
|
111
|
-
if (currentNode.kind === "NonNullType") {
|
|
112
|
-
return nodeToExpression({
|
|
113
|
-
currentNode: currentNode.type,
|
|
114
|
-
isArray,
|
|
115
|
-
config,
|
|
116
|
-
idFactory
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
else if (currentNode.kind === "NamedType") {
|
|
120
|
-
if (isArray) {
|
|
121
|
-
return {
|
|
122
|
-
expression: typeToFunctionWithArray(currentNode.name.value, config, idFactory)
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
return {
|
|
127
|
-
expression: typeToFunction(currentNode.name.value, config, idFactory)
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
else if (currentNode.kind === "ListType") {
|
|
132
|
-
return nodeToExpression({ currentNode: currentNode.type, isArray: true, config, idFactory });
|
|
133
|
-
}
|
|
134
|
-
throw new Error("Unknown node kind");
|
|
135
|
-
};
|
|
136
|
-
const SUPPORTED_EXAMPLE_DIRECTIVES = ["exampleID", "exampleString", "exampleInt", "exampleFloat", "exampleBoolean"];
|
|
137
|
-
const isIdType = (node) => {
|
|
138
|
-
if (node.kind === "NonNullType") {
|
|
139
|
-
return isIdType(node.type);
|
|
140
|
-
}
|
|
141
|
-
else if (node.kind === "NamedType") {
|
|
142
|
-
return node.name.value === "ID";
|
|
143
|
-
}
|
|
144
|
-
else if (node.kind === "ListType") {
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
return false;
|
|
148
|
-
};
|
|
149
|
-
function parseFieldOrInputValueDefinition({ node, convertedTypeName, config, idFactory }) {
|
|
150
|
-
const exampleDirective = node.directives?.find(d => {
|
|
151
|
-
return SUPPORTED_EXAMPLE_DIRECTIVES.includes(d.name.value);
|
|
152
|
-
});
|
|
153
|
-
// fake
|
|
154
|
-
// if @example directive is not found, return random value for the scalar type
|
|
155
|
-
if (!exampleDirective) {
|
|
156
|
-
return {
|
|
157
|
-
example: nodeToExpression({ currentNode: node.type, config, idFactory })
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
if (!exampleDirective.arguments) {
|
|
161
|
-
throw new Error("@example directive must have arguments");
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* @exampleID(value: "id")
|
|
165
|
-
* -> { value: "id1" }
|
|
166
|
-
* @exampleString(value: "value")
|
|
167
|
-
* -> { value: "value" }
|
|
168
|
-
* @exampleInt(value: 1)
|
|
169
|
-
* -> { value: 1 }
|
|
170
|
-
* @exampleFloat(value: 1.1)
|
|
171
|
-
* -> { value: 1.1 }
|
|
172
|
-
* @exampleBoolean(value: true)
|
|
173
|
-
* -> { value: true }
|
|
174
|
-
*/
|
|
175
|
-
const value = exampleDirective.arguments.find(a => a.name.value === "value");
|
|
176
|
-
if (!value) {
|
|
177
|
-
throw new Error(`@${exampleDirective.name.value} directive must have value argument.`);
|
|
178
|
-
}
|
|
179
|
-
const rawValue = valueOf(value.value);
|
|
180
|
-
// if node type is not equal to the value type, throw an error
|
|
181
|
-
const nodeType = parseTypeNodeStructure(node.type);
|
|
182
|
-
const fieldName = node.name.value;
|
|
183
|
-
// array, object, string, number, boolean, null
|
|
184
|
-
const rawValueType = Object.prototype.toString.call(rawValue).slice(8, -1).toLowerCase();
|
|
185
|
-
if (nodeType !== rawValueType) {
|
|
186
|
-
throw new Error(`${convertedTypeName}.${fieldName}: @${exampleDirective.name.value} directive value type must be ${nodeType}. Got ${rawValueType}`);
|
|
187
|
-
}
|
|
188
|
-
// if ID type, add idFactory() to the value
|
|
189
|
-
// e.g. @exampleID(value: "id") -> { value: "id1" }
|
|
190
|
-
const isExampleIdDirective = exampleDirective.name.value === "exampleID";
|
|
191
|
-
const exampleValue = isExampleIdDirective && typeof rawValue === "string" ? `${idFactory(rawValue)}` : rawValue;
|
|
192
|
-
return { example: { value: exampleValue } };
|
|
193
|
-
}
|
|
194
|
-
function parseObjectTypeOrInputObjectTypeDefinition({ node, config, idFactory }) {
|
|
195
|
-
const originalTypeName = node.name.value;
|
|
196
|
-
const convertedTypeName = convertName(originalTypeName, config);
|
|
197
|
-
return {
|
|
198
|
-
type: 'object',
|
|
199
|
-
name: originalTypeName,
|
|
200
|
-
fields: [
|
|
201
|
-
...(node.fields ?? []).map((field) => ({
|
|
202
|
-
name: field.name.value,
|
|
203
|
-
...parseFieldOrInputValueDefinition({
|
|
204
|
-
node: field,
|
|
205
|
-
convertedTypeName,
|
|
206
|
-
config,
|
|
207
|
-
idFactory
|
|
208
|
-
}),
|
|
209
|
-
})),
|
|
210
|
-
],
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
export function getTypeInfos(config, schema) {
|
|
214
|
-
const types = Object.values(schema.getTypeMap());
|
|
215
|
-
const idFactory = createIDFactory();
|
|
216
|
-
const userDefinedTypeDefinitions = types
|
|
217
|
-
.map((type) => type.astNode)
|
|
218
|
-
.filter((node) => {
|
|
219
|
-
if (!node)
|
|
220
|
-
return false;
|
|
221
|
-
return (node.kind === Kind.OBJECT_TYPE_DEFINITION ||
|
|
222
|
-
node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ||
|
|
223
|
-
node.kind === Kind.INTERFACE_TYPE_DEFINITION ||
|
|
224
|
-
node.kind === Kind.UNION_TYPE_DEFINITION);
|
|
225
|
-
});
|
|
226
|
-
const objectTypeDefinitions = userDefinedTypeDefinitions.filter((node) => {
|
|
227
|
-
if (!node)
|
|
228
|
-
return false;
|
|
229
|
-
return node.kind === Kind.OBJECT_TYPE_DEFINITION;
|
|
230
|
-
});
|
|
231
|
-
return types
|
|
232
|
-
.map((type) => type.astNode)
|
|
233
|
-
.filter((node) => {
|
|
234
|
-
if (!node)
|
|
235
|
-
return false;
|
|
236
|
-
return (node.kind === Kind.OBJECT_TYPE_DEFINITION ||
|
|
237
|
-
node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ||
|
|
238
|
-
node.kind === Kind.INTERFACE_TYPE_DEFINITION ||
|
|
239
|
-
node.kind === Kind.UNION_TYPE_DEFINITION);
|
|
240
|
-
})
|
|
241
|
-
.map((node) => {
|
|
242
|
-
if (node?.kind === Kind.OBJECT_TYPE_DEFINITION || node?.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION) {
|
|
243
|
-
return parseObjectTypeOrInputObjectTypeDefinition({ node, config, idFactory });
|
|
244
|
-
}
|
|
245
|
-
else if (node?.kind === Kind.INTERFACE_TYPE_DEFINITION) {
|
|
246
|
-
return {
|
|
247
|
-
type: 'abstract',
|
|
248
|
-
name: convertName(node.name.value, config),
|
|
249
|
-
possibleTypes: objectTypeDefinitions
|
|
250
|
-
.filter((objectTypeDefinitionNode) => (objectTypeDefinitionNode.interfaces ?? []).some((i) => i.name.value === node.name.value))
|
|
251
|
-
.map((objectTypeDefinitionNode) => convertName(objectTypeDefinitionNode.name.value, config)),
|
|
252
|
-
comment: node.description ? transformComment(node.description) : undefined,
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
else {
|
|
256
|
-
return {
|
|
257
|
-
type: 'abstract',
|
|
258
|
-
name: convertName(node.name.value, config),
|
|
259
|
-
possibleTypes: (node.types ?? []).map((type) => convertName(type.name.value, config)),
|
|
260
|
-
comment: node.description ? transformComment(node.description) : undefined,
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
//# sourceMappingURL=schema-scanner.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schema-scanner.js","sourceRoot":"","sources":["../../src/schema-scanner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gB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W,CAAC,wBAAwB,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;gBAChG,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,gBAAgB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS;aAC7E,CAAC;QACN,CAAC;aAAM,CAAC;YACJ,OAAO;gBACH,IAAI,EAAE,UAAU;gBAChB,IAAI,EAAE,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC;gBAC1C,aAAa,EAAE,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;gBACrF,OAAO,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,gBAAgB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,SAAS;aAC7E,CAAC;QACN,CAAC;IACL,CAAC,CAAC,CAAC;AACX,CAAC"}
|
package/src/code-generator.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { Config } from './config.js';
|
|
2
|
-
import { ExampleDirective, ObjectTypeInfo, TypeInfo } from './schema-scanner.js';
|
|
3
|
-
import { GraphQLSchema } from "graphql/index.js";
|
|
4
|
-
|
|
5
|
-
function generatePreludeCode(config: Config, typeInfos: TypeInfo[]): string {
|
|
6
|
-
const joinedTypeNames = typeInfos
|
|
7
|
-
.filter(({ type }) => type === 'object')
|
|
8
|
-
.map(({ name }) => ` ${name}`)
|
|
9
|
-
.join(',\n');
|
|
10
|
-
const code = `
|
|
11
|
-
import type {
|
|
12
|
-
${joinedTypeNames},
|
|
13
|
-
} from '${config.typesFile}';
|
|
14
|
-
`.trim();
|
|
15
|
-
return `${code}\n`;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const handleExample = (exampleDirective: ExampleDirective): string => {
|
|
19
|
-
if ("value" in exampleDirective) {
|
|
20
|
-
return JSON.stringify(exampleDirective.value);
|
|
21
|
-
} else if ("expression" in exampleDirective) {
|
|
22
|
-
return exampleDirective.expression;
|
|
23
|
-
}
|
|
24
|
-
throw new Error(`Invalid example directive${JSON.stringify(exampleDirective)}`);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function generateApolloFakeServer(config: Config, typeInfo: ObjectTypeInfo) {
|
|
28
|
-
const header = `import { ApolloServer } from '@apollo/server';
|
|
29
|
-
import { startStandaloneServer } from '@apollo/server/standalone';
|
|
30
|
-
import { addMocksToSchema } from '@graphql-tools/mock';
|
|
31
|
-
import { makeExecutableSchema } from '@graphql-tools/schema';
|
|
32
|
-
`;
|
|
33
|
-
|
|
34
|
-
const body = `
|
|
35
|
-
const server = new ApolloServer({
|
|
36
|
-
schema: addMocksToSchema({
|
|
37
|
-
schema: makeExecutableSchema({ typeDefs }),
|
|
38
|
-
mocks,
|
|
39
|
-
}),
|
|
40
|
-
});
|
|
41
|
-
const { url } = await startStandaloneServer(server, { listen: { port: 4000 } });
|
|
42
|
-
console.log(\`🚀 Server listening at: \${url}\`);
|
|
43
|
-
`;
|
|
44
|
-
return {
|
|
45
|
-
header,
|
|
46
|
-
body
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function generateExampleCode(config: Config, typeInfo: ObjectTypeInfo): string {
|
|
51
|
-
const { name } = typeInfo;
|
|
52
|
-
const indent = ' ';
|
|
53
|
-
return `
|
|
54
|
-
/**
|
|
55
|
-
* Default ${name} model using @example directive.
|
|
56
|
-
*/
|
|
57
|
-
const ${name} = {
|
|
58
|
-
${typeInfo.fields.flatMap((field) => {
|
|
59
|
-
const example = field.example;
|
|
60
|
-
if (example) {
|
|
61
|
-
return [`${indent}${field.name}: ${handleExample(example)}`];
|
|
62
|
-
}
|
|
63
|
-
return [];
|
|
64
|
-
}).join(',\n')}
|
|
65
|
-
};
|
|
66
|
-
exports.${name} = ${name};
|
|
67
|
-
`.trimStart();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function generateCode(config: Config, typeInfos: TypeInfo[]): string {
|
|
71
|
-
let code = '';
|
|
72
|
-
// code += generatePreludeCode(config, typeInfos);
|
|
73
|
-
// code += apolloFakeServer.header;
|
|
74
|
-
// code += '\n';
|
|
75
|
-
for (const typeInfo of typeInfos) {
|
|
76
|
-
if (typeInfo.type === 'object') {
|
|
77
|
-
code += generateExampleCode(config, typeInfo);
|
|
78
|
-
code += '\n';
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return code;
|
|
82
|
-
}
|
package/src/config.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { ConvertFn, RawTypesConfig, convertFactory } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
-
|
|
3
|
-
export type RawConfig = {
|
|
4
|
-
typesFile: string;
|
|
5
|
-
skipTypename?: RawTypesConfig['skipTypename'];
|
|
6
|
-
skipIsAbstractType?: boolean | undefined;
|
|
7
|
-
nonOptionalDefaultFields?: boolean | undefined;
|
|
8
|
-
namingConvention?: RawTypesConfig['namingConvention'];
|
|
9
|
-
typesPrefix?: RawTypesConfig['typesPrefix'];
|
|
10
|
-
typesSuffix?: RawTypesConfig['typesSuffix'];
|
|
11
|
-
// TODO: support addIsAbstractType
|
|
12
|
-
defaultValues?: {
|
|
13
|
-
String?: string
|
|
14
|
-
Int?: number
|
|
15
|
-
Float?: number
|
|
16
|
-
Boolean?: boolean
|
|
17
|
-
ID?: string
|
|
18
|
-
listLength?: number
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const DefaultValues = {
|
|
23
|
-
String: "string",
|
|
24
|
-
Int: 42,
|
|
25
|
-
Float: 4.2,
|
|
26
|
-
Boolean: true,
|
|
27
|
-
ID: "xxxx-xxxx-xxxx-xxxx",
|
|
28
|
-
listLength: 3
|
|
29
|
-
}
|
|
30
|
-
export type Config = {
|
|
31
|
-
typesFile: string;
|
|
32
|
-
skipTypename: Exclude<RawTypesConfig['skipTypename'], undefined>;
|
|
33
|
-
skipIsAbstractType: boolean;
|
|
34
|
-
nonOptionalDefaultFields: boolean;
|
|
35
|
-
typesPrefix: Exclude<RawTypesConfig['typesPrefix'], undefined>;
|
|
36
|
-
typesSuffix: Exclude<RawTypesConfig['typesSuffix'], undefined>;
|
|
37
|
-
convert: ConvertFn;
|
|
38
|
-
// TODO: support addIsAbstractType
|
|
39
|
-
defaultValues: {
|
|
40
|
-
String: string
|
|
41
|
-
Int: number
|
|
42
|
-
Float: number
|
|
43
|
-
Boolean: boolean
|
|
44
|
-
ID: string,
|
|
45
|
-
listLength: number
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export function validateConfig(rawConfig: unknown): asserts rawConfig is RawConfig {
|
|
50
|
-
if (typeof rawConfig !== 'object' || rawConfig === null) {
|
|
51
|
-
throw new Error('`options` must be an object');
|
|
52
|
-
}
|
|
53
|
-
if (!('typesFile' in rawConfig)) {
|
|
54
|
-
throw new Error('`option.typesFile` is required');
|
|
55
|
-
}
|
|
56
|
-
if (typeof rawConfig['typesFile'] !== 'string') {
|
|
57
|
-
throw new Error('`options.typesFile` must be a string');
|
|
58
|
-
}
|
|
59
|
-
if ('skipTypename' in rawConfig && typeof rawConfig['skipTypename'] !== 'boolean') {
|
|
60
|
-
throw new Error('`options.skipTypename` must be a boolean');
|
|
61
|
-
}
|
|
62
|
-
if ('skipIsAbstractType' in rawConfig && typeof rawConfig['skipIsAbstractType'] !== 'boolean') {
|
|
63
|
-
throw new Error('`options.skipIsAbstractType` must be a boolean');
|
|
64
|
-
}
|
|
65
|
-
if ('nonOptionalDefaultFields' in rawConfig && typeof rawConfig['nonOptionalDefaultFields'] !== 'boolean') {
|
|
66
|
-
throw new Error('`options.nonOptionalDefaultFields` must be a boolean');
|
|
67
|
-
}
|
|
68
|
-
if ('typesPrefix' in rawConfig && typeof rawConfig['typesPrefix'] !== 'string') {
|
|
69
|
-
throw new Error('`options.typesPrefix` must be a string');
|
|
70
|
-
}
|
|
71
|
-
if ('typesSuffix' in rawConfig && typeof rawConfig['typesSuffix'] !== 'string') {
|
|
72
|
-
throw new Error('`options.typesSuffix` must be a string');
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function normalizeConfig(rawConfig: RawConfig): Config {
|
|
77
|
-
return {
|
|
78
|
-
typesFile: rawConfig.typesFile,
|
|
79
|
-
skipTypename: rawConfig.skipTypename ?? false,
|
|
80
|
-
skipIsAbstractType: rawConfig.skipIsAbstractType ?? true,
|
|
81
|
-
nonOptionalDefaultFields: rawConfig.nonOptionalDefaultFields ?? false,
|
|
82
|
-
typesPrefix: rawConfig.typesPrefix ?? '',
|
|
83
|
-
typesSuffix: rawConfig.typesSuffix ?? '',
|
|
84
|
-
convert: rawConfig.namingConvention
|
|
85
|
-
? convertFactory({ namingConvention: rawConfig.namingConvention })
|
|
86
|
-
: convertFactory({}),
|
|
87
|
-
defaultValues: {
|
|
88
|
-
String: rawConfig.defaultValues?.String ?? DefaultValues.String,
|
|
89
|
-
Int: rawConfig.defaultValues?.Int ?? DefaultValues.Int,
|
|
90
|
-
Float: rawConfig.defaultValues?.Float ?? DefaultValues.Float,
|
|
91
|
-
Boolean: rawConfig.defaultValues?.Boolean ?? DefaultValues.Boolean,
|
|
92
|
-
ID: rawConfig.defaultValues?.ID ?? DefaultValues.ID,
|
|
93
|
-
listLength: rawConfig.defaultValues?.listLength ?? DefaultValues.listLength
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|