@centeredgesoft/runtime-mock-routes 2.5.0 → 2.8.2

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/README.md CHANGED
@@ -28,7 +28,7 @@ type SupportedMethod = SupportedMethodsType[number];
28
28
 
29
29
  export interface RuntimeRequestMethodBody {
30
30
  body: any;
31
- status?: number | ((...args : any[])=>number);
31
+ status?: any;
32
32
  headers?: Record<string, string>;
33
33
  }
34
34
 
@@ -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?: number | ((...args: any[]) => number);
7
+ status?: any;
8
8
  headers?: Record<string, string>;
9
9
  }
10
10
  export interface RequestParameters {
@@ -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;
@@ -95,7 +95,8 @@ const appFactory = (runtimeCollection) => {
95
95
  const app = express_1.default();
96
96
  app.use(cors_1.default());
97
97
  app.use(express_1.default.json({
98
- strict: false
98
+ strict: false,
99
+ type: ['application/json', 'application/*+json', 'text/json']
99
100
  }));
100
101
  app.get('/', (_req, res) => res.send(sort_keys_1.default(runtimeRequestCollection, { deep: true })));
101
102
  app.post('/', (req, res) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@centeredgesoft/runtime-mock-routes",
3
- "version": "2.5.0",
3
+ "version": "2.8.2",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
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?: number | ((...args: any[])=>number);
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
  }
@@ -110,7 +110,8 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
110
110
 
111
111
  app.use(cors());
112
112
  app.use(express.json({
113
- strict: false
113
+ strict: false,
114
+ type: ['application/json', 'application/*+json', 'text/json']
114
115
  }));
115
116
 
116
117
  app.get('/', (_req, res) => res.send(sortKeys(runtimeRequestCollection, { deep: true })));