@centeredgesoft/runtime-mock-routes 2.8.0 → 2.9.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/appV2.d.ts +1 -1
- package/build/src/appV2.js +9 -3
- package/package.json +1 -1
- package/src/appV2.test.ts +45 -0
- package/src/appV2.ts +15 -10
package/build/src/appV2.d.ts
CHANGED
|
@@ -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?:
|
|
8
|
+
headers?: any;
|
|
9
9
|
}
|
|
10
10
|
export interface RequestParameters {
|
|
11
11
|
query: any;
|
package/build/src/appV2.js
CHANGED
|
@@ -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) => {
|
|
@@ -156,8 +157,13 @@ const appFactory = (runtimeCollection) => {
|
|
|
156
157
|
res.status(status);
|
|
157
158
|
}
|
|
158
159
|
if (method.headers) {
|
|
159
|
-
|
|
160
|
-
|
|
160
|
+
if (typeof method.headers === 'function') {
|
|
161
|
+
res.set(method.headers(tokenParams));
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const processedHeaders = Object.keys(method.headers).reduce((acc, curr) => (Object.assign(Object.assign({}, acc), { [curr]: handlebars_1.default.compile(method.headers[curr])(tokenParams) })), {});
|
|
165
|
+
res.set(processedHeaders);
|
|
166
|
+
}
|
|
161
167
|
}
|
|
162
168
|
let { body } = method;
|
|
163
169
|
// if body is a function, execute and then process the response
|
package/package.json
CHANGED
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?:
|
|
39
|
+
headers?: any;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
export interface RequestParameters {
|
|
@@ -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 })));
|
|
@@ -204,14 +205,18 @@ export const appFactory = (runtimeCollection?: RuntimeRequestCollection) => {
|
|
|
204
205
|
}
|
|
205
206
|
|
|
206
207
|
if (method.headers) {
|
|
207
|
-
|
|
208
|
-
(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
208
|
+
if (typeof method.headers === 'function') {
|
|
209
|
+
res.set(method.headers(tokenParams))
|
|
210
|
+
} else {
|
|
211
|
+
const processedHeaders = Object.keys(method.headers).reduce(
|
|
212
|
+
(acc, curr) => ({
|
|
213
|
+
...acc,
|
|
214
|
+
[curr]: Handlebars.compile(method.headers[curr])(tokenParams)
|
|
215
|
+
}),
|
|
216
|
+
{} as Record<string, string>
|
|
217
|
+
);
|
|
218
|
+
res.set(processedHeaders);
|
|
219
|
+
}
|
|
215
220
|
}
|
|
216
221
|
|
|
217
222
|
let { body } = method;
|