ts-procedures 5.3.0 → 5.4.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 +90 -0
- package/agent_config/claude-code/agents/ts-procedures-architect.md +15 -0
- package/agent_config/claude-code/skills/guide/anti-patterns.md +106 -0
- package/agent_config/claude-code/skills/guide/api-reference.md +150 -4
- package/agent_config/claude-code/skills/guide/patterns.md +155 -0
- package/agent_config/claude-code/skills/review/checklist.md +22 -0
- package/agent_config/claude-code/skills/scaffold/SKILL.md +3 -1
- package/agent_config/claude-code/skills/scaffold/templates/hono-api.md +169 -0
- package/agent_config/copilot/copilot-instructions.md +35 -0
- package/agent_config/cursor/cursorrules +35 -0
- package/build/implementations/http/hono-api/index.d.ts +102 -0
- package/build/implementations/http/hono-api/index.js +339 -0
- package/build/implementations/http/hono-api/index.js.map +1 -0
- package/build/implementations/http/hono-api/index.test.d.ts +1 -0
- package/build/implementations/http/hono-api/index.test.js +983 -0
- package/build/implementations/http/hono-api/index.test.js.map +1 -0
- package/build/implementations/http/hono-api/types.d.ts +13 -0
- package/build/implementations/http/hono-api/types.js +2 -0
- package/build/implementations/http/hono-api/types.js.map +1 -0
- package/build/implementations/types.d.ts +44 -0
- package/build/index.d.ts +28 -6
- package/build/index.js +28 -0
- package/build/index.js.map +1 -1
- package/build/schema/compute-schema.d.ts +5 -0
- package/build/schema/compute-schema.js +8 -1
- package/build/schema/compute-schema.js.map +1 -1
- package/build/schema/parser.d.ts +6 -5
- package/build/schema/parser.js +54 -0
- package/build/schema/parser.js.map +1 -1
- package/package.json +8 -4
- package/src/errors.test.ts +0 -163
- package/src/errors.ts +0 -107
- package/src/exports.ts +0 -7
- package/src/implementations/http/README.md +0 -217
- package/src/implementations/http/express-rpc/README.md +0 -281
- package/src/implementations/http/express-rpc/index.test.ts +0 -957
- package/src/implementations/http/express-rpc/index.ts +0 -265
- package/src/implementations/http/express-rpc/types.ts +0 -16
- package/src/implementations/http/hono-rpc/README.md +0 -358
- package/src/implementations/http/hono-rpc/index.test.ts +0 -1075
- package/src/implementations/http/hono-rpc/index.ts +0 -237
- package/src/implementations/http/hono-rpc/types.ts +0 -16
- package/src/implementations/http/hono-stream/README.md +0 -526
- package/src/implementations/http/hono-stream/index.test.ts +0 -1676
- package/src/implementations/http/hono-stream/index.ts +0 -435
- package/src/implementations/http/hono-stream/types.ts +0 -29
- package/src/implementations/types.ts +0 -75
- package/src/index.test.ts +0 -1194
- package/src/index.ts +0 -435
- package/src/schema/compute-schema.test.ts +0 -128
- package/src/schema/compute-schema.ts +0 -67
- package/src/schema/extract-json-schema.test.ts +0 -25
- package/src/schema/extract-json-schema.ts +0 -15
- package/src/schema/parser.test.ts +0 -182
- package/src/schema/parser.ts +0 -148
- package/src/schema/resolve-schema-lib.test.ts +0 -19
- package/src/schema/resolve-schema-lib.ts +0 -29
- package/src/schema/types.ts +0 -20
- package/src/stack-utils.test.ts +0 -94
- package/src/stack-utils.ts +0 -129
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
import { Hono, Context } from 'hono'
|
|
2
|
-
import { kebabCase } from 'es-toolkit/string'
|
|
3
|
-
import { TProcedureRegistration } from '../../../index.js'
|
|
4
|
-
import {
|
|
5
|
-
ExtractConfig,
|
|
6
|
-
ExtractContext,
|
|
7
|
-
ProceduresFactory,
|
|
8
|
-
RPCConfig,
|
|
9
|
-
RPCHttpRouteDoc,
|
|
10
|
-
} from '../../types.js'
|
|
11
|
-
import { castArray } from 'es-toolkit/compat'
|
|
12
|
-
import { HonoFactoryItem } from './types.js'
|
|
13
|
-
|
|
14
|
-
export type { RPCConfig, RPCHttpRouteDoc }
|
|
15
|
-
|
|
16
|
-
export type HonoRPCAppBuilderConfig = {
|
|
17
|
-
/**
|
|
18
|
-
* An existing Hono application instance to use.
|
|
19
|
-
* If not provided, a new instance will be created.
|
|
20
|
-
*/
|
|
21
|
-
app?: Hono
|
|
22
|
-
/** Optional path prefix for all RPC routes. */
|
|
23
|
-
pathPrefix?: string
|
|
24
|
-
onRequestStart?: (c: Context) => void
|
|
25
|
-
onRequestEnd?: (c: Context) => void
|
|
26
|
-
onSuccess?: (procedure: TProcedureRegistration, c: Context) => void
|
|
27
|
-
/**
|
|
28
|
-
* Error handler called when a procedure throws an error.
|
|
29
|
-
* @param procedure
|
|
30
|
-
* @param c
|
|
31
|
-
* @param error
|
|
32
|
-
*/
|
|
33
|
-
onError?: (
|
|
34
|
-
procedure: TProcedureRegistration,
|
|
35
|
-
c: Context,
|
|
36
|
-
error: Error
|
|
37
|
-
) => Response | Promise<Response>
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Builder class for creating a Hono application with RPC routes.
|
|
42
|
-
*
|
|
43
|
-
* Usage:
|
|
44
|
-
* const PublicRPC = Procedures<PublicRPCContext, RPCConfig>()
|
|
45
|
-
* const ProtectedRPC = Procedures<ProtectedRPCContext, RPCConfig>()
|
|
46
|
-
*
|
|
47
|
-
* const rpcApp = new HonoRPCAppBuilder()
|
|
48
|
-
* .register(PublicRPC, (c): Promise<PublicRPCContext> => { /* context resolution logic * / })
|
|
49
|
-
* .register(ProtectedRPC, (c): Promise<ProtectedRPCContext> => { /* context resolution logic * / })
|
|
50
|
-
* .build();
|
|
51
|
-
*
|
|
52
|
-
* const app = rpcApp.app; // Hono application
|
|
53
|
-
* const docs = rpcApp.docs; // RPC route documentation
|
|
54
|
-
*/
|
|
55
|
-
export class HonoRPCAppBuilder {
|
|
56
|
-
/**
|
|
57
|
-
* Constructor for HonoRPCAppBuilder.
|
|
58
|
-
*
|
|
59
|
-
* @param config
|
|
60
|
-
*/
|
|
61
|
-
constructor(readonly config?: HonoRPCAppBuilderConfig) {
|
|
62
|
-
if (config?.app) {
|
|
63
|
-
this._app = config.app
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (config?.onRequestStart) {
|
|
67
|
-
this._app.use('*', async (c, next) => {
|
|
68
|
-
config.onRequestStart!(c)
|
|
69
|
-
await next()
|
|
70
|
-
})
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (config?.onRequestEnd) {
|
|
74
|
-
this._app.use('*', async (c, next) => {
|
|
75
|
-
await next()
|
|
76
|
-
config.onRequestEnd!(c)
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Generates the RPC route path based on the RPC configuration.
|
|
83
|
-
* The RPCConfig name can be a string or an array of strings to form nested paths.
|
|
84
|
-
*
|
|
85
|
-
* Example
|
|
86
|
-
* name: ['string', 'string-string', 'string']
|
|
87
|
-
* path: /string/string-string/string/version
|
|
88
|
-
* @param config
|
|
89
|
-
*/
|
|
90
|
-
static makeRPCHttpRoutePath({
|
|
91
|
-
name,
|
|
92
|
-
config,
|
|
93
|
-
prefix,
|
|
94
|
-
}: {
|
|
95
|
-
name: string
|
|
96
|
-
prefix?: string
|
|
97
|
-
config: RPCConfig
|
|
98
|
-
}) {
|
|
99
|
-
const normalizedPrefix = prefix ? (prefix.startsWith('/') ? prefix : `/${prefix}`) : ''
|
|
100
|
-
|
|
101
|
-
return `${normalizedPrefix}/${castArray(config.scope).map(kebabCase).join('/')}/${kebabCase(name)}/${String(config.version).trim()}`
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Instance method wrapper for makeRPCHttpRoutePath that uses the builder's pathPrefix.
|
|
106
|
-
* @param config - The RPC configuration
|
|
107
|
-
*/
|
|
108
|
-
makeRPCHttpRoutePath(name: string, config: RPCConfig): string {
|
|
109
|
-
return HonoRPCAppBuilder.makeRPCHttpRoutePath({
|
|
110
|
-
name,
|
|
111
|
-
config,
|
|
112
|
-
prefix: this.config?.pathPrefix,
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private factories: HonoFactoryItem<any>[] = []
|
|
117
|
-
|
|
118
|
-
private _app: Hono = new Hono()
|
|
119
|
-
private _docs: (RPCHttpRouteDoc & object)[] = []
|
|
120
|
-
|
|
121
|
-
get app(): Hono {
|
|
122
|
-
return this._app
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
get docs(): RPCHttpRouteDoc[] {
|
|
126
|
-
return this._docs
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Registers a procedure factory with its context.
|
|
131
|
-
* @param factory - The procedure factory created by Procedures<Context, RPCConfig>()
|
|
132
|
-
* @param factoryContext - The context for procedure handlers. Can be a direct value,
|
|
133
|
-
* a sync function (c) => Context, or an async function (c) => Promise<Context>
|
|
134
|
-
* @param extendProcedureDoc - A custom function to extend the generated RPC route documentation for each procedure.
|
|
135
|
-
*/
|
|
136
|
-
register<TFactory extends ProceduresFactory>(
|
|
137
|
-
factory: TFactory,
|
|
138
|
-
factoryContext:
|
|
139
|
-
| ExtractContext<TFactory>
|
|
140
|
-
| ((c: Context) => ExtractContext<TFactory> | Promise<ExtractContext<TFactory>>),
|
|
141
|
-
extendProcedureDoc?: (params: {
|
|
142
|
-
/* RPC App builder base http route doc */
|
|
143
|
-
base: RPCHttpRouteDoc
|
|
144
|
-
/* Procedure registration */
|
|
145
|
-
procedure: TProcedureRegistration<any, ExtractConfig<TFactory>>
|
|
146
|
-
}) => Record<string, any>
|
|
147
|
-
): this {
|
|
148
|
-
this.factories.push({ factory, factoryContext, extendProcedureDoc } as HonoFactoryItem<any>)
|
|
149
|
-
return this
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Builds and returns the Hono application with registered RPC routes.
|
|
154
|
-
* @return Hono
|
|
155
|
-
*/
|
|
156
|
-
build(): Hono {
|
|
157
|
-
this.factories.forEach(({ factory, factoryContext, extendProcedureDoc }) => {
|
|
158
|
-
factory.getProcedures().map((procedure: TProcedureRegistration<any, RPCConfig>) => {
|
|
159
|
-
const route = this.buildRpcHttpRouteDoc(procedure, extendProcedureDoc)
|
|
160
|
-
|
|
161
|
-
this._docs.push(route)
|
|
162
|
-
|
|
163
|
-
this._app.post(route.path, async (c) => {
|
|
164
|
-
try {
|
|
165
|
-
const context =
|
|
166
|
-
typeof factoryContext === 'function'
|
|
167
|
-
? await factoryContext(c)
|
|
168
|
-
: (factoryContext as ExtractContext<typeof factory>)
|
|
169
|
-
|
|
170
|
-
// Hono uses c.req.json() for body parsing
|
|
171
|
-
const body = await c.req.json().catch(() => ({}))
|
|
172
|
-
const result = await procedure.handler({ ...context, signal: c.req.raw.signal }, body)
|
|
173
|
-
|
|
174
|
-
if (this.config?.onSuccess) {
|
|
175
|
-
this.config.onSuccess(procedure, c)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// Hono returns Response objects via c.json()
|
|
179
|
-
return c.json(result)
|
|
180
|
-
} catch (error) {
|
|
181
|
-
if (this.config?.onError) {
|
|
182
|
-
return this.config.onError(procedure, c, error as Error)
|
|
183
|
-
}
|
|
184
|
-
// Default error handling
|
|
185
|
-
return c.json({ error: (error as Error).message }, 500)
|
|
186
|
-
}
|
|
187
|
-
})
|
|
188
|
-
})
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
return this._app
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Generates the RPC HTTP route for the given procedure.
|
|
196
|
-
* @param procedure
|
|
197
|
-
*/
|
|
198
|
-
private buildRpcHttpRouteDoc(
|
|
199
|
-
procedure: TProcedureRegistration<any, RPCConfig>,
|
|
200
|
-
extendProcedureDoc: HonoFactoryItem['extendProcedureDoc']
|
|
201
|
-
): RPCHttpRouteDoc {
|
|
202
|
-
const { config } = procedure
|
|
203
|
-
const path = HonoRPCAppBuilder.makeRPCHttpRoutePath({
|
|
204
|
-
name: procedure.name,
|
|
205
|
-
config,
|
|
206
|
-
prefix: this.config?.pathPrefix,
|
|
207
|
-
})
|
|
208
|
-
const method = 'post' as const // RPCs use POST method
|
|
209
|
-
const jsonSchema: { body?: Record<string, unknown>; response?: Record<string, unknown> } = {}
|
|
210
|
-
|
|
211
|
-
if (config.schema?.params) {
|
|
212
|
-
jsonSchema.body = config.schema.params
|
|
213
|
-
}
|
|
214
|
-
if (config.schema?.returnType) {
|
|
215
|
-
jsonSchema.response = config.schema.returnType
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
const base = {
|
|
219
|
-
name: procedure.name,
|
|
220
|
-
version: config.version,
|
|
221
|
-
scope: config.scope,
|
|
222
|
-
path,
|
|
223
|
-
method,
|
|
224
|
-
jsonSchema,
|
|
225
|
-
}
|
|
226
|
-
let extendedDoc: object = {}
|
|
227
|
-
|
|
228
|
-
if (extendProcedureDoc) {
|
|
229
|
-
extendedDoc = extendProcedureDoc({ base, procedure })
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return {
|
|
233
|
-
...extendedDoc,
|
|
234
|
-
...base,
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { ExtractConfig, ExtractContext, RPCConfig, RPCHttpRouteDoc } from '../../types.js'
|
|
2
|
-
import { Procedures, TProcedureRegistration } from '../../../index.js'
|
|
3
|
-
import { Context } from 'hono'
|
|
4
|
-
|
|
5
|
-
export type HonoFactoryItem<TFactory = ReturnType<typeof Procedures<any, RPCConfig>>> = {
|
|
6
|
-
factory: TFactory
|
|
7
|
-
factoryContext:
|
|
8
|
-
| ExtractContext<TFactory>
|
|
9
|
-
| ((c: Context) => ExtractContext<TFactory> | Promise<ExtractContext<TFactory>>)
|
|
10
|
-
extendProcedureDoc?: (params: {
|
|
11
|
-
/* RPC App builder base http route doc */
|
|
12
|
-
base: RPCHttpRouteDoc
|
|
13
|
-
/* Procedure registration */
|
|
14
|
-
procedure: TProcedureRegistration<any, ExtractConfig<TFactory>>
|
|
15
|
-
}) => Record<string, any>
|
|
16
|
-
}
|