@restorecommerce/facade 0.1.42 → 0.2.1
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/CHANGELOG.md +51 -0
- package/copy-hbs.sh +6 -0
- package/debug-run.ts +2 -1
- package/dist/gql/protos/graphql.d.ts +1 -1
- package/dist/gql/protos/graphql.js +38 -14
- package/dist/gql/protos/registry.d.ts +5 -0
- package/dist/gql/protos/registry.js +73 -12
- package/dist/gql/protos/utils.d.ts +21 -0
- package/dist/gql/protos/utils.js +88 -1
- package/dist/index.d.ts +1 -0
- package/dist/interfaces.d.ts +1 -0
- package/dist/modules/identity/gql/schema.generated.d.ts +26 -20
- package/dist/modules/identity/gql/types.js +2 -1
- package/dist/modules/identity/grpc/index.d.ts +2 -0
- package/dist/modules/identity/grpc/index.js +2 -0
- package/dist/modules/identity/index.js +6 -2
- package/dist/modules/identity/interfaces.d.ts +1 -0
- package/dist/modules/identity/oauth/oauth.d.ts +11 -0
- package/dist/modules/identity/oauth/oauth.js +137 -0
- package/dist/modules/identity/oauth/views/account.hbs +23 -0
- package/dist/modules/identity/oauth/views/layout.hbs +34 -0
- package/dist/modules/identity/oauth/views/login.hbs +27 -0
- package/dist/modules/identity/oauth/views/register.hbs +134 -0
- package/dist/modules/identity/oidc/router.d.ts +2 -2
- package/dist/modules/identity/oidc/router.js +4 -4
- package/dist/modules/identity/oidc/views/consent.hbs +49 -0
- package/dist/modules/identity/oidc/views/layout.hbs +79 -0
- package/dist/modules/identity/oidc/views/login.hbs +62 -0
- package/dist/modules/resource/gql/schema.generated.d.ts +3 -6
- package/package.json +15 -10
package/CHANGELOG.md
CHANGED
@@ -3,6 +3,57 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5
5
|
|
6
|
+
## [0.2.1](https://github.com/restorecommerce/libs/compare/@restorecommerce/facade@0.2.0...@restorecommerce/facade@0.2.1) (2022-02-14)
|
7
|
+
|
8
|
+
|
9
|
+
### Bug Fixes
|
10
|
+
|
11
|
+
* **facade:** regenerated schema ([a8fba43](https://github.com/restorecommerce/libs/commit/a8fba43233999191d2583139cefe9a8af647706b))
|
12
|
+
* bypass non-compliant koa modules ([8a92b83](https://github.com/restorecommerce/libs/commit/8a92b83ce717f52caa843b00d5db894ca07ab1a9))
|
13
|
+
* copy hbs on build ([efd8e78](https://github.com/restorecommerce/libs/commit/efd8e783a09badb84d76f2b72377de438f34d014))
|
14
|
+
* switch to sh ([5756072](https://github.com/restorecommerce/libs/commit/57560723184a300b2d315068f29339a5f2dc517e))
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
# [0.2.0](https://github.com/restorecommerce/libs/compare/@restorecommerce/facade@0.1.44...@restorecommerce/facade@0.2.0) (2022-02-09)
|
21
|
+
|
22
|
+
|
23
|
+
### Features
|
24
|
+
|
25
|
+
* add oauth ([3e7798e](https://github.com/restorecommerce/libs/commit/3e7798e3aa10ef092872928f5254cd5fbb125f3b))
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
## [0.1.44](https://github.com/restorecommerce/libs/compare/@restorecommerce/facade@0.1.43...@restorecommerce/facade@0.1.44) (2022-02-02)
|
32
|
+
|
33
|
+
|
34
|
+
### Bug Fixes
|
35
|
+
|
36
|
+
* **facade:** for recursive circular references (ex: attributes -> having nested attributes) ([59c2904](https://github.com/restorecommerce/libs/commit/59c29049c23ecb5f3265ea37a6757ca551023b38))
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
## [0.1.43](https://github.com/restorecommerce/libs/compare/@restorecommerce/facade@0.1.42...@restorecommerce/facade@0.1.43) (2022-02-02)
|
43
|
+
|
44
|
+
|
45
|
+
### Bug Fixes
|
46
|
+
|
47
|
+
* **facade:** enum convert string to integer ([47127ef](https://github.com/restorecommerce/libs/commit/47127ef323f5853a4c6573b01dd36c3725074d4e))
|
48
|
+
* **facade:** fix for mutation resolvers to convert enums string values to integer ([28d0fea](https://github.com/restorecommerce/libs/commit/28d0fea912778b3cf4f748bc68b1e0fafaea60af))
|
49
|
+
* **facade:** fixed read operations to convert enums to number ([81083c0](https://github.com/restorecommerce/libs/commit/81083c0162d605d6a3c8394e06ba093d1e29948e))
|
50
|
+
* **facade:** move conversion of enum to int to utils ([72c9a1f](https://github.com/restorecommerce/libs/commit/72c9a1f0be269b68bc9b2501a38789ce7409453f))
|
51
|
+
* **facade:** recursive enum path fix ([244f7fc](https://github.com/restorecommerce/libs/commit/244f7fc782d10ffb5f774f0245174446288cacb5))
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
6
57
|
## [0.1.42](https://github.com/restorecommerce/libs/compare/@restorecommerce/facade@0.1.41...@restorecommerce/facade@0.1.42) (2022-01-28)
|
7
58
|
|
8
59
|
**Note:** Version bump only for package @restorecommerce/facade
|
package/copy-hbs.sh
ADDED
package/debug-run.ts
CHANGED
@@ -15,7 +15,7 @@ import {
|
|
15
15
|
ostorageModule,
|
16
16
|
paymentModule,
|
17
17
|
schedulingModule
|
18
|
-
} from './
|
18
|
+
} from './dist';
|
19
19
|
import { join } from 'path';
|
20
20
|
|
21
21
|
const CONFIG_PATH = __dirname;
|
@@ -51,6 +51,7 @@ function createTestFacade() {
|
|
51
51
|
],
|
52
52
|
jwks,
|
53
53
|
},
|
54
|
+
oauth: true,
|
54
55
|
apiKey: 'api_key'
|
55
56
|
}))
|
56
57
|
.useModule(resourceModule({config: serviceConfig.get('resource')}))
|
@@ -15,7 +15,7 @@ declare type ServiceClient<Context extends Pick<Context, Key>, Key extends keyof
|
|
15
15
|
};
|
16
16
|
};
|
17
17
|
export declare const getGQLResolverFunctions: <T extends Record<string, any>, CTX extends ServiceClient<CTX, keyof CTX, T>, SRV = any, R = ResolverFn<any, any, ServiceClient<CTX, keyof CTX, T>, any>, B extends keyof T = any, NS extends keyof CTX = any>(service: ServiceDescriptorProto, key: NS, serviceKey: B, grpcClientConfig: GrpcClientConfig) => { [key in keyof SRV]: R; };
|
18
|
-
export declare const registerResolverFunction: <T extends Record<string, any>, CTX extends ServiceClient<CTX, keyof CTX, T>>(namespace: string, name: string, func: ResolverFn<any, any, ServiceClient<CTX, keyof CTX, T>, any>, mutation?: boolean, subspace?: string | undefined) => void;
|
18
|
+
export declare const registerResolverFunction: <T extends Record<string, any>, CTX extends ServiceClient<CTX, keyof CTX, T>>(namespace: string, name: string, func: ResolverFn<any, any, ServiceClient<CTX, keyof CTX, T>, any>, mutation?: boolean, subspace?: string | undefined, service?: ServiceDescriptorProto | undefined) => void;
|
19
19
|
export declare const generateResolver: (...namespaces: string[]) => any;
|
20
20
|
export declare const registerResolverSchema: (namespace: string, name: string, schema: Thunk<GraphQLFieldConfig<any, any>>, mutation?: boolean, subspace?: string | undefined) => void;
|
21
21
|
export declare const generateSchema: (setup: {
|
@@ -10,6 +10,7 @@ const stream = require("stream");
|
|
10
10
|
const _ = require("lodash");
|
11
11
|
const typeCache = new Map();
|
12
12
|
const Mutate = ['Create', 'Update', 'Upsert'];
|
13
|
+
const inputMethodType = new Map();
|
13
14
|
const getGQLSchema = (method) => {
|
14
15
|
const fields = {
|
15
16
|
// operationStatus: {
|
@@ -109,9 +110,8 @@ const getGQLResolverFunctions = (service, key, serviceKey, grpcClientConfig) =>
|
|
109
110
|
}
|
110
111
|
const defaults = typing.processor.fromPartial({});
|
111
112
|
let subjectField = null;
|
112
|
-
|
113
|
-
|
114
|
-
for (let field of inputTyping.meta.field) {
|
113
|
+
if (typing) {
|
114
|
+
for (let field of typing.meta.field) {
|
115
115
|
if (field.typeName === types_1.authSubjectType) {
|
116
116
|
subjectField = field.name;
|
117
117
|
break;
|
@@ -124,11 +124,13 @@ const getGQLResolverFunctions = (service, key, serviceKey, grpcClientConfig) =>
|
|
124
124
|
const service = client[serviceKey];
|
125
125
|
try {
|
126
126
|
const converted = await exports.recursiveUploadToBuffer(args.input, typing.input);
|
127
|
-
|
127
|
+
let req = {
|
128
128
|
// Fill defaults
|
129
129
|
...defaults,
|
130
130
|
...converted
|
131
131
|
};
|
132
|
+
// convert enum strings to integers
|
133
|
+
req = utils_1.convertEnumToInt(typing, req);
|
132
134
|
if (subjectField !== null) {
|
133
135
|
req.subject = registry_1.getTyping(types_1.authSubjectType).processor.fromPartial({});
|
134
136
|
const authToken = context.request.req.headers['authorization'];
|
@@ -192,13 +194,20 @@ const getGQLResolverFunctions = (service, key, serviceKey, grpcClientConfig) =>
|
|
192
194
|
});
|
193
195
|
return { details: aggregatedResponse, operationStatus };
|
194
196
|
}
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
197
|
+
if ('items' in result) {
|
198
|
+
let items = utils_1.decodeBufferFields(result.items, bufferFields);
|
199
|
+
return {
|
200
|
+
details: {
|
201
|
+
items: items,
|
202
|
+
operationStatus: result.operationStatus // overall status
|
203
|
+
},
|
204
|
+
};
|
205
|
+
}
|
206
|
+
else {
|
207
|
+
return {
|
208
|
+
details: utils_1.decodeBufferFields([result], bufferFields)[0]
|
209
|
+
};
|
210
|
+
}
|
202
211
|
}
|
203
212
|
catch (error) {
|
204
213
|
console.error(error);
|
@@ -251,6 +260,12 @@ const MutateResolver = async (req, ctx, schema) => {
|
|
251
260
|
else if (mode === 'UPSERT') {
|
252
261
|
method = 'Upsert';
|
253
262
|
}
|
263
|
+
const inputMethodTypeKey = module_name.toLowerCase() + '.' + key.toLowerCase() + '.' + method.toLowerCase();
|
264
|
+
const nsType = inputMethodType.get(inputMethodTypeKey);
|
265
|
+
if (nsType) {
|
266
|
+
const inputTyping = registry_1.getTyping(nsType);
|
267
|
+
utils_1.convertEnumToInt(inputTyping, input);
|
268
|
+
}
|
254
269
|
// check service object contains requested mode's method def
|
255
270
|
if (!service[method]) {
|
256
271
|
throw new Error(`Method ${method} not defined on ${module_name}`);
|
@@ -361,7 +376,7 @@ const MutateResolver = async (req, ctx, schema) => {
|
|
361
376
|
};
|
362
377
|
}
|
363
378
|
};
|
364
|
-
const registerResolverFunction = (namespace, name, func, mutation = false, subspace = undefined) => {
|
379
|
+
const registerResolverFunction = (namespace, name, func, mutation = false, subspace = undefined, service) => {
|
365
380
|
if (!namespaceResolverRegistry.has(namespace)) {
|
366
381
|
namespaceResolverRegistry.set(namespace, new Map());
|
367
382
|
}
|
@@ -383,6 +398,13 @@ const registerResolverFunction = (namespace, name, func, mutation = false, subsp
|
|
383
398
|
throw new Error(`Namespace "${namespace}" already contains a function: ${name} (mutation: ${mutation})`);
|
384
399
|
}
|
385
400
|
}
|
401
|
+
if (service) {
|
402
|
+
const key = (namespace === null || namespace === void 0 ? void 0 : namespace.toLocaleLowerCase()) + '.' + (subspace === null || subspace === void 0 ? void 0 : subspace.toLocaleLowerCase()) + '.' + (name === null || name === void 0 ? void 0 : name.toLocaleLowerCase());
|
403
|
+
const value = service.method.find((m) => m.name === name);
|
404
|
+
if (key && (value === null || value === void 0 ? void 0 : value.inputType)) {
|
405
|
+
inputMethodType.set(key, value.inputType);
|
406
|
+
}
|
407
|
+
}
|
386
408
|
// custom mutation resolver for create, update and upsert - Mutate
|
387
409
|
if (Mutate.indexOf(name) > -1) {
|
388
410
|
name = 'Mutate';
|
@@ -599,7 +621,7 @@ const getAndGenerateResolvers = (service, namespace, cfg, queryList, subspace =
|
|
599
621
|
const { mutations, queries } = exports.getWhitelistBlacklistConfig(service, queryList, cfg);
|
600
622
|
const func = exports.getGQLResolverFunctions(service, namespace, serviceKey || subspace || namespace, cfg.client);
|
601
623
|
Object.keys(func).forEach(k => {
|
602
|
-
exports.registerResolverFunction(namespace, k, func[k], !queries.has(k) && mutations.has(k), subspace);
|
624
|
+
exports.registerResolverFunction(namespace, k, func[k], !queries.has(k) && mutations.has(k), subspace, service);
|
603
625
|
});
|
604
626
|
return exports.generateResolver(namespace);
|
605
627
|
};
|
@@ -626,7 +648,9 @@ const generateSubServiceResolvers = (subServices, config, namespace) => {
|
|
626
648
|
const { mutations, queries } = exports.getWhitelistBlacklistConfig(sub.service, sub.queries, config);
|
627
649
|
const func = exports.getGQLResolverFunctions(sub.service, namespace, sub.name || namespace, config.client);
|
628
650
|
Object.keys(func).forEach(k => {
|
629
|
-
|
651
|
+
const regNamespace = config.root ? sub.name : namespace;
|
652
|
+
const regSubspace = config.root ? undefined : sub.name;
|
653
|
+
exports.registerResolverFunction(regNamespace, k, func[k], !queries.has(k) && mutations.has(k), regSubspace, sub.service);
|
630
654
|
});
|
631
655
|
});
|
632
656
|
if (config.root) {
|
@@ -8,8 +8,13 @@ export interface TypingData {
|
|
8
8
|
meta: DescriptorProto | EnumDescriptorProto;
|
9
9
|
processor?: any;
|
10
10
|
}
|
11
|
+
export declare const registeredTypings: Map<string, TypingData>;
|
12
|
+
export declare const scalarTypes: string[];
|
11
13
|
export declare const clearRegistry: () => void;
|
12
14
|
export declare const registerPackagesRecursive: (...protoMetadata: ProtoMetadata[]) => void;
|
15
|
+
export declare const getRegisteredEnumTypings: () => string[];
|
16
|
+
export declare const getNameSpaceTypeName: (typeName: string) => string | undefined;
|
17
|
+
export declare const recursiveEnumCheck: (typeName: string, enumMap: Map<string, string>, prevFieldName: string, traversedFields: string[]) => Map<string, string>;
|
13
18
|
export declare const registerTyping: (protoPackage: string, message: DescriptorProto, methodDef: MethodDescriptorProto[], opts?: Omit<Readonly<GraphQLObjectTypeConfig<any, any>>, "fields"> | undefined, inputOpts?: Omit<Readonly<GraphQLInputObjectTypeConfig>, "fields"> | undefined) => void;
|
14
19
|
export declare const registerEnumTyping: <T = {
|
15
20
|
[key: string]: any;
|
@@ -1,13 +1,17 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.getTyping = exports.registerEnumTyping = exports.registerTyping = exports.registerPackagesRecursive = exports.clearRegistry = void 0;
|
3
|
+
exports.getTyping = exports.registerEnumTyping = exports.registerTyping = exports.recursiveEnumCheck = exports.getNameSpaceTypeName = exports.getRegisteredEnumTypings = exports.registerPackagesRecursive = exports.clearRegistry = exports.scalarTypes = exports.registeredTypings = void 0;
|
4
4
|
const graphql_1 = require("graphql");
|
5
5
|
const definition_1 = require("graphql/type/definition");
|
6
6
|
const graphql_upload_1 = require("graphql-upload");
|
7
7
|
const utils_1 = require("./utils");
|
8
8
|
const types_1 = require("./types");
|
9
9
|
const descriptor_1 = require("ts-proto-descriptors/google/protobuf/descriptor");
|
10
|
-
const
|
10
|
+
const _ = require("lodash");
|
11
|
+
exports.registeredTypings = new Map();
|
12
|
+
exports.scalarTypes = ['Boolean', 'Int', 'Float', 'String', 'ID', 'Upload'];
|
13
|
+
const typeNameAndNameSpaceMapping = new Map();
|
14
|
+
const registeredEnumTypes = [];
|
11
15
|
const MapScalar = new definition_1.GraphQLScalarType({
|
12
16
|
name: 'MapScalar',
|
13
17
|
});
|
@@ -22,7 +26,7 @@ const TodoScalar = new definition_1.GraphQLScalarType({
|
|
22
26
|
}
|
23
27
|
});
|
24
28
|
const clearRegistry = () => {
|
25
|
-
registeredTypings.clear();
|
29
|
+
exports.registeredTypings.clear();
|
26
30
|
};
|
27
31
|
exports.clearRegistry = clearRegistry;
|
28
32
|
const registerPackagesRecursive = (...protoMetadata) => {
|
@@ -34,11 +38,65 @@ const registerPackagesRecursive = (...protoMetadata) => {
|
|
34
38
|
exports.registerEnumTyping(meta.fileDescriptor.package, m);
|
35
39
|
});
|
36
40
|
Object.keys(meta.references).forEach(key => {
|
37
|
-
registeredTypings.get(key).processor = meta.references[key];
|
41
|
+
exports.registeredTypings.get(key).processor = meta.references[key];
|
38
42
|
});
|
39
43
|
});
|
40
44
|
};
|
41
45
|
exports.registerPackagesRecursive = registerPackagesRecursive;
|
46
|
+
const getRegisteredEnumTypings = () => {
|
47
|
+
return registeredEnumTypes;
|
48
|
+
};
|
49
|
+
exports.getRegisteredEnumTypings = getRegisteredEnumTypings;
|
50
|
+
const getNameSpaceTypeName = (typeName) => {
|
51
|
+
return typeNameAndNameSpaceMapping.get(typeName);
|
52
|
+
};
|
53
|
+
exports.getNameSpaceTypeName = getNameSpaceTypeName;
|
54
|
+
// Iterate through the object and collect list of all enum types with their keys / paths
|
55
|
+
const recursiveEnumCheck = (typeName, enumMap, prevFieldName, traversedFields) => {
|
56
|
+
if (exports.scalarTypes.indexOf(typeName) <= -1) {
|
57
|
+
if (typeName && typeName.startsWith('[') && typeName.endsWith('!]')) {
|
58
|
+
typeName = typeName.substring(1, typeName.length - 2);
|
59
|
+
}
|
60
|
+
const objectNameSpace = exports.getNameSpaceTypeName(typeName);
|
61
|
+
if (objectNameSpace) {
|
62
|
+
const objectType = exports.getTyping(objectNameSpace);
|
63
|
+
if ((objectType === null || objectType === void 0 ? void 0 : objectType.input) && (registeredEnumTypes.indexOf(objectType.input.toString()) > -1)) {
|
64
|
+
enumMap.set(objectType === null || objectType === void 0 ? void 0 : objectType.input.toString(), prevFieldName);
|
65
|
+
prevFieldName = '';
|
66
|
+
}
|
67
|
+
else if (objectType === null || objectType === void 0 ? void 0 : objectType.input) {
|
68
|
+
// get nested fields from this object and check recursively
|
69
|
+
const gqlFields = objectType.input.getFields();
|
70
|
+
if (gqlFields) {
|
71
|
+
const fieldNames = Object.keys(gqlFields);
|
72
|
+
for (let fieldName of fieldNames) {
|
73
|
+
let fieldType = gqlFields[fieldName].type.toString();
|
74
|
+
// if fieldType is not basic type, get the object and make recursive check till no more objects are found
|
75
|
+
if (exports.scalarTypes.indexOf(fieldType) <= -1) {
|
76
|
+
// check if fieldName already exists in the traversedFields (to avoid circular reference for infinite loop)
|
77
|
+
if (traversedFields.indexOf(fieldName) <= -1) {
|
78
|
+
traversedFields.push(fieldName);
|
79
|
+
}
|
80
|
+
else if (traversedFields.indexOf(fieldName) > -1) {
|
81
|
+
// skip loop as this GQL type is already traversed
|
82
|
+
continue;
|
83
|
+
}
|
84
|
+
if (!prevFieldName || _.isEmpty(prevFieldName)) {
|
85
|
+
prevFieldName = fieldName;
|
86
|
+
}
|
87
|
+
if (prevFieldName && prevFieldName != fieldName) {
|
88
|
+
fieldName = prevFieldName + '.' + fieldName;
|
89
|
+
}
|
90
|
+
exports.recursiveEnumCheck(fieldType, enumMap, fieldName, traversedFields);
|
91
|
+
}
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
return enumMap;
|
98
|
+
};
|
99
|
+
exports.recursiveEnumCheck = recursiveEnumCheck;
|
42
100
|
const registerMessageTypesRecursive = (packageName, methodDef, ...types) => {
|
43
101
|
types.forEach(m => {
|
44
102
|
exports.registerTyping(packageName, m, methodDef);
|
@@ -78,7 +136,7 @@ const registerTyping = (protoPackage, message, methodDef, opts, inputOpts) => {
|
|
78
136
|
}
|
79
137
|
}
|
80
138
|
}
|
81
|
-
if (registeredTypings.has(type)) {
|
139
|
+
if (exports.registeredTypings.has(type)) {
|
82
140
|
// TODO Log debug "Typings for object are already registered"
|
83
141
|
return;
|
84
142
|
}
|
@@ -125,7 +183,8 @@ const registerTyping = (protoPackage, message, methodDef, opts, inputOpts) => {
|
|
125
183
|
...(inputOpts || { name: inputName }),
|
126
184
|
fields: inputFields,
|
127
185
|
});
|
128
|
-
|
186
|
+
typeNameAndNameSpaceMapping.set(resultInputObj.name, type);
|
187
|
+
exports.registeredTypings.set(type, {
|
129
188
|
output: resultObj,
|
130
189
|
input: resultInputObj,
|
131
190
|
meta: message
|
@@ -135,7 +194,7 @@ exports.registerTyping = registerTyping;
|
|
135
194
|
const registerEnumTyping = (protoPackage, message, opts) => {
|
136
195
|
var _a;
|
137
196
|
const type = (protoPackage.startsWith('.') ? '' : '.') + protoPackage + '.' + message.name;
|
138
|
-
if (registeredTypings.has(type)) {
|
197
|
+
if (exports.registeredTypings.has(type)) {
|
139
198
|
// TODO Log debug "Typings for enum are already registered"
|
140
199
|
return;
|
141
200
|
}
|
@@ -150,7 +209,9 @@ const registerEnumTyping = (protoPackage, message, opts) => {
|
|
150
209
|
...(opts || { name }),
|
151
210
|
values
|
152
211
|
});
|
153
|
-
|
212
|
+
registeredEnumTypes.push(name);
|
213
|
+
typeNameAndNameSpaceMapping.set(name, type);
|
214
|
+
exports.registeredTypings.set(type, {
|
154
215
|
output: result,
|
155
216
|
input: result,
|
156
217
|
meta: message
|
@@ -158,7 +219,7 @@ const registerEnumTyping = (protoPackage, message, opts) => {
|
|
158
219
|
};
|
159
220
|
exports.registerEnumTyping = registerEnumTyping;
|
160
221
|
const getTyping = (type) => {
|
161
|
-
return registeredTypings.get(type);
|
222
|
+
return exports.registeredTypings.get(type);
|
162
223
|
};
|
163
224
|
exports.getTyping = getTyping;
|
164
225
|
const resolveMeta = (key, field, rootObjType, objName, input) => {
|
@@ -173,17 +234,17 @@ const resolveMeta = (key, field, rootObjType, objName, input) => {
|
|
173
234
|
case descriptor_1.FieldDescriptorProto_Type.TYPE_ENUM:
|
174
235
|
case descriptor_1.FieldDescriptorProto_Type.TYPE_MESSAGE:
|
175
236
|
const objType = field.typeName;
|
176
|
-
if (!registeredTypings.has(objType)) {
|
237
|
+
if (!exports.registeredTypings.has(objType)) {
|
177
238
|
throw new Error("Typing '" + objType + "' not registered for key '" + key + "' in object: " + objName);
|
178
239
|
}
|
179
240
|
if (!input) {
|
180
|
-
result = registeredTypings.get(objType).output;
|
241
|
+
result = exports.registeredTypings.get(objType).output;
|
181
242
|
break;
|
182
243
|
}
|
183
244
|
if (objType === types_1.authSubjectType) {
|
184
245
|
return null;
|
185
246
|
}
|
186
|
-
result = registeredTypings.get(objType).input;
|
247
|
+
result = exports.registeredTypings.get(objType).input;
|
187
248
|
break;
|
188
249
|
case descriptor_1.FieldDescriptorProto_Type.TYPE_BYTES:
|
189
250
|
if (input) {
|
@@ -1,4 +1,25 @@
|
|
1
|
+
import { TypingData } from './registry';
|
1
2
|
export declare const capitalizeProtoName: (name: string) => string;
|
2
3
|
export declare const convertyCamelToSnakeCase: (entity: string) => string;
|
3
4
|
export declare const getKeys: (obj: any) => string[];
|
4
5
|
export declare const decodeBufferFields: (items: any, bufferFields: string[]) => any;
|
6
|
+
interface EnumMetaData {
|
7
|
+
name: string;
|
8
|
+
number: number;
|
9
|
+
options?: any;
|
10
|
+
}
|
11
|
+
/**
|
12
|
+
* recursively find the path and updates the object with given value, this function
|
13
|
+
* also takes care to handle if there is an array at any position in the path
|
14
|
+
* @param path path in dot notation
|
15
|
+
* @param val value to be updated in Object
|
16
|
+
* @param obj Object
|
17
|
+
*/
|
18
|
+
export declare const updateJSON: (path: string, val: EnumMetaData[], obj: any) => void;
|
19
|
+
/**
|
20
|
+
* converts enum string values to integers reading from the inputTyping
|
21
|
+
* @param TypingData input typing
|
22
|
+
* @param req request object from which the enum strings to be replaced
|
23
|
+
*/
|
24
|
+
export declare const convertEnumToInt: (inputTyping: TypingData, req: any) => any;
|
25
|
+
export {};
|
package/dist/gql/protos/utils.js
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.decodeBufferFields = exports.getKeys = exports.convertyCamelToSnakeCase = exports.capitalizeProtoName = void 0;
|
3
|
+
exports.convertEnumToInt = exports.updateJSON = exports.decodeBufferFields = exports.getKeys = exports.convertyCamelToSnakeCase = exports.capitalizeProtoName = void 0;
|
4
4
|
const _ = require("lodash");
|
5
|
+
const registry_1 = require("./registry");
|
5
6
|
const capitalizeProtoName = (name) => {
|
6
7
|
return name.replace(/(?:\.|^|_)(\w)/g, v => v.toUpperCase()).replace(/[._]/g, '');
|
7
8
|
};
|
@@ -48,3 +49,89 @@ const decodeBufferFields = (items, bufferFields) => {
|
|
48
49
|
}
|
49
50
|
};
|
50
51
|
exports.decodeBufferFields = decodeBufferFields;
|
52
|
+
/**
|
53
|
+
* recursively find the path and updates the object with given value, this function
|
54
|
+
* also takes care to handle if there is an array at any position in the path
|
55
|
+
* @param path path in dot notation
|
56
|
+
* @param val value to be updated in Object
|
57
|
+
* @param obj Object
|
58
|
+
*/
|
59
|
+
const updateJSON = (path, val, obj) => {
|
60
|
+
let fields = path.split('.');
|
61
|
+
let result = obj;
|
62
|
+
let j = 0;
|
63
|
+
for (let i = 0, n = fields.length; i < n && result !== undefined; i++) {
|
64
|
+
let field = fields[i];
|
65
|
+
if (i === n - 1) {
|
66
|
+
// reset value finally after iterating to the position (only if value already exists)
|
67
|
+
if (result[field]) {
|
68
|
+
const foundElement = val.find((e) => e.name === result[field]);
|
69
|
+
result[field] = foundElement === null || foundElement === void 0 ? void 0 : foundElement.number;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
else {
|
73
|
+
if (_.isArray(result[field])) {
|
74
|
+
// till i < n concat new fields
|
75
|
+
let newField = '';
|
76
|
+
for (let k = i + 1; k < n; k++) {
|
77
|
+
if (newField && !_.isEmpty(newField)) {
|
78
|
+
newField = newField + '.' + fields[k];
|
79
|
+
}
|
80
|
+
else {
|
81
|
+
newField = fields[k];
|
82
|
+
}
|
83
|
+
}
|
84
|
+
for (; j < result[field].length; j++) {
|
85
|
+
// recurisve call to update each element if its an array
|
86
|
+
exports.updateJSON(newField, val, result[field][j]);
|
87
|
+
}
|
88
|
+
}
|
89
|
+
else {
|
90
|
+
// update object till final path is reached
|
91
|
+
result = result[field];
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
95
|
+
};
|
96
|
+
exports.updateJSON = updateJSON;
|
97
|
+
/**
|
98
|
+
* converts enum string values to integers reading from the inputTyping
|
99
|
+
* @param TypingData input typing
|
100
|
+
* @param req request object from which the enum strings to be replaced
|
101
|
+
*/
|
102
|
+
const convertEnumToInt = (inputTyping, req) => {
|
103
|
+
let enumMap = new Map();
|
104
|
+
// enumMap populated with key as enum name space type and value as the path (to replace from request object)
|
105
|
+
if (inputTyping) {
|
106
|
+
const gqlInputObject = inputTyping.input;
|
107
|
+
const gqlFields = gqlInputObject.getFields();
|
108
|
+
if (gqlFields) {
|
109
|
+
const fieldNames = Object.keys(gqlFields);
|
110
|
+
for (let fieldName of fieldNames) {
|
111
|
+
// gql fieldName from input is of format [IIoRestorecommerceResourcebaseSort!]
|
112
|
+
// below check is to remove `[` and `!]`
|
113
|
+
let fieldType = gqlFields[fieldName].type.toString();
|
114
|
+
if (fieldType.startsWith('[') && fieldType.endsWith('!]')) {
|
115
|
+
fieldType = fieldType.substring(1, fieldType.length - 2);
|
116
|
+
}
|
117
|
+
// if fieldType is not basic type, then check if its fieldType belongs to Enum
|
118
|
+
// if not get the object and make recursive check till no more objects are found
|
119
|
+
if (registry_1.scalarTypes.indexOf(fieldType) <= -1) {
|
120
|
+
enumMap = registry_1.recursiveEnumCheck(fieldType, enumMap, fieldName, []);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
}
|
124
|
+
}
|
125
|
+
for (let [key, val] of enumMap) {
|
126
|
+
const enumNameSpace = registry_1.getNameSpaceTypeName(key);
|
127
|
+
if (enumNameSpace && typeof enumNameSpace === 'string') {
|
128
|
+
const enumTyping = registry_1.getTyping(enumNameSpace);
|
129
|
+
const enumIntMapping = (enumTyping === null || enumTyping === void 0 ? void 0 : enumTyping.meta).value;
|
130
|
+
if (enumIntMapping && _.isArray(enumIntMapping) && enumIntMapping.length > 0) {
|
131
|
+
exports.updateJSON(val, enumIntMapping, req);
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
return req;
|
136
|
+
};
|
137
|
+
exports.convertEnumToInt = convertEnumToInt;
|
package/dist/index.d.ts
CHANGED
package/dist/interfaces.d.ts
CHANGED