galbe 0.13.0 → 0.14.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 +18 -1
- package/bin/commands/build.ts +10 -4
- package/bin/commands/generate/cli/index.ts +156 -0
- package/bin/commands/generate/cli/targets/cac.ts +535 -0
- package/bin/commands/generate/client.ts +32 -109
- package/bin/commands/generate/code/openapi.parser.ts +428 -133
- package/bin/commands/generate/code/route-merge.ts +248 -0
- package/bin/commands/generate/code.ts +110 -23
- package/bin/commands/generate/index.ts +2 -0
- package/package.json +4 -1
- package/src/cookies.ts +87 -0
- package/src/extras/spec/openapi.serializer.ts +236 -101
- package/src/extras.ts +1 -0
- package/src/index.ts +4 -7
- package/src/parser.ts +104 -63
- package/src/router.ts +34 -20
- package/src/routes.ts +14 -10
- package/src/schema.ts +82 -95
- package/src/server.ts +35 -21
- package/src/types.ts +180 -61
- package/src/util.ts +10 -18
- package/src/validator.ts +62 -32
- package/bin/res/cli.template.js +0 -122
|
@@ -1,26 +1,20 @@
|
|
|
1
|
-
import { $ } from 'bun'
|
|
2
|
-
import { devNull } from 'os'
|
|
3
1
|
import { Script, createContext } from 'vm'
|
|
4
2
|
import { Command, Option } from 'commander'
|
|
5
3
|
import { resolve, extname } from 'path'
|
|
6
|
-
import { rm } from 'fs/promises'
|
|
7
4
|
import { transformSync } from '@swc/core'
|
|
8
|
-
import { CWD, fmtList, instanciateRoutes, silentExec
|
|
9
|
-
import { $T, Galbe,
|
|
5
|
+
import { CWD, fmtList, instanciateRoutes, silentExec } from '../../util'
|
|
6
|
+
import { $T, Galbe, Method, Route, STResponse } from '../../../src'
|
|
10
7
|
import { walkRoutes } from '../../../src/util'
|
|
11
|
-
import { schemaToTypeStr,
|
|
8
|
+
import { schemaToTypeStr, STSchema } from '../../../src/schema'
|
|
12
9
|
|
|
13
|
-
const clientTargets = ['ts', 'js'
|
|
10
|
+
const clientTargets = ['ts', 'js']
|
|
14
11
|
|
|
15
12
|
export default (cmd: Command) => {
|
|
16
13
|
cmd
|
|
17
14
|
.description('generate a \x1b[1;30m\x1b[36mGalbe\x1b[0m client')
|
|
18
15
|
.argument('<index>', 'index file')
|
|
19
16
|
.addOption(
|
|
20
|
-
new Option('-o, --out <file>', 'output file').default(
|
|
21
|
-
null,
|
|
22
|
-
fmtList(['dist/client.ts', 'dist/client.js', 'dist/cli'])
|
|
23
|
-
)
|
|
17
|
+
new Option('-o, --out <file>', 'output file').default(null, fmtList(['dist/client.ts', 'dist/client.js']))
|
|
24
18
|
)
|
|
25
19
|
.addOption(
|
|
26
20
|
new Option('-t, --target <target>', `build target ${fmtList(clientTargets)}`).argParser(v => {
|
|
@@ -32,7 +26,7 @@ export default (cmd: Command) => {
|
|
|
32
26
|
.action(async (index, props) => {
|
|
33
27
|
let { target, out } = props
|
|
34
28
|
if (!target) target = clientTargets.includes(extname(index)?.slice(1)) ? extname(index)?.slice(1) : 'ts'
|
|
35
|
-
if (!out) out = { ts: 'dist/client.ts', js: 'dist/client.js'
|
|
29
|
+
if (!out) out = { ts: 'dist/client.ts', js: 'dist/client.js' }[target]
|
|
36
30
|
let pckg: any = {}
|
|
37
31
|
try {
|
|
38
32
|
pckg = await Bun.file(resolve(CWD, 'package.json')).json()
|
|
@@ -66,7 +60,6 @@ export default (cmd: Command) => {
|
|
|
66
60
|
head: [],
|
|
67
61
|
}
|
|
68
62
|
const types: Record<string, STResponse> = {}
|
|
69
|
-
let commands: GalbeCLICommand[] = []
|
|
70
63
|
const metaRoutes = g.meta?.reduce(
|
|
71
64
|
(routes, c) => ({ ...routes, ...c.routes }),
|
|
72
65
|
{} as Record<string, Record<string, Record<string, any>>>
|
|
@@ -130,109 +123,39 @@ export default (cmd: Command) => {
|
|
|
130
123
|
types[s.id] = schemaToTypeStr(s)
|
|
131
124
|
})
|
|
132
125
|
routes[r.method.toLocaleLowerCase()].push(route)
|
|
133
|
-
if (target === 'cli' && meta?.operationId)
|
|
134
|
-
commands.push({
|
|
135
|
-
name: meta.operationId,
|
|
136
|
-
tags: meta?.tags ? (Array.isArray(meta.tags) ? meta.tags : [meta.tags]) : [],
|
|
137
|
-
description: route.summary || route.description,
|
|
138
|
-
route,
|
|
139
|
-
arguments:
|
|
140
|
-
Object.entries((route?.params || {}) as Record<string, { type: string; description?: string }>)?.map(
|
|
141
|
-
([k, p]) => {
|
|
142
|
-
return {
|
|
143
|
-
name: k,
|
|
144
|
-
type: p.type === 'boolean' ? '' : `<${p.type}>`,
|
|
145
|
-
description: p?.description || '',
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
) || [],
|
|
149
|
-
options:
|
|
150
|
-
Object.entries((r.schema?.query || {}) as Record<string, STSchema>)?.map(([k, o]) => {
|
|
151
|
-
let type = schemaToTypeStr({ ...o, [Optional]: false })
|
|
152
|
-
return {
|
|
153
|
-
name: k,
|
|
154
|
-
short: abbreviateVar(k),
|
|
155
|
-
type: type === 'boolean' ? '' : `<${type}>`,
|
|
156
|
-
description: o?.description || '',
|
|
157
|
-
default: o.default,
|
|
158
|
-
}
|
|
159
|
-
}) || [],
|
|
160
|
-
})
|
|
161
126
|
})
|
|
162
127
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
let
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
types,
|
|
173
|
-
}
|
|
174
|
-
createContext(sandbox)
|
|
175
|
-
let res = script.runInNewContext(sandbox)
|
|
176
|
-
if (typeof res === 'string') res = res.split('\n')
|
|
177
|
-
if (Array.isArray(res)) return res.map((s, i) => (i === 0 ? s : `${idt}${s}`)).join('\n')
|
|
178
|
-
return res ?? ''
|
|
179
|
-
})
|
|
180
|
-
|
|
181
|
-
if (target === 'js') {
|
|
182
|
-
filled = transformSync(filled, {
|
|
183
|
-
jsc: {
|
|
184
|
-
parser: {
|
|
185
|
-
syntax: 'typescript',
|
|
186
|
-
},
|
|
187
|
-
preserveAllComments: true,
|
|
188
|
-
target: 'esnext',
|
|
189
|
-
},
|
|
190
|
-
}).code
|
|
128
|
+
const file = await Bun.file(resolve(import.meta.dir, '..', '..', 'res', 'client.template.ts')).text()
|
|
129
|
+
let filled = file.replaceAll(/\/\*\%([\s\S]*?)\%\*\//g, (_match, p) => {
|
|
130
|
+
let idt = p.match(/^\n*([ \t]*)/, p)?.[1] || ''
|
|
131
|
+
const script = new Script(p)
|
|
132
|
+
const sandbox = {
|
|
133
|
+
console,
|
|
134
|
+
version: pckg?.version || '0.1.0',
|
|
135
|
+
routes,
|
|
136
|
+
types,
|
|
191
137
|
}
|
|
138
|
+
createContext(sandbox)
|
|
139
|
+
let res = script.runInNewContext(sandbox)
|
|
140
|
+
if (typeof res === 'string') res = res.split('\n')
|
|
141
|
+
if (Array.isArray(res)) return res.map((s, i) => (i === 0 ? s : `${idt}${s}`)).join('\n')
|
|
142
|
+
return res ?? ''
|
|
143
|
+
})
|
|
192
144
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
(p, c) => {
|
|
202
|
-
if (c.tags.length)
|
|
203
|
-
for (const t of c.tags) {
|
|
204
|
-
if (!(t in p)) p[t] = []
|
|
205
|
-
p[t].push(c)
|
|
206
|
-
}
|
|
207
|
-
else p[''].push(c)
|
|
208
|
-
return p
|
|
145
|
+
if (target === 'js') {
|
|
146
|
+
filled = transformSync(filled, {
|
|
147
|
+
jsc: {
|
|
148
|
+
parser: {
|
|
149
|
+
syntax: 'typescript',
|
|
150
|
+
},
|
|
151
|
+
preserveAllComments: true,
|
|
152
|
+
target: 'esnext',
|
|
209
153
|
},
|
|
210
|
-
|
|
211
|
-
)
|
|
212
|
-
|
|
213
|
-
let filled = file.replaceAll(/\/\*\%([\s\S]*?)\%\*\//g, (_match, p) => {
|
|
214
|
-
let idt = p.match(/^\n*([ \t]*)/, p)?.[1] || ''
|
|
215
|
-
const script = new Script(p)
|
|
216
|
-
const sandbox = {
|
|
217
|
-
console,
|
|
218
|
-
name: pckg?.name || 'Galbe app CLI',
|
|
219
|
-
description: pckg?.description || '',
|
|
220
|
-
version: pckg?.version || '0.1.0',
|
|
221
|
-
tags,
|
|
222
|
-
}
|
|
223
|
-
createContext(sandbox)
|
|
224
|
-
let res = script.runInNewContext(sandbox)
|
|
225
|
-
if (typeof res === 'string') res = res.split('\n')
|
|
226
|
-
if (Array.isArray(res)) return res.map((s, i) => (i === 0 ? s : `${idt}${s}`)).join('\n')
|
|
227
|
-
return res ?? ''
|
|
228
|
-
})
|
|
229
|
-
const buildId = crypto.randomUUID()
|
|
230
|
-
const buildPath = resolve(CWD, '.galbe', 'client', `${buildId}.js`)
|
|
231
|
-
await Bun.write(buildPath, filled)
|
|
232
|
-
await $`bun build --compile ${buildPath} --outfile ${resolve(CWD, out)} > ${devNull} && printf "\u200B"`
|
|
233
|
-
await rm(resolve(CWD, '.galbe'), { recursive: true })
|
|
154
|
+
}).code
|
|
234
155
|
}
|
|
235
156
|
|
|
157
|
+
await Bun.write(out, filled)
|
|
158
|
+
|
|
236
159
|
Bun.write(Bun.stdout, ' : \x1b[1;30m\x1b[32mdone\x1b[0m\n')
|
|
237
160
|
process.exit(0)
|
|
238
161
|
})
|