-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-generator
135 lines (107 loc) · 3.41 KB
/
fetch-generator
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
134
135
#! /usr/bin/node
const { writeFile } = require('fs/promises');
/**
*
* @param {string} arg
* @returns {string}
*/
const capitalize = (arg) => arg.charAt(0).toUpperCase() + arg.slice(1);
const singular = (arg) => arg.slice(0, -1);
const args = process.argv;
const endpointArg = args[2];
// const fileArg = args[3];
const [_, endpoint] = endpointArg.split('=');
if (!endpoint) {
console.log('Endpoint is required!');
process.exit(1);
}
// const [__, file] = fileArg.split('=');
// if (!file) {
// console.log('File path is required!');
// process.exit(1);
// }
const stub = `
const baseUrl = '/api';
type Get${capitalize(endpoint)}Request = {};
type Get${capitalize(endpoint)}Response = {};
export const get${capitalize(endpoint)} = async (request: Get${capitalize(endpoint)}Request) => {
const search = new URLSearchParams(request);
const response = await fetch(\`/${endpoint}\`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
}
});
if (!response.ok) {
throw new Error('Request: get${capitalize(endpoint)} failed to fetch data');
}
return response.json() as Promise<Get${capitalize(endpoint)}Response>;
}
type Get${capitalize(singular(endpoint))}Response = {}
export const get${capitalize(singular(endpoint))} = async (id: number) => {
const response = await fetch(\`${endpoint}/{id}\`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error('Request: get${capitalize(singular(endpoint))} failed to fetch data');
}
return response.json() as Promise<Get${capitalize(singular(endpoint))}Response>;
}
type Create${capitalize(singular(endpoint))}Request = {}
type Create${capitalize(singular(endpoint))}Response = {}
export const create${capitalize(singular(endpoint))} = async (payload: Create${capitalize(
singular(endpoint)
)}Request) => {
const response = await fetch('baseUrl + endpoint', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error('Request: create${capitalize(singular(endpoint))} failed to fetch data');
}
return response.json() as Promise<Create${capitalize(singular(endpoint))}Response>;
}
type Update${capitalize(singular(endpoint))}Request = {}
export const update${capitalize(singular(endpoint))} = async (id: number, payload: Update${capitalize(
singular(endpoint)
)}Request) => {
const response = await fetch(\`baseUrl${endpoint}\`, {
method: 'PUT',
body: JSON.stringify(payload),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error('Request: update${capitalize(singular(endpoint))} failed to fetch data');
}
return response.json();
}
export const delete${capitalize(singular(endpoint))} = async (id: number) => {
const response = await fetch(\`baseUrl${endpoint}\`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})
if (!response.ok) {
throw new Error('Request: delete${capitalize(singular(endpoint))} failed to fetch data');
}
return response.json();
}
`;
console.log(stub);
(async () => {
await writeFile(process.cwd() + '/abc.ts', stub, { encoding: 'utf-8' });
})();