1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const types_1 = require("../helpers/types");
|
5 | const validate_arg_1 = require("./validate-arg");
|
6 | const auth_middleware_1 = require("../helpers/auth-middleware");
|
7 | function getParams(params, resolverData, globalValidate, pubSub) {
|
8 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
9 | return Promise.all(params
|
10 | .sort((a, b) => a.index - b.index)
|
11 | .map((paramInfo) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
12 | switch (paramInfo.kind) {
|
13 | case "args":
|
14 | return yield validate_arg_1.validateArg(types_1.convertToType(paramInfo.getType(), resolverData.args), globalValidate, paramInfo.validate);
|
15 | case "arg":
|
16 | return yield validate_arg_1.validateArg(types_1.convertToType(paramInfo.getType(), resolverData.args[paramInfo.name]), globalValidate, paramInfo.validate);
|
17 | case "context":
|
18 | if (paramInfo.propertyName) {
|
19 | return resolverData.context[paramInfo.propertyName];
|
20 | }
|
21 | return resolverData.context;
|
22 | case "root":
|
23 | const rootValue = paramInfo.propertyName
|
24 | ? resolverData.root[paramInfo.propertyName]
|
25 | : resolverData.root;
|
26 | if (!paramInfo.getType) {
|
27 | return rootValue;
|
28 | }
|
29 | return types_1.convertToType(paramInfo.getType(), rootValue);
|
30 | case "info":
|
31 | return resolverData.info;
|
32 | case "pubSub":
|
33 | if (paramInfo.triggerKey) {
|
34 | return (payload) => pubSub.publish(paramInfo.triggerKey, payload);
|
35 | }
|
36 | return pubSub;
|
37 | case "custom":
|
38 | return yield paramInfo.resolver(resolverData);
|
39 | }
|
40 | })));
|
41 | });
|
42 | }
|
43 | exports.getParams = getParams;
|
44 | function applyAuthChecker(middlewares, authMode, authChecker, roles) {
|
45 | if (authChecker && roles) {
|
46 | middlewares.unshift(auth_middleware_1.AuthMiddleware(authChecker, authMode, roles));
|
47 | }
|
48 | }
|
49 | exports.applyAuthChecker = applyAuthChecker;
|
50 | function applyMiddlewares(container, resolverData, middlewares, resolverHandlerFunction) {
|
51 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
52 | let middlewaresIndex = -1;
|
53 | function dispatchHandler(currentIndex) {
|
54 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
55 | if (currentIndex <= middlewaresIndex) {
|
56 | throw new Error("next() called multiple times");
|
57 | }
|
58 | middlewaresIndex = currentIndex;
|
59 | let handlerFn;
|
60 | if (currentIndex === middlewares.length) {
|
61 | handlerFn = resolverHandlerFunction;
|
62 | }
|
63 | else {
|
64 | const currentMiddleware = middlewares[currentIndex];
|
65 |
|
66 | if (currentMiddleware.prototype !== undefined) {
|
67 | const middlewareClassInstance = container.getInstance(currentMiddleware, resolverData);
|
68 | handlerFn = middlewareClassInstance.use.bind(middlewareClassInstance);
|
69 | }
|
70 | else {
|
71 | handlerFn = currentMiddleware;
|
72 | }
|
73 | }
|
74 | let nextResult;
|
75 | const result = yield handlerFn(resolverData, () => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
76 | nextResult = yield dispatchHandler(currentIndex + 1);
|
77 | return nextResult;
|
78 | }));
|
79 | return result !== undefined ? result : nextResult;
|
80 | });
|
81 | }
|
82 | return dispatchHandler(0);
|
83 | });
|
84 | }
|
85 | exports.applyMiddlewares = applyMiddlewares;
|