ts-procedures 5.4.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/package.json +1 -2
- 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 -260
- 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-api/index.test.ts +0 -1328
- package/src/implementations/http/hono-api/index.ts +0 -461
- package/src/implementations/http/hono-api/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 -127
- package/src/index.test.ts +0 -1194
- package/src/index.ts +0 -512
- package/src/schema/compute-schema.test.ts +0 -128
- package/src/schema/compute-schema.ts +0 -88
- 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 -215
- 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-procedures",
|
|
3
|
-
"version": "5.4.
|
|
3
|
+
"version": "5.4.1",
|
|
4
4
|
"description": "A TypeScript RPC framework that creates type-safe, schema-validated procedure calls with a single function definition. Define your procedures once and get full type inference, runtime validation, and framework integration hooks.",
|
|
5
5
|
"main": "build/exports.js",
|
|
6
6
|
"types": "build/exports.d.ts",
|
|
@@ -46,7 +46,6 @@
|
|
|
46
46
|
"files": [
|
|
47
47
|
"assets",
|
|
48
48
|
"build",
|
|
49
|
-
"src",
|
|
50
49
|
"agent_config"
|
|
51
50
|
],
|
|
52
51
|
"keywords": [
|
package/src/errors.test.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
ProcedureError,
|
|
4
|
-
ProcedureValidationError,
|
|
5
|
-
ProcedureRegistrationError,
|
|
6
|
-
} from './errors.js'
|
|
7
|
-
import { DefinitionInfo } from './stack-utils.js'
|
|
8
|
-
|
|
9
|
-
describe('Error Classes', () => {
|
|
10
|
-
test('ProcedureError has correct properties', () => {
|
|
11
|
-
const err = new ProcedureError('TestProc', 'Something failed', { code: 123 })
|
|
12
|
-
|
|
13
|
-
expect(err.name).toBe('ProcedureError')
|
|
14
|
-
expect(err.procedureName).toBe('TestProc')
|
|
15
|
-
expect(err.message).toBe('Something failed')
|
|
16
|
-
expect(err.meta).toEqual({ code: 123 })
|
|
17
|
-
expect(err instanceof Error).toBe(true)
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test('ProcedureValidationError extends ProcedureError', () => {
|
|
21
|
-
const err = new ProcedureValidationError('TestProc', 'Validation failed', [])
|
|
22
|
-
|
|
23
|
-
expect(err instanceof ProcedureError).toBe(true)
|
|
24
|
-
expect(err instanceof Error).toBe(true)
|
|
25
|
-
expect(err.name).toBe('ProcedureValidationError')
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
test('ProcedureRegistrationError extends ProcedureError', () => {
|
|
29
|
-
const err = new ProcedureRegistrationError('TestProc', 'Registration failed')
|
|
30
|
-
|
|
31
|
-
expect(err instanceof ProcedureError).toBe(true)
|
|
32
|
-
expect(err instanceof Error).toBe(true)
|
|
33
|
-
expect(err.name).toBe('ProcedureRegistrationError')
|
|
34
|
-
expect(err.procedureName).toBe('TestProc')
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test('ProcedureError supports cause property', () => {
|
|
38
|
-
const cause = new Error('Original error')
|
|
39
|
-
const err = new ProcedureError('TestProc', 'Wrapped error')
|
|
40
|
-
err.cause = cause
|
|
41
|
-
|
|
42
|
-
expect(err.cause).toBe(cause)
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
test('All error types have consistent procedureName property', () => {
|
|
46
|
-
const baseErr = new ProcedureError('Proc1', 'message')
|
|
47
|
-
const validationErr = new ProcedureValidationError('Proc2', 'message', [])
|
|
48
|
-
const registrationErr = new ProcedureRegistrationError('Proc3', 'message')
|
|
49
|
-
|
|
50
|
-
expect(baseErr.procedureName).toBe('Proc1')
|
|
51
|
-
expect(validationErr.procedureName).toBe('Proc2')
|
|
52
|
-
expect(registrationErr.procedureName).toBe('Proc3')
|
|
53
|
-
})
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
describe('Error Classes - Definition Info', () => {
|
|
57
|
-
const mockDefinitionInfo: DefinitionInfo = {
|
|
58
|
-
definedAt: {
|
|
59
|
-
file: '/app/procedures/user.ts',
|
|
60
|
-
line: 25,
|
|
61
|
-
column: 3,
|
|
62
|
-
raw: 'at Object.<anonymous> (/app/procedures/user.ts:25:3)',
|
|
63
|
-
},
|
|
64
|
-
definitionStack: 'Error\n at Object.<anonymous> (/app/procedures/user.ts:25:3)',
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
test('ProcedureError includes definedAt when provided', () => {
|
|
68
|
-
const err = new ProcedureError('TestProc', 'Something failed', undefined, mockDefinitionInfo)
|
|
69
|
-
|
|
70
|
-
expect(err.definedAt).toBeDefined()
|
|
71
|
-
expect(err.definedAt?.file).toBe('/app/procedures/user.ts')
|
|
72
|
-
expect(err.definedAt?.line).toBe(25)
|
|
73
|
-
expect(err.definedAt?.column).toBe(3)
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
test('ProcedureError includes definitionStack when provided', () => {
|
|
77
|
-
const err = new ProcedureError('TestProc', 'Something failed', undefined, mockDefinitionInfo)
|
|
78
|
-
|
|
79
|
-
expect(err.definitionStack).toBeDefined()
|
|
80
|
-
expect(err.definitionStack).toContain('/app/procedures/user.ts')
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
test('ProcedureError works without definitionInfo (backward compat)', () => {
|
|
84
|
-
const err = new ProcedureError('TestProc', 'Something failed', { code: 123 })
|
|
85
|
-
|
|
86
|
-
expect(err.definedAt).toBeUndefined()
|
|
87
|
-
expect(err.definitionStack).toBeUndefined()
|
|
88
|
-
expect(err.procedureName).toBe('TestProc')
|
|
89
|
-
expect(err.message).toBe('Something failed')
|
|
90
|
-
expect(err.meta).toEqual({ code: 123 })
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
test('ProcedureValidationError includes definedAt when provided', () => {
|
|
94
|
-
const err = new ProcedureValidationError('TestProc', 'Validation failed', [], mockDefinitionInfo)
|
|
95
|
-
|
|
96
|
-
expect(err.definedAt).toBeDefined()
|
|
97
|
-
expect(err.definedAt?.file).toBe('/app/procedures/user.ts')
|
|
98
|
-
expect(err.definedAt?.line).toBe(25)
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
test('ProcedureValidationError works without definitionInfo (backward compat)', () => {
|
|
102
|
-
const err = new ProcedureValidationError('TestProc', 'Validation failed', [])
|
|
103
|
-
|
|
104
|
-
expect(err.definedAt).toBeUndefined()
|
|
105
|
-
expect(err.definitionStack).toBeUndefined()
|
|
106
|
-
expect(err.name).toBe('ProcedureValidationError')
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
test('ProcedureRegistrationError includes definedAt when provided', () => {
|
|
110
|
-
const err = new ProcedureRegistrationError('TestProc', 'Registration failed', mockDefinitionInfo)
|
|
111
|
-
|
|
112
|
-
expect(err.definedAt).toBeDefined()
|
|
113
|
-
expect(err.definedAt?.file).toBe('/app/procedures/user.ts')
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
test('ProcedureRegistrationError works without definitionInfo (backward compat)', () => {
|
|
117
|
-
const err = new ProcedureRegistrationError('TestProc', 'Registration failed')
|
|
118
|
-
|
|
119
|
-
expect(err.definedAt).toBeUndefined()
|
|
120
|
-
expect(err.definitionStack).toBeUndefined()
|
|
121
|
-
expect(err.name).toBe('ProcedureRegistrationError')
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
test('getDefinitionLocation returns formatted location string', () => {
|
|
125
|
-
const err = new ProcedureError('TestProc', 'Something failed', undefined, mockDefinitionInfo)
|
|
126
|
-
|
|
127
|
-
const location = err.getDefinitionLocation()
|
|
128
|
-
|
|
129
|
-
expect(location).toBe('/app/procedures/user.ts:25:3')
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
test('getDefinitionLocation returns undefined when no definedAt', () => {
|
|
133
|
-
const err = new ProcedureError('TestProc', 'Something failed')
|
|
134
|
-
|
|
135
|
-
const location = err.getDefinitionLocation()
|
|
136
|
-
|
|
137
|
-
expect(location).toBeUndefined()
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
test('enhanced stack contains definition location', () => {
|
|
141
|
-
const err = new ProcedureError('TestProc', 'Something failed', undefined, mockDefinitionInfo)
|
|
142
|
-
|
|
143
|
-
expect(err.stack).toContain('--- Procedure "TestProc" defined at ---')
|
|
144
|
-
expect(err.stack).toContain('/app/procedures/user.ts:25:3')
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
test('stack is not modified when no definitionInfo', () => {
|
|
148
|
-
const err = new ProcedureError('TestProc', 'Something failed')
|
|
149
|
-
|
|
150
|
-
expect(err.stack).toBeDefined()
|
|
151
|
-
expect(err.stack).not.toContain('--- Procedure')
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
test('all error types enhance stack with definition location', () => {
|
|
155
|
-
const baseErr = new ProcedureError('Proc1', 'message', undefined, mockDefinitionInfo)
|
|
156
|
-
const validationErr = new ProcedureValidationError('Proc2', 'message', [], mockDefinitionInfo)
|
|
157
|
-
const registrationErr = new ProcedureRegistrationError('Proc3', 'message', mockDefinitionInfo)
|
|
158
|
-
|
|
159
|
-
expect(baseErr.stack).toContain('--- Procedure "Proc1" defined at ---')
|
|
160
|
-
expect(validationErr.stack).toContain('--- Procedure "Proc2" defined at ---')
|
|
161
|
-
expect(registrationErr.stack).toContain('--- Procedure "Proc3" defined at ---')
|
|
162
|
-
})
|
|
163
|
-
})
|
package/src/errors.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { TSchemaValidationError } from './schema/parser.js'
|
|
2
|
-
import { DefinitionInfo, DefinitionLocation, formatDefinitionInfo } from './stack-utils.js'
|
|
3
|
-
import { kebabCase } from 'es-toolkit/string'
|
|
4
|
-
|
|
5
|
-
export class ProcedureError extends Error {
|
|
6
|
-
cause?: unknown
|
|
7
|
-
readonly definedAt?: DefinitionLocation
|
|
8
|
-
readonly definitionStack?: string
|
|
9
|
-
|
|
10
|
-
constructor(
|
|
11
|
-
readonly procedureName: string,
|
|
12
|
-
readonly message: string,
|
|
13
|
-
readonly meta?: object,
|
|
14
|
-
// Used for error stack trace details
|
|
15
|
-
definitionInfo?: DefinitionInfo
|
|
16
|
-
) {
|
|
17
|
-
super(message)
|
|
18
|
-
this.name = 'ProcedureError'
|
|
19
|
-
|
|
20
|
-
if (definitionInfo) {
|
|
21
|
-
this.definedAt = definitionInfo.definedAt
|
|
22
|
-
this.definitionStack = definitionInfo.definitionStack
|
|
23
|
-
this.enhanceStack()
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
27
|
-
Object.setPrototypeOf(this, ProcedureError.prototype)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Returns a formatted string showing where the procedure was defined.
|
|
32
|
-
*/
|
|
33
|
-
getDefinitionLocation(): string | undefined {
|
|
34
|
-
if (!this.definedAt) {
|
|
35
|
-
return undefined
|
|
36
|
-
}
|
|
37
|
-
return `${this.definedAt.file}:${this.definedAt.line}:${this.definedAt.column}`
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Enhances the error stack with definition location information.
|
|
42
|
-
*/
|
|
43
|
-
private enhanceStack(): void {
|
|
44
|
-
if (!this.stack || !this.definedAt) {
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const definitionSection = formatDefinitionInfo(
|
|
49
|
-
{ definedAt: this.definedAt, definitionStack: this.definitionStack },
|
|
50
|
-
this.procedureName
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
if (definitionSection) {
|
|
54
|
-
this.stack = this.stack + definitionSection
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export class ProcedureValidationError extends ProcedureError {
|
|
60
|
-
constructor(
|
|
61
|
-
readonly procedureName: string,
|
|
62
|
-
message: string,
|
|
63
|
-
readonly errors?: TSchemaValidationError[],
|
|
64
|
-
// Used for error stack trace details
|
|
65
|
-
definitionInfo?: DefinitionInfo
|
|
66
|
-
) {
|
|
67
|
-
const readableErrors = errors
|
|
68
|
-
?.map((err) => `- ${kebabCase(err.instancePath).replace('-', '.')} ${err.message}`)
|
|
69
|
-
.join(', ')
|
|
70
|
-
super(procedureName, message + ' ' + readableErrors, { errors }, definitionInfo)
|
|
71
|
-
this.name = 'ProcedureValidationError'
|
|
72
|
-
|
|
73
|
-
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
74
|
-
Object.setPrototypeOf(this, ProcedureValidationError.prototype)
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export class ProcedureRegistrationError extends ProcedureError {
|
|
79
|
-
constructor(
|
|
80
|
-
readonly procedureName: string,
|
|
81
|
-
message: string,
|
|
82
|
-
// Used for error stack trace details
|
|
83
|
-
definitionInfo?: DefinitionInfo
|
|
84
|
-
) {
|
|
85
|
-
super(procedureName, message, undefined, definitionInfo)
|
|
86
|
-
this.name = 'ProcedureRegistrationError'
|
|
87
|
-
|
|
88
|
-
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
89
|
-
Object.setPrototypeOf(this, ProcedureRegistrationError.prototype)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export class ProcedureYieldValidationError extends ProcedureError {
|
|
94
|
-
constructor(
|
|
95
|
-
readonly procedureName: string,
|
|
96
|
-
message: string,
|
|
97
|
-
readonly errors?: TSchemaValidationError[],
|
|
98
|
-
// Used for error stack trace details
|
|
99
|
-
definitionInfo?: DefinitionInfo
|
|
100
|
-
) {
|
|
101
|
-
super(procedureName, message, undefined, definitionInfo)
|
|
102
|
-
this.name = 'ProcedureYieldValidationError'
|
|
103
|
-
|
|
104
|
-
// https://www.dannyguo.com/blog/how-to-fix-instanceof-not-working-for-custom-errors-in-typescript/
|
|
105
|
-
Object.setPrototypeOf(this, ProcedureYieldValidationError.prototype)
|
|
106
|
-
}
|
|
107
|
-
}
|
package/src/exports.ts
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
# HTTP Implementations
|
|
2
|
-
|
|
3
|
-
HTTP implementation builders for `ts-procedures` that create type-safe, versioned endpoints with automatic path generation, schema-based validation, and route documentation.
|
|
4
|
-
|
|
5
|
-
## Available Implementations
|
|
6
|
-
|
|
7
|
-
### RPC (Request/Response)
|
|
8
|
-
|
|
9
|
-
For procedures created with `Create()` - standard request/response pattern using POST.
|
|
10
|
-
|
|
11
|
-
| Framework | Package | Description |
|
|
12
|
-
|-----------|---------|-------------|
|
|
13
|
-
| [Express RPC](./express-rpc/README.md) | `express-rpc` | Express.js integration |
|
|
14
|
-
| [Hono RPC](./hono-rpc/README.md) | `hono-rpc` | Hono integration (Bun, Deno, Cloudflare Workers, Node.js) |
|
|
15
|
-
|
|
16
|
-
### Streaming
|
|
17
|
-
|
|
18
|
-
For procedures created with `CreateStream()` - server-sent events and streaming responses.
|
|
19
|
-
|
|
20
|
-
| Framework | Package | Description |
|
|
21
|
-
|-----------|---------|-------------|
|
|
22
|
-
| [Hono Stream](./hono-stream/README.md) | `hono-stream` | SSE and text streaming for async generators |
|
|
23
|
-
|
|
24
|
-
### API (REST-style)
|
|
25
|
-
|
|
26
|
-
For procedures using `schema.input` — per-channel input validation with standard HTTP methods.
|
|
27
|
-
|
|
28
|
-
| Framework | Package | Description |
|
|
29
|
-
|-----------|---------|-------------|
|
|
30
|
-
| [Hono API](./hono-api/) | `hono-api` | REST-style routing by HTTP method (GET, POST, PUT, DELETE, PATCH, HEAD) |
|
|
31
|
-
|
|
32
|
-
## Procedure Types
|
|
33
|
-
|
|
34
|
-
| Type | Created With | Handler Return | HTTP Methods | Use Case |
|
|
35
|
-
|------|--------------|----------------|--------------|----------|
|
|
36
|
-
| RPC | `Create()` | `Promise<T>` | POST | Standard request/response |
|
|
37
|
-
| Stream | `CreateStream()` | `AsyncGenerator<T>` | GET, POST | Real-time updates, SSE |
|
|
38
|
-
| API | `Create()` | `Promise<T>` | GET, POST, PUT, DELETE, PATCH, HEAD | REST-style endpoints with per-channel input |
|
|
39
|
-
|
|
40
|
-
## Core Concepts
|
|
41
|
-
|
|
42
|
-
### Config Interface
|
|
43
|
-
|
|
44
|
-
All HTTP implementations use a shared configuration interface:
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
interface RPCConfig {
|
|
48
|
-
scope: string | string[] // Route path segment(s)
|
|
49
|
-
version: number // API version number
|
|
50
|
-
}
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
#### APIConfig (REST-style)
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
interface APIConfig {
|
|
57
|
-
path: string // Route path with Hono params (e.g., '/users/:id')
|
|
58
|
-
method: HttpMethod // 'get' | 'post' | 'put' | 'delete' | 'patch' | 'head'
|
|
59
|
-
successStatus?: number // Default: POST→201, DELETE→204, others→200
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
API routes use developer-defined paths — no auto-generation from scope/version.
|
|
64
|
-
|
|
65
|
-
### Path Generation
|
|
66
|
-
|
|
67
|
-
Routes are generated using kebab-case conversion:
|
|
68
|
-
|
|
69
|
-
```
|
|
70
|
-
/{pathPrefix}/{scope...}/{procedureName}/{version}
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
**Examples:**
|
|
74
|
-
|
|
75
|
-
| Scope | Procedure Name | Version | Generated Path |
|
|
76
|
-
|-------|----------------|---------|----------------|
|
|
77
|
-
| `'users'` | `'Create'` | `1` | `/users/create/1` |
|
|
78
|
-
| `'users'` | `'GetById'` | `1` | `/users/get-by-id/1` |
|
|
79
|
-
| `['users', 'admin']` | `'List'` | `1` | `/users/admin/list/1` |
|
|
80
|
-
| `['UserModule', 'permissions']` | `'Update'` | `2` | `/user-module/permissions/update/2` |
|
|
81
|
-
|
|
82
|
-
**With pathPrefix `/api/v1`:**
|
|
83
|
-
|
|
84
|
-
| Scope | Procedure Name | Version | Generated Path |
|
|
85
|
-
|-------|----------------|---------|----------------|
|
|
86
|
-
| `'users'` | `'Create'` | `1` | `/api/v1/users/create/1` |
|
|
87
|
-
| `['users', 'admin']` | `'Delete'` | `2` | `/api/v1/users/admin/delete/2` |
|
|
88
|
-
|
|
89
|
-
### Context Resolution
|
|
90
|
-
|
|
91
|
-
The `factoryContext` parameter supports three patterns:
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
// 1. Static object
|
|
95
|
-
builder.register(Factory, { userId: 'static-123' })
|
|
96
|
-
|
|
97
|
-
// 2. Sync function
|
|
98
|
-
builder.register(Factory, (c) => ({
|
|
99
|
-
userId: c.req.header('x-user-id')
|
|
100
|
-
}))
|
|
101
|
-
|
|
102
|
-
// 3. Async function
|
|
103
|
-
builder.register(Factory, async (c) => {
|
|
104
|
-
const user = await validateToken(c.req.header('authorization'))
|
|
105
|
-
return { userId: user.id }
|
|
106
|
-
})
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### Abort Signal
|
|
110
|
-
|
|
111
|
-
All HTTP implementations automatically inject an `AbortSignal` into the handler context as `ctx.signal`. This signal aborts when the client disconnects, enabling handlers to cancel in-flight work (fetch calls, database queries, etc.).
|
|
112
|
-
|
|
113
|
-
| Framework | Signal Source | Behavior |
|
|
114
|
-
|-----------|-------------|----------|
|
|
115
|
-
| Hono RPC | `c.req.raw.signal` | Web standard Request signal |
|
|
116
|
-
| Hono Stream | `c.req.raw.signal` | Combined with internal stream AbortController via `AbortSignal.any()` |
|
|
117
|
-
| Express RPC | Lazy `AbortController` | Created on first `ctx.signal` access, wired to `req.on('close')` |
|
|
118
|
-
|
|
119
|
-
For streaming procedures, `signal.reason` is `'stream-completed'` on normal completion, allowing handlers to distinguish from client disconnection.
|
|
120
|
-
|
|
121
|
-
### Lifecycle Hooks
|
|
122
|
-
|
|
123
|
-
**RPC Implementations:**
|
|
124
|
-
|
|
125
|
-
```
|
|
126
|
-
onRequestStart → handler → onSuccess → onRequestEnd
|
|
127
|
-
↓
|
|
128
|
-
(on error)
|
|
129
|
-
↓
|
|
130
|
-
onError handler
|
|
131
|
-
↓
|
|
132
|
-
onRequestEnd
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
**Stream Implementations:**
|
|
136
|
-
|
|
137
|
-
```
|
|
138
|
-
onRequestStart → onStreamStart → [yields...] → onStreamEnd → onRequestEnd
|
|
139
|
-
↓
|
|
140
|
-
(on error)
|
|
141
|
-
↓
|
|
142
|
-
error in stream
|
|
143
|
-
↓
|
|
144
|
-
onStreamEnd
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
| Hook | Available In | Trigger |
|
|
148
|
-
|------|--------------|---------|
|
|
149
|
-
| `onRequestStart` | Both | Before route handler |
|
|
150
|
-
| `onRequestEnd` | Both | After response sent |
|
|
151
|
-
| `onSuccess` | RPC, API | After successful handler |
|
|
152
|
-
| `onError` | RPC, API | On handler error |
|
|
153
|
-
| `onStreamStart` | Stream | Before first yield |
|
|
154
|
-
| `onStreamEnd` | Stream | After stream completes |
|
|
155
|
-
| `onPreStreamError` | Stream | On pre-stream error (validation, auth) |
|
|
156
|
-
| `onMidStreamError` | Stream | On mid-stream error (generator throws) |
|
|
157
|
-
|
|
158
|
-
### Route Documentation
|
|
159
|
-
|
|
160
|
-
Each registered procedure generates documentation accessible via `builder.docs`.
|
|
161
|
-
|
|
162
|
-
**RPC Documentation (`RPCHttpRouteDoc`):**
|
|
163
|
-
|
|
164
|
-
```typescript
|
|
165
|
-
interface RPCHttpRouteDoc {
|
|
166
|
-
name: string
|
|
167
|
-
path: string
|
|
168
|
-
method: 'post'
|
|
169
|
-
scope: string | string[]
|
|
170
|
-
version: number
|
|
171
|
-
jsonSchema: {
|
|
172
|
-
body?: object // From schema.params
|
|
173
|
-
response?: object // From schema.returnType
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
**Stream Documentation (`StreamHttpRouteDoc`):**
|
|
179
|
-
|
|
180
|
-
```typescript
|
|
181
|
-
interface StreamHttpRouteDoc {
|
|
182
|
-
name: string
|
|
183
|
-
path: string
|
|
184
|
-
methods: ('get' | 'post')[]
|
|
185
|
-
streamMode: 'sse' | 'text'
|
|
186
|
-
scope: string | string[]
|
|
187
|
-
version: number
|
|
188
|
-
jsonSchema: {
|
|
189
|
-
params?: object // From schema.params
|
|
190
|
-
yieldType?: object // From schema.yieldType
|
|
191
|
-
returnType?: object // From schema.returnType
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
**API Documentation (`APIHttpRouteDoc`):**
|
|
197
|
-
|
|
198
|
-
```typescript
|
|
199
|
-
interface APIHttpRouteDoc {
|
|
200
|
-
name: string
|
|
201
|
-
path: string
|
|
202
|
-
method: HttpMethod
|
|
203
|
-
fullPath: string
|
|
204
|
-
successStatus?: number
|
|
205
|
-
jsonSchema: {
|
|
206
|
-
pathParams?: object // From schema.input.pathParams
|
|
207
|
-
query?: object // From schema.input.query
|
|
208
|
-
body?: object // From schema.input.body
|
|
209
|
-
headers?: object // From schema.input.headers
|
|
210
|
-
response?: object // From schema.returnType
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### Builder Pattern
|
|
216
|
-
|
|
217
|
-
All implementations follow the same builder pattern:
|
|
218
|
-
|
|
219
|
-
```typescript
|
|
220
|
-
const builder = new AppBuilder(config)
|
|
221
|
-
.register(PublicFactory, publicContextResolver)
|
|
222
|
-
.register(ProtectedFactory, protectedContextResolver)
|
|
223
|
-
|
|
224
|
-
const app = builder.build()
|
|
225
|
-
const docs = builder.docs
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
**Note:** `HonoAPIAppBuilder.build()` is async (resolves query parser on first call).
|
|
229
|
-
|
|
230
|
-
**Key methods:**
|
|
231
|
-
|
|
232
|
-
| Method | Returns | Description |
|
|
233
|
-
|--------|---------|-------------|
|
|
234
|
-
| `register(factory, context, options?)` | `this` | Register a procedure factory |
|
|
235
|
-
| `build()` | Framework app | Create routes and return the application |
|
|
236
|
-
|
|
237
|
-
**Properties:**
|
|
238
|
-
|
|
239
|
-
| Property | Type | Description |
|
|
240
|
-
|----------|------|-------------|
|
|
241
|
-
| `app` | Framework app | The underlying framework application |
|
|
242
|
-
| `docs` | Route doc array | Route documentation (populated after `build()`) |
|
|
243
|
-
|
|
244
|
-
## Framework Comparison
|
|
245
|
-
|
|
246
|
-
| Aspect | Express | Hono |
|
|
247
|
-
|--------|---------|------|
|
|
248
|
-
| Context param | `req: express.Request` | `c: Context` |
|
|
249
|
-
| Error handler return | `void` (mutates res) | `Response` |
|
|
250
|
-
| Body access | `req.body` | `await c.req.json()` |
|
|
251
|
-
| Header access | `req.headers['x-id']` | `c.req.header('x-id')` |
|
|
252
|
-
| JSON middleware | Auto-added (or manual) | Built-in |
|
|
253
|
-
| Streaming support | Not yet | `hono-stream` |
|
|
254
|
-
|
|
255
|
-
## TypeScript Types
|
|
256
|
-
|
|
257
|
-
```typescript
|
|
258
|
-
import { RPCConfig, RPCHttpRouteDoc, StreamHttpRouteDoc, StreamMode } from 'ts-procedures/implementations/types'
|
|
259
|
-
import type { APIConfig, APIHttpRouteDoc, APIInput, HttpMethod } from 'ts-procedures/http'
|
|
260
|
-
```
|