UNPKG

2.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ContextUtils = void 0;
4const constants_1 = require("@nestjs/common/constants");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const execution_context_host_1 = require("./execution-context-host");
7class ContextUtils {
8 mapParamType(key) {
9 const keyPair = key.split(':');
10 return keyPair[0];
11 }
12 reflectCallbackParamtypes(instance, methodName) {
13 return Reflect.getMetadata(constants_1.PARAMTYPES_METADATA, instance, methodName);
14 }
15 reflectCallbackMetadata(instance, methodName, metadataKey) {
16 return Reflect.getMetadata(metadataKey, instance.constructor, methodName);
17 }
18 reflectPassthrough(instance, methodName) {
19 return Reflect.getMetadata(constants_1.RESPONSE_PASSTHROUGH_METADATA, instance.constructor, methodName);
20 }
21 getArgumentsLength(keys, metadata) {
22 return keys.length
23 ? Math.max(...keys.map(key => metadata[key].index)) + 1
24 : 0;
25 }
26 createNullArray(length) {
27 const a = new Array(length);
28 for (let i = 0; i < length; ++i)
29 a[i] = undefined;
30 return a;
31 }
32 mergeParamsMetatypes(paramsProperties, paramtypes) {
33 if (!paramtypes) {
34 return paramsProperties;
35 }
36 return paramsProperties.map(param => ({
37 ...param,
38 metatype: paramtypes[param.index],
39 }));
40 }
41 getCustomFactory(factory, data, contextFactory) {
42 return (0, shared_utils_1.isFunction)(factory)
43 ? (...args) => factory(data, contextFactory(args))
44 : () => null;
45 }
46 getContextFactory(contextType, instance, callback) {
47 const type = instance && instance.constructor;
48 return (args) => {
49 const ctx = new execution_context_host_1.ExecutionContextHost(args, type, callback);
50 ctx.setType(contextType);
51 return ctx;
52 };
53 }
54}
55exports.ContextUtils = ContextUtils;