ts-procedures 9.1.0 → 9.2.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/CHANGELOG.md +48 -0
- package/agent_config/claude-code/skills/ts-procedures/api-reference.md +4 -0
- package/agent_config/copilot/copilot-instructions.md +2 -0
- package/agent_config/cursor/cursorrules +2 -0
- package/build/codegen/bin/cli.d.ts +4 -0
- package/build/codegen/bin/cli.js +19 -0
- package/build/codegen/bin/cli.js.map +1 -1
- package/build/codegen/bin/cli.test.js +21 -0
- package/build/codegen/bin/cli.test.js.map +1 -1
- package/build/codegen/bin/flag-specs.js +3 -0
- package/build/codegen/bin/flag-specs.js.map +1 -1
- package/build/codegen/check-output.d.ts +28 -0
- package/build/codegen/check-output.js +216 -0
- package/build/codegen/check-output.js.map +1 -0
- package/build/codegen/check-output.test.d.ts +1 -0
- package/build/codegen/check-output.test.js +160 -0
- package/build/codegen/check-output.test.js.map +1 -0
- package/build/codegen/emit/api-route.js +1 -1
- package/build/codegen/emit/api-route.js.map +1 -1
- package/build/codegen/emit/context.d.ts +9 -0
- package/build/codegen/emit/http-stream-route.js +1 -1
- package/build/codegen/emit/http-stream-route.js.map +1 -1
- package/build/codegen/emit/rpc-route.js +1 -1
- package/build/codegen/emit/rpc-route.js.map +1 -1
- package/build/codegen/emit/scope-file.js +14 -0
- package/build/codegen/emit/scope-file.js.map +1 -1
- package/build/codegen/emit/stream-route.js +1 -1
- package/build/codegen/emit/stream-route.js.map +1 -1
- package/build/codegen/emit-scope.test.js +55 -0
- package/build/codegen/emit-scope.test.js.map +1 -1
- package/build/codegen/emit-types.js +19 -0
- package/build/codegen/emit-types.js.map +1 -1
- package/build/codegen/emit-types.test.js +34 -0
- package/build/codegen/emit-types.test.js.map +1 -1
- package/build/codegen/index.d.ts +14 -0
- package/build/codegen/index.js +11 -1
- package/build/codegen/index.js.map +1 -1
- package/build/codegen/targets/ts/name-collision.test.d.ts +1 -0
- package/build/codegen/targets/ts/name-collision.test.js +30 -0
- package/build/codegen/targets/ts/name-collision.test.js.map +1 -0
- package/build/codegen/targets/ts/run.js +13 -0
- package/build/codegen/targets/ts/run.js.map +1 -1
- package/build/errors.d.ts +33 -0
- package/build/errors.js +91 -0
- package/build/errors.js.map +1 -0
- package/build/errors.test.d.ts +1 -0
- package/build/errors.test.js +122 -0
- package/build/errors.test.js.map +1 -0
- package/build/implementations/http/doc-registry.d.ts +28 -0
- package/build/implementations/http/doc-registry.js +96 -0
- package/build/implementations/http/doc-registry.js.map +1 -0
- package/build/implementations/http/doc-registry.test.d.ts +1 -0
- package/build/implementations/http/doc-registry.test.js +516 -0
- package/build/implementations/http/doc-registry.test.js.map +1 -0
- package/build/implementations/http/error-taxonomy.d.ts +249 -0
- package/build/implementations/http/error-taxonomy.js +252 -0
- package/build/implementations/http/error-taxonomy.js.map +1 -0
- package/build/implementations/http/error-taxonomy.test.d.ts +1 -0
- package/build/implementations/http/error-taxonomy.test.js +399 -0
- package/build/implementations/http/error-taxonomy.test.js.map +1 -0
- package/build/implementations/http/express-rpc/error-taxonomy.test.d.ts +1 -0
- package/build/implementations/http/express-rpc/error-taxonomy.test.js +83 -0
- package/build/implementations/http/express-rpc/error-taxonomy.test.js.map +1 -0
- package/build/implementations/http/express-rpc/index.d.ts +125 -0
- package/build/implementations/http/express-rpc/index.js +216 -0
- package/build/implementations/http/express-rpc/index.js.map +1 -0
- package/build/implementations/http/express-rpc/index.test.d.ts +1 -0
- package/build/implementations/http/express-rpc/index.test.js +684 -0
- package/build/implementations/http/express-rpc/index.test.js.map +1 -0
- package/build/implementations/http/express-rpc/types.d.ts +11 -0
- package/build/implementations/http/express-rpc/types.js +2 -0
- package/build/implementations/http/express-rpc/types.js.map +1 -0
- package/build/implementations/http/hono-api/error-taxonomy.test.d.ts +1 -0
- package/build/implementations/http/hono-api/error-taxonomy.test.js +137 -0
- package/build/implementations/http/hono-api/error-taxonomy.test.js.map +1 -0
- package/build/implementations/http/hono-api/index.d.ts +151 -0
- package/build/implementations/http/hono-api/index.js +344 -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 +992 -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/http/hono-rpc/error-taxonomy.test.d.ts +1 -0
- package/build/implementations/http/hono-rpc/error-taxonomy.test.js +64 -0
- package/build/implementations/http/hono-rpc/error-taxonomy.test.js.map +1 -0
- package/build/implementations/http/hono-rpc/index.d.ts +130 -0
- package/build/implementations/http/hono-rpc/index.js +209 -0
- package/build/implementations/http/hono-rpc/index.js.map +1 -0
- package/build/implementations/http/hono-rpc/index.test.d.ts +1 -0
- package/build/implementations/http/hono-rpc/index.test.js +828 -0
- package/build/implementations/http/hono-rpc/index.test.js.map +1 -0
- package/build/implementations/http/hono-rpc/types.d.ts +11 -0
- package/build/implementations/http/hono-rpc/types.js +2 -0
- package/build/implementations/http/hono-rpc/types.js.map +1 -0
- package/build/implementations/http/hono-stream/error-taxonomy.test.d.ts +1 -0
- package/build/implementations/http/hono-stream/error-taxonomy.test.js +159 -0
- package/build/implementations/http/hono-stream/error-taxonomy.test.js.map +1 -0
- package/build/implementations/http/hono-stream/index.d.ts +171 -0
- package/build/implementations/http/hono-stream/index.js +415 -0
- package/build/implementations/http/hono-stream/index.js.map +1 -0
- package/build/implementations/http/hono-stream/index.test.d.ts +1 -0
- package/build/implementations/http/hono-stream/index.test.js +1383 -0
- package/build/implementations/http/hono-stream/index.test.js.map +1 -0
- package/build/implementations/http/hono-stream/types.d.ts +15 -0
- package/build/implementations/http/hono-stream/types.js +2 -0
- package/build/implementations/http/hono-stream/types.js.map +1 -0
- package/build/implementations/http/on-request-error.test.d.ts +1 -0
- package/build/implementations/http/on-request-error.test.js +173 -0
- package/build/implementations/http/on-request-error.test.js.map +1 -0
- package/build/implementations/http/route-errors.test.d.ts +1 -0
- package/build/implementations/http/route-errors.test.js +139 -0
- package/build/implementations/http/route-errors.test.js.map +1 -0
- package/build/implementations/types.d.ts +192 -0
- package/build/implementations/types.js +2 -0
- package/build/implementations/types.js.map +1 -0
- package/build/index.d.ts +165 -0
- package/build/index.js +258 -0
- package/build/index.js.map +1 -0
- package/build/index.test.d.ts +1 -0
- package/build/index.test.js +920 -0
- package/build/index.test.js.map +1 -0
- package/build/schema/extract-json-schema.d.ts +2 -0
- package/build/schema/extract-json-schema.js +12 -0
- package/build/schema/extract-json-schema.js.map +1 -0
- package/build/schema/extract-json-schema.test.d.ts +1 -0
- package/build/schema/extract-json-schema.test.js +23 -0
- package/build/schema/extract-json-schema.test.js.map +1 -0
- package/build/schema/parser.d.ts +28 -0
- package/build/schema/parser.js +170 -0
- package/build/schema/parser.js.map +1 -0
- package/build/schema/parser.test.d.ts +1 -0
- package/build/schema/parser.test.js +120 -0
- package/build/schema/parser.test.js.map +1 -0
- package/build/schema/resolve-schema-lib.d.ts +12 -0
- package/build/schema/resolve-schema-lib.js +11 -0
- package/build/schema/resolve-schema-lib.js.map +1 -0
- package/build/schema/resolve-schema-lib.test.d.ts +1 -0
- package/build/schema/resolve-schema-lib.test.js +17 -0
- package/build/schema/resolve-schema-lib.test.js.map +1 -0
- package/build/schema/types.d.ts +8 -0
- package/build/schema/types.js +2 -0
- package/build/schema/types.js.map +1 -0
- package/build/stack-utils.d.ts +25 -0
- package/build/stack-utils.js +95 -0
- package/build/stack-utils.js.map +1 -0
- package/build/stack-utils.test.d.ts +1 -0
- package/build/stack-utils.test.js +80 -0
- package/build/stack-utils.test.js.map +1 -0
- package/docs/client-and-codegen.md +17 -0
- package/docs/migration-v8-to-v9.md +12 -4
- package/package.json +1 -1
- package/src/codegen/bin/cli.test.ts +28 -0
- package/src/codegen/bin/cli.ts +26 -0
- package/src/codegen/bin/flag-specs.ts +3 -0
- package/src/codegen/check-output.test.ts +177 -0
- package/src/codegen/check-output.ts +262 -0
- package/src/codegen/emit/api-route.ts +1 -1
- package/src/codegen/emit/context.ts +9 -0
- package/src/codegen/emit/http-stream-route.ts +1 -1
- package/src/codegen/emit/rpc-route.ts +1 -1
- package/src/codegen/emit/scope-file.ts +17 -0
- package/src/codegen/emit/stream-route.ts +1 -1
- package/src/codegen/emit-scope.test.ts +60 -0
- package/src/codegen/emit-types.test.ts +36 -0
- package/src/codegen/emit-types.ts +20 -0
- package/src/codegen/index.ts +23 -1
- package/src/codegen/targets/ts/name-collision.test.ts +34 -0
- package/src/codegen/targets/ts/run.ts +16 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import { ProcedureError, ProcedureValidationError, ProcedureYieldValidationError, } from '../../errors.js';
|
|
3
|
+
import { defineErrorTaxonomy, resolveErrorResponse, defaultErrorTaxonomy, } from './error-taxonomy.js';
|
|
4
|
+
class UseCaseError extends Error {
|
|
5
|
+
externalMsg;
|
|
6
|
+
internalMsg;
|
|
7
|
+
constructor(externalMsg, internalMsg) {
|
|
8
|
+
super(externalMsg);
|
|
9
|
+
this.externalMsg = externalMsg;
|
|
10
|
+
this.internalMsg = internalMsg;
|
|
11
|
+
this.name = 'UseCaseError';
|
|
12
|
+
Object.setPrototypeOf(this, UseCaseError.prototype);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
class AuthError extends Error {
|
|
16
|
+
reason;
|
|
17
|
+
constructor(reason) {
|
|
18
|
+
super(reason);
|
|
19
|
+
this.reason = reason;
|
|
20
|
+
this.name = 'AuthError';
|
|
21
|
+
Object.setPrototypeOf(this, AuthError.prototype);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const fakeProcedure = { name: 'Test', config: {}, handler: async () => { } };
|
|
25
|
+
describe('defineErrorTaxonomy', () => {
|
|
26
|
+
test('validates exactly one discriminator per entry', () => {
|
|
27
|
+
expect(() => defineErrorTaxonomy({
|
|
28
|
+
Bad: { statusCode: 400 },
|
|
29
|
+
})).toThrow(/exactly one of/);
|
|
30
|
+
expect(() => defineErrorTaxonomy({
|
|
31
|
+
Bad: {
|
|
32
|
+
class: Error,
|
|
33
|
+
match: (e) => e instanceof Error,
|
|
34
|
+
statusCode: 400,
|
|
35
|
+
},
|
|
36
|
+
})).toThrow(/exactly one of/);
|
|
37
|
+
});
|
|
38
|
+
test('accepts a valid entry', () => {
|
|
39
|
+
const t = defineErrorTaxonomy({
|
|
40
|
+
UseCaseError: { class: UseCaseError, statusCode: 422 },
|
|
41
|
+
});
|
|
42
|
+
expect(t.UseCaseError.statusCode).toBe(422);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe('resolveErrorResponse', () => {
|
|
46
|
+
test('class match uses toResponse output', () => {
|
|
47
|
+
const taxonomy = defineErrorTaxonomy({
|
|
48
|
+
UseCaseError: {
|
|
49
|
+
class: UseCaseError,
|
|
50
|
+
statusCode: 422,
|
|
51
|
+
toResponse: (err) => ({ name: 'UseCaseError', message: err.externalMsg }),
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
const resolved = resolveErrorResponse({
|
|
55
|
+
err: new UseCaseError('external', 'internal'),
|
|
56
|
+
userTaxonomy: taxonomy,
|
|
57
|
+
procedure: fakeProcedure,
|
|
58
|
+
raw: {},
|
|
59
|
+
});
|
|
60
|
+
expect(resolved?.statusCode).toBe(422);
|
|
61
|
+
expect(resolved?.body).toEqual({ name: 'UseCaseError', message: 'external' });
|
|
62
|
+
});
|
|
63
|
+
test('match predicate catches 3rd-party errors', () => {
|
|
64
|
+
const mongoLike = Object.assign(new Error('dup'), { name: 'MongoServerError', code: 11000 });
|
|
65
|
+
const taxonomy = defineErrorTaxonomy({
|
|
66
|
+
MongoDuplicateKey: {
|
|
67
|
+
match: (e) => e instanceof Error && e.code === 11000,
|
|
68
|
+
statusCode: 409,
|
|
69
|
+
toResponse: () => ({ name: 'Conflict' }),
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
const resolved = resolveErrorResponse({
|
|
73
|
+
err: mongoLike,
|
|
74
|
+
userTaxonomy: taxonomy,
|
|
75
|
+
procedure: fakeProcedure,
|
|
76
|
+
raw: {},
|
|
77
|
+
});
|
|
78
|
+
expect(resolved?.statusCode).toBe(409);
|
|
79
|
+
expect(resolved?.body).toEqual({ name: 'Conflict' });
|
|
80
|
+
});
|
|
81
|
+
test('default toResponse emits { name, message } from entry key', () => {
|
|
82
|
+
const taxonomy = defineErrorTaxonomy({
|
|
83
|
+
AuthError: { class: AuthError, statusCode: 401 },
|
|
84
|
+
});
|
|
85
|
+
const resolved = resolveErrorResponse({
|
|
86
|
+
err: new AuthError('unauthenticated'),
|
|
87
|
+
userTaxonomy: taxonomy,
|
|
88
|
+
procedure: fakeProcedure,
|
|
89
|
+
raw: {},
|
|
90
|
+
});
|
|
91
|
+
expect(resolved?.body).toEqual({ name: 'AuthError', message: 'unauthenticated' });
|
|
92
|
+
});
|
|
93
|
+
test('first matching entry wins — subclass declared before base', () => {
|
|
94
|
+
const taxonomy = defineErrorTaxonomy({
|
|
95
|
+
ProcedureValidationError: {
|
|
96
|
+
class: ProcedureValidationError,
|
|
97
|
+
statusCode: 400,
|
|
98
|
+
toResponse: () => ({ kind: 'validation' }),
|
|
99
|
+
},
|
|
100
|
+
ProcedureError: {
|
|
101
|
+
class: ProcedureError,
|
|
102
|
+
statusCode: 500,
|
|
103
|
+
toResponse: () => ({ kind: 'base' }),
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
const resolved = resolveErrorResponse({
|
|
107
|
+
err: new ProcedureValidationError('Test', 'bad', []),
|
|
108
|
+
userTaxonomy: taxonomy,
|
|
109
|
+
procedure: fakeProcedure,
|
|
110
|
+
includeDefaults: false,
|
|
111
|
+
raw: {},
|
|
112
|
+
});
|
|
113
|
+
expect(resolved?.statusCode).toBe(400);
|
|
114
|
+
expect(resolved?.body).toEqual({ name: 'ProcedureValidationError', kind: 'validation' });
|
|
115
|
+
});
|
|
116
|
+
test('topological sort fixes a subclass that was declared after its base', () => {
|
|
117
|
+
// Under first-match-wins without sorting, the base would catch the
|
|
118
|
+
// subclass. defineErrorTaxonomy reorders to keep the subclass entry first.
|
|
119
|
+
const taxonomy = defineErrorTaxonomy({
|
|
120
|
+
ProcedureError: {
|
|
121
|
+
class: ProcedureError,
|
|
122
|
+
statusCode: 500,
|
|
123
|
+
toResponse: () => ({ kind: 'base' }),
|
|
124
|
+
},
|
|
125
|
+
ProcedureValidationError: {
|
|
126
|
+
class: ProcedureValidationError,
|
|
127
|
+
statusCode: 400,
|
|
128
|
+
toResponse: () => ({ kind: 'validation' }),
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
const resolved = resolveErrorResponse({
|
|
132
|
+
err: new ProcedureValidationError('Test', 'bad', []),
|
|
133
|
+
userTaxonomy: taxonomy,
|
|
134
|
+
procedure: fakeProcedure,
|
|
135
|
+
includeDefaults: false,
|
|
136
|
+
raw: {},
|
|
137
|
+
});
|
|
138
|
+
expect(resolved?.statusCode).toBe(400);
|
|
139
|
+
expect(resolved?.body).toEqual({ name: 'ProcedureValidationError', kind: 'validation' });
|
|
140
|
+
});
|
|
141
|
+
test('falls through to unknownError when nothing matches', () => {
|
|
142
|
+
const resolved = resolveErrorResponse({
|
|
143
|
+
err: new Error('boom'),
|
|
144
|
+
userTaxonomy: defineErrorTaxonomy({
|
|
145
|
+
AuthError: { class: AuthError, statusCode: 401 },
|
|
146
|
+
}),
|
|
147
|
+
includeDefaults: false,
|
|
148
|
+
unknownError: {
|
|
149
|
+
statusCode: 500,
|
|
150
|
+
toResponse: () => ({ name: 'InternalServerError' }),
|
|
151
|
+
},
|
|
152
|
+
procedure: fakeProcedure,
|
|
153
|
+
raw: {},
|
|
154
|
+
});
|
|
155
|
+
expect(resolved?.statusCode).toBe(500);
|
|
156
|
+
expect(resolved?.body).toEqual({ name: 'InternalServerError' });
|
|
157
|
+
});
|
|
158
|
+
test('returns null when nothing matches and no unknownError', () => {
|
|
159
|
+
const resolved = resolveErrorResponse({
|
|
160
|
+
err: new Error('boom'),
|
|
161
|
+
userTaxonomy: defineErrorTaxonomy({
|
|
162
|
+
AuthError: { class: AuthError, statusCode: 401 },
|
|
163
|
+
}),
|
|
164
|
+
includeDefaults: false,
|
|
165
|
+
procedure: fakeProcedure,
|
|
166
|
+
raw: {},
|
|
167
|
+
});
|
|
168
|
+
expect(resolved).toBeNull();
|
|
169
|
+
});
|
|
170
|
+
test('default taxonomy catches ProcedureValidationError with status 400', () => {
|
|
171
|
+
const resolved = resolveErrorResponse({
|
|
172
|
+
err: new ProcedureValidationError('Test', 'bad', []),
|
|
173
|
+
procedure: fakeProcedure,
|
|
174
|
+
raw: {},
|
|
175
|
+
});
|
|
176
|
+
expect(resolved?.statusCode).toBe(400);
|
|
177
|
+
expect((resolved?.body).name).toBe('ProcedureValidationError');
|
|
178
|
+
});
|
|
179
|
+
test('default taxonomy catches ProcedureYieldValidationError with status 500', () => {
|
|
180
|
+
const resolved = resolveErrorResponse({
|
|
181
|
+
err: new ProcedureYieldValidationError('Test', 'bad yield', []),
|
|
182
|
+
procedure: fakeProcedure,
|
|
183
|
+
raw: {},
|
|
184
|
+
});
|
|
185
|
+
expect(resolved?.statusCode).toBe(500);
|
|
186
|
+
expect((resolved?.body).name).toBe('ProcedureYieldValidationError');
|
|
187
|
+
});
|
|
188
|
+
test('includeDefaults: false disables the default taxonomy', () => {
|
|
189
|
+
const resolved = resolveErrorResponse({
|
|
190
|
+
err: new ProcedureValidationError('Test', 'bad', []),
|
|
191
|
+
includeDefaults: false,
|
|
192
|
+
procedure: fakeProcedure,
|
|
193
|
+
raw: {},
|
|
194
|
+
});
|
|
195
|
+
expect(resolved).toBeNull();
|
|
196
|
+
});
|
|
197
|
+
test('user entry overrides default for the same class', () => {
|
|
198
|
+
const resolved = resolveErrorResponse({
|
|
199
|
+
err: new ProcedureValidationError('Test', 'bad', []),
|
|
200
|
+
userTaxonomy: defineErrorTaxonomy({
|
|
201
|
+
ProcedureValidationError: {
|
|
202
|
+
class: ProcedureValidationError,
|
|
203
|
+
statusCode: 418,
|
|
204
|
+
toResponse: () => ({ overridden: true }),
|
|
205
|
+
},
|
|
206
|
+
}),
|
|
207
|
+
procedure: fakeProcedure,
|
|
208
|
+
raw: {},
|
|
209
|
+
});
|
|
210
|
+
expect(resolved?.statusCode).toBe(418);
|
|
211
|
+
expect(resolved?.body).toEqual({ name: 'ProcedureValidationError', overridden: true });
|
|
212
|
+
});
|
|
213
|
+
test('onCatch is awaited via runOnCatch', async () => {
|
|
214
|
+
const calls = [];
|
|
215
|
+
const taxonomy = defineErrorTaxonomy({
|
|
216
|
+
UseCaseError: {
|
|
217
|
+
class: UseCaseError,
|
|
218
|
+
statusCode: 422,
|
|
219
|
+
onCatch: async (err) => {
|
|
220
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
221
|
+
calls.push(err.internalMsg);
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
const resolved = resolveErrorResponse({
|
|
226
|
+
err: new UseCaseError('ext', 'internal-log'),
|
|
227
|
+
userTaxonomy: taxonomy,
|
|
228
|
+
procedure: fakeProcedure,
|
|
229
|
+
raw: {},
|
|
230
|
+
});
|
|
231
|
+
expect(calls).toEqual([]);
|
|
232
|
+
await resolved.runOnCatch();
|
|
233
|
+
expect(calls).toEqual(['internal-log']);
|
|
234
|
+
});
|
|
235
|
+
test('unknownError onCatch is awaited', async () => {
|
|
236
|
+
const calls = [];
|
|
237
|
+
const resolved = resolveErrorResponse({
|
|
238
|
+
err: new Error('boom'),
|
|
239
|
+
includeDefaults: false,
|
|
240
|
+
unknownError: {
|
|
241
|
+
toResponse: () => ({}),
|
|
242
|
+
onCatch: async (err) => {
|
|
243
|
+
await Promise.resolve();
|
|
244
|
+
calls.push(err);
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
procedure: fakeProcedure,
|
|
248
|
+
raw: {},
|
|
249
|
+
});
|
|
250
|
+
await resolved.runOnCatch();
|
|
251
|
+
expect(calls).toHaveLength(1);
|
|
252
|
+
expect(calls[0].message).toBe('boom');
|
|
253
|
+
});
|
|
254
|
+
test('onCatch receives procedure, key and raw', async () => {
|
|
255
|
+
let received;
|
|
256
|
+
const taxonomy = defineErrorTaxonomy({
|
|
257
|
+
AuthError: {
|
|
258
|
+
class: AuthError,
|
|
259
|
+
statusCode: 401,
|
|
260
|
+
onCatch: (_err, ctx) => {
|
|
261
|
+
received = ctx;
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
const resolved = resolveErrorResponse({
|
|
266
|
+
err: new AuthError('forbidden'),
|
|
267
|
+
userTaxonomy: taxonomy,
|
|
268
|
+
procedure: fakeProcedure,
|
|
269
|
+
raw: { marker: 'hono-context' },
|
|
270
|
+
});
|
|
271
|
+
await resolved.runOnCatch();
|
|
272
|
+
expect(received.procedure).toBe(fakeProcedure);
|
|
273
|
+
expect(received.key).toBe('AuthError');
|
|
274
|
+
expect(received.raw).toEqual({ marker: 'hono-context' });
|
|
275
|
+
});
|
|
276
|
+
test('defaultErrorTaxonomy exposes all four framework error mappings', () => {
|
|
277
|
+
expect(defaultErrorTaxonomy.ProcedureValidationError.statusCode).toBe(400);
|
|
278
|
+
expect(defaultErrorTaxonomy.ProcedureYieldValidationError.statusCode).toBe(500);
|
|
279
|
+
expect(defaultErrorTaxonomy.ProcedureError.statusCode).toBe(500);
|
|
280
|
+
});
|
|
281
|
+
test('user taxonomy matches cause inside a ProcedureError wrapper', () => {
|
|
282
|
+
// Simulates what the core does when a non-ProcedureError is thrown: wraps
|
|
283
|
+
// into ProcedureError with `cause` preserved.
|
|
284
|
+
const original = new UseCaseError('public', 'private');
|
|
285
|
+
const wrapped = new ProcedureError('Test', 'wrapped');
|
|
286
|
+
wrapped.cause = original;
|
|
287
|
+
const taxonomy = defineErrorTaxonomy({
|
|
288
|
+
UseCaseError: {
|
|
289
|
+
class: UseCaseError,
|
|
290
|
+
statusCode: 422,
|
|
291
|
+
toResponse: (err) => ({ name: 'UseCaseError', message: err.externalMsg }),
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
const resolved = resolveErrorResponse({
|
|
295
|
+
err: wrapped,
|
|
296
|
+
userTaxonomy: taxonomy,
|
|
297
|
+
procedure: fakeProcedure,
|
|
298
|
+
raw: {},
|
|
299
|
+
});
|
|
300
|
+
expect(resolved?.statusCode).toBe(422);
|
|
301
|
+
expect(resolved?.body).toEqual({ name: 'UseCaseError', message: 'public' });
|
|
302
|
+
});
|
|
303
|
+
test('wrapped ProcedureError falls through default taxonomy to unknownError', () => {
|
|
304
|
+
const original = new TypeError('db-broke');
|
|
305
|
+
const wrapped = new ProcedureError('Test', 'wrapped');
|
|
306
|
+
wrapped.cause = original;
|
|
307
|
+
const resolved = resolveErrorResponse({
|
|
308
|
+
err: wrapped,
|
|
309
|
+
unknownError: {
|
|
310
|
+
statusCode: 500,
|
|
311
|
+
toResponse: (err) => ({ name: 'InternalServerError', type: err.constructor.name }),
|
|
312
|
+
},
|
|
313
|
+
procedure: fakeProcedure,
|
|
314
|
+
raw: {},
|
|
315
|
+
});
|
|
316
|
+
expect(resolved?.statusCode).toBe(500);
|
|
317
|
+
expect(resolved?.body).toEqual({ name: 'InternalServerError', type: 'TypeError' });
|
|
318
|
+
});
|
|
319
|
+
test('direct ProcedureError (no cause) still caught by default entry', () => {
|
|
320
|
+
const direct = new ProcedureError('Test', 'from ctx.error');
|
|
321
|
+
const resolved = resolveErrorResponse({
|
|
322
|
+
err: direct,
|
|
323
|
+
procedure: fakeProcedure,
|
|
324
|
+
raw: {},
|
|
325
|
+
});
|
|
326
|
+
expect(resolved?.statusCode).toBe(500);
|
|
327
|
+
expect((resolved?.body).name).toBe('ProcedureError');
|
|
328
|
+
});
|
|
329
|
+
test('auto-injects name when toResponse omits it', () => {
|
|
330
|
+
const taxonomy = defineErrorTaxonomy({
|
|
331
|
+
UseCaseError: {
|
|
332
|
+
class: UseCaseError,
|
|
333
|
+
statusCode: 422,
|
|
334
|
+
// Returns a body without `name` — resolver should add one.
|
|
335
|
+
toResponse: (err) => ({ message: err.externalMsg, detail: err.internalMsg }),
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
const resolved = resolveErrorResponse({
|
|
339
|
+
err: new UseCaseError('ext', 'int'),
|
|
340
|
+
userTaxonomy: taxonomy,
|
|
341
|
+
procedure: fakeProcedure,
|
|
342
|
+
raw: {},
|
|
343
|
+
});
|
|
344
|
+
expect(resolved?.body).toEqual({ name: 'UseCaseError', message: 'ext', detail: 'int' });
|
|
345
|
+
});
|
|
346
|
+
test('preserves explicit name in toResponse output', () => {
|
|
347
|
+
const taxonomy = defineErrorTaxonomy({
|
|
348
|
+
UseCaseError: {
|
|
349
|
+
class: UseCaseError,
|
|
350
|
+
statusCode: 422,
|
|
351
|
+
toResponse: () => ({ name: 'CustomAlias', reason: 'x' }),
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
const resolved = resolveErrorResponse({
|
|
355
|
+
err: new UseCaseError('ext', 'int'),
|
|
356
|
+
userTaxonomy: taxonomy,
|
|
357
|
+
procedure: fakeProcedure,
|
|
358
|
+
raw: {},
|
|
359
|
+
});
|
|
360
|
+
expect((resolved?.body).name).toBe('CustomAlias');
|
|
361
|
+
});
|
|
362
|
+
test('defineErrorTaxonomy topologically sorts class entries (subclass before base)', () => {
|
|
363
|
+
// Declared base-before-subclass on purpose — sort must swap them.
|
|
364
|
+
const taxonomy = defineErrorTaxonomy({
|
|
365
|
+
ProcedureError: {
|
|
366
|
+
class: ProcedureError,
|
|
367
|
+
statusCode: 500,
|
|
368
|
+
toResponse: () => ({ kind: 'base' }),
|
|
369
|
+
},
|
|
370
|
+
ProcedureValidationError: {
|
|
371
|
+
class: ProcedureValidationError,
|
|
372
|
+
statusCode: 400,
|
|
373
|
+
toResponse: () => ({ kind: 'validation' }),
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
const keys = Object.keys(taxonomy);
|
|
377
|
+
expect(keys).toEqual(['ProcedureValidationError', 'ProcedureError']);
|
|
378
|
+
const resolved = resolveErrorResponse({
|
|
379
|
+
err: new ProcedureValidationError('Test', 'bad', []),
|
|
380
|
+
userTaxonomy: taxonomy,
|
|
381
|
+
includeDefaults: false,
|
|
382
|
+
procedure: fakeProcedure,
|
|
383
|
+
raw: {},
|
|
384
|
+
});
|
|
385
|
+
expect(resolved?.statusCode).toBe(400);
|
|
386
|
+
});
|
|
387
|
+
test('topological sort preserves declared order for unrelated classes', () => {
|
|
388
|
+
class A extends Error {
|
|
389
|
+
}
|
|
390
|
+
class B extends Error {
|
|
391
|
+
}
|
|
392
|
+
const taxonomy = defineErrorTaxonomy({
|
|
393
|
+
B: { class: B, statusCode: 400 },
|
|
394
|
+
A: { class: A, statusCode: 400 },
|
|
395
|
+
});
|
|
396
|
+
expect(Object.keys(taxonomy)).toEqual(['B', 'A']);
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
//# sourceMappingURL=error-taxonomy.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-taxonomy.test.js","sourceRoot":"","sources":["../../../src/implementations/http/error-taxonomy.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAA;AAC/C,OAAO,EACL,cAAc,EACd,wBAAwB,EACxB,6BAA6B,GAC9B,MAAM,iBAAiB,CAAA;AAExB,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,qBAAqB,CAAA;AAE5B,MAAM,YAAa,SAAQ,KAAK;IAEnB;IACA;IAFX,YACW,WAAmB,EACnB,WAAmB;QAE5B,KAAK,CAAC,WAAW,CAAC,CAAA;QAHT,gBAAW,GAAX,WAAW,CAAQ;QACnB,gBAAW,GAAX,WAAW,CAAQ;QAG5B,IAAI,CAAC,IAAI,GAAG,cAAc,CAAA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A;QACF,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QAClC,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,0BAA0B,EAAE,gBAAgB,CAAC,CAAC,CAAA;QAEpE,MAAM,QAAQ,GAAG,oBAAoB,CAAC;YACpC,GAAG,EAAE,IAAI,wBAAwB,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;YACpD,YAAY,EAAE,QAAQ;YACtB,eAAe,EAAE,KAAK;YACtB,SAAS,EAAE,aAAa;YACxB,GAAG,EAAE,EAAE;SACR,CAAC,CAAA;QACF,MAAM,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,iEAAiE,EAAE,GAAG,EAAE;QAC3E,MAAM,CAAE,SAAQ,KAAK;SAAG;QACxB,MAAM,CAAE,SAAQ,KAAK;SAAG;QACxB,MAAM,QAAQ,GAAG,mBAAmB,CAAC;YACnC,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE;YAChC,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE;SACjC,CAAC,CAAA;QACF,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAA;IACnD,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from 'vitest';
|
|
2
|
+
import request from 'supertest';
|
|
3
|
+
import { Type } from 'typebox';
|
|
4
|
+
import { Procedures } from '../../../index.js';
|
|
5
|
+
import { ExpressRPCAppBuilder, defineErrorTaxonomy } from './index.js';
|
|
6
|
+
class UseCaseError extends Error {
|
|
7
|
+
externalMsg;
|
|
8
|
+
internalMsg;
|
|
9
|
+
constructor(externalMsg, internalMsg) {
|
|
10
|
+
super(externalMsg);
|
|
11
|
+
this.externalMsg = externalMsg;
|
|
12
|
+
this.internalMsg = internalMsg;
|
|
13
|
+
this.name = 'UseCaseError';
|
|
14
|
+
Object.setPrototypeOf(this, UseCaseError.prototype);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
describe('ExpressRPCAppBuilder — error taxonomy', () => {
|
|
18
|
+
test('taxonomy catches user error thrown from RPC handler', async () => {
|
|
19
|
+
const errors = defineErrorTaxonomy({
|
|
20
|
+
UseCaseError: {
|
|
21
|
+
class: UseCaseError,
|
|
22
|
+
statusCode: 422,
|
|
23
|
+
toResponse: (err) => ({ name: 'UseCaseError', message: err.externalMsg }),
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
const RPC = Procedures();
|
|
27
|
+
RPC.Create('Boom', { scope: 'test', version: 1, schema: { params: Type.Object({}) } }, async () => {
|
|
28
|
+
throw new UseCaseError('ext', 'int');
|
|
29
|
+
});
|
|
30
|
+
const app = new ExpressRPCAppBuilder({ errors }).register(RPC, () => ({})).build();
|
|
31
|
+
const res = await request(app).post('/test/boom/1').send({});
|
|
32
|
+
expect(res.status).toBe(422);
|
|
33
|
+
expect(res.body).toEqual({ name: 'UseCaseError', message: 'ext' });
|
|
34
|
+
});
|
|
35
|
+
test('unknownError catches unmapped errors', async () => {
|
|
36
|
+
const RPC = Procedures();
|
|
37
|
+
RPC.Create('Boom', { scope: 'test', version: 1, schema: { params: Type.Object({}) } }, async () => {
|
|
38
|
+
throw new TypeError('db down');
|
|
39
|
+
});
|
|
40
|
+
const app = new ExpressRPCAppBuilder({
|
|
41
|
+
unknownError: { statusCode: 503, toResponse: () => ({ name: 'ServiceUnavailable' }) },
|
|
42
|
+
})
|
|
43
|
+
.register(RPC, () => ({}))
|
|
44
|
+
.build();
|
|
45
|
+
const res = await request(app).post('/test/boom/1').send({});
|
|
46
|
+
expect(res.status).toBe(503);
|
|
47
|
+
expect(res.body).toEqual({ name: 'ServiceUnavailable' });
|
|
48
|
+
});
|
|
49
|
+
test('onCatch receives { req, res } as raw context', async () => {
|
|
50
|
+
let rawSeen;
|
|
51
|
+
const errors = defineErrorTaxonomy({
|
|
52
|
+
UseCaseError: {
|
|
53
|
+
class: UseCaseError,
|
|
54
|
+
statusCode: 422,
|
|
55
|
+
onCatch: (_err, ctx) => {
|
|
56
|
+
rawSeen = ctx.raw;
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
const RPC = Procedures();
|
|
61
|
+
RPC.Create('Boom', { scope: 'test', version: 1, schema: { params: Type.Object({}) } }, async () => {
|
|
62
|
+
throw new UseCaseError('ext', 'int');
|
|
63
|
+
});
|
|
64
|
+
const app = new ExpressRPCAppBuilder({ errors }).register(RPC, () => ({})).build();
|
|
65
|
+
await request(app).post('/test/boom/1').send({});
|
|
66
|
+
expect(rawSeen.req).toBeDefined();
|
|
67
|
+
expect(rawSeen.res).toBeDefined();
|
|
68
|
+
});
|
|
69
|
+
test('onError callback handles errors not matched by the taxonomy', async () => {
|
|
70
|
+
const onError = vi.fn((_p, _req, res) => {
|
|
71
|
+
res.status(418).json({ legacy: true });
|
|
72
|
+
});
|
|
73
|
+
const RPC = Procedures();
|
|
74
|
+
RPC.Create('Boom', { scope: 'test', version: 1, schema: { params: Type.Object({}) } }, async () => {
|
|
75
|
+
throw new TypeError('legacy');
|
|
76
|
+
});
|
|
77
|
+
const app = new ExpressRPCAppBuilder({ onError }).register(RPC, () => ({})).build();
|
|
78
|
+
const res = await request(app).post('/test/boom/1').send({});
|
|
79
|
+
expect(res.status).toBe(418);
|
|
80
|
+
expect(onError).toHaveBeenCalledOnce();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
//# sourceMappingURL=error-taxonomy.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-taxonomy.test.js","sourceRoot":"","sources":["../../../../src/implementations/http/express-rpc/error-taxonomy.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AACnD,OAAO,OAAO,MAAM,WAAW,CAAA;AAC/B,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAE9C,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAEtE,MAAM,YAAa,SAAQ,KAAK;IAEnB;IACA;IAFX,YACW,WAAmB,EACnB,WAAmB;QAE5B,KAAK,CAAC,WAAW,CAAC,CAAA;QAHT,gBAAW,GAAX,WAAW,CAAQ;QACnB,gBAAW,GAAX,WAAW,CAAQ;QAG5B,IAAI,CAAC,IAAI,GAAG,cAAc,CAAA;QAC1B,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,YAAY,CAAC,SAAS,CAAC,CAAA;IACrD,CAAC;CACF;AAED,QAAQ,CAAC,uCAAuC,EAAE,GAAG,EAAE;IACrD,IAAI,CAAC,qDAAqD,EAAE,KAAK,IAAI,EAAE;QACrE,MAAM,MAAM,GAAG,mBAAmB,CAAC;YACjC,YAAY,EAAE;gBACZ,KAAK,EAAE,YAAY;gBACnB,UAAU,EAAE,GAAG;gBACf,UAAU,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,GAAG,CAAC,WAAW,EAAE,CAAC;aAC1E;SACF,CAAC,CAAA;QACF,MAAM,GAAG,GAAG,UAAU,EAAiB,CAAA;QACvC,GAAG,CAAC,MAAM,CACR,MAAM,EACN,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,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AAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { TProcedureRegistration } from '../../../index.js';
|
|
3
|
+
import { ExtractConfig, ExtractContext, ProceduresFactory, RPCConfig, RPCHttpRouteDoc } from '../../types.js';
|
|
4
|
+
import { ErrorTaxonomy, ErrorTaxonomyEntry, UnknownErrorConfig, defineErrorTaxonomy } from '../error-taxonomy.js';
|
|
5
|
+
export type { RPCConfig, RPCHttpRouteDoc };
|
|
6
|
+
export { defineErrorTaxonomy };
|
|
7
|
+
export type { ErrorTaxonomy, ErrorTaxonomyEntry, UnknownErrorConfig };
|
|
8
|
+
export type ExpressRPCAppBuilderConfig = {
|
|
9
|
+
/**
|
|
10
|
+
* An existing Express application instance to use.
|
|
11
|
+
* When provided, ensure to set up necessary middleware (e.g., json/body parser) beforehand.
|
|
12
|
+
* If not provided, a new instance will be created.
|
|
13
|
+
*/
|
|
14
|
+
app?: express.Express;
|
|
15
|
+
/** Optional path prefix for all RPC routes. */
|
|
16
|
+
pathPrefix?: string;
|
|
17
|
+
onRequestStart?: (req: express.Request) => void;
|
|
18
|
+
onRequestEnd?: (req: express.Request, res: express.Response) => void;
|
|
19
|
+
onSuccess?: (procedure: TProcedureRegistration, req: express.Request, res: express.Response) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Declarative error-to-response mapping (one of the two peer error modes).
|
|
22
|
+
* See hono-api for the full taxonomy contract. The `raw` field passed to
|
|
23
|
+
* taxonomy callbacks is `{ req, res }`.
|
|
24
|
+
*/
|
|
25
|
+
errors?: ErrorTaxonomy;
|
|
26
|
+
/** Fallback serializer for errors not matched by the taxonomy. */
|
|
27
|
+
unknownError?: UnknownErrorConfig;
|
|
28
|
+
/**
|
|
29
|
+
* Imperative error callback — the other peer error mode. Receives every
|
|
30
|
+
* error directly and writes the response via `res`. Use this when you want
|
|
31
|
+
* full control, or alongside `errors` for the tail of errors the taxonomy
|
|
32
|
+
* doesn't cover.
|
|
33
|
+
*/
|
|
34
|
+
onError?: (procedure: TProcedureRegistration, req: express.Request, res: express.Response, error: Error) => void;
|
|
35
|
+
/**
|
|
36
|
+
* Cross-cutting observer — fires for every caught error, BEFORE dispatch.
|
|
37
|
+
* Awaited. Cannot write to `res` (observer only — check `res.headersSent`
|
|
38
|
+
* if you must touch it). Thrown errors inside the observer are swallowed
|
|
39
|
+
* and logged.
|
|
40
|
+
*/
|
|
41
|
+
onRequestError?: (ctx: OnRequestErrorContext) => void | Promise<void>;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Context passed to the `onRequestError` observer. `raw` is `{ req, res }`
|
|
45
|
+
* for the in-flight request.
|
|
46
|
+
*/
|
|
47
|
+
export type OnRequestErrorContext = {
|
|
48
|
+
err: unknown;
|
|
49
|
+
procedure: TProcedureRegistration;
|
|
50
|
+
raw: {
|
|
51
|
+
req: express.Request;
|
|
52
|
+
res: express.Response;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Builder class for creating an Express application with RPC routes.
|
|
57
|
+
*
|
|
58
|
+
* Usage:
|
|
59
|
+
* const PublicRPC = Procedures<PublicRPCContext, RPCConfig>()
|
|
60
|
+
* const ProtectedRPC = Procedures<ProtectedRPCContext, RPCConfig>()
|
|
61
|
+
*
|
|
62
|
+
* const rpcApp = new ExpressRPCAppBuilder()
|
|
63
|
+
* .register(PublicRPC, (req): Promise<PublicRPCContext> => { /* context resolution logic * / })
|
|
64
|
+
* .register(ProtectedRPC, (req): Promise<ProtectedRPCContext> => { /* context resolution logic * / })
|
|
65
|
+
* .build();
|
|
66
|
+
*
|
|
67
|
+
* const app = rpcApp.app; // Express application
|
|
68
|
+
* const docs = rpcApp.docs; // RPC route documentation
|
|
69
|
+
*/
|
|
70
|
+
export declare class ExpressRPCAppBuilder {
|
|
71
|
+
readonly config?: ExpressRPCAppBuilderConfig | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Constructor for ExpressRPCAppBuilder.
|
|
74
|
+
*
|
|
75
|
+
* @param config
|
|
76
|
+
*/
|
|
77
|
+
constructor(config?: ExpressRPCAppBuilderConfig | undefined);
|
|
78
|
+
/**
|
|
79
|
+
* Generates the RPC route path based on the RPC configuration.
|
|
80
|
+
* `RPCConfig.scope` can be a string or an array of strings to form nested paths.
|
|
81
|
+
*
|
|
82
|
+
* Example
|
|
83
|
+
* name: 'GetUser'
|
|
84
|
+
* scope: ['users', 'profile']
|
|
85
|
+
* version: 1
|
|
86
|
+
* path: /users/profile/get-user/1
|
|
87
|
+
* @param config
|
|
88
|
+
*/
|
|
89
|
+
static makeRPCHttpRoutePath({ name, config, prefix, }: {
|
|
90
|
+
name: string;
|
|
91
|
+
prefix?: string;
|
|
92
|
+
config: RPCConfig;
|
|
93
|
+
}): string;
|
|
94
|
+
/**
|
|
95
|
+
* Instance method wrapper for makeRPCHttpRoutePath that uses the builder's pathPrefix.
|
|
96
|
+
* @param config - The RPC configuration
|
|
97
|
+
*/
|
|
98
|
+
makeRPCHttpRoutePath(name: string, config: RPCConfig): string;
|
|
99
|
+
private factories;
|
|
100
|
+
private _app;
|
|
101
|
+
private _docs;
|
|
102
|
+
get app(): express.Express;
|
|
103
|
+
get docs(): RPCHttpRouteDoc[];
|
|
104
|
+
/**
|
|
105
|
+
* Registers a procedure factory with its context.
|
|
106
|
+
* @param factory - The procedure factory created by Procedures<Context, RPCConfig>()
|
|
107
|
+
* @param factoryContext - The context for procedure handlers. Can be a direct value,
|
|
108
|
+
* a sync function (req) => Context, or an async function (req) => Promise<Context>
|
|
109
|
+
* @param extendProcedureDoc - A custom function to extend the generated RPC route documentation for each procedure.
|
|
110
|
+
*/
|
|
111
|
+
register<TFactory extends ProceduresFactory>(factory: TFactory, factoryContext: ExtractContext<TFactory> | ((req: express.Request) => ExtractContext<TFactory> | Promise<ExtractContext<TFactory>>), extendProcedureDoc?: (params: {
|
|
112
|
+
base: RPCHttpRouteDoc;
|
|
113
|
+
procedure: TProcedureRegistration<any, ExtractConfig<TFactory>>;
|
|
114
|
+
}) => Record<string, any>): this;
|
|
115
|
+
/**
|
|
116
|
+
* Builds and returns the Express application with registered RPC routes.
|
|
117
|
+
* @return express.Application
|
|
118
|
+
*/
|
|
119
|
+
build(): express.Application;
|
|
120
|
+
/**
|
|
121
|
+
* Generates the RPC HTTP route for the given procedure.
|
|
122
|
+
* @param procedure
|
|
123
|
+
*/
|
|
124
|
+
private buildRpcHttpRouteDoc;
|
|
125
|
+
}
|