@anthonylzq/simba.js 9.0.1 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +59 -30
- package/lib/index.js +42 -5
- package/lib/src/functions/api/database.js +40 -218
- package/lib/src/functions/api/express.js +100 -604
- package/lib/src/functions/api/fastify.js +93 -645
- package/lib/src/functions/api/hono.js +157 -0
- package/lib/src/functions/api/index.js +37 -59
- package/lib/src/functions/api/schemas.js +20 -84
- package/lib/src/functions/api/services.js +28 -210
- package/lib/src/functions/api/utils.js +10 -432
- package/lib/src/functions/biome.js +3 -2
- package/lib/src/functions/changelog.js +5 -5
- package/lib/src/functions/docker.js +7 -39
- package/lib/src/functions/ghat.js +12 -94
- package/lib/src/functions/gitignore.js +3 -134
- package/lib/src/functions/index.js +1 -2
- package/lib/src/functions/license.js +10 -2
- package/lib/src/functions/packageJson.js +13 -37
- package/lib/src/functions/readme.js +6 -5
- package/lib/src/functions/tests.js +17 -320
- package/lib/src/functions/tsconfig.js +13 -114
- package/lib/src/index.js +45 -21
- package/lib/src/utils/entity.js +115 -0
- package/lib/src/utils/index.js +2 -0
- package/lib/src/utils/mkdirs.js +9 -0
- package/lib/src/utils/renderTemplate.js +22 -0
- package/lib/src/utils/titleCase.js +5 -10
- package/lib/src/utils/writeFile.js +3 -10
- package/lib/templates/api/database/connection.ts.ejs +40 -0
- package/lib/templates/api/database/db-index.ts.ejs +2 -0
- package/lib/templates/api/database/index.ts.ejs +1 -0
- package/lib/templates/api/database/queries-entity.ts.ejs +98 -0
- package/lib/templates/api/database/queries-index.ts.ejs +1 -0
- package/lib/templates/api/database/schema.prisma.ejs +25 -0
- package/lib/templates/api/express/network/index.ts.ejs +2 -0
- package/lib/templates/api/express/network/models/entity.ts.ejs +21 -0
- package/lib/templates/api/express/network/models/index.ts.ejs +1 -0
- package/lib/templates/api/express/network/resolvers/entity.ts.ejs +41 -0
- package/lib/templates/api/express/network/resolvers/index.ts.ejs +13 -0
- package/lib/templates/api/express/network/response.ts.ejs +17 -0
- package/lib/templates/api/express/network/router.ts.ejs +39 -0
- package/lib/templates/api/express/network/routes/docs.ts.ejs +43 -0
- package/lib/templates/api/express/network/routes/entity.ts.ejs +165 -0
- package/lib/templates/api/express/network/routes/home.ts.ejs +16 -0
- package/lib/templates/api/express/network/routes/index.ts.ejs +5 -0
- package/lib/templates/api/express/network/server.ts.ejs +139 -0
- package/lib/templates/api/express/network/utils/index.ts.ejs +45 -0
- package/lib/templates/api/express/types/graphQL/context.d.ts.ejs +3 -0
- package/lib/templates/api/express/types/index.d.ts.ejs +4 -0
- package/lib/templates/api/fastify/network/index.ts.ejs +2 -0
- package/lib/templates/api/fastify/network/models/entity.ts.ejs +21 -0
- package/lib/templates/api/fastify/network/models/index.ts.ejs +1 -0
- package/lib/templates/api/fastify/network/resolvers/entity.ts.ejs +41 -0
- package/lib/templates/api/fastify/network/resolvers/index.ts.ejs +13 -0
- package/lib/templates/api/fastify/network/response.ts.ejs +17 -0
- package/lib/templates/api/fastify/network/router.ts.ejs +36 -0
- package/lib/templates/api/fastify/network/routes/docs.ts.ejs +41 -0
- package/lib/templates/api/fastify/network/routes/entity.ts.ejs +116 -0
- package/lib/templates/api/fastify/network/routes/home.ts.ejs +15 -0
- package/lib/templates/api/fastify/network/routes/index.ts.ejs +5 -0
- package/lib/templates/api/fastify/network/server.ts.ejs +129 -0
- package/lib/templates/api/fastify/types/graphQL/context.d.ts.ejs +3 -0
- package/lib/templates/api/fastify/types/index.d.ts.ejs +4 -0
- package/lib/templates/api/hono/network/index.ts.ejs +2 -0
- package/lib/templates/api/hono/network/models/entity.ts.ejs +21 -0
- package/lib/templates/api/hono/network/models/index.ts.ejs +1 -0
- package/lib/templates/api/hono/network/resolvers/entity.ts.ejs +41 -0
- package/lib/templates/api/hono/network/resolvers/index.ts.ejs +13 -0
- package/lib/templates/api/hono/network/response.ts.ejs +18 -0
- package/lib/templates/api/hono/network/router.ts.ejs +45 -0
- package/lib/templates/api/hono/network/routes/docs.ts.ejs +39 -0
- package/lib/templates/api/hono/network/routes/entity.ts.ejs +104 -0
- package/lib/templates/api/hono/network/routes/home.ts.ejs +15 -0
- package/lib/templates/api/hono/network/routes/index.ts.ejs +5 -0
- package/lib/templates/api/hono/network/server.ts.ejs +160 -0
- package/lib/templates/api/hono/network/utils/index.ts.ejs +23 -0
- package/lib/templates/api/hono/types/graphQL/context.d.ts.ejs +3 -0
- package/lib/templates/api/hono/types/index.d.ts.ejs +4 -0
- package/lib/templates/api/schemas/entity.ts.ejs +43 -0
- package/lib/templates/api/schemas/id.ts.ejs +11 -0
- package/lib/templates/api/schemas/index.ts.ejs +2 -0
- package/lib/templates/api/services/BaseHttp.ts.ejs +73 -0
- package/lib/templates/api/services/entity.ts.ejs +75 -0
- package/lib/templates/api/services/index.ts.ejs +1 -0
- package/lib/templates/api/services/utils/index.ts.ejs +1 -0
- package/lib/templates/api/services/utils/messages/entity.ts.ejs +11 -0
- package/lib/templates/api/services/utils/messages/index.ts.ejs +6 -0
- package/lib/templates/api/utils/Logger.ts.ejs +41 -0
- package/lib/templates/api/utils/index.ts.ejs +1 -0
- package/lib/templates/config/.dockerignore.ejs +20 -0
- package/lib/templates/config/.env.ejs +3 -0
- package/lib/templates/config/.gitignore.ejs +129 -0
- package/lib/templates/config/CHANGELOG.md.ejs +1 -0
- package/lib/templates/config/Dockerfile.ejs +13 -0
- package/lib/templates/config/README.md.ejs +3 -0
- package/lib/templates/config/ghat/lint.yml.ejs +46 -0
- package/lib/templates/config/ghat/test.yml.ejs +29 -0
- package/lib/templates/config/index.ts.ejs +3 -0
- package/lib/templates/config/package.json.ejs +30 -0
- package/lib/templates/config/test/index.test.ts.ejs +260 -0
- package/lib/templates/config/tsconfig.base.json.ejs +43 -0
- package/lib/templates/config/tsconfig.json.ejs +16 -0
- package/lib/templates/config/vitest.config.ts.ejs +19 -0
- package/package.json +45 -17
- package/lib/src/functions/api/types.js +0 -108
- package/lib/src/functions/eslint.js +0 -125
|
@@ -1,37 +1,39 @@
|
|
|
1
|
-
const
|
|
2
|
-
const { promisify } = require('node:util')
|
|
3
|
-
const exec = promisify(require('node:child_process').exec)
|
|
1
|
+
const mkdirs = require('../../utils/mkdirs')
|
|
4
2
|
|
|
5
3
|
const db = require('./database')
|
|
6
4
|
const schemas = require('./schemas')
|
|
7
5
|
const services = require('./services')
|
|
8
6
|
const writeFile = require('../../utils/writeFile')
|
|
7
|
+
const { renderTemplate } = require('../../utils/renderTemplate')
|
|
9
8
|
const utils = require('./utils')
|
|
10
9
|
|
|
10
|
+
const t = (templatePath, data = {}) =>
|
|
11
|
+
renderTemplate(`api/express/${templatePath}`, data)
|
|
12
|
+
|
|
11
13
|
/**
|
|
12
14
|
* @param {Object} args
|
|
13
15
|
* @param {String} args.projectName
|
|
14
16
|
* @param {Boolean} args.dbIsSQL
|
|
17
|
+
* @param {Boolean} args.graphQL
|
|
15
18
|
*/
|
|
16
|
-
const types = async ({ projectName, dbIsSQL }) => {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
+
const types = async ({ projectName, dbIsSQL, graphQL }) => {
|
|
20
|
+
const dirs = [`${projectName}/src/@types`]
|
|
21
|
+
if (!dbIsSQL) dirs.push(`${projectName}/src/@types/models`)
|
|
22
|
+
if (graphQL) dirs.push(`${projectName}/src/@types/graphQL`)
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
await exec(createFoldersCommand.replaceAll('/', '\\'))
|
|
22
|
-
else await exec(createFoldersCommand)
|
|
24
|
+
await mkdirs(...dirs)
|
|
23
25
|
|
|
24
|
-
const
|
|
25
|
-
index
|
|
26
|
-
|
|
27
|
-
declare global {}
|
|
26
|
+
const processes = [
|
|
27
|
+
writeFile(`${projectName}/src/@types/index.d.ts`, t('types/index.d.ts.ejs'))
|
|
28
|
+
]
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
if (graphQL)
|
|
31
|
+
processes.push(
|
|
32
|
+
writeFile(
|
|
33
|
+
`${projectName}/src/@types/graphQL/context.d.ts`,
|
|
34
|
+
t('types/graphQL/context.d.ts.ejs')
|
|
35
|
+
)
|
|
36
|
+
)
|
|
35
37
|
|
|
36
38
|
await Promise.all(processes)
|
|
37
39
|
}
|
|
@@ -39,589 +41,90 @@ export {}
|
|
|
39
41
|
/**
|
|
40
42
|
* @param {Object} args
|
|
41
43
|
* @param {String} args.projectName
|
|
42
|
-
* @param {String} args.
|
|
43
|
-
* @param {
|
|
44
|
+
* @param {String} args.email
|
|
45
|
+
* @param {Boolean} args.graphQL
|
|
46
|
+
* @param {Boolean} args.dbIsSQL
|
|
47
|
+
* @param {import('../../utils/entity').EntityContext} args.entityContext
|
|
44
48
|
*/
|
|
45
|
-
const network = async ({
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}`
|
|
52
|
-
|
|
53
|
-
if (platform() === 'win32')
|
|
54
|
-
await exec(createFoldersCommand.replaceAll('/', '\\'))
|
|
55
|
-
else await exec(createFoldersCommand)
|
|
56
|
-
|
|
57
|
-
const network = {
|
|
58
|
-
index: {
|
|
59
|
-
content: `export * from './routes'\nexport * from './server'\n`,
|
|
60
|
-
file: `${projectName}/src/network/index.ts`
|
|
61
|
-
},
|
|
62
|
-
response: {
|
|
63
|
-
content: `import { type Response } from 'express'
|
|
64
|
-
|
|
65
|
-
const response = ({
|
|
66
|
-
error,
|
|
67
|
-
message,
|
|
68
|
-
res,
|
|
69
|
-
status
|
|
70
|
-
}: {
|
|
71
|
-
error: boolean
|
|
72
|
-
message: unknown
|
|
73
|
-
res: Response
|
|
74
|
-
status: number
|
|
49
|
+
const network = async ({
|
|
50
|
+
projectName,
|
|
51
|
+
email,
|
|
52
|
+
graphQL,
|
|
53
|
+
dbIsSQL,
|
|
54
|
+
entityContext
|
|
75
55
|
}) => {
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
import httpErrors from 'http-errors'
|
|
86
|
-
|
|
87
|
-
import { response } from './response'
|
|
88
|
-
${
|
|
89
|
-
graphQL
|
|
90
|
-
? "import { Home } from './routes'"
|
|
91
|
-
: "import { Home, User } from './routes'"
|
|
92
|
-
}
|
|
93
|
-
import { docs } from 'utils'
|
|
94
|
-
|
|
95
|
-
${graphQL ? 'const routers: Router[] = []' : 'const routers = [User]'}
|
|
96
|
-
const applyRoutes = (app: Application): void => {
|
|
97
|
-
app.use('/', Home)
|
|
98
|
-
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(docs))
|
|
99
|
-
routers.forEach((router: Router): Application => app.use('/api', router))
|
|
100
|
-
|
|
101
|
-
// Handling 404 error
|
|
102
|
-
app.use((_req, _res, next) => {
|
|
103
|
-
next(new httpErrors.NotFound('This route does not exists'))
|
|
104
|
-
})
|
|
105
|
-
app.use(
|
|
106
|
-
(
|
|
107
|
-
error: httpErrors.HttpError,
|
|
108
|
-
_req: Request,
|
|
109
|
-
res: Response,
|
|
110
|
-
next: NextFunction
|
|
111
|
-
) => {
|
|
112
|
-
response({
|
|
113
|
-
error: true,
|
|
114
|
-
message: error.message,
|
|
115
|
-
res,
|
|
116
|
-
status: error.status
|
|
117
|
-
})
|
|
118
|
-
next()
|
|
119
|
-
}
|
|
120
|
-
)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export { applyRoutes }
|
|
124
|
-
`,
|
|
125
|
-
file: `${projectName}/src/network/router.ts`
|
|
126
|
-
},
|
|
127
|
-
server: {
|
|
128
|
-
content: graphQL
|
|
129
|
-
? `import type { Server as HttpServer } from 'node:http'
|
|
130
|
-
import express from 'express'
|
|
131
|
-
import cors from 'cors'
|
|
132
|
-
import debug from 'debug'
|
|
133
|
-
import { ApolloServer } from '@apollo/server'
|
|
134
|
-
import { expressMiddleware } from '@as-integrations/express5'
|
|
135
|
-
|
|
136
|
-
import { dbConnection } from 'database'
|
|
137
|
-
import { applyRoutes } from './router'
|
|
138
|
-
import { buildSchemas } from './resolvers'
|
|
139
|
-
import { type Log } from 'utils'
|
|
140
|
-
|
|
141
|
-
const d = debug('App:Network:Server')
|
|
142
|
-
const PORT = (process.env.PORT as string) || 1996
|
|
143
|
-
|
|
144
|
-
class Server implements Log {
|
|
145
|
-
#app: express.Application
|
|
146
|
-
#server: HttpServer | undefined
|
|
147
|
-
#connection: Awaited<ReturnType<typeof dbConnection>>
|
|
148
|
-
#apolloServer: ApolloServer | undefined
|
|
149
|
-
|
|
150
|
-
constructor() {
|
|
151
|
-
this.#app = express()
|
|
152
|
-
this.#connection = dbConnection(d)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async #config() {
|
|
156
|
-
await this.#apolloConfig()
|
|
157
|
-
this.#app.use(cors())
|
|
158
|
-
this.#app.use(express.json())
|
|
159
|
-
this.#app.use(express.urlencoded({ extended: false }))
|
|
160
|
-
this.#app.use(
|
|
161
|
-
(
|
|
162
|
-
_req: express.Request,
|
|
163
|
-
res: express.Response,
|
|
164
|
-
next: express.NextFunction
|
|
165
|
-
) => {
|
|
166
|
-
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE')
|
|
167
|
-
res.header('Access-Control-Allow-Origin', '*')
|
|
168
|
-
res.header(
|
|
169
|
-
'Access-Control-Allow-Headers',
|
|
170
|
-
'Authorization, Content-Type'
|
|
171
|
-
)
|
|
172
|
-
res.header('x-powered-by', 'Simba.js')
|
|
173
|
-
next()
|
|
174
|
-
}
|
|
175
|
-
)
|
|
176
|
-
applyRoutes(this.#app)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
async #apolloConfig() {
|
|
180
|
-
this.#apolloServer = new ApolloServer({
|
|
181
|
-
schema: await buildSchemas()
|
|
182
|
-
})
|
|
183
|
-
await this.#apolloServer.start()
|
|
184
|
-
this.#app.use(
|
|
185
|
-
'/graphql',
|
|
186
|
-
cors(),
|
|
187
|
-
express.json(),
|
|
188
|
-
expressMiddleware(this.#apolloServer)
|
|
189
|
-
)
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
async start() {
|
|
193
|
-
try {
|
|
194
|
-
await this.#config()
|
|
195
|
-
await this.#connection?.connect()
|
|
196
|
-
this.#server = this.#app.listen(PORT, () => {
|
|
197
|
-
d(\`HTTP server listening on port \${PORT}.\`)
|
|
198
|
-
})
|
|
199
|
-
} catch (e) {
|
|
200
|
-
this.log({
|
|
201
|
-
method: this.start.name,
|
|
202
|
-
value: 'error',
|
|
203
|
-
content: e
|
|
204
|
-
})
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
async stop() {
|
|
209
|
-
try {
|
|
210
|
-
await this.#connection?.disconnect()
|
|
211
|
-
this.#server?.close()
|
|
212
|
-
await this.#apolloServer?.stop()
|
|
213
|
-
} catch (e) {
|
|
214
|
-
this.log({
|
|
215
|
-
method: this.stop.name,
|
|
216
|
-
value: 'error',
|
|
217
|
-
content: e
|
|
218
|
-
})
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
log({
|
|
223
|
-
method,
|
|
224
|
-
value,
|
|
225
|
-
content
|
|
226
|
-
}: {
|
|
227
|
-
method: string
|
|
228
|
-
value: string
|
|
229
|
-
content: unknown
|
|
230
|
-
}) {
|
|
231
|
-
d(
|
|
232
|
-
\`Server invoked -> \${
|
|
233
|
-
this.constructor.name
|
|
234
|
-
} ~ \${method} ~ value: \${value} ~ content: \${content}\`
|
|
235
|
-
)
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const server = new Server()
|
|
240
|
-
|
|
241
|
-
export { server as Server }\n`
|
|
242
|
-
: `import type { Server as HttpServer } from 'node:http'
|
|
243
|
-
import express from 'express'
|
|
244
|
-
import cors from 'cors'
|
|
245
|
-
import debug from 'debug'
|
|
246
|
-
|
|
247
|
-
import { dbConnection } from 'database'
|
|
248
|
-
import { applyRoutes } from './router'
|
|
249
|
-
import { type Log } from 'utils'
|
|
250
|
-
|
|
251
|
-
const d = debug('App:Network:Server')
|
|
252
|
-
const PORT = (process.env.PORT as string) || 1996
|
|
253
|
-
|
|
254
|
-
class Server implements Log {
|
|
255
|
-
#app: express.Application
|
|
256
|
-
#server: HttpServer | undefined
|
|
257
|
-
#connection: Awaited<ReturnType<typeof dbConnection>>
|
|
258
|
-
|
|
259
|
-
constructor() {
|
|
260
|
-
this.#app = express()
|
|
261
|
-
this.#connection = dbConnection(d)
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
#config() {
|
|
265
|
-
this.#app.use(cors())
|
|
266
|
-
this.#app.use(express.json())
|
|
267
|
-
this.#app.use(express.urlencoded({ extended: false }))
|
|
268
|
-
this.#app.use(
|
|
269
|
-
(
|
|
270
|
-
_req: express.Request,
|
|
271
|
-
res: express.Response,
|
|
272
|
-
next: express.NextFunction
|
|
273
|
-
) => {
|
|
274
|
-
res.header('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE')
|
|
275
|
-
res.header('Access-Control-Allow-Origin', '*')
|
|
276
|
-
res.header(
|
|
277
|
-
'Access-Control-Allow-Headers',
|
|
278
|
-
'Authorization, Content-Type'
|
|
279
|
-
)
|
|
280
|
-
res.header('x-powered-by', 'Simba.js')
|
|
281
|
-
next()
|
|
282
|
-
}
|
|
283
|
-
)
|
|
284
|
-
applyRoutes(this.#app)
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
async start(): Promise<void> {
|
|
288
|
-
try {
|
|
289
|
-
this.#config()
|
|
290
|
-
await this.#connection?.connect()
|
|
291
|
-
this.#server = this.#app.listen(PORT, () => {
|
|
292
|
-
d(\`HTTP server listening on port \${PORT}.\`)
|
|
293
|
-
})
|
|
294
|
-
} catch (e) {
|
|
295
|
-
this.log({
|
|
296
|
-
method: this.start.name,
|
|
297
|
-
value: 'error',
|
|
298
|
-
content: e
|
|
299
|
-
})
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
async stop(): Promise<void> {
|
|
304
|
-
try {
|
|
305
|
-
await this.#connection?.disconnect()
|
|
306
|
-
this.#server?.close()
|
|
307
|
-
} catch (e) {
|
|
308
|
-
this.log({
|
|
309
|
-
method: this.stop.name,
|
|
310
|
-
value: 'error',
|
|
311
|
-
content: e
|
|
312
|
-
})
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
log({
|
|
317
|
-
method,
|
|
318
|
-
value,
|
|
319
|
-
content
|
|
320
|
-
}: {
|
|
321
|
-
method: string
|
|
322
|
-
value: string
|
|
323
|
-
content: unknown
|
|
324
|
-
}) {
|
|
325
|
-
d(
|
|
326
|
-
\`Server invoked -> \${
|
|
327
|
-
this.constructor.name
|
|
328
|
-
} ~ \${method} ~ value: \${value} ~ content: \${JSON.stringify(content)}\`
|
|
329
|
-
)
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
const server = new Server()
|
|
334
|
-
|
|
335
|
-
export { server as Server }\n`,
|
|
336
|
-
file: `${projectName}/src/network/server.ts`
|
|
337
|
-
},
|
|
338
|
-
routes: {
|
|
339
|
-
home: {
|
|
340
|
-
content: `import { Response, Request, Router } from 'express'
|
|
341
|
-
|
|
342
|
-
import { response } from 'network/response'
|
|
343
|
-
|
|
344
|
-
const Home = Router()
|
|
345
|
-
|
|
346
|
-
Home.route('').get((_req: Request, res: Response) => {
|
|
347
|
-
response({
|
|
348
|
-
error: false,
|
|
349
|
-
message: 'Welcome to your Express Backend!',
|
|
350
|
-
res,
|
|
351
|
-
status: 200
|
|
352
|
-
})
|
|
353
|
-
})
|
|
354
|
-
|
|
355
|
-
export { Home }
|
|
356
|
-
`,
|
|
357
|
-
file: `${projectName}/src/network/routes/home.ts`
|
|
358
|
-
},
|
|
359
|
-
index: {
|
|
360
|
-
content: `export * from './home'
|
|
361
|
-
${graphQL ? '' : "export * from './user'\n"}`,
|
|
362
|
-
file: `${projectName}/src/network/routes/index.ts`
|
|
363
|
-
},
|
|
364
|
-
...(!graphQL && {
|
|
365
|
-
user: {
|
|
366
|
-
content: `import { type NextFunction, type Request, type Response, Router } from 'express'
|
|
367
|
-
|
|
368
|
-
import { response } from 'network/response'
|
|
369
|
-
import { UserService } from 'services'
|
|
370
|
-
import { idSchema, storeUserDto, UserDTO } from 'schemas'
|
|
371
|
-
import { validatorCompiler } from './utils'
|
|
372
|
-
|
|
373
|
-
const User = Router()
|
|
374
|
-
|
|
375
|
-
User.route('/users').post(
|
|
376
|
-
validatorCompiler(storeUserDto, 'body'),
|
|
377
|
-
async (
|
|
378
|
-
req: Request<
|
|
379
|
-
{
|
|
380
|
-
[key: string]: string
|
|
381
|
-
},
|
|
382
|
-
Record<string, unknown>,
|
|
383
|
-
{ args: UserDTO }
|
|
384
|
-
>,
|
|
385
|
-
res: Response,
|
|
386
|
-
next: NextFunction
|
|
387
|
-
): Promise<void> => {
|
|
388
|
-
try {
|
|
389
|
-
const {
|
|
390
|
-
body: {
|
|
391
|
-
args: { lastName, name }
|
|
392
|
-
}
|
|
393
|
-
} = req
|
|
394
|
-
const us = new UserService()
|
|
395
|
-
const user = await us.store({ lastName, name })
|
|
396
|
-
|
|
397
|
-
response({ error: false, message: user, res, status: 201 })
|
|
398
|
-
} catch (error) {
|
|
399
|
-
next(error)
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
)
|
|
403
|
-
|
|
404
|
-
User.route('/user/:id')
|
|
405
|
-
.get(
|
|
406
|
-
validatorCompiler(idSchema, 'params'),
|
|
407
|
-
async (
|
|
408
|
-
req: Request<{ id: string }, Record<string, unknown>>,
|
|
409
|
-
res: Response,
|
|
410
|
-
next: NextFunction
|
|
411
|
-
): Promise<void> => {
|
|
412
|
-
try {
|
|
413
|
-
const {
|
|
414
|
-
params: { id }
|
|
415
|
-
} = req
|
|
416
|
-
const us = new UserService()
|
|
417
|
-
const user = await us.getById(${dbIsSQL ? 'parseInt(id)' : 'id'})
|
|
418
|
-
|
|
419
|
-
response({ error: false, message: user, res, status: 200 })
|
|
420
|
-
} catch (error) {
|
|
421
|
-
next(error)
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
)
|
|
425
|
-
.patch(
|
|
426
|
-
validatorCompiler(idSchema, 'params'),
|
|
427
|
-
validatorCompiler(storeUserDto, 'body'),
|
|
428
|
-
async (
|
|
429
|
-
req: Request<{ id: string }, Record<string, unknown>, { args: UserDTO }>,
|
|
430
|
-
res: Response,
|
|
431
|
-
next: NextFunction
|
|
432
|
-
): Promise<void> => {
|
|
433
|
-
try {
|
|
434
|
-
const {
|
|
435
|
-
body: {
|
|
436
|
-
args: { name, lastName }
|
|
437
|
-
},
|
|
438
|
-
params: { id }
|
|
439
|
-
} = req
|
|
440
|
-
const us = new UserService()
|
|
441
|
-
const user = await us.update(${
|
|
442
|
-
dbIsSQL ? 'parseInt(id)' : 'id'
|
|
443
|
-
}, { name, lastName })
|
|
444
|
-
|
|
445
|
-
response({ error: false, message: user, res, status: 200 })
|
|
446
|
-
} catch (error) {
|
|
447
|
-
next(error)
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
)
|
|
451
|
-
.delete(
|
|
452
|
-
validatorCompiler(idSchema, 'params'),
|
|
453
|
-
async (
|
|
454
|
-
req: Request<{ id: string }>,
|
|
455
|
-
res: Response,
|
|
456
|
-
next: NextFunction
|
|
457
|
-
): Promise<void> => {
|
|
458
|
-
try {
|
|
459
|
-
const {
|
|
460
|
-
params: { id }
|
|
461
|
-
} = req
|
|
462
|
-
const us = new UserService()
|
|
463
|
-
const result = await us.deleteById(${dbIsSQL ? 'parseInt(id)' : 'id'})
|
|
464
|
-
|
|
465
|
-
response({ error: false, message: result, res, status: 200 })
|
|
466
|
-
} catch (error) {
|
|
467
|
-
next(error)
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
)
|
|
471
|
-
|
|
472
|
-
export { User }\n`,
|
|
473
|
-
file: `${projectName}/src/network/routes/user.ts`
|
|
474
|
-
}
|
|
475
|
-
}),
|
|
476
|
-
utils: {
|
|
477
|
-
index: {
|
|
478
|
-
content: `import { type NextFunction, type Request, type Response } from 'express'
|
|
479
|
-
import httpErrors from 'http-errors'
|
|
480
|
-
import { ZodType } from 'zod'
|
|
481
|
-
|
|
482
|
-
type Middleware = (req: Request, res: Response, next: NextFunction) => void
|
|
483
|
-
|
|
484
|
-
const validatorCompiler = (
|
|
485
|
-
schema: ZodType,
|
|
486
|
-
value: 'body' | 'params'
|
|
487
|
-
): Middleware => {
|
|
488
|
-
return (req: Request, _res: Response, next: NextFunction) => {
|
|
489
|
-
const result = schema.safeParse(req[value])
|
|
490
|
-
|
|
491
|
-
if (result.success) return next()
|
|
492
|
-
|
|
493
|
-
return next(
|
|
494
|
-
new httpErrors.UnprocessableEntity(JSON.stringify(result.error))
|
|
56
|
+
const dirs = [
|
|
57
|
+
`${projectName}/src/network`,
|
|
58
|
+
`${projectName}/src/network/routes`,
|
|
59
|
+
`${projectName}/src/network/utils`
|
|
60
|
+
]
|
|
61
|
+
if (graphQL) {
|
|
62
|
+
dirs.push(
|
|
63
|
+
`${projectName}/src/network/models`,
|
|
64
|
+
`${projectName}/src/network/resolvers`
|
|
495
65
|
)
|
|
496
66
|
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
export { validatorCompiler }\n`,
|
|
500
|
-
file: `${projectName}/src/network/routes/utils/index.ts`
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
},
|
|
504
|
-
...(graphQL && {
|
|
505
|
-
models: {
|
|
506
|
-
index: {
|
|
507
|
-
content: "export * from './User'\n",
|
|
508
|
-
file: `${projectName}/src/network/models/index.ts`
|
|
509
|
-
},
|
|
510
|
-
User: {
|
|
511
|
-
content: `import 'reflect-metadata'
|
|
512
|
-
import { Field, ${dbIsSQL ? 'Int' : 'ID'}, ObjectType } from 'type-graphql'
|
|
513
|
-
|
|
514
|
-
@ObjectType()
|
|
515
|
-
class User {
|
|
516
|
-
@Field(() => ${dbIsSQL ? 'Int' : 'ID'})
|
|
517
|
-
id!: number
|
|
518
67
|
|
|
519
|
-
|
|
520
|
-
lastName!: string
|
|
68
|
+
await mkdirs(...dirs)
|
|
521
69
|
|
|
522
|
-
|
|
523
|
-
name!: string
|
|
524
|
-
|
|
525
|
-
@Field({ nullable: true })
|
|
526
|
-
createdAt?: string
|
|
527
|
-
|
|
528
|
-
@Field({ nullable: true })
|
|
529
|
-
updatedAt?: string
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
export { User }\n`,
|
|
533
|
-
file: `${projectName}/src/network/models/User.ts`
|
|
534
|
-
}
|
|
535
|
-
},
|
|
536
|
-
resolvers: {
|
|
537
|
-
index: {
|
|
538
|
-
content: `import { buildSchema } from 'type-graphql'
|
|
539
|
-
import { UserResolver } from './User'
|
|
540
|
-
|
|
541
|
-
const buildSchemas = async () => {
|
|
542
|
-
const schema = await buildSchema({
|
|
543
|
-
resolvers: [UserResolver],
|
|
544
|
-
validate: { forbidUnknownValues: false }
|
|
545
|
-
})
|
|
546
|
-
|
|
547
|
-
return schema
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
export { buildSchemas }\n`,
|
|
551
|
-
file: `${projectName}/src/network/resolvers/index.ts`
|
|
552
|
-
},
|
|
553
|
-
User: {
|
|
554
|
-
content: `import 'reflect-metadata'
|
|
555
|
-
import { Arg, Field, InputType, Mutation, Query, Resolver } from 'type-graphql'
|
|
556
|
-
|
|
557
|
-
import { User } from 'network/models'
|
|
558
|
-
import { UserService } from 'services'
|
|
559
|
-
|
|
560
|
-
@InputType()
|
|
561
|
-
class UserInput {
|
|
562
|
-
@Field()
|
|
563
|
-
name!: string
|
|
564
|
-
|
|
565
|
-
@Field()
|
|
566
|
-
lastName!: string
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
@Resolver(User)
|
|
570
|
-
class UserResolver {
|
|
571
|
-
readonly #userService = new UserService()
|
|
572
|
-
|
|
573
|
-
@Query(() => User)
|
|
574
|
-
async getById(@Arg('id') id: ${dbIsSQL ? 'number' : 'string'}) {
|
|
575
|
-
return this.#userService.getById(id)
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
@Mutation(() => User)
|
|
579
|
-
async store(@Arg('user') user: UserInput) {
|
|
580
|
-
return this.#userService.store(user)
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
@Mutation(() => User)
|
|
584
|
-
async update(@Arg('id') id: ${
|
|
585
|
-
dbIsSQL ? 'number' : 'string'
|
|
586
|
-
}, @Arg('user') user: UserInput) {
|
|
587
|
-
return this.#userService.update(id, user)
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
@Mutation(() => String)
|
|
591
|
-
async deleteById(@Arg('id') id: ${dbIsSQL ? 'number' : 'string'}) {
|
|
592
|
-
return this.#userService.deleteById(id)
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
export { UserResolver }\n`,
|
|
597
|
-
file: `${projectName}/src/network/resolvers/User.ts`
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
})
|
|
601
|
-
}
|
|
70
|
+
const data = { graphQL, dbIsSQL, ...entityContext }
|
|
602
71
|
const processes = [
|
|
603
|
-
writeFile(network
|
|
604
|
-
writeFile(
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
writeFile(network.routes.index.file, network.routes.index.content),
|
|
72
|
+
writeFile(`${projectName}/src/network/index.ts`, t('network/index.ts.ejs')),
|
|
73
|
+
writeFile(
|
|
74
|
+
`${projectName}/src/network/response.ts`,
|
|
75
|
+
t('network/response.ts.ejs')
|
|
76
|
+
),
|
|
609
77
|
writeFile(
|
|
610
|
-
network.
|
|
611
|
-
network.
|
|
78
|
+
`${projectName}/src/network/router.ts`,
|
|
79
|
+
t('network/router.ts.ejs', data)
|
|
80
|
+
),
|
|
81
|
+
writeFile(
|
|
82
|
+
`${projectName}/src/network/server.ts`,
|
|
83
|
+
t('network/server.ts.ejs', data)
|
|
84
|
+
),
|
|
85
|
+
writeFile(
|
|
86
|
+
`${projectName}/src/network/routes/home.ts`,
|
|
87
|
+
t('network/routes/home.ts.ejs')
|
|
88
|
+
),
|
|
89
|
+
writeFile(
|
|
90
|
+
`${projectName}/src/network/routes/docs.ts`,
|
|
91
|
+
t('network/routes/docs.ts.ejs', { email, graphQL, ...entityContext })
|
|
92
|
+
),
|
|
93
|
+
writeFile(
|
|
94
|
+
`${projectName}/src/network/routes/index.ts`,
|
|
95
|
+
t('network/routes/index.ts.ejs', data)
|
|
96
|
+
),
|
|
97
|
+
writeFile(
|
|
98
|
+
`${projectName}/src/network/utils/index.ts`,
|
|
99
|
+
t('network/utils/index.ts.ejs')
|
|
612
100
|
)
|
|
613
101
|
]
|
|
614
102
|
|
|
615
103
|
if (graphQL)
|
|
616
|
-
processes.
|
|
617
|
-
writeFile(
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
104
|
+
processes.push(
|
|
105
|
+
writeFile(
|
|
106
|
+
`${projectName}/src/network/models/index.ts`,
|
|
107
|
+
t('network/models/index.ts.ejs', entityContext)
|
|
108
|
+
),
|
|
109
|
+
writeFile(
|
|
110
|
+
`${projectName}/src/network/models/${entityContext.Entity}.ts`,
|
|
111
|
+
t('network/models/entity.ts.ejs', data)
|
|
112
|
+
),
|
|
113
|
+
writeFile(
|
|
114
|
+
`${projectName}/src/network/resolvers/index.ts`,
|
|
115
|
+
t('network/resolvers/index.ts.ejs', entityContext)
|
|
116
|
+
),
|
|
117
|
+
writeFile(
|
|
118
|
+
`${projectName}/src/network/resolvers/${entityContext.Entity}.ts`,
|
|
119
|
+
t('network/resolvers/entity.ts.ejs', data)
|
|
120
|
+
)
|
|
121
|
+
)
|
|
622
122
|
else
|
|
623
123
|
processes.push(
|
|
624
|
-
writeFile(
|
|
124
|
+
writeFile(
|
|
125
|
+
`${projectName}/src/network/routes/${entityContext.entity}.ts`,
|
|
126
|
+
t('network/routes/entity.ts.ejs', data)
|
|
127
|
+
)
|
|
625
128
|
)
|
|
626
129
|
|
|
627
130
|
await Promise.all(processes)
|
|
@@ -631,32 +134,25 @@ export { UserResolver }\n`,
|
|
|
631
134
|
* @param {Object} args
|
|
632
135
|
* @param {String} args.projectName
|
|
633
136
|
* @param {String} args.email
|
|
634
|
-
* @param {String} args.projectVersion
|
|
635
137
|
* @param {Boolean} args.graphQL
|
|
636
138
|
* @param {import('../../../../').Config['database']} args.database
|
|
139
|
+
* @param {import('../../utils/entity').EntityContext} args.entityContext
|
|
637
140
|
*/
|
|
638
141
|
const main = async ({
|
|
639
142
|
projectName,
|
|
640
143
|
email,
|
|
641
|
-
projectVersion,
|
|
642
144
|
graphQL,
|
|
643
|
-
database
|
|
145
|
+
database,
|
|
146
|
+
entityContext
|
|
644
147
|
}) => {
|
|
645
148
|
const dbIsSQL = database !== 'mongo'
|
|
646
149
|
|
|
647
|
-
await utils({
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
dbIsSQL
|
|
654
|
-
})
|
|
655
|
-
await types({ projectName, dbIsSQL })
|
|
656
|
-
await network({ projectName, graphQL, dbIsSQL })
|
|
657
|
-
await schemas({ projectName, dbIsSQL })
|
|
658
|
-
await services({ projectName, dbIsSQL })
|
|
659
|
-
await db({ projectName, database })
|
|
150
|
+
await utils({ projectName })
|
|
151
|
+
await types({ projectName, dbIsSQL, graphQL })
|
|
152
|
+
await network({ projectName, email, graphQL, dbIsSQL, entityContext })
|
|
153
|
+
await schemas({ projectName, dbIsSQL, entityContext })
|
|
154
|
+
await services({ projectName, dbIsSQL, entityContext })
|
|
155
|
+
await db({ projectName, database, entityContext })
|
|
660
156
|
}
|
|
661
157
|
|
|
662
158
|
module.exports = main
|