1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const fg = require('fast-glob');
|
15 |
|
16 | const build = () => {
|
17 | const handlers = fg.sync(['features/**/controller/*.js']);
|
18 |
|
19 | return handlers.map(path => {
|
20 | const pattern = /([\.\w]+)\/controller\/(\w+)/;
|
21 | const [_, resource, operation] = pattern.exec(path);
|
22 |
|
23 | return { resource, operation, path };
|
24 | });
|
25 | };
|
26 |
|
27 | const translate = (name, resource) =>
|
28 | ({
|
29 | browse: { method: 'get', route: `/${resource}` },
|
30 | fetch: { method: 'get', route: `/${resource}/:id` },
|
31 | create: { method: 'post', route: `/${resource}` },
|
32 | update: { method: 'put', route: `/${resource}/:id` },
|
33 | destroy: { method: 'delete', route: `/${resource}/:id` }
|
34 | }[name]);
|
35 |
|
36 | module.exports = {
|
37 | build,
|
38 | translate
|
39 | };
|