hono-webhooks 0.5.0 → 0.5.1
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 +2 -0
- package/dist/cli.js +5167 -5021
- package/package.json +2 -2
- package/src/cli-commands.ts +181 -167
- package/src/cli.ts +1 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-webhooks",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Type-safe webhook routes for Hono, validated with any Standard Schema library (Zod, Valibot, ArkType)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"@opentelemetry/sdk-trace-base": "^2.12.0",
|
|
89
89
|
"@types/bun": "^1.4.2",
|
|
90
90
|
"@typescript/native-preview": "^7.0.0-dev.20260707.2",
|
|
91
|
-
"hanzio": "^3.
|
|
91
|
+
"hanzio": "^3.3.0",
|
|
92
92
|
"hono": "^4.13.10",
|
|
93
93
|
"husky": "^9.1.7",
|
|
94
94
|
"typescript": "^7.0.2",
|
package/src/cli-commands.ts
CHANGED
|
@@ -2,7 +2,13 @@ import { spawnSync } from 'node:child_process'
|
|
|
2
2
|
import { existsSync } from 'node:fs'
|
|
3
3
|
import { resolve } from 'node:path'
|
|
4
4
|
import { pathToFileURL } from 'node:url'
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
CliError,
|
|
7
|
+
CliUsageError,
|
|
8
|
+
type CommandContext,
|
|
9
|
+
defineCli,
|
|
10
|
+
defineCommand
|
|
11
|
+
} from 'hanzio/cli'
|
|
6
12
|
import { z } from 'zod'
|
|
7
13
|
import {
|
|
8
14
|
createWebhooks,
|
|
@@ -78,193 +84,201 @@ const BUN_ONLY_ERRORS = new Set([
|
|
|
78
84
|
/**
|
|
79
85
|
* Under Node, a Bun project's TypeScript (extensionless imports, for one)
|
|
80
86
|
* fails to load. Rerun the same command under Bun when it is installed and
|
|
81
|
-
*
|
|
87
|
+
* exit with its code.
|
|
82
88
|
*/
|
|
83
|
-
function rerunUnderBun(error: unknown
|
|
89
|
+
function rerunUnderBun(error: unknown): never {
|
|
84
90
|
const code = (error as { code?: string }).code
|
|
85
91
|
if (process.versions.bun || !code || !BUN_ONLY_ERRORS.has(code)) throw error
|
|
86
|
-
stderr.write(`Node can't load the module (${code}), trying Bun\n`)
|
|
92
|
+
process.stderr.write(`Node can't load the module (${code}), trying Bun\n`)
|
|
87
93
|
const run = spawnSync('bun', process.argv.slice(1), { stdio: 'inherit' })
|
|
88
94
|
if (run.error) {
|
|
89
95
|
throw new CliError(
|
|
90
96
|
'Bun is not installed either. Run the CLI with a runtime that can load your code, e.g. bunx --bun hono-webhooks ...'
|
|
91
97
|
)
|
|
92
98
|
}
|
|
93
|
-
|
|
99
|
+
process.exit(run.status ?? 1)
|
|
94
100
|
}
|
|
95
101
|
|
|
96
|
-
|
|
97
|
-
// names first, so it can't put the module before them.
|
|
98
|
-
export const cli = defineCli({
|
|
99
|
-
name: 'hono-webhooks',
|
|
100
|
-
commands: defineCommand({
|
|
101
|
-
description: `<module> exports a createWebhooks() app or an array of webhooks, as default
|
|
102
|
-
or named export.
|
|
102
|
+
type Ctx = CommandContext<WebhooksApp>
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
list List the webhooks
|
|
106
|
-
schema <path> Print the JSON Schemas of the webhooks at <path>
|
|
107
|
-
test <method> <path> Send a request through the webhooks app
|
|
104
|
+
const print = (ctx: Ctx, line: string) => ctx.stdout.write(`${line}\n`)
|
|
108
105
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
preload: z.array(z.string()).meta({
|
|
129
|
-
alias: 'r',
|
|
130
|
-
description: 'Import a module first (e.g. to load secrets), repeatable'
|
|
131
|
-
}),
|
|
132
|
-
body: z.string().optional().meta({
|
|
133
|
-
alias: 'b',
|
|
134
|
-
description:
|
|
135
|
-
'test: request body (JSON sets content-type: application/json)'
|
|
136
|
-
}),
|
|
137
|
-
query: z.string().optional().meta({
|
|
138
|
-
alias: 'q',
|
|
139
|
-
description: 'test: query string, e.g. "a=1&b=2"'
|
|
140
|
-
}),
|
|
141
|
-
header: z.array(z.string()).meta({
|
|
142
|
-
alias: 'H',
|
|
143
|
-
description: 'test: request header "name: value", repeatable'
|
|
144
|
-
}),
|
|
145
|
-
sign: z
|
|
146
|
-
.string()
|
|
147
|
-
.optional()
|
|
148
|
-
.meta({
|
|
149
|
-
alias: 's',
|
|
150
|
-
description: `test: sign the body with ${Object.keys(schemes).join(', ')}`
|
|
151
|
-
}),
|
|
152
|
-
secretEnv: z.string().optional().meta({
|
|
153
|
-
alias: 'e',
|
|
154
|
-
description: 'test: environment variable holding the signing secret'
|
|
155
|
-
})
|
|
156
|
-
}),
|
|
157
|
-
async run(input, ctx) {
|
|
158
|
-
const print = (line: string) => ctx.stdout.write(`${line}\n`)
|
|
159
|
-
if (!input.module || !input.command) {
|
|
160
|
-
ctx.exitCode = await cli.run(['--help'], ctx)
|
|
161
|
-
return
|
|
162
|
-
}
|
|
163
|
-
let app: WebhooksApp
|
|
164
|
-
try {
|
|
165
|
-
for (const preload of input.preload) await importModule(preload)
|
|
166
|
-
app = await loadApp(input.module)
|
|
167
|
-
} catch (error) {
|
|
168
|
-
ctx.exitCode = rerunUnderBun(error, ctx.stderr)
|
|
169
|
-
return
|
|
170
|
-
}
|
|
106
|
+
const list = defineCommand({
|
|
107
|
+
description: 'List the webhooks',
|
|
108
|
+
examples: ['hono-webhooks ./src/webhooks.ts list'],
|
|
109
|
+
run(_, ctx: Ctx) {
|
|
110
|
+
for (const { config } of ctx.context.webhooks) {
|
|
111
|
+
const validates = [
|
|
112
|
+
...(['params', 'query', 'headers', 'body'] as const).filter(
|
|
113
|
+
(key) => config[key]
|
|
114
|
+
),
|
|
115
|
+
...(config.verify ? ['signature'] : [])
|
|
116
|
+
].join(', ')
|
|
117
|
+
print(
|
|
118
|
+
ctx,
|
|
119
|
+
`${config.method.padEnd(7)} ${config.path}${config.description ? ` ${config.description}` : ''}`
|
|
120
|
+
)
|
|
121
|
+
if (validates) print(ctx, ` validates ${validates}`)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
})
|
|
171
125
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
126
|
+
const schema = defineCommand({
|
|
127
|
+
description: 'Print the JSON Schemas of the webhooks at <path>',
|
|
128
|
+
examples: ['hono-webhooks ./src/webhooks.ts schema /orders/:id'],
|
|
129
|
+
input: z.object({
|
|
130
|
+
path: z.string().meta({ positional: true, description: 'Webhook path' })
|
|
131
|
+
}),
|
|
132
|
+
run({ path }, ctx: Ctx) {
|
|
133
|
+
const app = ctx.context
|
|
134
|
+
const descriptions = describeWebhooks(app)
|
|
135
|
+
const indexes = app.webhooks.flatMap((w, i) =>
|
|
136
|
+
w.config.path === path ? [i] : []
|
|
137
|
+
)
|
|
138
|
+
if (!indexes.length) throw new CliError(`No webhook at ${path}`)
|
|
139
|
+
for (const i of indexes) {
|
|
140
|
+
const { config } = app.webhooks[i]!
|
|
141
|
+
print(ctx, `${config.method} ${config.path}`)
|
|
142
|
+
for (const part of [
|
|
143
|
+
'params',
|
|
144
|
+
'query',
|
|
145
|
+
'headers',
|
|
146
|
+
'body',
|
|
147
|
+
'response'
|
|
148
|
+
] as const) {
|
|
149
|
+
if (!config[part]) continue
|
|
150
|
+
const schema = descriptions[i]!.jsonSchema[part]
|
|
151
|
+
print(
|
|
152
|
+
ctx,
|
|
153
|
+
` ${part}: ${schema ? JSON.stringify(schema, null, 2).replaceAll('\n', '\n ') : '(no JSON Schema support)'}`
|
|
154
|
+
)
|
|
186
155
|
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
})
|
|
187
159
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
160
|
+
const testCommand = defineCommand({
|
|
161
|
+
description: 'Send a request through the webhooks app',
|
|
162
|
+
examples: [
|
|
163
|
+
`hono-webhooks ./src/webhooks.ts test POST /orders/42 -b '{"total":10}'`,
|
|
164
|
+
`hono-webhooks ./src/webhooks.ts test POST /shopify -b '{"id":1}' \\
|
|
165
|
+
-r ./src/secrets.ts --sign shopify --secret-env SHOPIFY_WEBHOOK_SECRET`
|
|
166
|
+
],
|
|
167
|
+
input: z.object({
|
|
168
|
+
method: z
|
|
169
|
+
.string()
|
|
170
|
+
.meta({ positional: true, description: 'HTTP method, e.g. POST' }),
|
|
171
|
+
path: z.string().meta({ positional: true, description: 'Request path' }),
|
|
172
|
+
body: z.string().optional().meta({
|
|
173
|
+
alias: 'b',
|
|
174
|
+
description: 'Request body (JSON sets content-type: application/json)'
|
|
175
|
+
}),
|
|
176
|
+
query: z.string().optional().meta({
|
|
177
|
+
alias: 'q',
|
|
178
|
+
description: 'Query string, e.g. "a=1&b=2"'
|
|
179
|
+
}),
|
|
180
|
+
header: z.array(z.string()).meta({
|
|
181
|
+
alias: 'H',
|
|
182
|
+
description: 'Request header "name: value", repeatable'
|
|
183
|
+
}),
|
|
184
|
+
sign: z
|
|
185
|
+
.string()
|
|
186
|
+
.optional()
|
|
187
|
+
.meta({
|
|
188
|
+
alias: 's',
|
|
189
|
+
description: `Sign the body: ${Object.keys(schemes).join(', ')}`
|
|
190
|
+
}),
|
|
191
|
+
secretEnv: z.string().optional().meta({
|
|
192
|
+
alias: 'e',
|
|
193
|
+
description: 'Environment variable holding the signing secret'
|
|
194
|
+
})
|
|
195
|
+
}),
|
|
196
|
+
async run(input, ctx: Ctx) {
|
|
197
|
+
const { method, path } = input
|
|
198
|
+
const headers = new Headers()
|
|
199
|
+
for (const header of input.header) {
|
|
200
|
+
const at = header.indexOf(':')
|
|
201
|
+
if (at < 1) {
|
|
202
|
+
throw new CliError(
|
|
203
|
+
`Header must look like "name: value", got "${header}"`
|
|
193
204
|
)
|
|
194
|
-
if (!indexes.length) throw new CliError(`No webhook at ${path}`)
|
|
195
|
-
for (const i of indexes) {
|
|
196
|
-
const { config } = app.webhooks[i]!
|
|
197
|
-
print(`${config.method} ${config.path}`)
|
|
198
|
-
for (const part of [
|
|
199
|
-
'params',
|
|
200
|
-
'query',
|
|
201
|
-
'headers',
|
|
202
|
-
'body',
|
|
203
|
-
'response'
|
|
204
|
-
] as const) {
|
|
205
|
-
if (!config[part]) continue
|
|
206
|
-
const schema = descriptions[i]!.jsonSchema[part]
|
|
207
|
-
print(
|
|
208
|
-
` ${part}: ${schema ? JSON.stringify(schema, null, 2).replaceAll('\n', '\n ') : '(no JSON Schema support)'}`
|
|
209
|
-
)
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
return
|
|
213
205
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
`Header must look like "name: value", got "${header}"`
|
|
223
|
-
)
|
|
224
|
-
}
|
|
225
|
-
headers.set(header.slice(0, at).trim(), header.slice(at + 1).trim())
|
|
206
|
+
headers.set(header.slice(0, at).trim(), header.slice(at + 1).trim())
|
|
207
|
+
}
|
|
208
|
+
if (input.body && !headers.has('content-type')) {
|
|
209
|
+
try {
|
|
210
|
+
JSON.parse(input.body)
|
|
211
|
+
headers.set('content-type', 'application/json')
|
|
212
|
+
} catch {
|
|
213
|
+
headers.set('content-type', 'text/plain')
|
|
226
214
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
215
|
+
}
|
|
216
|
+
if (input.sign) {
|
|
217
|
+
const scheme = schemes[input.sign]
|
|
218
|
+
if (!scheme) {
|
|
219
|
+
throw new CliError(
|
|
220
|
+
`Unknown scheme "${input.sign}", use one of: ${Object.keys(schemes).join(', ')}`
|
|
221
|
+
)
|
|
234
222
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
if (!name) throw new CliError('--sign needs --secret-env <name>')
|
|
244
|
-
const secret = ctx.env[name]
|
|
245
|
-
if (secret === undefined) throw new CliError(`${name} is not set`)
|
|
246
|
-
for (const [key, value] of Object.entries(
|
|
247
|
-
await scheme.sign(input.body ?? '', secret)
|
|
248
|
-
)) {
|
|
249
|
-
headers.set(key, value)
|
|
250
|
-
}
|
|
223
|
+
const name = input.secretEnv
|
|
224
|
+
if (!name) throw new CliError('--sign needs --secret-env <name>')
|
|
225
|
+
const secret = ctx.env[name]
|
|
226
|
+
if (secret === undefined) throw new CliError(`${name} is not set`)
|
|
227
|
+
for (const [key, value] of Object.entries(
|
|
228
|
+
await scheme.sign(input.body ?? '', secret)
|
|
229
|
+
)) {
|
|
230
|
+
headers.set(key, value)
|
|
251
231
|
}
|
|
232
|
+
}
|
|
252
233
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
234
|
+
const url = `${path.startsWith('/') ? path : `/${path}`}${input.query ? `?${input.query}` : ''}`
|
|
235
|
+
const start = performance.now()
|
|
236
|
+
const res = await ctx.context.request(url, {
|
|
237
|
+
method: method.toUpperCase(),
|
|
238
|
+
headers,
|
|
239
|
+
body: input.body
|
|
240
|
+
})
|
|
241
|
+
const text = await res.text()
|
|
242
|
+
let body = text
|
|
243
|
+
try {
|
|
244
|
+
body = JSON.stringify(JSON.parse(text), null, 2)
|
|
245
|
+
} catch {}
|
|
246
|
+
print(ctx, `${res.status} (${(performance.now() - start).toFixed(1)}ms)`)
|
|
247
|
+
if (body) print(ctx, body)
|
|
248
|
+
if (!res.ok) ctx.exitCode = 1
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
export const cli = defineCli({
|
|
253
|
+
name: 'hono-webhooks',
|
|
254
|
+
description: `<module> exports a createWebhooks() app or an array of webhooks, as default
|
|
255
|
+
or named export.`,
|
|
256
|
+
globals: z.object({
|
|
257
|
+
module: z
|
|
258
|
+
.string()
|
|
259
|
+
.meta({ positional: true, description: 'Module with the webhooks' }),
|
|
260
|
+
preload: z.array(z.string()).meta({
|
|
261
|
+
alias: 'r',
|
|
262
|
+
description: 'Import a module first (e.g. to load secrets), repeatable'
|
|
263
|
+
})
|
|
264
|
+
}),
|
|
265
|
+
async context({ module, preload }) {
|
|
266
|
+
try {
|
|
267
|
+
for (const specifier of preload) await importModule(specifier)
|
|
268
|
+
return await loadApp(module)
|
|
269
|
+
} catch (error) {
|
|
270
|
+
return rerunUnderBun(error)
|
|
268
271
|
}
|
|
269
|
-
}
|
|
272
|
+
},
|
|
273
|
+
commands: { list, schema, test: testCommand },
|
|
274
|
+
onError(error, io) {
|
|
275
|
+
// No module or command: print help and succeed, as `--help` does.
|
|
276
|
+
if (error instanceof CliUsageError && error.kind === 'missing-command') {
|
|
277
|
+
io.stdout.write(error.help)
|
|
278
|
+
return 0
|
|
279
|
+
}
|
|
280
|
+
// Bad arguments exit 1 like every other failure.
|
|
281
|
+
const code = io.print()
|
|
282
|
+
return code === 2 ? 1 : code
|
|
283
|
+
}
|
|
270
284
|
})
|
package/src/cli.ts
CHANGED