UNPKG

1.66 kBJavaScriptView Raw
1// Copyright 2019 Zaiste & contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14const fg = require('fast-glob');
15const { sep, parse } = require('path');
16
17const build = () => {
18 const handlers = fg.sync(['features/**/Controller/**/*.(js|ts)']);
19
20 return handlers.map(path => {
21 const pattern = /([\.\w]+)\/Controller\/([\w\/]+).(js|ts)/;
22 const [_, resource, operation] = pattern.exec(path);
23
24 const { dir } = parse(path);
25
26 if (operation.includes(sep)) {
27 const segments = operation.split(sep);
28 const action = segments.pop();
29
30 return {
31 resource: [resource, ...segments].join('/'),
32 operation: action,
33 dir
34 };
35 } else {
36 return { resource, operation, dir };
37 }
38 });
39};
40
41const translate = (name, resource) =>
42 ({
43 browse: { method: 'get', route: `/${resource}` },
44 fetch: { method: 'get', route: `/${resource}/:id` },
45 create: { method: 'post', route: `/${resource}` },
46 update: { method: 'put', route: `/${resource}/:id` },
47 destroy: { method: 'delete', route: `/${resource}/:id` }
48 }[name]);
49
50module.exports = {
51 build,
52 translate
53};