UNPKG

1.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transformPatternToRoute = void 0;
4const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
5/**
6 * Transforms the Pattern to Route.
7 * 1. If Pattern is a `string`, it will be returned as it is.
8 * 2. If Pattern is a `number`, it will be converted to `string`.
9 * 3. If Pattern is a `JSON` object, it will be transformed to Route. For that end,
10 * the function will sort properties of `JSON` Object and creates `route` string
11 * according to the following template:
12 * <key1>:<value1>/<key2>:<value2>/.../<keyN>:<valueN>
13 *
14 * @param {MsPattern} pattern - client pattern
15 * @returns string
16 */
17function transformPatternToRoute(pattern) {
18 if ((0, shared_utils_1.isString)(pattern) || (0, shared_utils_1.isNumber)(pattern)) {
19 return `${pattern}`;
20 }
21 if (!(0, shared_utils_1.isObject)(pattern)) {
22 return pattern;
23 }
24 const sortedKeys = Object.keys(pattern).sort((a, b) => ('' + a).localeCompare(b));
25 // Creates the array of Pattern params from sorted keys and their corresponding values
26 const sortedPatternParams = sortedKeys.map(key => {
27 let partialRoute = `"${key}":`;
28 partialRoute += (0, shared_utils_1.isString)(pattern[key])
29 ? `"${transformPatternToRoute(pattern[key])}"`
30 : transformPatternToRoute(pattern[key]);
31 return partialRoute;
32 });
33 const route = sortedPatternParams.join(',');
34 return `{${route}}`;
35}
36exports.transformPatternToRoute = transformPatternToRoute;