ts-procedures 8.2.1 → 8.4.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/agent_config/claude-code/skills/ts-procedures/SKILL.md +31 -9
- package/agent_config/claude-code/skills/ts-procedures/api-reference.md +3 -1
- package/agent_config/claude-code/skills/ts-procedures/patterns.md +30 -6
- package/agent_config/claude-code/skills/ts-procedures/templates/client.md +3 -3
- package/agent_config/claude-code/skills/ts-procedures/templates/hono.md +3 -3
- package/agent_config/claude-code/skills/ts-procedures/templates/procedure.md +3 -3
- package/agent_config/claude-code/skills/ts-procedures/templates/stream-procedure.md +3 -3
- package/agent_config/copilot/copilot-instructions.md +10 -6
- package/agent_config/cursor/cursorrules +10 -6
- package/build/client/call.js +1 -1
- package/build/client/call.js.map +1 -1
- package/build/client/index.d.ts +1 -1
- package/build/client/index.js +23 -1
- package/build/client/index.js.map +1 -1
- package/build/client/index.test.js +87 -0
- package/build/client/index.test.js.map +1 -1
- package/build/client/resolve-options.d.ts +5 -4
- package/build/client/resolve-options.js +18 -7
- package/build/client/resolve-options.js.map +1 -1
- package/build/client/resolve-options.test.js +53 -24
- package/build/client/resolve-options.test.js.map +1 -1
- package/build/client/stream.js +1 -1
- package/build/client/stream.js.map +1 -1
- package/build/client/types.d.ts +31 -3
- package/build/codegen/__fixtures__/make-envelope.d.ts +41 -0
- package/build/codegen/__fixtures__/make-envelope.js +38 -0
- package/build/codegen/__fixtures__/make-envelope.js.map +1 -0
- package/build/codegen/bin/cli.d.ts +11 -0
- package/build/codegen/bin/cli.js +30 -21
- package/build/codegen/bin/cli.js.map +1 -1
- package/build/codegen/bin/cli.test.js +36 -1
- package/build/codegen/bin/cli.test.js.map +1 -1
- package/build/codegen/bin/flag-specs.d.ts +10 -0
- package/build/codegen/bin/flag-specs.js +60 -0
- package/build/codegen/bin/flag-specs.js.map +1 -0
- package/build/codegen/bin/flag-specs.test.d.ts +1 -0
- package/build/codegen/bin/flag-specs.test.js +26 -0
- package/build/codegen/bin/flag-specs.test.js.map +1 -0
- package/build/codegen/collect-models.d.ts +37 -0
- package/build/codegen/collect-models.js +74 -0
- package/build/codegen/collect-models.js.map +1 -0
- package/build/codegen/collect-models.test.d.ts +1 -0
- package/build/codegen/collect-models.test.js +40 -0
- package/build/codegen/collect-models.test.js.map +1 -0
- package/build/codegen/emit-client-runtime.js +1 -0
- package/build/codegen/emit-client-runtime.js.map +1 -1
- package/build/codegen/emit-errors.integration.test.js +22 -0
- package/build/codegen/emit-errors.integration.test.js.map +1 -1
- package/build/codegen/emit-models.d.ts +26 -0
- package/build/codegen/emit-models.js +53 -0
- package/build/codegen/emit-models.js.map +1 -0
- package/build/codegen/emit-models.test.d.ts +1 -0
- package/build/codegen/emit-models.test.js +42 -0
- package/build/codegen/emit-models.test.js.map +1 -0
- package/build/codegen/emit-scope.d.ts +10 -0
- package/build/codegen/emit-scope.js +119 -34
- package/build/codegen/emit-scope.js.map +1 -1
- package/build/codegen/emit-types.d.ts +26 -1
- package/build/codegen/emit-types.js +27 -5
- package/build/codegen/emit-types.js.map +1 -1
- package/build/codegen/index.d.ts +5 -0
- package/build/codegen/index.js +2 -0
- package/build/codegen/index.js.map +1 -1
- package/build/codegen/model-refs.d.ts +27 -0
- package/build/codegen/model-refs.js +49 -0
- package/build/codegen/model-refs.js.map +1 -0
- package/build/codegen/model-refs.test.d.ts +1 -0
- package/build/codegen/model-refs.test.js +33 -0
- package/build/codegen/model-refs.test.js.map +1 -0
- package/build/codegen/pipeline.d.ts +3 -0
- package/build/codegen/pipeline.js +3 -1
- package/build/codegen/pipeline.js.map +1 -1
- package/build/codegen/schema-walk.d.ts +13 -0
- package/build/codegen/schema-walk.js +26 -0
- package/build/codegen/schema-walk.js.map +1 -0
- package/build/codegen/schema-walk.test.d.ts +1 -0
- package/build/codegen/schema-walk.test.js +35 -0
- package/build/codegen/schema-walk.test.js.map +1 -0
- package/build/codegen/targets/_shared/target-run.d.ts +5 -0
- package/build/codegen/targets/ts/run.js +28 -1
- package/build/codegen/targets/ts/run.js.map +1 -1
- package/build/codegen/targets/ts/shared-models.test.d.ts +1 -0
- package/build/codegen/targets/ts/shared-models.test.js +258 -0
- package/build/codegen/targets/ts/shared-models.test.js.map +1 -0
- package/build/doc-envelope.d.ts +13 -0
- package/build/doc-envelope.js +23 -0
- package/build/doc-envelope.js.map +1 -0
- package/build/doc-envelope.test.d.ts +1 -0
- package/build/doc-envelope.test.js +31 -0
- package/build/doc-envelope.test.js.map +1 -0
- package/build/exports.d.ts +2 -0
- package/build/exports.js +1 -0
- package/build/exports.js.map +1 -1
- package/build/implementations/http/error-taxonomy.d.ts +40 -0
- package/build/implementations/http/error-taxonomy.js +57 -5
- package/build/implementations/http/error-taxonomy.js.map +1 -1
- package/build/implementations/http/error-taxonomy.test.js +95 -1
- package/build/implementations/http/error-taxonomy.test.js.map +1 -1
- package/build/implementations/http/hono/handlers/http.js +19 -24
- package/build/implementations/http/hono/handlers/http.js.map +1 -1
- package/build/implementations/http/hono/handlers/http.test.js +64 -1
- package/build/implementations/http/hono/handlers/http.test.js.map +1 -1
- package/docs/client-and-codegen.md +109 -0
- package/docs/core.md +2 -0
- package/docs/handoffs/ajsc-named-type-collision.md +134 -0
- package/docs/handoffs/ajsc-named-type-support.md +181 -0
- package/docs/http-integrations.md +4 -0
- package/docs/superpowers/plans/2026-06-05-dx-feedback-round.md +1292 -0
- package/docs/superpowers/specs/2026-06-05-dx-feedback-round-design.md +285 -0
- package/package.json +2 -2
- package/src/client/call.ts +1 -1
- package/src/client/index.test.ts +98 -0
- package/src/client/index.ts +32 -1
- package/src/client/resolve-options.test.ts +73 -26
- package/src/client/resolve-options.ts +23 -9
- package/src/client/stream.ts +1 -1
- package/src/client/types.ts +34 -3
- package/src/codegen/__fixtures__/make-envelope.ts +89 -0
- package/src/codegen/bin/cli.test.ts +38 -1
- package/src/codegen/bin/cli.ts +33 -22
- package/src/codegen/bin/flag-specs.test.ts +27 -0
- package/src/codegen/bin/flag-specs.ts +69 -0
- package/src/codegen/collect-models.test.ts +46 -0
- package/src/codegen/collect-models.ts +108 -0
- package/src/codegen/emit-client-runtime.ts +1 -0
- package/src/codegen/emit-errors.integration.test.ts +26 -0
- package/src/codegen/emit-models.test.ts +48 -0
- package/src/codegen/emit-models.ts +63 -0
- package/src/codegen/emit-scope.ts +145 -33
- package/src/codegen/emit-types.ts +48 -7
- package/src/codegen/index.ts +7 -0
- package/src/codegen/model-refs.test.ts +37 -0
- package/src/codegen/model-refs.ts +57 -0
- package/src/codegen/pipeline.ts +6 -1
- package/src/codegen/schema-walk.test.ts +37 -0
- package/src/codegen/schema-walk.ts +23 -0
- package/src/codegen/targets/_shared/target-run.ts +5 -0
- package/src/codegen/targets/ts/run.ts +33 -0
- package/src/codegen/targets/ts/shared-models.test.ts +283 -0
- package/src/doc-envelope.test.ts +35 -0
- package/src/doc-envelope.ts +30 -0
- package/src/exports.ts +2 -0
- package/src/implementations/http/error-taxonomy.test.ts +111 -0
- package/src/implementations/http/error-taxonomy.ts +60 -5
- package/src/implementations/http/hono/handlers/http.test.ts +69 -1
- package/src/implementations/http/hono/handlers/http.ts +19 -21
|
@@ -5,10 +5,14 @@ import {
|
|
|
5
5
|
ProcedureYieldValidationError,
|
|
6
6
|
} from '../../errors.js'
|
|
7
7
|
import type { TProcedureRegistration } from '../../index.js'
|
|
8
|
+
import * as AJV from 'ajv'
|
|
8
9
|
import {
|
|
9
10
|
defineErrorTaxonomy,
|
|
10
11
|
resolveErrorResponse,
|
|
11
12
|
defaultErrorTaxonomy,
|
|
13
|
+
taxonomyToErrorDocs,
|
|
14
|
+
defaultErrorSchema,
|
|
15
|
+
defaultErrorBody,
|
|
12
16
|
} from './error-taxonomy.js'
|
|
13
17
|
|
|
14
18
|
class UseCaseError extends Error {
|
|
@@ -436,3 +440,110 @@ describe('resolveErrorResponse', () => {
|
|
|
436
440
|
expect(Object.keys(taxonomy)).toEqual(['B', 'A'])
|
|
437
441
|
})
|
|
438
442
|
})
|
|
443
|
+
|
|
444
|
+
describe('taxonomyToErrorDocs', () => {
|
|
445
|
+
test('synthesizes a { name const, message } schema for class+statusCode-only entries', () => {
|
|
446
|
+
const taxonomy = defineErrorTaxonomy({
|
|
447
|
+
AuthError: { class: AuthError, statusCode: 401 },
|
|
448
|
+
})
|
|
449
|
+
const docs = taxonomyToErrorDocs(taxonomy)
|
|
450
|
+
const auth = docs.find((d) => d.name === 'AuthError')
|
|
451
|
+
expect(auth?.statusCode).toBe(401)
|
|
452
|
+
expect(auth?.schema).toEqual({
|
|
453
|
+
type: 'object',
|
|
454
|
+
properties: {
|
|
455
|
+
name: { type: 'string', const: 'AuthError' },
|
|
456
|
+
message: { type: 'string' },
|
|
457
|
+
},
|
|
458
|
+
required: ['name', 'message'],
|
|
459
|
+
})
|
|
460
|
+
})
|
|
461
|
+
|
|
462
|
+
test('does NOT synthesize a schema when a custom toResponse is present', () => {
|
|
463
|
+
const taxonomy = defineErrorTaxonomy({
|
|
464
|
+
UseCaseError: {
|
|
465
|
+
class: UseCaseError,
|
|
466
|
+
statusCode: 422,
|
|
467
|
+
toResponse: (err) => ({ name: 'UseCaseError', message: err.externalMsg }),
|
|
468
|
+
},
|
|
469
|
+
})
|
|
470
|
+
const docs = taxonomyToErrorDocs(taxonomy)
|
|
471
|
+
const useCase = docs.find((d) => d.name === 'UseCaseError')
|
|
472
|
+
expect(useCase?.schema).toBeUndefined()
|
|
473
|
+
})
|
|
474
|
+
|
|
475
|
+
test('preserves an explicit schema untouched', () => {
|
|
476
|
+
const explicitSchema = {
|
|
477
|
+
type: 'object',
|
|
478
|
+
properties: {
|
|
479
|
+
name: { type: 'string', const: 'UseCaseError' },
|
|
480
|
+
reason: { type: 'string' },
|
|
481
|
+
},
|
|
482
|
+
required: ['name', 'reason'],
|
|
483
|
+
}
|
|
484
|
+
const taxonomy = defineErrorTaxonomy({
|
|
485
|
+
UseCaseError: {
|
|
486
|
+
class: UseCaseError,
|
|
487
|
+
statusCode: 422,
|
|
488
|
+
schema: explicitSchema,
|
|
489
|
+
},
|
|
490
|
+
})
|
|
491
|
+
const docs = taxonomyToErrorDocs(taxonomy)
|
|
492
|
+
const useCase = docs.find((d) => d.name === 'UseCaseError')
|
|
493
|
+
expect(useCase?.schema).toBe(explicitSchema)
|
|
494
|
+
})
|
|
495
|
+
})
|
|
496
|
+
|
|
497
|
+
describe('defaultErrorSchema', () => {
|
|
498
|
+
test('synthesizes the { name, message } envelope for a bare entry', () => {
|
|
499
|
+
expect(defaultErrorSchema('AuthError', { class: AuthError, statusCode: 401 })).toEqual({
|
|
500
|
+
type: 'object',
|
|
501
|
+
properties: {
|
|
502
|
+
name: { type: 'string', const: 'AuthError' },
|
|
503
|
+
message: { type: 'string' },
|
|
504
|
+
},
|
|
505
|
+
required: ['name', 'message'],
|
|
506
|
+
})
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
test('returns undefined when a custom toResponse is present (shape unknown)', () => {
|
|
510
|
+
expect(
|
|
511
|
+
defaultErrorSchema('UseCaseError', {
|
|
512
|
+
class: UseCaseError,
|
|
513
|
+
statusCode: 422,
|
|
514
|
+
toResponse: () => ({ name: 'UseCaseError' }),
|
|
515
|
+
})
|
|
516
|
+
).toBeUndefined()
|
|
517
|
+
})
|
|
518
|
+
|
|
519
|
+
test('returns the explicit schema when one is set', () => {
|
|
520
|
+
const schema = { type: 'object', properties: {} }
|
|
521
|
+
expect(
|
|
522
|
+
defaultErrorSchema('UseCaseError', { class: UseCaseError, statusCode: 422, schema })
|
|
523
|
+
).toBe(schema)
|
|
524
|
+
})
|
|
525
|
+
})
|
|
526
|
+
|
|
527
|
+
// The synthesized schema and the runtime body share one source (defaultErrorBody).
|
|
528
|
+
// This locks the invariant: whatever the default branch serializes must validate
|
|
529
|
+
// against the schema codegen turns into the client error class. If either side
|
|
530
|
+
// changes shape, this fails before consumers see a mismatch.
|
|
531
|
+
describe('defaultErrorBody / defaultErrorSchema invariant', () => {
|
|
532
|
+
const ajv = new AJV.Ajv()
|
|
533
|
+
|
|
534
|
+
test('default body validates against the synthesized schema', () => {
|
|
535
|
+
const schema = defaultErrorSchema('AuthError', { class: AuthError, statusCode: 401 })
|
|
536
|
+
const validate = ajv.compile(schema!)
|
|
537
|
+
|
|
538
|
+
expect(validate(defaultErrorBody('AuthError', new Error('nope')))).toBe(true)
|
|
539
|
+
// A non-Error throw stringifies to a message — still valid.
|
|
540
|
+
expect(validate(defaultErrorBody('AuthError', 'plain string'))).toBe(true)
|
|
541
|
+
// Wrong discriminator name is rejected by the `const` — proves the schema
|
|
542
|
+
// actually constrains the wire shape the client dispatcher keys on.
|
|
543
|
+
expect(validate({ name: 'SomethingElse', message: 'x' })).toBe(false)
|
|
544
|
+
})
|
|
545
|
+
|
|
546
|
+
test('default body carries exactly the schema-described keys', () => {
|
|
547
|
+
expect(Object.keys(defaultErrorBody('X', new Error('m'))).sort()).toEqual(['message', 'name'])
|
|
548
|
+
})
|
|
549
|
+
})
|
|
@@ -204,9 +204,67 @@ export const PROCEDURE_REGISTRATION_ERROR_DOC: ErrorDoc = {
|
|
|
204
204
|
},
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
+
/**
|
|
208
|
+
* The default response body for an entry without a custom `toResponse`:
|
|
209
|
+
* `{ name: <key>, message }`. This is the single source of truth for the default
|
|
210
|
+
* wire shape — `resolveErrorResponse` serializes with it and
|
|
211
|
+
* {@link defaultErrorSchema} describes it. Keeping both derived from one place
|
|
212
|
+
* means the synthesized schema can never drift from what the runtime emits.
|
|
213
|
+
*/
|
|
214
|
+
export function defaultErrorBody(key: string, err: unknown): { name: string; message: string } {
|
|
215
|
+
return {
|
|
216
|
+
name: key,
|
|
217
|
+
message: err instanceof Error ? err.message : String(err),
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Synthesizes the response-body JSON Schema for a taxonomy entry that ships
|
|
223
|
+
* neither an explicit `schema` nor a custom `toResponse`.
|
|
224
|
+
*
|
|
225
|
+
* The common case for `defineErrorTaxonomy` is `{ class, statusCode }` only.
|
|
226
|
+
* For those entries the default `toResponse` (see `resolveErrorResponse`) emits
|
|
227
|
+
* exactly `{ name: <key>, message }`. Without a schema, `taxonomyToErrorDocs`
|
|
228
|
+
* produced a schema-less `ErrorDoc`, and codegen (`emit-errors.ts`) only emits a
|
|
229
|
+
* typed client error class for docs that carry a schema — so these entries
|
|
230
|
+
* silently fell back to the untyped `ClientHttpError`, while framework errors
|
|
231
|
+
* (which ship schemas) worked. That mismatch was confusing.
|
|
232
|
+
*
|
|
233
|
+
* By describing the default envelope here, the entry becomes self-describing and
|
|
234
|
+
* codegen emits a typed client error class with zero ceremony from the consumer.
|
|
235
|
+
*
|
|
236
|
+
* Rules:
|
|
237
|
+
* - Entry has an explicit `schema` → caller keeps it (this is not consulted).
|
|
238
|
+
* - Entry has a custom `toResponse` but no `schema` → returns `undefined`; the
|
|
239
|
+
* body shape is unknown and we never guess it.
|
|
240
|
+
* - Entry has neither → returns the `{ name: const <key>, message }` schema that
|
|
241
|
+
* describes {@link defaultErrorBody} — the exact body the runtime serializes.
|
|
242
|
+
* (An invariant test keeps the two from drifting.)
|
|
243
|
+
*/
|
|
244
|
+
export function defaultErrorSchema(
|
|
245
|
+
key: string,
|
|
246
|
+
entry: ErrorTaxonomyEntry
|
|
247
|
+
): Record<string, unknown> | undefined {
|
|
248
|
+
if (entry.schema) return entry.schema
|
|
249
|
+
if (entry.toResponse) return undefined
|
|
250
|
+
return {
|
|
251
|
+
type: 'object',
|
|
252
|
+
properties: {
|
|
253
|
+
name: { type: 'string', const: key },
|
|
254
|
+
message: { type: 'string' },
|
|
255
|
+
},
|
|
256
|
+
required: ['name', 'message'],
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
207
260
|
/**
|
|
208
261
|
* Converts a taxonomy into {@link ErrorDoc} objects suitable for a DocEnvelope.
|
|
209
262
|
*
|
|
263
|
+
* For entries that supply neither a `schema` nor a custom `toResponse`, the
|
|
264
|
+
* schema of the default `{ name, message }` envelope is synthesized (see
|
|
265
|
+
* {@link defaultErrorSchema}) so client codegen emits a typed error class for
|
|
266
|
+
* them too — matching the behavior of the schema-carrying framework defaults.
|
|
267
|
+
*
|
|
210
268
|
* @internal Used by `DocRegistry` to merge taxonomy entries into the envelope.
|
|
211
269
|
* Consumers should pass their taxonomy directly to `new DocRegistry({ errors: taxonomy })`
|
|
212
270
|
* rather than calling this helper — the constructor handles the conversion.
|
|
@@ -216,7 +274,7 @@ export function taxonomyToErrorDocs(taxonomy: ErrorTaxonomy): ErrorDoc[] {
|
|
|
216
274
|
name: key,
|
|
217
275
|
statusCode: entry.statusCode,
|
|
218
276
|
description: entry.description ?? '',
|
|
219
|
-
schema: entry
|
|
277
|
+
schema: defaultErrorSchema(key, entry),
|
|
220
278
|
}))
|
|
221
279
|
}
|
|
222
280
|
|
|
@@ -314,10 +372,7 @@ export function resolveErrorResponse(params: {
|
|
|
314
372
|
|
|
315
373
|
const rawBody = entry.toResponse
|
|
316
374
|
? entry.toResponse(candidate as any, { key })
|
|
317
|
-
:
|
|
318
|
-
name: key,
|
|
319
|
-
message: candidate instanceof Error ? candidate.message : String(candidate),
|
|
320
|
-
}
|
|
375
|
+
: defaultErrorBody(key, candidate)
|
|
321
376
|
const body = ensureName(rawBody, key)
|
|
322
377
|
|
|
323
378
|
return {
|
|
@@ -120,11 +120,79 @@ describe('installHttpRoute', () => {
|
|
|
120
120
|
path: '/q', method: 'get',
|
|
121
121
|
schema: { req: { query: Type.Any() }, res: { body: Type.Any() } },
|
|
122
122
|
},
|
|
123
|
-
async (_ctx, { query }) =>
|
|
123
|
+
async (_ctx, { query }) => query)
|
|
124
124
|
}, { api: { queryParser } })
|
|
125
125
|
|
|
126
126
|
const res = await app.request('/q?a=1&b=2')
|
|
127
127
|
expect(queryParser).toHaveBeenCalledWith('a=1&b=2')
|
|
128
128
|
expect(await res.json()).toEqual({ parsed: true, raw: 'a=1&b=2' })
|
|
129
129
|
})
|
|
130
|
+
|
|
131
|
+
test('domain object with a top-level "body" field serializes whole (no res.headers)', async () => {
|
|
132
|
+
const { app } = buildApp((P) => {
|
|
133
|
+
P.CreateHttp('GetMessage', {
|
|
134
|
+
path: '/msg', method: 'get',
|
|
135
|
+
schema: { res: { body: Type.Object({ id: Type.String(), body: Type.String() }) } },
|
|
136
|
+
}, async () => ({ id: '1', body: 'hello text' }))
|
|
137
|
+
})
|
|
138
|
+
const res = await app.request('/msg')
|
|
139
|
+
expect(res.status).toBe(200)
|
|
140
|
+
expect(await res.json()).toEqual({ id: '1', body: 'hello text' })
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('envelope unwraps when res.headers is declared', async () => {
|
|
144
|
+
const { app } = buildApp((P) => {
|
|
145
|
+
P.CreateHttp('Enveloped', {
|
|
146
|
+
path: '/env', method: 'get',
|
|
147
|
+
schema: { res: { body: Type.Object({ ok: Type.Boolean() }), headers: Type.Object({}) } },
|
|
148
|
+
}, async () => ({ body: { ok: true }, headers: { 'x-trace': 'z' } }))
|
|
149
|
+
})
|
|
150
|
+
const res = await app.request('/env')
|
|
151
|
+
expect(res.status).toBe(200)
|
|
152
|
+
expect(res.headers.get('x-trace')).toBe('z')
|
|
153
|
+
expect(await res.json()).toEqual({ ok: true })
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
// Headers-only response shape — `res: { headers }` with no `res.body`. The
|
|
157
|
+
// handler returns `{ headers }` (no body key). The response must be bodyless,
|
|
158
|
+
// not an empty `application/json` payload that a client can't parse.
|
|
159
|
+
test('headers-only res schema returns a clean bodyless response', async () => {
|
|
160
|
+
const { app } = buildApp((P) => {
|
|
161
|
+
P.CreateHttp('HeadOnly', {
|
|
162
|
+
path: '/ho', method: 'get',
|
|
163
|
+
schema: { res: { headers: Type.Object({}) } },
|
|
164
|
+
}, async () => ({ headers: { 'x-trace': 'h1' } }))
|
|
165
|
+
})
|
|
166
|
+
const res = await app.request('/ho')
|
|
167
|
+
expect(res.status).toBe(200)
|
|
168
|
+
expect(res.headers.get('x-trace')).toBe('h1')
|
|
169
|
+
expect(res.headers.get('content-type')).toBeNull()
|
|
170
|
+
expect(await res.text()).toBe('')
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
// A void handler on a non-204 status sends a clean bodyless response rather
|
|
174
|
+
// than an empty `application/json` payload — uniform with the headers-only and
|
|
175
|
+
// 204 paths (an undefined body is always bodyless).
|
|
176
|
+
test('void handler on a 200 status returns a clean bodyless response', async () => {
|
|
177
|
+
const { app } = buildApp((P) => {
|
|
178
|
+
P.CreateHttp('Void', { path: '/void', method: 'get', schema: {} }, async () => undefined)
|
|
179
|
+
})
|
|
180
|
+
const res = await app.request('/void')
|
|
181
|
+
expect(res.status).toBe(200)
|
|
182
|
+
expect(res.headers.get('content-type')).toBeNull()
|
|
183
|
+
expect(await res.text()).toBe('')
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('res.headers declared with a 204 status applies headers and no body', async () => {
|
|
187
|
+
const { app } = buildApp((P) => {
|
|
188
|
+
P.CreateHttp('NoContent', {
|
|
189
|
+
path: '/nc', method: 'delete',
|
|
190
|
+
schema: { res: { headers: Type.Object({}) } },
|
|
191
|
+
}, async () => ({ headers: { 'x-trace': 'd1' } }))
|
|
192
|
+
})
|
|
193
|
+
const res = await app.request('/nc', { method: 'DELETE' })
|
|
194
|
+
expect(res.status).toBe(204)
|
|
195
|
+
expect(res.headers.get('x-trace')).toBe('d1')
|
|
196
|
+
expect(await res.text()).toBe('')
|
|
197
|
+
})
|
|
130
198
|
})
|
|
@@ -103,32 +103,30 @@ export function installHttpRoute(params: {
|
|
|
103
103
|
|
|
104
104
|
cfg.api?.onSuccess?.(procedure, c)
|
|
105
105
|
|
|
106
|
-
//
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
106
|
+
// The `{ body, headers }` envelope is OPT-IN via `schema.res.headers`: when
|
|
107
|
+
// the route declares response headers, the handler returns `{ body, headers }`;
|
|
108
|
+
// otherwise its return value IS the body. Gating on the schema (rather than
|
|
109
|
+
// duck-typing the result for a `body`/`headers` key) lets a domain object
|
|
110
|
+
// safely carry its own `body`/`headers` field without being unwrapped.
|
|
111
|
+
// Normalize both shapes to a single `{ body, headers }` so the response
|
|
112
|
+
// decision below is uniform for every legal return shape.
|
|
113
|
+
const hasResHeaders = procedure.config.schema?.res?.headers != null
|
|
114
|
+
const { body, headers } = hasResHeaders
|
|
115
|
+
? ((result ?? {}) as { body?: unknown; headers?: unknown })
|
|
116
|
+
: { body: result, headers: undefined }
|
|
117
|
+
|
|
118
|
+
if (headers && typeof headers === 'object') {
|
|
119
|
+
for (const [k, v] of Object.entries(headers as Record<string, string>)) c.header(k, v)
|
|
114
120
|
}
|
|
115
121
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
headers = (result as any).headers as Record<string, string>
|
|
122
|
-
} else if (result && typeof result === 'object' && 'headers' in result && !('body' in result)) {
|
|
123
|
-
for (const [k, v] of Object.entries((result as any).headers as Record<string, string>)) {
|
|
124
|
-
c.header(k, v)
|
|
125
|
-
}
|
|
122
|
+
// No body to send — a 204 status, or an `undefined` return (a `void`
|
|
123
|
+
// handler, or a headers-only `res: { headers }` envelope). Emit a clean
|
|
124
|
+
// bodyless response instead of `c.json(undefined)`, which would send an
|
|
125
|
+
// empty, unparseable body under an `application/json` content-type.
|
|
126
|
+
if (successStatus === 204 || body === undefined) {
|
|
126
127
|
return c.body(null, successStatus as any)
|
|
127
|
-
} else if (result && typeof result === 'object' && 'body' in result && !('headers' in result)) {
|
|
128
|
-
body = (result as any).body
|
|
129
128
|
}
|
|
130
129
|
|
|
131
|
-
if (headers) for (const [k, v] of Object.entries(headers)) c.header(k, v)
|
|
132
130
|
return c.json(body, successStatus as any)
|
|
133
131
|
} catch (error) {
|
|
134
132
|
return dispatchPreStreamError({
|