-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·133 lines (116 loc) · 2.92 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
const camelcase = require('camelcase');
const chalk = require('chalk');
const inquirer = require('inquirer');
const xregexp = require('xregexp');
require('loud-rejection/register');
const questions = [{
name: 'eventType',
type: 'list',
message: 'Which event type?',
choices: [
'HTTP API',
'REST API',
],
default: 'HTTP API',
validate: (x) => x.length !== 0,
}, {
name: 'route',
type: 'input',
message: 'What is the new endpoint\'s path?',
validate: (x) => !!x,
}, {
name: 'methods',
type: 'checkbox',
message: 'Which methods are supported?',
choices: [
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
],
validate: (x) => x.length !== 0,
}, {
name: 'isPrivate',
type: 'confirm',
message: 'Is the endpoint private (requires API key)?',
default: true,
when: (answers) => answers.eventType === 'REST API',
}];
const getParams = (route) => xregexp.match(route, /\{\w+\}/g, 'all');
const getName = ({ method, route, params }) => {
let name = camelcase(`${method}-${route}`.replace(/\//g, '-'));
params.forEach((param) => {
name = name.replace(`-${param}`, '');
});
return name;
};
const logIntroduction = () => {
console.log();
console.log('Use {named} variables in the path to declare path parameters.');
console.log();
};
const logEndpoints = ({
eventType,
isPrivate,
methods,
route,
}) => {
console.log();
console.log(chalk.yellow('Copy and paste this into your serverless.yml file:'));
console.log();
console.log('functions:');
// ex. /users/{id}/settings/{setting}
const params = getParams(route);
methods.forEach((method) => {
const name = getName({ method, route, params });
console.log(` ${name}:
handler: handler.${name}
events:
- ${eventType === 'HTTP API' ? 'httpApi' : 'http'}:
method: ${method}
path: ${route}${eventType === 'REST API' ? `
private: ${isPrivate}` : ''}
${params.length === 0 ? '' : `
request:
parameters:
paths:`}`);
params.forEach((param) => {
console.log(` ${param.replace('{', '').replace('}', '')}: true`);
});
});
console.log();
};
const logHandlers = ({ methods, route }) => {
console.log();
console.log(chalk.yellow('Copy and paste this into your handler.js file:'));
console.log();
const params = getParams(route);
methods.forEach((method) => {
const name = getName({ method, route, params });
console.log(`module.exports.${name} = async () => {
return {
statusCode: ${method === 'POST' ? 201 : 200},${method === 'GET' ? `
body: JSON.stringify({}),` : ''}
};
};`);
console.log();
});
};
(async () => {
logIntroduction();
const {
eventType,
isPrivate,
methods,
route,
} = await inquirer.prompt(questions);
logEndpoints({
eventType,
isPrivate,
methods,
route,
});
logHandlers({ methods, route });
})();