@vertikalx/vtx-backend-client 1.0.0-dev.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.md +12 -0
- package/index.d.ts +25 -0
- package/index.js +44 -0
- package/index.js.map +1 -0
- package/index.ts +65 -0
- package/package.json +15 -0
- package/runtime/batcher.d.ts +36 -0
- package/runtime/batcher.js +123 -0
- package/runtime/batcher.js.map +1 -0
- package/runtime/batcher.ts +275 -0
- package/runtime/createClient.d.ts +23 -0
- package/runtime/createClient.js +28 -0
- package/runtime/createClient.js.map +1 -0
- package/runtime/createClient.ts +68 -0
- package/runtime/error.d.ts +15 -0
- package/runtime/error.js +19 -0
- package/runtime/error.js.map +1 -0
- package/runtime/error.ts +29 -0
- package/runtime/fetcher.d.ts +10 -0
- package/runtime/fetcher.js +68 -0
- package/runtime/fetcher.js.map +1 -0
- package/runtime/fetcher.ts +97 -0
- package/runtime/generateGraphqlOperation.d.ts +30 -0
- package/runtime/generateGraphqlOperation.js +134 -0
- package/runtime/generateGraphqlOperation.js.map +1 -0
- package/runtime/generateGraphqlOperation.ts +225 -0
- package/runtime/index.d.ts +11 -0
- package/runtime/index.js +17 -0
- package/runtime/index.js.map +1 -0
- package/runtime/index.ts +13 -0
- package/runtime/linkTypeMap.d.ts +9 -0
- package/runtime/linkTypeMap.js +95 -0
- package/runtime/linkTypeMap.js.map +1 -0
- package/runtime/linkTypeMap.ts +156 -0
- package/runtime/typeSelection.d.ts +28 -0
- package/runtime/typeSelection.js +3 -0
- package/runtime/typeSelection.js.map +1 -0
- package/runtime/typeSelection.ts +95 -0
- package/runtime/types.d.ts +55 -0
- package/runtime/types.js +3 -0
- package/runtime/types.js.map +1 -0
- package/runtime/types.ts +69 -0
- package/schema.d.ts +406 -0
- package/schema.graphql +162 -0
- package/schema.js +109 -0
- package/schema.js.map +1 -0
- package/schema.ts +420 -0
- package/tsconfig.lib.tsbuildinfo +1 -0
- package/types.d.ts +207 -0
- package/types.js +518 -0
- package/types.js.map +1 -0
- package/types.ts +515 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import type { LinkedField, LinkedType } from './types'
|
|
3
|
+
|
|
4
|
+
export interface Args {
|
|
5
|
+
[arg: string]: any | undefined
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface Fields {
|
|
9
|
+
[field: string]: Request
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type Request = boolean | number | Fields
|
|
13
|
+
|
|
14
|
+
export interface Variables {
|
|
15
|
+
[name: string]: {
|
|
16
|
+
value: any
|
|
17
|
+
typing: [LinkedType, string]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface Context {
|
|
22
|
+
root: LinkedType
|
|
23
|
+
varCounter: number
|
|
24
|
+
variables: Variables
|
|
25
|
+
fragmentCounter: number
|
|
26
|
+
fragments: string[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface GraphqlOperation {
|
|
30
|
+
query: string
|
|
31
|
+
variables?: { [name: string]: any }
|
|
32
|
+
operationName?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const parseRequest = (
|
|
36
|
+
request: Request | undefined,
|
|
37
|
+
ctx: Context,
|
|
38
|
+
path: string[],
|
|
39
|
+
): string => {
|
|
40
|
+
if (typeof request === 'object' && '__args' in request) {
|
|
41
|
+
const args: any = request.__args
|
|
42
|
+
let fields: Request | undefined = { ...request }
|
|
43
|
+
delete fields.__args
|
|
44
|
+
const argNames = Object.keys(args)
|
|
45
|
+
|
|
46
|
+
if (argNames.length === 0) {
|
|
47
|
+
return parseRequest(fields, ctx, path)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const field = getFieldFromPath(ctx.root, path)
|
|
51
|
+
|
|
52
|
+
const argStrings = argNames.map((argName) => {
|
|
53
|
+
ctx.varCounter++
|
|
54
|
+
const varName = `v${ctx.varCounter}`
|
|
55
|
+
|
|
56
|
+
const typing = field.args && field.args[argName] // typeMap used here, .args
|
|
57
|
+
|
|
58
|
+
if (!typing) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
`no typing defined for argument \`${argName}\` in path \`${path.join(
|
|
61
|
+
'.',
|
|
62
|
+
)}\``,
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
ctx.variables[varName] = {
|
|
67
|
+
value: args[argName],
|
|
68
|
+
typing,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return `${argName}:$${varName}`
|
|
72
|
+
})
|
|
73
|
+
return `(${argStrings})${parseRequest(fields, ctx, path)}`
|
|
74
|
+
} else if (typeof request === 'object' && Object.keys(request).length > 0) {
|
|
75
|
+
const fields = request
|
|
76
|
+
const fieldNames = Object.keys(fields).filter((k) => Boolean(fields[k]))
|
|
77
|
+
|
|
78
|
+
if (fieldNames.length === 0) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`field selection should not be empty: ${path.join('.')}`,
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const type =
|
|
85
|
+
path.length > 0 ? getFieldFromPath(ctx.root, path).type : ctx.root
|
|
86
|
+
const scalarFields = type.scalar
|
|
87
|
+
|
|
88
|
+
let scalarFieldsFragment: string | undefined
|
|
89
|
+
|
|
90
|
+
if (fieldNames.includes('__scalar')) {
|
|
91
|
+
const falsyFieldNames = new Set(
|
|
92
|
+
Object.keys(fields).filter((k) => !Boolean(fields[k])),
|
|
93
|
+
)
|
|
94
|
+
if (scalarFields?.length) {
|
|
95
|
+
ctx.fragmentCounter++
|
|
96
|
+
scalarFieldsFragment = `f${ctx.fragmentCounter}`
|
|
97
|
+
|
|
98
|
+
ctx.fragments.push(
|
|
99
|
+
`fragment ${scalarFieldsFragment} on ${
|
|
100
|
+
type.name
|
|
101
|
+
}{${scalarFields
|
|
102
|
+
.filter((f) => !falsyFieldNames.has(f))
|
|
103
|
+
.join(',')}}`,
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const fieldsSelection = fieldNames
|
|
109
|
+
.filter((f) => !['__scalar', '__name'].includes(f))
|
|
110
|
+
.map((f) => {
|
|
111
|
+
const parsed = parseRequest(fields[f], ctx, [...path, f])
|
|
112
|
+
|
|
113
|
+
if (f.startsWith('on_')) {
|
|
114
|
+
ctx.fragmentCounter++
|
|
115
|
+
const implementationFragment = `f${ctx.fragmentCounter}`
|
|
116
|
+
|
|
117
|
+
const typeMatch = f.match(/^on_(.+)/)
|
|
118
|
+
|
|
119
|
+
if (!typeMatch || !typeMatch[1])
|
|
120
|
+
throw new Error('match failed')
|
|
121
|
+
|
|
122
|
+
ctx.fragments.push(
|
|
123
|
+
`fragment ${implementationFragment} on ${typeMatch[1]}${parsed}`,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
return `...${implementationFragment}`
|
|
127
|
+
} else {
|
|
128
|
+
return `${f}${parsed}`
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
.concat(scalarFieldsFragment ? [`...${scalarFieldsFragment}`] : [])
|
|
132
|
+
.join(',')
|
|
133
|
+
|
|
134
|
+
return `{${fieldsSelection}}`
|
|
135
|
+
} else {
|
|
136
|
+
return ''
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export const generateGraphqlOperation = (
|
|
141
|
+
operation: 'query' | 'mutation' | 'subscription',
|
|
142
|
+
root: LinkedType,
|
|
143
|
+
fields?: Fields,
|
|
144
|
+
): GraphqlOperation => {
|
|
145
|
+
const ctx: Context = {
|
|
146
|
+
root: root,
|
|
147
|
+
varCounter: 0,
|
|
148
|
+
variables: {},
|
|
149
|
+
fragmentCounter: 0,
|
|
150
|
+
fragments: [],
|
|
151
|
+
}
|
|
152
|
+
const result = parseRequest(fields, ctx, [])
|
|
153
|
+
|
|
154
|
+
const varNames = Object.keys(ctx.variables)
|
|
155
|
+
|
|
156
|
+
const varsString =
|
|
157
|
+
varNames.length > 0
|
|
158
|
+
? `(${varNames.map((v) => {
|
|
159
|
+
const variableType = ctx.variables[v].typing[1]
|
|
160
|
+
return `$${v}:${variableType}`
|
|
161
|
+
})})`
|
|
162
|
+
: ''
|
|
163
|
+
|
|
164
|
+
const operationName = fields?.__name || ''
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
query: [
|
|
168
|
+
`${operation} ${operationName}${varsString}${result}`,
|
|
169
|
+
...ctx.fragments,
|
|
170
|
+
].join(','),
|
|
171
|
+
variables: Object.keys(ctx.variables).reduce<{ [name: string]: any }>(
|
|
172
|
+
(r, v) => {
|
|
173
|
+
r[v] = ctx.variables[v].value
|
|
174
|
+
return r
|
|
175
|
+
},
|
|
176
|
+
{},
|
|
177
|
+
),
|
|
178
|
+
...(operationName ? { operationName: operationName.toString() } : {}),
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export const getFieldFromPath = (
|
|
183
|
+
root: LinkedType | undefined,
|
|
184
|
+
path: string[],
|
|
185
|
+
) => {
|
|
186
|
+
let current: LinkedField | undefined
|
|
187
|
+
|
|
188
|
+
if (!root) throw new Error('root type is not provided')
|
|
189
|
+
|
|
190
|
+
if (path.length === 0) throw new Error(`path is empty`)
|
|
191
|
+
|
|
192
|
+
path.forEach((f) => {
|
|
193
|
+
const type = current ? current.type : root
|
|
194
|
+
|
|
195
|
+
if (!type.fields)
|
|
196
|
+
throw new Error(`type \`${type.name}\` does not have fields`)
|
|
197
|
+
|
|
198
|
+
const possibleTypes = Object.keys(type.fields)
|
|
199
|
+
.filter((i) => i.startsWith('on_'))
|
|
200
|
+
.reduce(
|
|
201
|
+
(types, fieldName) => {
|
|
202
|
+
const field = type.fields && type.fields[fieldName]
|
|
203
|
+
if (field) types.push(field.type)
|
|
204
|
+
return types
|
|
205
|
+
},
|
|
206
|
+
[type],
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
let field: LinkedField | null = null
|
|
210
|
+
|
|
211
|
+
possibleTypes.forEach((type) => {
|
|
212
|
+
const found = type.fields && type.fields[f]
|
|
213
|
+
if (found) field = found
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
if (!field)
|
|
217
|
+
throw new Error(
|
|
218
|
+
`type \`${type.name}\` does not have a field \`${f}\``,
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
current = field
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
return current as LinkedField
|
|
225
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { createClient } from './createClient';
|
|
2
|
+
export type { ClientOptions } from './createClient';
|
|
3
|
+
export type { FieldsSelection } from './typeSelection';
|
|
4
|
+
export { generateGraphqlOperation } from './generateGraphqlOperation';
|
|
5
|
+
export type { GraphqlOperation } from './generateGraphqlOperation';
|
|
6
|
+
export { linkTypeMap } from './linkTypeMap';
|
|
7
|
+
export { createFetcher } from './fetcher';
|
|
8
|
+
export { GenqlError } from './error';
|
|
9
|
+
export declare const everything: {
|
|
10
|
+
__scalar: boolean;
|
|
11
|
+
};
|
package/runtime/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.everything = exports.GenqlError = exports.createFetcher = exports.linkTypeMap = exports.generateGraphqlOperation = exports.createClient = void 0;
|
|
4
|
+
var createClient_1 = require("./createClient");
|
|
5
|
+
Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return createClient_1.createClient; } });
|
|
6
|
+
var generateGraphqlOperation_1 = require("./generateGraphqlOperation");
|
|
7
|
+
Object.defineProperty(exports, "generateGraphqlOperation", { enumerable: true, get: function () { return generateGraphqlOperation_1.generateGraphqlOperation; } });
|
|
8
|
+
var linkTypeMap_1 = require("./linkTypeMap");
|
|
9
|
+
Object.defineProperty(exports, "linkTypeMap", { enumerable: true, get: function () { return linkTypeMap_1.linkTypeMap; } });
|
|
10
|
+
var fetcher_1 = require("./fetcher");
|
|
11
|
+
Object.defineProperty(exports, "createFetcher", { enumerable: true, get: function () { return fetcher_1.createFetcher; } });
|
|
12
|
+
var error_1 = require("./error");
|
|
13
|
+
Object.defineProperty(exports, "GenqlError", { enumerable: true, get: function () { return error_1.GenqlError; } });
|
|
14
|
+
exports.everything = {
|
|
15
|
+
__scalar: true,
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/vtx-backend-client/runtime/index.ts"],"names":[],"mappings":";;;AACA,+CAA6C;AAApC,4GAAA,YAAY,OAAA;AAGrB,uEAAqE;AAA5D,oIAAA,wBAAwB,OAAA;AAEjC,6CAA2C;AAAlC,0GAAA,WAAW,OAAA;AAEpB,qCAAyC;AAAhC,wGAAA,aAAa,OAAA;AACtB,iCAAoC;AAA3B,mGAAA,UAAU,OAAA;AACN,QAAA,UAAU,GAAG;IACtB,QAAQ,EAAE,IAAI;CACjB,CAAA"}
|
package/runtime/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
export { createClient } from './createClient'
|
|
3
|
+
export type { ClientOptions } from './createClient'
|
|
4
|
+
export type { FieldsSelection } from './typeSelection'
|
|
5
|
+
export { generateGraphqlOperation } from './generateGraphqlOperation'
|
|
6
|
+
export type { GraphqlOperation } from './generateGraphqlOperation'
|
|
7
|
+
export { linkTypeMap } from './linkTypeMap'
|
|
8
|
+
// export { Observable } from 'zen-observable-ts'
|
|
9
|
+
export { createFetcher } from './fetcher'
|
|
10
|
+
export { GenqlError } from './error'
|
|
11
|
+
export const everything = {
|
|
12
|
+
__scalar: true,
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { CompressedTypeMap, LinkedArgMap, LinkedTypeMap } from './types';
|
|
2
|
+
export interface PartialLinkedFieldMap {
|
|
3
|
+
[field: string]: {
|
|
4
|
+
type: string;
|
|
5
|
+
args?: LinkedArgMap;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export declare const linkTypeMap: (typeMap: CompressedTypeMap<number>) => LinkedTypeMap;
|
|
9
|
+
export declare const resolveConcreteTypes: (linkedTypeMap: LinkedTypeMap) => LinkedTypeMap;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.resolveConcreteTypes = exports.linkTypeMap = void 0;
|
|
4
|
+
const linkTypeMap = (typeMap) => {
|
|
5
|
+
const indexToName = Object.assign({}, ...Object.keys(typeMap.types).map((k, i) => ({ [i]: k })));
|
|
6
|
+
let intermediaryTypeMap = Object.assign({}, ...Object.keys(typeMap.types || {}).map((k) => {
|
|
7
|
+
const type = typeMap.types[k];
|
|
8
|
+
const fields = type || {};
|
|
9
|
+
return {
|
|
10
|
+
[k]: {
|
|
11
|
+
name: k,
|
|
12
|
+
scalar: Object.keys(fields).filter((f) => {
|
|
13
|
+
const [type] = fields[f] || [];
|
|
14
|
+
const isScalar = type && typeMap.scalars.includes(type);
|
|
15
|
+
if (!isScalar) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const args = fields[f]?.[1];
|
|
19
|
+
const argTypes = Object.values(args || {})
|
|
20
|
+
.map((x) => x?.[1])
|
|
21
|
+
.filter(Boolean);
|
|
22
|
+
const hasRequiredArgs = argTypes.some((str) => str && str.endsWith('!'));
|
|
23
|
+
if (hasRequiredArgs) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}),
|
|
28
|
+
fields: Object.assign({}, ...Object.keys(fields).map((f) => {
|
|
29
|
+
const [typeIndex, args] = fields[f] || [];
|
|
30
|
+
if (typeIndex == null) {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
[f]: {
|
|
35
|
+
type: indexToName[typeIndex],
|
|
36
|
+
args: Object.assign({}, ...Object.keys(args || {}).map((k) => {
|
|
37
|
+
if (!args || !args[k]) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const [argTypeName, argTypeString,] = args[k];
|
|
41
|
+
return {
|
|
42
|
+
[k]: [
|
|
43
|
+
indexToName[argTypeName],
|
|
44
|
+
argTypeString ||
|
|
45
|
+
indexToName[argTypeName],
|
|
46
|
+
],
|
|
47
|
+
};
|
|
48
|
+
})),
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
})),
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}));
|
|
55
|
+
const res = (0, exports.resolveConcreteTypes)(intermediaryTypeMap);
|
|
56
|
+
return res;
|
|
57
|
+
};
|
|
58
|
+
exports.linkTypeMap = linkTypeMap;
|
|
59
|
+
const resolveConcreteTypes = (linkedTypeMap) => {
|
|
60
|
+
Object.keys(linkedTypeMap).forEach((typeNameFromKey) => {
|
|
61
|
+
const type = linkedTypeMap[typeNameFromKey];
|
|
62
|
+
if (!type.fields) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const fields = type.fields;
|
|
66
|
+
Object.keys(fields).forEach((f) => {
|
|
67
|
+
const field = fields[f];
|
|
68
|
+
if (field.args) {
|
|
69
|
+
const args = field.args;
|
|
70
|
+
Object.keys(args).forEach((key) => {
|
|
71
|
+
const arg = args[key];
|
|
72
|
+
if (arg) {
|
|
73
|
+
const [typeName] = arg;
|
|
74
|
+
if (typeof typeName === 'string') {
|
|
75
|
+
if (!linkedTypeMap[typeName]) {
|
|
76
|
+
linkedTypeMap[typeName] = { name: typeName };
|
|
77
|
+
}
|
|
78
|
+
arg[0] = linkedTypeMap[typeName];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const typeName = field.type;
|
|
84
|
+
if (typeof typeName === 'string') {
|
|
85
|
+
if (!linkedTypeMap[typeName]) {
|
|
86
|
+
linkedTypeMap[typeName] = { name: typeName };
|
|
87
|
+
}
|
|
88
|
+
field.type = linkedTypeMap[typeName];
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
return linkedTypeMap;
|
|
93
|
+
};
|
|
94
|
+
exports.resolveConcreteTypes = resolveConcreteTypes;
|
|
95
|
+
//# sourceMappingURL=linkTypeMap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"linkTypeMap.js","sourceRoot":"","sources":["../../../../libs/vtx-backend-client/runtime/linkTypeMap.ts"],"names":[],"mappings":";;;AAiBO,MAAM,WAAW,GAAG,CACvB,OAAkC,EACrB,EAAE;IACf,MAAM,WAAW,GAA2B,MAAM,CAAC,MAAM,CACrD,EAAE,EACF,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAC5D,CAAA;IAED,IAAI,mBAAmB,GAAG,MAAM,CAAC,MAAM,CACnC,EAAE,EACF,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,GAAG,CACnC,CAAC,CAAC,EAA8B,EAAE;QAC9B,MAAM,IAAI,GAAmB,OAAO,CAAC,KAAK,CAAC,CAAC,CAAE,CAAA;QAC9C,MAAM,MAAM,GAAG,IAAI,IAAI,EAAE,CAAA;QACzB,OAAO;YACH,CAAC,CAAC,CAAC,EAAE;gBACD,IAAI,EAAE,CAAC;gBAEP,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE;oBACrC,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;oBAE9B,MAAM,QAAQ,GACV,IAAI,IA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AAG,EAAE,CAAC;wBACN,MAAM,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAA;wBAEtB,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;4BAC/B,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;gCAC3B,aAAa,CAAC,QAAQ,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAA;4BAChD,CAAC;4BAED,GAAG,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAE,CAAA;wBACrC,CAAC;oBACL,CAAC;gBACL,CAAC,CAAC,CAAA;YACN,CAAC;YAED,MAAM,QAAQ,GAAG,KAAK,CAAC,IAA2B,CAAA;YAElD,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;gBAC/B,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;oBAC3B,aAAa,CAAC,QAAQ,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAA;gBAChD,CAAC;gBAED,KAAK,CAAC,IAAI,GAAG,aAAa,CAAC,QAAQ,CAAE,CAAA;YACzC,CAAC;QACL,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;IAEF,OAAO,aAAa,CAAA;AACxB,CAAC,CAAA;AA7CY,QAAA,oBAAoB,wBA6ChC"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import type {
|
|
3
|
+
CompressedType,
|
|
4
|
+
CompressedTypeMap,
|
|
5
|
+
LinkedArgMap,
|
|
6
|
+
LinkedField,
|
|
7
|
+
LinkedType,
|
|
8
|
+
LinkedTypeMap,
|
|
9
|
+
} from './types'
|
|
10
|
+
|
|
11
|
+
export interface PartialLinkedFieldMap {
|
|
12
|
+
[field: string]: {
|
|
13
|
+
type: string
|
|
14
|
+
args?: LinkedArgMap
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const linkTypeMap = (
|
|
19
|
+
typeMap: CompressedTypeMap<number>,
|
|
20
|
+
): LinkedTypeMap => {
|
|
21
|
+
const indexToName: Record<number, string> = Object.assign(
|
|
22
|
+
{},
|
|
23
|
+
...Object.keys(typeMap.types).map((k, i) => ({ [i]: k })),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
let intermediaryTypeMap = Object.assign(
|
|
27
|
+
{},
|
|
28
|
+
...Object.keys(typeMap.types || {}).map(
|
|
29
|
+
(k): Record<string, LinkedType> => {
|
|
30
|
+
const type: CompressedType = typeMap.types[k]!
|
|
31
|
+
const fields = type || {}
|
|
32
|
+
return {
|
|
33
|
+
[k]: {
|
|
34
|
+
name: k,
|
|
35
|
+
// type scalar properties
|
|
36
|
+
scalar: Object.keys(fields).filter((f) => {
|
|
37
|
+
const [type] = fields[f] || []
|
|
38
|
+
|
|
39
|
+
const isScalar =
|
|
40
|
+
type && typeMap.scalars.includes(type)
|
|
41
|
+
if (!isScalar) {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
const args = fields[f]?.[1]
|
|
45
|
+
const argTypes = Object.values(args || {})
|
|
46
|
+
.map((x) => x?.[1])
|
|
47
|
+
.filter(Boolean)
|
|
48
|
+
|
|
49
|
+
const hasRequiredArgs = argTypes.some(
|
|
50
|
+
(str) => str && str.endsWith('!'),
|
|
51
|
+
)
|
|
52
|
+
if (hasRequiredArgs) {
|
|
53
|
+
return false
|
|
54
|
+
}
|
|
55
|
+
return true
|
|
56
|
+
}),
|
|
57
|
+
// fields with corresponding `type` and `args`
|
|
58
|
+
fields: Object.assign(
|
|
59
|
+
{},
|
|
60
|
+
...Object.keys(fields).map(
|
|
61
|
+
(f): PartialLinkedFieldMap => {
|
|
62
|
+
const [typeIndex, args] = fields[f] || []
|
|
63
|
+
if (typeIndex == null) {
|
|
64
|
+
return {}
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
[f]: {
|
|
68
|
+
// replace index with type name
|
|
69
|
+
type: indexToName[typeIndex],
|
|
70
|
+
args: Object.assign(
|
|
71
|
+
{},
|
|
72
|
+
...Object.keys(args || {}).map(
|
|
73
|
+
(k) => {
|
|
74
|
+
// if argTypeString == argTypeName, argTypeString is missing, need to readd it
|
|
75
|
+
if (!args || !args[k]) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
const [
|
|
79
|
+
argTypeName,
|
|
80
|
+
argTypeString,
|
|
81
|
+
] = args[k] as any
|
|
82
|
+
return {
|
|
83
|
+
[k]: [
|
|
84
|
+
indexToName[
|
|
85
|
+
argTypeName
|
|
86
|
+
],
|
|
87
|
+
argTypeString ||
|
|
88
|
+
indexToName[
|
|
89
|
+
argTypeName
|
|
90
|
+
],
|
|
91
|
+
],
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
),
|
|
95
|
+
),
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
),
|
|
105
|
+
)
|
|
106
|
+
const res = resolveConcreteTypes(intermediaryTypeMap)
|
|
107
|
+
return res
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// replace typename with concrete type
|
|
111
|
+
export const resolveConcreteTypes = (linkedTypeMap: LinkedTypeMap) => {
|
|
112
|
+
Object.keys(linkedTypeMap).forEach((typeNameFromKey) => {
|
|
113
|
+
const type: LinkedType = linkedTypeMap[typeNameFromKey]!
|
|
114
|
+
// type.name = typeNameFromKey
|
|
115
|
+
if (!type.fields) {
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const fields = type.fields
|
|
120
|
+
|
|
121
|
+
Object.keys(fields).forEach((f) => {
|
|
122
|
+
const field: LinkedField = fields[f]!
|
|
123
|
+
|
|
124
|
+
if (field.args) {
|
|
125
|
+
const args = field.args
|
|
126
|
+
Object.keys(args).forEach((key) => {
|
|
127
|
+
const arg = args[key]
|
|
128
|
+
|
|
129
|
+
if (arg) {
|
|
130
|
+
const [typeName] = arg
|
|
131
|
+
|
|
132
|
+
if (typeof typeName === 'string') {
|
|
133
|
+
if (!linkedTypeMap[typeName]) {
|
|
134
|
+
linkedTypeMap[typeName] = { name: typeName }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
arg[0] = linkedTypeMap[typeName]!
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const typeName = field.type as LinkedType | string
|
|
144
|
+
|
|
145
|
+
if (typeof typeName === 'string') {
|
|
146
|
+
if (!linkedTypeMap[typeName]) {
|
|
147
|
+
linkedTypeMap[typeName] = { name: typeName }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
field.type = linkedTypeMap[typeName]!
|
|
151
|
+
}
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
return linkedTypeMap
|
|
156
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export type FieldsSelection<SRC extends Anify<DST> | undefined, DST> = {
|
|
2
|
+
scalar: SRC;
|
|
3
|
+
union: Handle__isUnion<SRC, DST>;
|
|
4
|
+
object: HandleObject<SRC, DST>;
|
|
5
|
+
array: SRC extends Nil ? never : SRC extends Array<infer T | null> ? Array<FieldsSelection<T, DST>> : never;
|
|
6
|
+
__scalar: Handle__scalar<SRC, DST>;
|
|
7
|
+
never: never;
|
|
8
|
+
}[DST extends Nil ? 'never' : DST extends false | 0 ? 'never' : SRC extends Scalar ? 'scalar' : SRC extends any[] ? 'array' : SRC extends {
|
|
9
|
+
__isUnion?: any;
|
|
10
|
+
} ? 'union' : DST extends {
|
|
11
|
+
__scalar?: any;
|
|
12
|
+
} ? '__scalar' : DST extends {} ? 'object' : 'never'];
|
|
13
|
+
type HandleObject<SRC extends Anify<DST>, DST> = DST extends boolean ? SRC : SRC extends Nil ? never : Pick<{
|
|
14
|
+
[Key in keyof SRC]: Key extends keyof DST ? FieldsSelection<SRC[Key], NonNullable<DST[Key]>> : SRC[Key];
|
|
15
|
+
}, Exclude<keyof DST, FieldsToRemove>>;
|
|
16
|
+
type Handle__scalar<SRC extends Anify<DST>, DST> = SRC extends Nil ? never : Pick<{
|
|
17
|
+
[Key in keyof SRC]: Key extends keyof DST ? FieldsSelection<SRC[Key], DST[Key]> : SRC[Key];
|
|
18
|
+
}, {
|
|
19
|
+
[Key in keyof SRC]: SRC[Key] extends Nil ? never : Key extends FieldsToRemove ? never : SRC[Key] extends Scalar ? Key : Key extends keyof DST ? Key : never;
|
|
20
|
+
}[keyof SRC]>;
|
|
21
|
+
type Handle__isUnion<SRC extends Anify<DST>, DST> = SRC extends Nil ? never : Omit<SRC, FieldsToRemove>;
|
|
22
|
+
type Scalar = string | number | Date | boolean | null | undefined;
|
|
23
|
+
type Anify<T> = {
|
|
24
|
+
[P in keyof T]?: any;
|
|
25
|
+
};
|
|
26
|
+
type FieldsToRemove = '__isUnion' | '__scalar' | '__name' | '__args';
|
|
27
|
+
type Nil = undefined | null;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typeSelection.js","sourceRoot":"","sources":["../../../../libs/vtx-backend-client/runtime/typeSelection.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
//////////////////////////////////////////////////
|
|
3
|
+
|
|
4
|
+
// SOME THINGS TO KNOW BEFORE DIVING IN
|
|
5
|
+
/*
|
|
6
|
+
0. DST is the request type, SRC is the response type
|
|
7
|
+
|
|
8
|
+
1. FieldsSelection uses an object because currently is impossible to make recursive types
|
|
9
|
+
|
|
10
|
+
2. FieldsSelection is a recursive type that makes a type based on request type and fields
|
|
11
|
+
|
|
12
|
+
3. HandleObject handles object types
|
|
13
|
+
|
|
14
|
+
4. Handle__scalar adds all scalar properties excluding non scalar props
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export type FieldsSelection<SRC extends Anify<DST> | undefined, DST> = {
|
|
18
|
+
scalar: SRC
|
|
19
|
+
union: Handle__isUnion<SRC, DST>
|
|
20
|
+
object: HandleObject<SRC, DST>
|
|
21
|
+
array: SRC extends Nil
|
|
22
|
+
? never
|
|
23
|
+
: SRC extends Array<infer T | null>
|
|
24
|
+
? Array<FieldsSelection<T, DST>>
|
|
25
|
+
: never
|
|
26
|
+
__scalar: Handle__scalar<SRC, DST>
|
|
27
|
+
never: never
|
|
28
|
+
}[DST extends Nil
|
|
29
|
+
? 'never'
|
|
30
|
+
: DST extends false | 0
|
|
31
|
+
? 'never'
|
|
32
|
+
: SRC extends Scalar
|
|
33
|
+
? 'scalar'
|
|
34
|
+
: SRC extends any[]
|
|
35
|
+
? 'array'
|
|
36
|
+
: SRC extends { __isUnion?: any }
|
|
37
|
+
? 'union'
|
|
38
|
+
: DST extends { __scalar?: any }
|
|
39
|
+
? '__scalar'
|
|
40
|
+
: DST extends {}
|
|
41
|
+
? 'object'
|
|
42
|
+
: 'never']
|
|
43
|
+
|
|
44
|
+
type HandleObject<SRC extends Anify<DST>, DST> = DST extends boolean
|
|
45
|
+
? SRC
|
|
46
|
+
: SRC extends Nil
|
|
47
|
+
? never
|
|
48
|
+
: Pick<
|
|
49
|
+
{
|
|
50
|
+
// using keyof SRC to maintain ?: relations of SRC type
|
|
51
|
+
[Key in keyof SRC]: Key extends keyof DST
|
|
52
|
+
? FieldsSelection<SRC[Key], NonNullable<DST[Key]>>
|
|
53
|
+
: SRC[Key]
|
|
54
|
+
},
|
|
55
|
+
Exclude<keyof DST, FieldsToRemove>
|
|
56
|
+
// {
|
|
57
|
+
// // remove falsy values
|
|
58
|
+
// [Key in keyof DST]: DST[Key] extends false | 0 ? never : Key
|
|
59
|
+
// }[keyof DST]
|
|
60
|
+
>
|
|
61
|
+
|
|
62
|
+
type Handle__scalar<SRC extends Anify<DST>, DST> = SRC extends Nil
|
|
63
|
+
? never
|
|
64
|
+
: Pick<
|
|
65
|
+
// continue processing fields that are in DST, directly pass SRC type if not in DST
|
|
66
|
+
{
|
|
67
|
+
[Key in keyof SRC]: Key extends keyof DST
|
|
68
|
+
? FieldsSelection<SRC[Key], DST[Key]>
|
|
69
|
+
: SRC[Key]
|
|
70
|
+
},
|
|
71
|
+
// remove fields that are not scalars or are not in DST
|
|
72
|
+
{
|
|
73
|
+
[Key in keyof SRC]: SRC[Key] extends Nil
|
|
74
|
+
? never
|
|
75
|
+
: Key extends FieldsToRemove
|
|
76
|
+
? never
|
|
77
|
+
: SRC[Key] extends Scalar
|
|
78
|
+
? Key
|
|
79
|
+
: Key extends keyof DST
|
|
80
|
+
? Key
|
|
81
|
+
: never
|
|
82
|
+
}[keyof SRC]
|
|
83
|
+
>
|
|
84
|
+
|
|
85
|
+
type Handle__isUnion<SRC extends Anify<DST>, DST> = SRC extends Nil
|
|
86
|
+
? never
|
|
87
|
+
: Omit<SRC, FieldsToRemove> // just return the union type
|
|
88
|
+
|
|
89
|
+
type Scalar = string | number | Date | boolean | null | undefined
|
|
90
|
+
|
|
91
|
+
type Anify<T> = { [P in keyof T]?: any }
|
|
92
|
+
|
|
93
|
+
type FieldsToRemove = '__isUnion' | '__scalar' | '__name' | '__args'
|
|
94
|
+
|
|
95
|
+
type Nil = undefined | null
|