-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
utils.ts
275 lines (217 loc) · 6.8 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { pascalCase } from 'change-case';
import {
NameNode,
Kind,
TypeNode,
NamedTypeNode,
isNonNullType,
GraphQLObjectType,
GraphQLNonNull,
GraphQLList,
isListType,
GraphQLOutputType,
GraphQLNamedType,
isScalarType,
GraphQLSchema,
GraphQLScalarType,
StringValueNode,
isEqualType,
} from 'graphql';
import { ScalarsMap } from './types';
function isWrapperType(t: GraphQLOutputType): t is GraphQLNonNull<any> | GraphQLList<any> {
return isListType(t) || isNonNullType(t);
}
export const getConfigValue = <T = any>(value: T, defaultValue: T): T => {
if (value === null || value === undefined) {
return defaultValue;
}
return value;
};
export function getBaseType(type: GraphQLOutputType): GraphQLNamedType {
if (isWrapperType(type)) {
return getBaseType(type.ofType);
} else {
return type;
}
}
export function quoteIfNeeded(array: string[], joinWith = ' & '): string {
if (array.length === 0) {
return '';
} else if (array.length === 1) {
return array[0];
} else {
return `(${array.join(joinWith)})`;
}
}
export function block(array) {
return array && array.length !== 0 ? '{\n' + array.join('\n') + '\n}' : '';
}
export function wrapWithSingleQuotes(str: string | NameNode): string {
return `'${str}'`;
}
export function breakLine(str: string): string {
return str + '\n';
}
export function indent(str: string, count = 1): string {
return new Array(count).fill(' ').join('') + str;
}
export function indentMultiline(str: string, count = 1): string {
const indentation = new Array(count).fill(' ').join('');
const replaceWith = '\n' + indentation;
return indentation + str.replace(/\n/g, replaceWith);
}
export interface DeclarationBlockConfig {
blockWrapper?: string;
blockTransformer?: (block: string) => string;
enumNameValueSeparator?: string;
}
export function transformComment(comment: string | StringValueNode, indentLevel = 0): string {
if (!comment || comment === '') {
return '';
}
if (isStringValueNode(comment)) {
comment = comment.value;
}
const lines = comment.split('\n');
return lines
.map((line, index) => {
const isLast = lines.length === index + 1;
const isFirst = index === 0;
if (isFirst && isLast) {
return indent(`/** ${comment} */\n`, indentLevel);
}
return indent(`${isFirst ? '/** ' : ' * '}${line}${isLast ? '\n */\n' : ''}`, indentLevel);
})
.join('\n');
}
export class DeclarationBlock {
_export = false;
_name = null;
_kind = null;
_methodName = null;
_content = null;
_block = null;
_nameGenerics = null;
_comment = null;
_ignoreBlockWrapper = false;
constructor(private _config: DeclarationBlockConfig) {
this._config = {
blockWrapper: '',
blockTransformer: block => block,
enumNameValueSeparator: ':',
...this._config,
};
}
export(exp = true): DeclarationBlock {
this._export = exp;
return this;
}
asKind(kind: string): DeclarationBlock {
this._kind = kind;
return this;
}
withComment(comment: string | StringValueNode | null): DeclarationBlock {
if (comment) {
this._comment = transformComment(comment, 0);
}
return this;
}
withMethodCall(methodName: string, ignoreBlockWrapper = false): DeclarationBlock {
this._methodName = methodName;
this._ignoreBlockWrapper = ignoreBlockWrapper;
return this;
}
withBlock(block: string): DeclarationBlock {
this._block = block;
return this;
}
withContent(content: string): DeclarationBlock {
this._content = content;
return this;
}
withName(name: string | NameNode, generics: string | null = null): DeclarationBlock {
this._name = name;
this._nameGenerics = generics;
return this;
}
public get string(): string {
let result = '';
if (this._export) {
result += 'export ';
}
if (this._kind) {
let extra = '';
let name = '';
if (['type', 'const', 'var', 'let'].includes(this._kind)) {
extra = '= ';
}
if (this._name) {
name = this._name + (this._nameGenerics || '') + ' ';
}
result += this._kind + ' ' + name + extra;
}
if (this._block) {
if (this._content) {
result += this._content;
}
const blockWrapper = this._ignoreBlockWrapper ? '' : this._config.blockWrapper;
const before = '{' + blockWrapper;
const after = blockWrapper + '}';
const block = [before, this._block, after].filter(val => !!val).join('\n');
if (this._methodName) {
result += `${this._methodName}(${this._config.blockTransformer!(block)})`;
} else {
result += this._config.blockTransformer!(block);
}
} else if (this._content) {
result += this._content;
} else if (this._kind) {
result += '{}';
}
return (this._comment ? this._comment : '') + result + (this._kind === 'interface' || this._kind === 'enum' || this._kind === 'namespace' ? '' : ';') + '\n';
}
}
export function getBaseTypeNode(typeNode: TypeNode): NamedTypeNode {
if (typeNode.kind === Kind.LIST_TYPE || typeNode.kind === Kind.NON_NULL_TYPE) {
return getBaseTypeNode(typeNode.type);
}
return typeNode;
}
export function convertNameParts(str: string, func: (str: string) => string, removeUnderscore = false): string {
if (removeUnderscore) {
return func(str);
}
return str
.split('_')
.map(s => func(s))
.join('_');
}
export function toPascalCase(str: string, transformUnderscore = false): string {
return convertNameParts(str, pascalCase, transformUnderscore);
}
export function buildScalars(schema: GraphQLSchema, scalarsMapping: ScalarsMap): ScalarsMap {
const typeMap = schema.getTypeMap();
let result = { ...scalarsMapping };
Object.keys(typeMap)
.map(typeName => typeMap[typeName])
.filter(type => isScalarType(type))
.map((scalarType: GraphQLScalarType) => {
const name = scalarType.name;
const value = scalarsMapping[name] || 'any';
result[name] = value;
});
return result;
}
function isStringValueNode(node: any): node is StringValueNode {
return node && typeof node === 'object' && node.kind === 'StringValue';
}
export function isRootType(type: GraphQLNamedType, schema: GraphQLSchema): type is GraphQLObjectType {
return isEqualType(type, schema.getQueryType()) || isEqualType(type, schema.getMutationType()) || isEqualType(type, schema.getSubscriptionType());
}
export function getRootTypeNames(schema: GraphQLSchema): string[] {
return [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()].filter(t => t).map(t => t.name);
}
export function stripMapperTypeInterpolation(identifier: string): string {
return identifier.trim().replace(/[^$\w].*$/, '');
}
export const OMIT_TYPE = 'export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;';