@centeredgesoft/runtime-mock-routes 2.1.0 → 2.5.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/build/src/app.js CHANGED
@@ -1,180 +1,188 @@
1
- "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
12
- };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.appFactory = exports.singleQueryParam = exports.cleanMethodBasedRuntimeRequestCollection = exports.cleanRuntimeRequestCollection = exports.isMethodBasedRuntimeRequestCollection = exports.isRuntimeRequestCollection = exports.isRuntimeRequestBody = exports.isSupportedMethod = exports.SupportedMethodsColection = void 0;
18
- const body_parser_1 = __importDefault(require("body-parser"));
19
- const chance_1 = __importDefault(require("chance"));
20
- const cors_1 = __importDefault(require("cors"));
21
- const express_1 = __importDefault(require("express"));
22
- const faker_1 = __importDefault(require("faker"));
23
- const feather_route_matcher_1 = __importDefault(require("feather-route-matcher"));
24
- const handlebars_1 = __importDefault(require("handlebars"));
25
- const lodash_1 = require("lodash");
26
- const sort_keys_1 = __importDefault(require("sort-keys"));
27
- const chance = new chance_1.default();
28
- handlebars_1.default.registerHelper('faker', (funcName, ...rest) => {
29
- try {
30
- const func = lodash_1.get(faker_1.default, funcName, () => '');
31
- return func(...rest);
32
- }
33
- catch (err) {
34
- return err;
35
- }
36
- });
37
- handlebars_1.default.registerHelper('chance', (funcName, ...rest) => {
38
- try {
39
- const func = lodash_1.get(chance, funcName, () => '');
40
- return func.call(chance, ...rest);
41
- }
42
- catch (err) {
43
- return err;
44
- }
45
- });
46
- exports.SupportedMethodsColection = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
47
- exports.isSupportedMethod = (obj) => exports.SupportedMethodsColection.includes(obj);
48
- exports.isRuntimeRequestBody = (obj) => {
49
- try {
50
- return typeof obj.path === 'string'
51
- && obj.hasOwnProperty('body')
52
- && (obj.status === undefined || !isNaN(Number(obj.status)))
53
- && exports.isSupportedMethod(obj.method);
54
- }
55
- catch (_) {
56
- return false;
57
- }
58
- };
59
- exports.isRuntimeRequestCollection = (obj) => {
60
- try {
61
- return !Object.keys(obj).some(k => !exports.isRuntimeRequestBody(obj[k]));
62
- }
63
- catch (_) {
64
- return false;
65
- }
66
- };
67
- exports.isMethodBasedRuntimeRequestCollection = (obj) => {
68
- try {
69
- return !Object.keys(obj).some(k => !(exports.isSupportedMethod(k) && exports.isRuntimeRequestCollection(obj[k])));
70
- }
71
- catch (_) {
72
- return false;
73
- }
74
- };
75
- exports.cleanRuntimeRequestCollection = (params) => {
76
- return Object.keys(params.collection).reduce((acc, curr) => {
77
- const sanitizedPath = `/${curr}`.replace(/\/\//g, '/');
78
- const fixedRequest = Object.assign(Object.assign({}, params.collection[curr]), { path: sanitizedPath, method: params.method });
79
- return Object.assign(Object.assign({}, acc), { [sanitizedPath]: fixedRequest });
80
- }, {});
81
- };
82
- exports.cleanMethodBasedRuntimeRequestCollection = (obj) => {
83
- return Object.keys(obj).reduce((acc, curr) => {
84
- const method = curr;
85
- const collection = obj[curr];
86
- return Object.assign(Object.assign({}, acc), { [curr]: exports.cleanRuntimeRequestCollection({ method, collection }) });
87
- }, {});
88
- };
89
- exports.singleQueryParam = (obj) => Array.isArray(obj) ? `${obj[0]}` : `${obj}`;
90
- exports.appFactory = (runtimeCollection) => {
91
- let runtimeRequestCollection = runtimeCollection || {};
92
- if (!exports.isMethodBasedRuntimeRequestCollection(runtimeRequestCollection)) {
93
- throw new Error('Intial requests JSON is invalid');
94
- }
95
- runtimeRequestCollection = exports.cleanMethodBasedRuntimeRequestCollection(runtimeRequestCollection);
96
- const app = express_1.default();
97
- app.use(cors_1.default());
98
- app.use(body_parser_1.default.json());
99
- app.get('/', (_req, res) => res.send(sort_keys_1.default(runtimeRequestCollection, { deep: true })));
100
- app.post('/', (req, res) => {
101
- const { body } = req;
102
- if (exports.isRuntimeRequestBody(body)) {
103
- const fixedPath = `/${body.path}`.replace(/\/\//g, '/');
104
- body.path = fixedPath;
105
- const method = body.method;
106
- runtimeRequestCollection = Object.assign(Object.assign({}, runtimeRequestCollection), { [method]: Object.assign(Object.assign({}, (runtimeRequestCollection[method] || {})), { [fixedPath]: body }) });
107
- return res.status(204).send();
108
- }
109
- return res.status(400).send();
110
- });
111
- app.delete('/', (req, res) => {
112
- let { path, method } = req.query;
113
- if (!path || !method) {
114
- return res.status(400).send();
115
- }
116
- const cleanPath = exports.singleQueryParam(path);
117
- const cleanMethod = exports.singleQueryParam(method);
118
- if (!exports.isSupportedMethod(cleanMethod)) {
119
- return res.status(400).send();
120
- }
121
- if (!Object.keys(runtimeRequestCollection).includes(cleanMethod)) {
122
- return res.status(204).send();
123
- }
124
- const _a = runtimeRequestCollection, _b = cleanMethod, oldCollection = _a[_b], otherMetods = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
125
- const _c = oldCollection, _d = cleanPath, _ = _c[_d], newCollection = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]);
126
- runtimeRequestCollection = Object.assign(Object.assign({}, otherMetods), { [cleanMethod]: newCollection });
127
- return res.status(204).send();
128
- });
129
- const catchAllHandler = (req, res) => {
130
- const method = req.method;
131
- if (!exports.isSupportedMethod(method)) {
132
- return res.status(400).send();
133
- }
134
- const collection = runtimeRequestCollection[method];
135
- if (!collection) {
136
- return res.status(404).send();
137
- }
138
- const matcher = feather_route_matcher_1.default(collection);
139
- const routeMatch = matcher(req.path);
140
- if (routeMatch) {
141
- const reqInfo = routeMatch.value;
142
- const params = routeMatch.params;
143
- const tokenParams = {
144
- query: req.query,
145
- params,
146
- body: req.body
147
- };
148
- if (reqInfo.status) {
149
- res.status(reqInfo.status);
150
- }
151
- const { body } = reqInfo;
152
- if (typeof body !== 'string') {
153
- return res.send(body);
154
- }
155
- const template = handlebars_1.default.compile(body);
156
- const result = template(tokenParams);
157
- try {
158
- const jsonResult = JSON.parse(result);
159
- return res.send(jsonResult);
160
- }
161
- catch (_) {
162
- return res.send(result);
163
- }
164
- }
165
- return res.status(404).send();
166
- };
167
- app.get('/*', catchAllHandler);
168
- app.post('/*', catchAllHandler);
169
- app.put('/*', catchAllHandler);
170
- app.patch('/*', catchAllHandler);
171
- app.delete('/*', catchAllHandler);
172
- Object.defineProperty(app, 'runtimeRequestCollection', {
173
- get: () => {
174
- return lodash_1.cloneDeep(runtimeRequestCollection);
175
- }
176
- });
177
- return app;
178
- };
179
- const app = exports.appFactory();
180
- exports.default = app;
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.appFactory = exports.singleQueryParam = exports.cleanMethodBasedRuntimeRequestCollection = exports.cleanRuntimeRequestCollection = exports.isMethodBasedRuntimeRequestCollection = exports.isRuntimeRequestCollection = exports.isRuntimeRequestBody = exports.isSupportedMethod = exports.SupportedMethodsColection = void 0;
18
+ const body_parser_1 = __importDefault(require("body-parser"));
19
+ const chance_1 = __importDefault(require("chance"));
20
+ const cors_1 = __importDefault(require("cors"));
21
+ const express_1 = __importDefault(require("express"));
22
+ const faker_1 = __importDefault(require("faker"));
23
+ const feather_route_matcher_1 = __importDefault(require("feather-route-matcher"));
24
+ const handlebars_1 = __importDefault(require("handlebars"));
25
+ const lodash_1 = require("lodash");
26
+ const sort_keys_1 = __importDefault(require("sort-keys"));
27
+ const chance = new chance_1.default();
28
+ handlebars_1.default.registerHelper('faker', (funcName, ...rest) => {
29
+ try {
30
+ const func = lodash_1.get(faker_1.default, funcName, () => '');
31
+ return func(...rest);
32
+ }
33
+ catch (err) {
34
+ return err;
35
+ }
36
+ });
37
+ handlebars_1.default.registerHelper('chance', (funcName, ...rest) => {
38
+ try {
39
+ const func = lodash_1.get(chance, funcName, () => '');
40
+ return func.call(chance, ...rest);
41
+ }
42
+ catch (err) {
43
+ return err;
44
+ }
45
+ });
46
+ exports.SupportedMethodsColection = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
47
+ const isSupportedMethod = (obj) => exports.SupportedMethodsColection.includes(obj);
48
+ exports.isSupportedMethod = isSupportedMethod;
49
+ const isRuntimeRequestBody = (obj) => {
50
+ try {
51
+ return typeof obj.path === 'string'
52
+ && obj.hasOwnProperty('body')
53
+ && (obj.status === undefined || !isNaN(Number(obj.status)))
54
+ && exports.isSupportedMethod(obj.method);
55
+ }
56
+ catch (_) {
57
+ return false;
58
+ }
59
+ };
60
+ exports.isRuntimeRequestBody = isRuntimeRequestBody;
61
+ const isRuntimeRequestCollection = (obj) => {
62
+ try {
63
+ return !Object.keys(obj).some(k => !exports.isRuntimeRequestBody(obj[k]));
64
+ }
65
+ catch (_) {
66
+ return false;
67
+ }
68
+ };
69
+ exports.isRuntimeRequestCollection = isRuntimeRequestCollection;
70
+ const isMethodBasedRuntimeRequestCollection = (obj) => {
71
+ try {
72
+ return !Object.keys(obj).some(k => !(exports.isSupportedMethod(k) && exports.isRuntimeRequestCollection(obj[k])));
73
+ }
74
+ catch (_) {
75
+ return false;
76
+ }
77
+ };
78
+ exports.isMethodBasedRuntimeRequestCollection = isMethodBasedRuntimeRequestCollection;
79
+ const cleanRuntimeRequestCollection = (params) => {
80
+ return Object.keys(params.collection).reduce((acc, curr) => {
81
+ const sanitizedPath = `/${curr}`.replace(/\/\//g, '/');
82
+ const fixedRequest = Object.assign(Object.assign({}, params.collection[curr]), { path: sanitizedPath, method: params.method });
83
+ return Object.assign(Object.assign({}, acc), { [sanitizedPath]: fixedRequest });
84
+ }, {});
85
+ };
86
+ exports.cleanRuntimeRequestCollection = cleanRuntimeRequestCollection;
87
+ const cleanMethodBasedRuntimeRequestCollection = (obj) => {
88
+ return Object.keys(obj).reduce((acc, curr) => {
89
+ const method = curr;
90
+ const collection = obj[curr];
91
+ return Object.assign(Object.assign({}, acc), { [curr]: exports.cleanRuntimeRequestCollection({ method, collection }) });
92
+ }, {});
93
+ };
94
+ exports.cleanMethodBasedRuntimeRequestCollection = cleanMethodBasedRuntimeRequestCollection;
95
+ const singleQueryParam = (obj) => Array.isArray(obj) ? `${obj[0]}` : `${obj}`;
96
+ exports.singleQueryParam = singleQueryParam;
97
+ const appFactory = (runtimeCollection) => {
98
+ let runtimeRequestCollection = runtimeCollection || {};
99
+ if (!exports.isMethodBasedRuntimeRequestCollection(runtimeRequestCollection)) {
100
+ throw new Error('Intial requests JSON is invalid');
101
+ }
102
+ runtimeRequestCollection = exports.cleanMethodBasedRuntimeRequestCollection(runtimeRequestCollection);
103
+ const app = express_1.default();
104
+ app.use(cors_1.default());
105
+ app.use(body_parser_1.default.json());
106
+ app.get('/', (_req, res) => res.send(sort_keys_1.default(runtimeRequestCollection, { deep: true })));
107
+ app.post('/', (req, res) => {
108
+ const { body } = req;
109
+ if (exports.isRuntimeRequestBody(body)) {
110
+ const fixedPath = `/${body.path}`.replace(/\/\//g, '/');
111
+ body.path = fixedPath;
112
+ const method = body.method;
113
+ runtimeRequestCollection = Object.assign(Object.assign({}, runtimeRequestCollection), { [method]: Object.assign(Object.assign({}, (runtimeRequestCollection[method] || {})), { [fixedPath]: body }) });
114
+ return res.status(204).send();
115
+ }
116
+ return res.status(400).send();
117
+ });
118
+ app.delete('/', (req, res) => {
119
+ let { path, method } = req.query;
120
+ if (!path || !method) {
121
+ return res.status(400).send();
122
+ }
123
+ const cleanPath = exports.singleQueryParam(path);
124
+ const cleanMethod = exports.singleQueryParam(method);
125
+ if (!exports.isSupportedMethod(cleanMethod)) {
126
+ return res.status(400).send();
127
+ }
128
+ if (!Object.keys(runtimeRequestCollection).includes(cleanMethod)) {
129
+ return res.status(204).send();
130
+ }
131
+ const _a = runtimeRequestCollection, _b = cleanMethod, oldCollection = _a[_b], otherMetods = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
132
+ const _c = oldCollection, _d = cleanPath, _ = _c[_d], newCollection = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]);
133
+ runtimeRequestCollection = Object.assign(Object.assign({}, otherMetods), { [cleanMethod]: newCollection });
134
+ return res.status(204).send();
135
+ });
136
+ const catchAllHandler = (req, res) => {
137
+ const method = req.method;
138
+ if (!exports.isSupportedMethod(method)) {
139
+ return res.status(400).send();
140
+ }
141
+ const collection = runtimeRequestCollection[method];
142
+ if (!collection) {
143
+ return res.status(404).send();
144
+ }
145
+ const matcher = feather_route_matcher_1.default(collection);
146
+ const routeMatch = matcher(req.path);
147
+ if (routeMatch) {
148
+ const reqInfo = routeMatch.value;
149
+ const params = routeMatch.params;
150
+ const tokenParams = {
151
+ query: req.query,
152
+ params,
153
+ body: req.body
154
+ };
155
+ if (reqInfo.status) {
156
+ res.status(reqInfo.status);
157
+ }
158
+ const { body } = reqInfo;
159
+ if (typeof body !== 'string') {
160
+ return res.send(body);
161
+ }
162
+ const template = handlebars_1.default.compile(body);
163
+ const result = template(tokenParams);
164
+ try {
165
+ const jsonResult = JSON.parse(result);
166
+ return res.send(jsonResult);
167
+ }
168
+ catch (_) {
169
+ return res.send(result);
170
+ }
171
+ }
172
+ return res.status(404).send();
173
+ };
174
+ app.get('/*', catchAllHandler);
175
+ app.post('/*', catchAllHandler);
176
+ app.put('/*', catchAllHandler);
177
+ app.patch('/*', catchAllHandler);
178
+ app.delete('/*', catchAllHandler);
179
+ Object.defineProperty(app, 'runtimeRequestCollection', {
180
+ get: () => {
181
+ return lodash_1.cloneDeep(runtimeRequestCollection);
182
+ }
183
+ });
184
+ return app;
185
+ };
186
+ exports.appFactory = appFactory;
187
+ const app = exports.appFactory();
188
+ exports.default = app;
@@ -1,23 +1,30 @@
1
- declare type SupportedMethodsType = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
2
- export declare const SupportedMethodsColection: SupportedMethodsType;
3
- export declare type SupportedMethod = SupportedMethodsType[number];
4
- export declare const isSupportedMethod: (obj: any) => obj is "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
5
- export interface RuntimeRequestMethodBody {
6
- body: any;
7
- status?: number;
8
- }
9
- export declare const isRuntimeRequestMethodBody: (obj: any) => obj is RuntimeRequestMethodBody;
10
- export declare type RuntimeRequestMethodBodyCollection = Partial<Record<SupportedMethod, RuntimeRequestMethodBody>>;
11
- export declare const isRuntimeRequestMethodBodyCollection: (obj: any) => obj is Partial<Record<"GET" | "POST" | "PUT" | "PATCH" | "DELETE", RuntimeRequestMethodBody>>;
12
- export interface RuntimeRequestBody {
13
- path: string;
14
- methods: RuntimeRequestMethodBodyCollection;
15
- }
16
- export declare const isRuntimeRequestBody: (obj: any) => obj is RuntimeRequestBody;
17
- export interface RuntimeRequestCollection {
18
- [path: string]: RuntimeRequestBody;
19
- }
20
- export declare const isRuntimeRequestCollection: (obj: any) => obj is RuntimeRequestCollection;
21
- export declare const appFactory: (runtimeCollection?: RuntimeRequestCollection) => import("express-serve-static-core").Express;
22
- declare const app: import("express-serve-static-core").Express;
23
- export default app;
1
+ declare type SupportedMethodsType = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
2
+ export declare const SupportedMethodsColection: SupportedMethodsType;
3
+ export declare type SupportedMethod = SupportedMethodsType[number];
4
+ export declare const isSupportedMethod: (obj: any) => obj is "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
5
+ export interface RuntimeRequestMethodBody {
6
+ body: any;
7
+ status?: number | ((...args: any[]) => number);
8
+ headers?: Record<string, string>;
9
+ }
10
+ export interface RequestParameters {
11
+ query: any;
12
+ params: any;
13
+ body: any;
14
+ headers: any;
15
+ }
16
+ export declare const isRuntimeRequestMethodBody: (obj: any) => obj is RuntimeRequestMethodBody;
17
+ export declare type RuntimeRequestMethodBodyCollection = Partial<Record<SupportedMethod, RuntimeRequestMethodBody>>;
18
+ export declare const isRuntimeRequestMethodBodyCollection: (obj: any) => obj is Partial<Record<"GET" | "POST" | "PUT" | "PATCH" | "DELETE", RuntimeRequestMethodBody>>;
19
+ export interface RuntimeRequestBody {
20
+ path: string;
21
+ methods: RuntimeRequestMethodBodyCollection;
22
+ }
23
+ export declare const isRuntimeRequestBody: (obj: any) => obj is RuntimeRequestBody;
24
+ export interface RuntimeRequestCollection {
25
+ [path: string]: RuntimeRequestBody;
26
+ }
27
+ export declare const isRuntimeRequestCollection: (obj: any) => obj is RuntimeRequestCollection;
28
+ export declare const appFactory: (runtimeCollection?: RuntimeRequestCollection) => import("express-serve-static-core").Express;
29
+ declare const app: import("express-serve-static-core").Express;
30
+ export default app;