-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
59 lines (48 loc) · 1.53 KB
/
parse.js
File metadata and controls
59 lines (48 loc) · 1.53 KB
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
49
50
51
52
53
54
55
56
57
58
59
import fs from 'fs'
import ejs from 'ejs'
import { capitalize, pluralize } from './string.js'
const cwd = process.cwd()
export const TEMPLATE = (name) => {
return fs.readFileSync(`${cwd}/assets/${name}-template.txt`, 'utf8')
}
export const parseTemplate = (template, data) => {
return ejs.render(template, data)
}
export const parseFileName = (data) => {
const resource = Object.keys(data)[0]
return `${resource}.js`
}
export const parseEndpoints = (data) => {
const resource = Object.keys(data)[0]
const endpoints = Object.entries(data[resource])
const parsedEndpoints = []
const methodsMap = {
GET: {
ONE: 'get',
MANY: 'query',
},
POST: 'add',
PATCH: 'update',
DELETE: 'delete',
}
for (let endpoint of endpoints) {
const [item, metadata] = endpoint
const [method, path] = item.split(' ')
const isMethodGet = method === 'GET'
const isDataArray = metadata.data.type === 'Array'
let action
if (isMethodGet) {
action = methodsMap[method][isDataArray ? 'MANY' : 'ONE']
action += isDataArray ? pluralize(capitalize(resource)) : capitalize(resource)
} else {
action = methodsMap[method]
action += capitalize(resource)
}
parsedEndpoints.push({
method: isMethodGet && isDataArray ? 'query' : method.toLowerCase(),
action,
path: path.replace(/{id}/g, '${id}'),
})
}
return parsedEndpoints
}