galbe 0.9.0 → 0.10.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/bin/commands/build.ts +12 -2
- package/bin/commands/dev.ts +22 -18
- package/bin/commands/generate/code/openapi.parser.ts +1 -1
- package/bin/commands/generate/spec.ts +4 -2
- package/bin/util.ts +3 -13
- package/bun.lock +904 -0
- package/docs/getting-started.md +72 -37
- package/package.json +2 -1
- package/src/extras/spec/openapi.serializer.ts +8 -4
- package/src/index.ts +2 -3
- package/src/parser.ts +1 -1
- package/src/routes.ts +1 -1
- package/src/server.ts +15 -8
- package/src/types.ts +21 -9
- package/src/util.ts +9 -0
- package/src/validator.ts +1 -0
- package/test/plugins.test.ts +1 -1
- package/test/responses.test.ts +3 -2
- package/test/routeFiles.test.ts +6 -6
- package/tsconfig.json +1 -1
- package/bun.lockb +0 -0
package/bin/commands/build.ts
CHANGED
|
@@ -9,9 +9,16 @@ import { CWD, fmtVal, silentExec } from '../util'
|
|
|
9
9
|
import { Galbe } from '../../src'
|
|
10
10
|
import { defineRoutes, GalbeProxy } from '../../src/routes'
|
|
11
11
|
import { BuildConfig } from 'bun'
|
|
12
|
+
import { existsSync } from 'fs'
|
|
12
13
|
|
|
13
14
|
const createBuildIndex = async (indexPath: string, g: Galbe, buildId: string) => {
|
|
14
15
|
const buildPath = resolve(tmpdir(), buildId)
|
|
16
|
+
const indexDir = dirname(indexPath)
|
|
17
|
+
|
|
18
|
+
let configPath = ''
|
|
19
|
+
if (existsSync(`${indexDir}/galbe.config.ts`)) configPath = `${indexDir}/galbe.config.ts`
|
|
20
|
+
else if (existsSync(`${indexDir}/galbe.config.js`)) configPath = `${indexDir}/galbe.config.js`
|
|
21
|
+
|
|
15
22
|
const routes = new Map<string, { filepath: string, static?: { path: string, root: string } }>()
|
|
16
23
|
let errors: any[] = []
|
|
17
24
|
// Create GalbeProxy here
|
|
@@ -30,6 +37,9 @@ const createBuildIndex = async (indexPath: string, g: Galbe, buildId: string) =>
|
|
|
30
37
|
|
|
31
38
|
let buildIndex =
|
|
32
39
|
`import galbe from '${relative(buildPath, indexPath)}';\n` +
|
|
40
|
+
(configPath ? `import config from '${relative(buildPath, configPath)}';\n` : '') +
|
|
41
|
+
(configPath ? `import {softMerge} from '${relative(buildPath, `${indexDir}/node_modules/galbe/src/util`)}';\n` : '') +
|
|
42
|
+
(configPath ? `let conf = galbe.config;\ngalbe.config = softMerge(config, conf)\n` : '') +
|
|
33
43
|
`${[...routes.values()].map((r, idx) => `import _${idx} from '${relative(buildPath, r.filepath)}'`).join(';\n')}\n` +
|
|
34
44
|
`Bun.env.BUN_ENV = 'production';\n` +
|
|
35
45
|
`Bun.env.GALBE_BUILD = '${buildId}';\n` +
|
|
@@ -92,10 +102,10 @@ export default (cmd: Command) => {
|
|
|
92
102
|
|
|
93
103
|
const buildConfig: BuildConfig = {
|
|
94
104
|
publicPath: `${outPath}/`,
|
|
95
|
-
|
|
105
|
+
sourcemap: 'external',
|
|
106
|
+
...Object.fromEntries(Object.entries(bunfig).filter(([_, v]) => v)),
|
|
96
107
|
entrypoints: [buildIndex],
|
|
97
108
|
outdir: outPath,
|
|
98
|
-
sourcemap: 'external',
|
|
99
109
|
target: 'bun',
|
|
100
110
|
}
|
|
101
111
|
|
package/bin/commands/dev.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { $ } from 'bun'
|
|
2
2
|
import { Command, Option } from 'commander'
|
|
3
|
-
import { resolve } from 'path'
|
|
3
|
+
import { resolve, dirname } from 'path'
|
|
4
4
|
|
|
5
|
-
import { CWD, fmtInterval, fmtVal, instanciateRoutes,
|
|
5
|
+
import { CWD, fmtInterval, fmtVal, instanciateRoutes, watchDir } from '../util'
|
|
6
6
|
import { Galbe } from '../../src'
|
|
7
|
+
import { softMerge } from '../../src/util'
|
|
8
|
+
import { existsSync } from 'fs'
|
|
7
9
|
|
|
8
|
-
const
|
|
10
|
+
const DEFAULT_PORT = 3000
|
|
9
11
|
|
|
10
12
|
export default (cmd: Command) => {
|
|
11
13
|
cmd
|
|
@@ -18,29 +20,29 @@ export default (cmd: Command) => {
|
|
|
18
20
|
console.log(`error: port range must be between ${fmtInterval(1, 65535)}`)
|
|
19
21
|
process.exit(1)
|
|
20
22
|
})
|
|
21
|
-
.default(null, fmtVal(
|
|
23
|
+
.default(null, fmtVal(DEFAULT_PORT))
|
|
22
24
|
)
|
|
23
|
-
.addOption(new Option('-w, --watch
|
|
25
|
+
.addOption(new Option('-w, --watch [dir]', 'watch file changes').default(false, fmtVal(false)))
|
|
24
26
|
.addOption(new Option('-wi, --watchignore <regexp>', 'ignore file changes').default(false, fmtVal(false)))
|
|
25
27
|
.addOption(new Option('-nc, --noclear', "don't clear on file changes").default(false, fmtVal(false)))
|
|
26
|
-
.addOption(
|
|
27
|
-
new Option('-f, --force', 'kills any process running on defined port before strating the server').default(
|
|
28
|
-
false,
|
|
29
|
-
fmtVal(false)
|
|
30
|
-
)
|
|
31
|
-
)
|
|
32
28
|
.action(async (index, props) => {
|
|
33
|
-
const { port, watch, watchignore, noclear
|
|
34
|
-
let watch_dir = typeof watch === 'string' ? watch : CWD
|
|
35
|
-
const clear = !noclear
|
|
29
|
+
const { port, watch, watchignore, noclear } = props
|
|
36
30
|
const indexPath = resolve(CWD, index)
|
|
31
|
+
const indexDir = dirname(indexPath)
|
|
32
|
+
let watch_dir = typeof watch === 'string' ? watch : watch ? indexDir : ''
|
|
33
|
+
const clear = !noclear
|
|
34
|
+
let galbeConfig = {}
|
|
37
35
|
let g: Galbe
|
|
38
36
|
|
|
39
|
-
if (
|
|
37
|
+
if (existsSync(`${indexDir}/galbe.config.ts`)) {
|
|
38
|
+
galbeConfig = (await import(`${indexDir}/galbe.config.ts`)).default
|
|
39
|
+
} else if (existsSync(`${indexDir}/galbe.config.js`)) {
|
|
40
|
+
galbeConfig = (await import(`${indexDir}/galbe.config.js`)).default
|
|
41
|
+
}
|
|
40
42
|
|
|
41
|
-
if (
|
|
43
|
+
if (!Bun.env.BUN_ENV) Bun.env.BUN_ENV = 'development'
|
|
42
44
|
|
|
43
|
-
if (!!
|
|
45
|
+
if (!!watch_dir) {
|
|
44
46
|
await watchDir(
|
|
45
47
|
watch_dir,
|
|
46
48
|
async () => {
|
|
@@ -55,8 +57,10 @@ export default (cmd: Command) => {
|
|
|
55
57
|
)
|
|
56
58
|
}
|
|
57
59
|
|
|
58
|
-
if (!!
|
|
60
|
+
if (!!watch_dir && clear) await $`clear`
|
|
59
61
|
g = (await import(indexPath)).default
|
|
62
|
+
let conf = g.config
|
|
63
|
+
g.config = softMerge(galbeConfig, conf)
|
|
60
64
|
await instanciateRoutes(g)
|
|
61
65
|
await g.listen(port)
|
|
62
66
|
})
|
|
@@ -282,7 +282,7 @@ const parseEndpointDef = (method: string, path: string, def?: OpenAPIV3.Operatio
|
|
|
282
282
|
)
|
|
283
283
|
|
|
284
284
|
let body = ''
|
|
285
|
-
if(!['get', 'options', 'head'].includes(method)){
|
|
285
|
+
if(!['get', 'delete', 'options', 'head'].includes(method)){
|
|
286
286
|
let _rb = def?.requestBody as OpenAPIV3.ReferenceObject
|
|
287
287
|
if (_rb?.$ref) {
|
|
288
288
|
body = unref(` body: %ref:${_rb.$ref}%`, m => {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Command, Option } from 'commander'
|
|
2
2
|
import { resolve, relative, extname } from 'path'
|
|
3
3
|
import { dump as ymlDump, load as ymlLoad } from 'js-yaml'
|
|
4
|
-
import { CWD, fmtList, instanciateRoutes, silentExec
|
|
4
|
+
import { CWD, fmtList, instanciateRoutes, silentExec } from '../../util'
|
|
5
5
|
import { Galbe } from '../../../src'
|
|
6
6
|
import { OpenAPISerializer } from '../../../src/extras'
|
|
7
|
+
import { softMerge } from '../../../src/util'
|
|
8
|
+
import { OpenAPIV3 } from 'openapi-types'
|
|
7
9
|
|
|
8
10
|
const specTargets = ['openapi:3.0:json', 'openapi:3.0:yaml']
|
|
9
11
|
const parsePckgAuthoRgx = /^\s*([^<(]*)(?:<([^>]+)>)?\s*(?:\(([^)]*)\))?\s*$/
|
|
@@ -85,7 +87,7 @@ export default (cmd: Command) => {
|
|
|
85
87
|
version: pckg?.version || '0.1.0'
|
|
86
88
|
}
|
|
87
89
|
}
|
|
88
|
-
openapiSpec = softMerge(openapiSpec, baseSpec)
|
|
90
|
+
openapiSpec = softMerge(openapiSpec, baseSpec) as OpenAPIV3.Document
|
|
89
91
|
Bun.write(resolve(CWD, out), tFormat === 'json' ? JSON.stringify(openapiSpec, null, 2) : ymlDump(openapiSpec))
|
|
90
92
|
}
|
|
91
93
|
|
package/bin/util.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { relative } from 'path'
|
|
2
2
|
import { watch } from 'chokidar'
|
|
3
|
-
import { Galbe, Route } from '../src'
|
|
3
|
+
import { Galbe, type Route } from '../src'
|
|
4
4
|
import { logRoute, walkRoutes } from '../src/util'
|
|
5
|
-
import { GalbeProxy, RouteMeta, defineRoutes } from '../src/routes'
|
|
5
|
+
import { GalbeProxy, type RouteMeta, defineRoutes } from '../src/routes'
|
|
6
6
|
|
|
7
7
|
export { default as pckg } from '../package.json'
|
|
8
8
|
|
|
@@ -15,7 +15,7 @@ export const fmtVal = (v: any) => {
|
|
|
15
15
|
if (typeof v === 'number') return `\x1b[36m${v}\x1b[0m`
|
|
16
16
|
return v
|
|
17
17
|
}
|
|
18
|
-
export const fmtList = (l: any) => `[${l.map(v => fmtVal(v)).join(', ')}]`
|
|
18
|
+
export const fmtList = (l: any) => `[${l.map((v:any) => fmtVal(v)).join(', ')}]`
|
|
19
19
|
export const fmtInterval = (a: any, b: any) => `[${fmtVal(a)}-${fmtVal(b)}]`
|
|
20
20
|
|
|
21
21
|
export const silentExec = async (fn: () => any) => {
|
|
@@ -95,16 +95,6 @@ export const instanciateRoutes = async (g: Galbe) => {
|
|
|
95
95
|
console.log('\x1b[1;30m\x1b[32mdone\x1b[0m\n')
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
export const softMerge = (base, override) => {
|
|
99
|
-
for (const key in override) {
|
|
100
|
-
if (override[key] instanceof Object && !(override[key] instanceof Array)) {
|
|
101
|
-
if (!base[key]) Object.assign(base, { [key]: {} })
|
|
102
|
-
softMerge(base[key], override[key])
|
|
103
|
-
} else Object.assign(base, { [key]: override[key] })
|
|
104
|
-
}
|
|
105
|
-
return base
|
|
106
|
-
}
|
|
107
|
-
|
|
108
98
|
export const killPort = async (port: number) => {
|
|
109
99
|
let getProcCmd: string[], killCmd: (port: string) => string[]
|
|
110
100
|
|