-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
48 lines (39 loc) · 1.07 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Import core modules using require
//
// const os = require('os');
// console.log(os.arch());
async function register(router, utils, importModule) {
// Commonly used functions are injected by default
//
//
const {mapJSON, customMapper, redirect} = utils; // eslint-disable-line
// Import custom modules using importModule
//
// get default export manually
const {default: fetch} = await importModule('node-fetch');
// import internal modules
// const log = await importModule('./log.mjs');
// Example
//
//
// 1. disable some api log
router.get('/', mapJSON(null, {canLog: false}));
// 2. change response body
router.get('/status', mapJSON((res, ctx) => {
return {
...res,
inject: 'Hello World!!!',
};
}));
// 3. async mapper
router.get('/users/:id', mapJSON(async (res, ctx) => {
const {id} = ctx.params;
const repos = await fetch(`https://api.github.com/users/${id}/repos`).then((res) => res.json());
return {
...res,
repos,
};
}));
}
// use commonjs export
exports.register = register;