@centeredgesoft/runtime-mock-routes 2.4.0 → 2.8.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/.devcontainer/Dockerfile +16 -0
- package/.devcontainer/devcontainer.json +32 -0
- package/README.md +1 -1
- package/build/src/appV2.d.ts +1 -1
- package/build/src/appV2.js +5 -2
- package/package.json +1 -1
- package/src/appV2.test.ts +53 -2
- package/src/appV2.ts +7 -3
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/typescript-node/.devcontainer/base.Dockerfile
|
|
2
|
+
|
|
3
|
+
# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 16, 14, 12, 16-bullseye, 14-bullseye, 12-bullseye, 16-buster, 14-buster, 12-buster
|
|
4
|
+
ARG VARIANT="16-bullseye"
|
|
5
|
+
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}
|
|
6
|
+
|
|
7
|
+
# [Optional] Uncomment this section to install additional OS packages.
|
|
8
|
+
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
|
|
9
|
+
# && apt-get -y install --no-install-recommends <your-package-list-here>
|
|
10
|
+
|
|
11
|
+
# [Optional] Uncomment if you want to install an additional version of node using nvm
|
|
12
|
+
# ARG EXTRA_NODE_VERSION=10
|
|
13
|
+
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"
|
|
14
|
+
|
|
15
|
+
# [Optional] Uncomment if you want to install more global node packages
|
|
16
|
+
# RUN su node -c "npm install -g <your-package-list -here>"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
|
|
2
|
+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.209.6/containers/typescript-node
|
|
3
|
+
{
|
|
4
|
+
"name": "Node.js & TypeScript",
|
|
5
|
+
"build": {
|
|
6
|
+
"dockerfile": "Dockerfile",
|
|
7
|
+
// Update 'VARIANT' to pick a Node version: 16, 14, 12.
|
|
8
|
+
// Append -bullseye or -buster to pin to an OS version.
|
|
9
|
+
// Use -bullseye variants on local on arm64/Apple Silicon.
|
|
10
|
+
"args": {
|
|
11
|
+
"VARIANT": "16-bullseye"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
// Set *default* container specific settings.json values on container create.
|
|
16
|
+
"settings": {},
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
// Add the IDs of extensions you want installed when the container is created.
|
|
20
|
+
"extensions": [
|
|
21
|
+
"dbaeumer.vscode-eslint"
|
|
22
|
+
],
|
|
23
|
+
|
|
24
|
+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
|
25
|
+
// "forwardPorts": [],
|
|
26
|
+
|
|
27
|
+
// Use 'postCreateCommand' to run commands after the container is created.
|
|
28
|
+
// "postCreateCommand": "yarn install",
|
|
29
|
+
|
|
30
|
+
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
|
|
31
|
+
"remoteUser": "node"
|
|
32
|
+
}
|
package/README.md
CHANGED
package/build/src/appV2.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare type SupportedMethod = SupportedMethodsType[number];
|
|
|
4
4
|
export declare const isSupportedMethod: (obj: any) => obj is "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
5
5
|
export interface RuntimeRequestMethodBody {
|
|
6
6
|
body: any;
|
|
7
|
-
status?:
|
|
7
|
+
status?: any;
|
|
8
8
|
headers?: Record<string, string>;
|
|
9
9
|
}
|
|
10
10
|
export interface RequestParameters {
|
package/build/src/appV2.js
CHANGED
|
@@ -47,7 +47,7 @@ const isSupportedMethod = (obj) => exports.SupportedMethodsColection.includes(ob
|
|
|
47
47
|
exports.isSupportedMethod = isSupportedMethod;
|
|
48
48
|
const isRuntimeRequestMethodBody = (obj) => {
|
|
49
49
|
try {
|
|
50
|
-
return !!obj.body && (!obj.status || !isNaN(Number(obj.status)));
|
|
50
|
+
return !!obj.body && (!obj.status || typeof obj.status === 'function' || !isNaN(Number(obj.status)));
|
|
51
51
|
}
|
|
52
52
|
catch (_) {
|
|
53
53
|
return false;
|
|
@@ -150,7 +150,10 @@ const appFactory = (runtimeCollection) => {
|
|
|
150
150
|
headers: req.headers
|
|
151
151
|
};
|
|
152
152
|
if (method.status) {
|
|
153
|
-
|
|
153
|
+
const status = typeof method.status === 'function'
|
|
154
|
+
? method.status(tokenParams)
|
|
155
|
+
: method.status;
|
|
156
|
+
res.status(status);
|
|
154
157
|
}
|
|
155
158
|
if (method.headers) {
|
|
156
159
|
const processedHeaders = Object.keys(method.headers).reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: handlebars_1.default.compile(method.headers[curr])(tokenParams) })), {});
|
package/package.json
CHANGED
package/src/appV2.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import request from 'supertest';
|
|
2
|
-
import { appFactory, isRuntimeRequestCollection, RuntimeRequestBody, RuntimeRequestCollection, SupportedMethodsColection, RequestParameters } from './appV2';
|
|
2
|
+
import { appFactory, isRuntimeRequestCollection, RuntimeRequestBody, RuntimeRequestCollection, SupportedMethodsColection, RequestParameters, RuntimeRequestMethodBody, isRuntimeRequestMethodBody } from './appV2';
|
|
3
3
|
|
|
4
4
|
describe('appFactory', () => {
|
|
5
5
|
test('Creates app with no seed', () => {
|
|
@@ -318,4 +318,55 @@ describe('isRuntimeRequestMethodBodyCollection', () => {
|
|
|
318
318
|
const valid = isRuntimeRequestCollection(seed);
|
|
319
319
|
expect(valid).toBeTruthy();
|
|
320
320
|
})
|
|
321
|
-
})
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
describe('Status as a function', () => {
|
|
324
|
+
test('is true for status that is function', () => {
|
|
325
|
+
const seed: RuntimeRequestMethodBody = {
|
|
326
|
+
body: "test",
|
|
327
|
+
status: () => {
|
|
328
|
+
return 200;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
const valid = isRuntimeRequestMethodBody(seed);
|
|
333
|
+
expect(valid).toBeTruthy();
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test('Executes body and status functions', async () => {
|
|
337
|
+
const seed: RuntimeRequestCollection = {
|
|
338
|
+
"/test": {
|
|
339
|
+
path: "/test",
|
|
340
|
+
methods: {
|
|
341
|
+
GET: {
|
|
342
|
+
body: function(rp: RequestParameters) {
|
|
343
|
+
if(rp.query.id == "2") {
|
|
344
|
+
return {id: 2};
|
|
345
|
+
} else {
|
|
346
|
+
return {id: 3};
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
status: function(rp: RequestParameters) {
|
|
350
|
+
if(rp.query.id == "2") {
|
|
351
|
+
return 200;
|
|
352
|
+
} else {
|
|
353
|
+
return 400;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const app = appFactory(seed);
|
|
362
|
+
expect(app).toBeDefined();
|
|
363
|
+
|
|
364
|
+
var response = await request(app).get('/test?id=2');
|
|
365
|
+
expect(response.body).toEqual({id: 2});
|
|
366
|
+
expect(response.status).toEqual(200);
|
|
367
|
+
|
|
368
|
+
var secondResponse = await request(app).get('/test?id=3');
|
|
369
|
+
expect(secondResponse.body).toEqual({id: 3});
|
|
370
|
+
expect(secondResponse.status).toEqual(400);
|
|
371
|
+
})
|
|
372
|
+
})
|
package/src/appV2.ts
CHANGED
|
@@ -35,7 +35,7 @@ export const isSupportedMethod = (obj: any): obj is SupportedMethod => Supported
|
|
|
35
35
|
|
|
36
36
|
export interface RuntimeRequestMethodBody {
|
|
37
37
|
body: any;
|
|
38
|
-
status?:
|
|
38
|
+
status?: any;
|
|
39
39
|
headers?: Record<string, string>;
|
|
40
40
|
}
|
|
41
41
|
|
|
@@ -48,7 +48,7 @@ export interface RequestParameters {
|
|
|
48
48
|
|
|
49
49
|
export const isRuntimeRequestMethodBody = (obj: any): obj is RuntimeRequestMethodBody => {
|
|
50
50
|
try {
|
|
51
|
-
return !!obj.body && (!obj.status || !isNaN(Number(obj.status)))
|
|
51
|
+
return !!obj.body && (!obj.status || typeof obj.status === 'function' || !isNaN(Number(obj.status)))
|
|
52
52
|
} catch (_) {
|
|
53
53
|
return false;
|
|
54
54
|
}
|
|
@@ -196,7 +196,11 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
|
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
if (method.status) {
|
|
199
|
-
|
|
199
|
+
const status = typeof method.status === 'function'
|
|
200
|
+
? method.status(tokenParams)
|
|
201
|
+
: method.status
|
|
202
|
+
|
|
203
|
+
res.status(status);
|
|
200
204
|
}
|
|
201
205
|
|
|
202
206
|
if (method.headers) {
|