@centeredgesoft/runtime-mock-routes 2.8.2 → 2.9.1-alpha.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.
@@ -5,7 +5,7 @@ export declare const isSupportedMethod: (obj: any) => obj is "GET" | "POST" | "P
5
5
  export interface RuntimeRequestMethodBody {
6
6
  body: any;
7
7
  status?: any;
8
- headers?: Record<string, string>;
8
+ headers?: any;
9
9
  }
10
10
  export interface RequestParameters {
11
11
  query: any;
@@ -98,6 +98,19 @@ const appFactory = (runtimeCollection) => {
98
98
  strict: false,
99
99
  type: ['application/json', 'application/*+json', 'text/json']
100
100
  }));
101
+ // log the request
102
+ app.use((req, res, next) => {
103
+ console.log(`Request: ${req.method} ${req.url}`);
104
+ console.log('Headers:', req.headers);
105
+ console.log('Body:', req.body);
106
+ // log the response
107
+ const originalSend = res.send;
108
+ res.send = function (body) {
109
+ console.log('Response:', body);
110
+ return originalSend.apply(this, arguments);
111
+ };
112
+ next();
113
+ });
101
114
  app.get('/', (_req, res) => res.send(sort_keys_1.default(runtimeRequestCollection, { deep: true })));
102
115
  app.post('/', (req, res) => {
103
116
  const { body } = req;
@@ -157,8 +170,13 @@ const appFactory = (runtimeCollection) => {
157
170
  res.status(status);
158
171
  }
159
172
  if (method.headers) {
160
- const processedHeaders = Object.keys(method.headers).reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: handlebars_1.default.compile(method.headers[curr])(tokenParams) })), {});
161
- res.set(processedHeaders);
173
+ if (typeof method.headers === 'function') {
174
+ res.set(method.headers(tokenParams));
175
+ }
176
+ else {
177
+ const processedHeaders = Object.keys(method.headers).reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: handlebars_1.default.compile(method.headers[curr])(tokenParams) })), {});
178
+ res.set(processedHeaders);
179
+ }
162
180
  }
163
181
  let { body } = method;
164
182
  // if body is a function, execute and then process the response
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@centeredgesoft/runtime-mock-routes",
3
- "version": "2.8.2",
3
+ "version": "2.9.1-alpha.2",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
package/src/appV2.test.ts CHANGED
@@ -370,3 +370,48 @@ describe('Status as a function', () => {
370
370
  expect(secondResponse.status).toEqual(400);
371
371
  })
372
372
  })
373
+
374
+ describe('Headers as a function', () => {
375
+ test('is true for headers that are a function', () => {
376
+ const seed: RuntimeRequestMethodBody = {
377
+ body: "test",
378
+ status: 200,
379
+ headers: () => {
380
+ return {'test-header': 5}
381
+ }
382
+ };
383
+
384
+ const valid = isRuntimeRequestMethodBody(seed);
385
+ expect(valid).toBeTruthy();
386
+ })
387
+
388
+ test('Executes header functions', async () => {
389
+ const seed: RuntimeRequestCollection = {
390
+ "/test": {
391
+ path: "/test",
392
+ methods: {
393
+ GET: {
394
+ body: {id: 1},
395
+ status: 200,
396
+ headers: function(rp: RequestParameters) {
397
+ if(rp.query.id == "2") {
398
+ return {'test-header': '2'};
399
+ } else {
400
+ return {'test-header': '3'}
401
+ }
402
+ }
403
+ }
404
+ }
405
+ }
406
+ }
407
+
408
+ const app = appFactory(seed);
409
+ expect(app).toBeDefined();
410
+
411
+ var response = await request(app).get('/test?id=2');
412
+ expect(response.headers['test-header']).toEqual('2');
413
+
414
+ var secondResponse = await request(app).get('/test?id=3');
415
+ expect(secondResponse.headers['test-header']).toEqual('3');
416
+ })
417
+ })
package/src/appV2.ts CHANGED
@@ -36,7 +36,7 @@ export const isSupportedMethod = (obj: any): obj is SupportedMethod => Supported
36
36
  export interface RuntimeRequestMethodBody {
37
37
  body: any;
38
38
  status?: any;
39
- headers?: Record<string, string>;
39
+ headers?: any;
40
40
  }
41
41
 
42
42
  export interface RequestParameters {
@@ -114,6 +114,22 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
114
114
  type: ['application/json', 'application/*+json', 'text/json']
115
115
  }));
116
116
 
117
+ // log the request
118
+ app.use((req, res, next) => {
119
+ console.log(`Request: ${req.method} ${req.url}`);
120
+ console.log('Headers:', req.headers);
121
+ console.log('Body:', req.body);
122
+
123
+ // log the response
124
+ const originalSend = res.send;
125
+ res.send = function (body) {
126
+ console.log('Response:', body);
127
+ return originalSend.apply(this, arguments);
128
+ };
129
+
130
+ next();
131
+ });
132
+
117
133
  app.get('/', (_req, res) => res.send(sortKeys(runtimeRequestCollection, { deep: true })));
118
134
 
119
135
  app.post('/', (req, res) => {
@@ -205,14 +221,18 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
205
221
  }
206
222
 
207
223
  if (method.headers) {
208
- const processedHeaders = Object.keys(method.headers).reduce(
209
- (acc, curr) => ({
210
- ...acc,
211
- [curr]: Handlebars.compile(method.headers[curr])(tokenParams)
212
- }),
213
- {} as Record<string, string>
214
- );
215
- res.set(processedHeaders);
224
+ if (typeof method.headers === 'function') {
225
+ res.set(method.headers(tokenParams))
226
+ } else {
227
+ const processedHeaders = Object.keys(method.headers).reduce(
228
+ (acc, curr) => ({
229
+ ...acc,
230
+ [curr]: Handlebars.compile(method.headers[curr])(tokenParams)
231
+ }),
232
+ {} as Record<string, string>
233
+ );
234
+ res.set(processedHeaders);
235
+ }
216
236
  }
217
237
 
218
238
  let { body } = method;