ts-procedures 5.9.1 → 5.10.2
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 +1 -1
- package/agent_config/bin/postinstall.mjs +3 -3
- package/agent_config/bin/setup.mjs +22 -11
- package/agent_config/claude-code/agents/ts-procedures-architect.md +46 -101
- package/agent_config/claude-code/skills/{guide → ts-procedures}/SKILL.md +50 -35
- package/agent_config/claude-code/skills/{guide → ts-procedures}/anti-patterns.md +6 -5
- package/agent_config/claude-code/skills/{guide → ts-procedures}/api-reference.md +60 -49
- package/agent_config/claude-code/skills/ts-procedures-review/SKILL.md +48 -0
- package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/SKILL.md +19 -24
- package/agent_config/claude-code/skills/ts-procedures-scaffold/templates/client.md +115 -0
- package/agent_config/lib/install-claude.mjs +35 -87
- package/build/src/client/call.d.ts +14 -0
- package/build/src/client/call.js +47 -0
- package/build/src/client/call.js.map +1 -0
- package/build/src/client/call.test.d.ts +1 -0
- package/build/src/client/call.test.js +124 -0
- package/build/src/client/call.test.js.map +1 -0
- package/build/src/client/errors.d.ts +25 -0
- package/build/src/client/errors.js +33 -0
- package/build/src/client/errors.js.map +1 -0
- package/build/src/client/errors.test.d.ts +1 -0
- package/build/src/client/errors.test.js +41 -0
- package/build/src/client/errors.test.js.map +1 -0
- package/build/src/client/fetch-adapter.d.ts +12 -0
- package/build/src/client/fetch-adapter.js +156 -0
- package/build/src/client/fetch-adapter.js.map +1 -0
- package/build/src/client/fetch-adapter.test.d.ts +1 -0
- package/build/src/client/fetch-adapter.test.js +271 -0
- package/build/src/client/fetch-adapter.test.js.map +1 -0
- package/build/src/client/hooks.d.ts +17 -0
- package/build/src/client/hooks.js +40 -0
- package/build/src/client/hooks.js.map +1 -0
- package/build/src/client/hooks.test.d.ts +1 -0
- package/build/src/client/hooks.test.js +163 -0
- package/build/src/client/hooks.test.js.map +1 -0
- package/build/src/client/index.d.ts +22 -0
- package/build/src/client/index.js +67 -0
- package/build/src/client/index.js.map +1 -0
- package/build/src/client/index.test.d.ts +1 -0
- package/build/src/client/index.test.js +231 -0
- package/build/src/client/index.test.js.map +1 -0
- package/build/src/client/request-builder.d.ts +13 -0
- package/build/src/client/request-builder.js +53 -0
- package/build/src/client/request-builder.js.map +1 -0
- package/build/src/client/request-builder.test.d.ts +1 -0
- package/build/src/client/request-builder.test.js +160 -0
- package/build/src/client/request-builder.test.js.map +1 -0
- package/build/src/client/stream.d.ts +27 -0
- package/build/src/client/stream.js +118 -0
- package/build/src/client/stream.js.map +1 -0
- package/build/src/client/stream.test.d.ts +1 -0
- package/build/src/client/stream.test.js +228 -0
- package/build/src/client/stream.test.js.map +1 -0
- package/build/src/client/types.d.ts +78 -0
- package/build/src/client/types.js +3 -0
- package/build/src/client/types.js.map +1 -0
- package/build/src/codegen/bin/cli.d.ts +45 -0
- package/build/src/codegen/bin/cli.js +246 -0
- package/build/src/codegen/bin/cli.js.map +1 -0
- package/build/src/codegen/bin/cli.test.d.ts +1 -0
- package/build/src/codegen/bin/cli.test.js +220 -0
- package/build/src/codegen/bin/cli.test.js.map +1 -0
- package/build/src/codegen/constants.d.ts +1 -0
- package/build/src/codegen/constants.js +2 -0
- package/build/src/codegen/constants.js.map +1 -0
- package/build/src/codegen/e2e.test.d.ts +1 -0
- package/build/src/codegen/e2e.test.js +464 -0
- package/build/src/codegen/e2e.test.js.map +1 -0
- package/build/src/codegen/emit-client-runtime.d.ts +9 -0
- package/build/src/codegen/emit-client-runtime.js +99 -0
- package/build/src/codegen/emit-client-runtime.js.map +1 -0
- package/build/src/codegen/emit-client-runtime.test.d.ts +1 -0
- package/build/src/codegen/emit-client-runtime.test.js +78 -0
- package/build/src/codegen/emit-client-runtime.test.js.map +1 -0
- package/build/src/codegen/emit-client-types.d.ts +8 -0
- package/build/src/codegen/emit-client-types.js +25 -0
- package/build/src/codegen/emit-client-types.js.map +1 -0
- package/build/src/codegen/emit-client-types.test.d.ts +1 -0
- package/build/src/codegen/emit-client-types.test.js +33 -0
- package/build/src/codegen/emit-client-types.test.js.map +1 -0
- package/build/src/codegen/emit-errors.d.ts +19 -0
- package/build/src/codegen/emit-errors.js +59 -0
- package/build/src/codegen/emit-errors.js.map +1 -0
- package/build/src/codegen/emit-errors.test.d.ts +1 -0
- package/build/src/codegen/emit-errors.test.js +175 -0
- package/build/src/codegen/emit-errors.test.js.map +1 -0
- package/build/src/codegen/emit-index.d.ts +12 -0
- package/build/src/codegen/emit-index.js +41 -0
- package/build/src/codegen/emit-index.js.map +1 -0
- package/build/src/codegen/emit-index.test.d.ts +1 -0
- package/build/src/codegen/emit-index.test.js +106 -0
- package/build/src/codegen/emit-index.test.js.map +1 -0
- package/build/src/codegen/emit-scope.d.ts +15 -0
- package/build/src/codegen/emit-scope.js +299 -0
- package/build/src/codegen/emit-scope.js.map +1 -0
- package/build/src/codegen/emit-scope.test.d.ts +1 -0
- package/build/src/codegen/emit-scope.test.js +559 -0
- package/build/src/codegen/emit-scope.test.js.map +1 -0
- package/build/src/codegen/emit-types.d.ts +43 -0
- package/build/src/codegen/emit-types.js +111 -0
- package/build/src/codegen/emit-types.js.map +1 -0
- package/build/src/codegen/emit-types.test.d.ts +1 -0
- package/build/src/codegen/emit-types.test.js +184 -0
- package/build/src/codegen/emit-types.test.js.map +1 -0
- package/build/src/codegen/group-routes.d.ts +23 -0
- package/build/src/codegen/group-routes.js +46 -0
- package/build/src/codegen/group-routes.js.map +1 -0
- package/build/src/codegen/group-routes.test.d.ts +1 -0
- package/build/src/codegen/group-routes.test.js +131 -0
- package/build/src/codegen/group-routes.test.js.map +1 -0
- package/build/src/codegen/index.d.ts +15 -0
- package/build/src/codegen/index.js +16 -0
- package/build/src/codegen/index.js.map +1 -0
- package/build/src/codegen/naming.d.ts +7 -0
- package/build/src/codegen/naming.js +21 -0
- package/build/src/codegen/naming.js.map +1 -0
- package/build/src/codegen/naming.test.d.ts +1 -0
- package/build/src/codegen/naming.test.js +40 -0
- package/build/src/codegen/naming.test.js.map +1 -0
- package/build/src/codegen/pipeline.d.ts +17 -0
- package/build/src/codegen/pipeline.js +78 -0
- package/build/src/codegen/pipeline.js.map +1 -0
- package/build/src/codegen/pipeline.test.d.ts +1 -0
- package/build/src/codegen/pipeline.test.js +269 -0
- package/build/src/codegen/pipeline.test.js.map +1 -0
- package/build/src/codegen/resolve-envelope.d.ts +7 -0
- package/build/src/codegen/resolve-envelope.js +46 -0
- package/build/src/codegen/resolve-envelope.js.map +1 -0
- package/build/src/codegen/resolve-envelope.test.d.ts +1 -0
- package/build/src/codegen/resolve-envelope.test.js +69 -0
- package/build/src/codegen/resolve-envelope.test.js.map +1 -0
- package/build/src/errors.d.ts +33 -0
- package/build/src/errors.js +91 -0
- package/build/src/errors.js.map +1 -0
- package/build/src/errors.test.d.ts +1 -0
- package/build/src/errors.test.js +122 -0
- package/build/src/errors.test.js.map +1 -0
- package/build/src/exports.d.ts +7 -0
- package/build/src/exports.js +8 -0
- package/build/src/exports.js.map +1 -0
- package/build/src/implementations/http/doc-registry.d.ts +12 -0
- package/build/src/implementations/http/doc-registry.js +114 -0
- package/build/src/implementations/http/doc-registry.js.map +1 -0
- package/build/src/implementations/http/doc-registry.test.d.ts +1 -0
- package/build/src/implementations/http/doc-registry.test.js +347 -0
- package/build/src/implementations/http/doc-registry.test.js.map +1 -0
- package/build/src/implementations/http/express-rpc/index.d.ts +94 -0
- package/build/src/implementations/http/express-rpc/index.js +185 -0
- package/build/src/implementations/http/express-rpc/index.js.map +1 -0
- package/build/src/implementations/http/express-rpc/index.test.d.ts +1 -0
- package/build/src/implementations/http/express-rpc/index.test.js +684 -0
- package/build/src/implementations/http/express-rpc/index.test.js.map +1 -0
- package/build/src/implementations/http/express-rpc/types.d.ts +11 -0
- package/build/src/implementations/http/express-rpc/types.js +2 -0
- package/build/src/implementations/http/express-rpc/types.js.map +1 -0
- package/build/src/implementations/http/hono-api/index.d.ts +102 -0
- package/build/src/implementations/http/hono-api/index.js +341 -0
- package/build/src/implementations/http/hono-api/index.js.map +1 -0
- package/build/src/implementations/http/hono-api/index.test.d.ts +1 -0
- package/build/src/implementations/http/hono-api/index.test.js +992 -0
- package/build/src/implementations/http/hono-api/index.test.js.map +1 -0
- package/build/src/implementations/http/hono-api/types.d.ts +13 -0
- package/build/src/implementations/http/hono-api/types.js +2 -0
- package/build/src/implementations/http/hono-api/types.js.map +1 -0
- package/build/src/implementations/http/hono-rpc/index.d.ts +92 -0
- package/build/src/implementations/http/hono-rpc/index.js +161 -0
- package/build/src/implementations/http/hono-rpc/index.js.map +1 -0
- package/build/src/implementations/http/hono-rpc/index.test.d.ts +1 -0
- package/build/src/implementations/http/hono-rpc/index.test.js +803 -0
- package/build/src/implementations/http/hono-rpc/index.test.js.map +1 -0
- package/build/src/implementations/http/hono-rpc/types.d.ts +11 -0
- package/build/src/implementations/http/hono-rpc/types.js +2 -0
- package/build/src/implementations/http/hono-rpc/types.js.map +1 -0
- package/build/src/implementations/http/hono-stream/index.d.ts +120 -0
- package/build/src/implementations/http/hono-stream/index.js +309 -0
- package/build/src/implementations/http/hono-stream/index.js.map +1 -0
- package/build/src/implementations/http/hono-stream/index.test.d.ts +1 -0
- package/build/src/implementations/http/hono-stream/index.test.js +1356 -0
- package/build/src/implementations/http/hono-stream/index.test.js.map +1 -0
- package/build/src/implementations/http/hono-stream/types.d.ts +15 -0
- package/build/src/implementations/http/hono-stream/types.js +2 -0
- package/build/src/implementations/http/hono-stream/types.js.map +1 -0
- package/build/src/implementations/types.d.ts +142 -0
- package/build/src/implementations/types.js +2 -0
- package/build/src/implementations/types.js.map +1 -0
- package/build/src/index.d.ts +165 -0
- package/build/src/index.js +253 -0
- package/build/src/index.js.map +1 -0
- package/build/src/index.test.d.ts +1 -0
- package/build/src/index.test.js +890 -0
- package/build/src/index.test.js.map +1 -0
- package/build/src/schema/compute-schema.d.ts +35 -0
- package/build/src/schema/compute-schema.js +41 -0
- package/build/src/schema/compute-schema.js.map +1 -0
- package/build/src/schema/compute-schema.test.d.ts +1 -0
- package/build/src/schema/compute-schema.test.js +107 -0
- package/build/src/schema/compute-schema.test.js.map +1 -0
- package/build/src/schema/extract-json-schema.d.ts +2 -0
- package/build/src/schema/extract-json-schema.js +12 -0
- package/build/src/schema/extract-json-schema.js.map +1 -0
- package/build/src/schema/extract-json-schema.test.d.ts +1 -0
- package/build/src/schema/extract-json-schema.test.js +23 -0
- package/build/src/schema/extract-json-schema.test.js.map +1 -0
- package/build/src/schema/parser.d.ts +28 -0
- package/build/src/schema/parser.js +170 -0
- package/build/src/schema/parser.js.map +1 -0
- package/build/src/schema/parser.test.d.ts +1 -0
- package/build/src/schema/parser.test.js +120 -0
- package/build/src/schema/parser.test.js.map +1 -0
- package/build/src/schema/resolve-schema-lib.d.ts +12 -0
- package/build/src/schema/resolve-schema-lib.js +11 -0
- package/build/src/schema/resolve-schema-lib.js.map +1 -0
- package/build/src/schema/resolve-schema-lib.test.d.ts +1 -0
- package/build/src/schema/resolve-schema-lib.test.js +17 -0
- package/build/src/schema/resolve-schema-lib.test.js.map +1 -0
- package/build/src/schema/types.d.ts +8 -0
- package/build/src/schema/types.js +2 -0
- package/build/src/schema/types.js.map +1 -0
- package/build/src/stack-utils.d.ts +25 -0
- package/build/src/stack-utils.js +95 -0
- package/build/src/stack-utils.js.map +1 -0
- package/build/src/stack-utils.test.d.ts +1 -0
- package/build/src/stack-utils.test.js +80 -0
- package/build/src/stack-utils.test.js.map +1 -0
- package/docs/ai-agent-setup.md +7 -6
- package/docs/core.md +5 -9
- package/docs/streaming.md +9 -9
- package/package.json +2 -13
- package/src/client/call.test.ts +162 -0
- package/src/client/errors.test.ts +43 -0
- package/src/client/fetch-adapter.test.ts +340 -0
- package/src/client/hooks.test.ts +191 -0
- package/src/client/index.test.ts +290 -0
- package/src/client/request-builder.test.ts +184 -0
- package/src/client/stream.test.ts +331 -0
- package/src/codegen/bin/cli.test.ts +260 -0
- package/src/codegen/bin/cli.ts +282 -0
- package/src/codegen/constants.ts +1 -0
- package/src/codegen/e2e.test.ts +565 -0
- package/src/codegen/emit-client-runtime.test.ts +93 -0
- package/src/codegen/emit-client-runtime.ts +114 -0
- package/src/codegen/emit-client-types.test.ts +39 -0
- package/src/codegen/emit-client-types.ts +27 -0
- package/src/codegen/emit-errors.test.ts +202 -0
- package/src/codegen/emit-errors.ts +80 -0
- package/src/codegen/emit-index.test.ts +127 -0
- package/src/codegen/emit-index.ts +58 -0
- package/src/codegen/emit-scope.test.ts +624 -0
- package/src/codegen/emit-scope.ts +389 -0
- package/src/codegen/emit-types.test.ts +205 -0
- package/src/codegen/emit-types.ts +158 -0
- package/src/codegen/group-routes.test.ts +159 -0
- package/src/codegen/group-routes.ts +61 -0
- package/src/codegen/index.ts +30 -0
- package/src/codegen/naming.test.ts +50 -0
- package/src/codegen/naming.ts +25 -0
- package/src/codegen/pipeline.test.ts +316 -0
- package/src/codegen/pipeline.ts +108 -0
- package/src/codegen/resolve-envelope.test.ts +76 -0
- package/src/codegen/resolve-envelope.ts +61 -0
- package/src/errors.test.ts +163 -0
- package/src/errors.ts +107 -0
- package/src/exports.ts +7 -0
- package/src/implementations/http/doc-registry.test.ts +415 -0
- package/src/implementations/http/doc-registry.ts +143 -0
- package/src/implementations/http/express-rpc/README.md +6 -6
- package/src/implementations/http/express-rpc/index.test.ts +957 -0
- package/src/implementations/http/express-rpc/index.ts +266 -0
- package/src/implementations/http/express-rpc/types.ts +16 -0
- package/src/implementations/http/hono-api/index.test.ts +1341 -0
- package/src/implementations/http/hono-api/index.ts +463 -0
- package/src/implementations/http/hono-api/types.ts +16 -0
- package/src/implementations/http/hono-rpc/README.md +6 -6
- package/src/implementations/http/hono-rpc/index.test.ts +1075 -0
- package/src/implementations/http/hono-rpc/index.ts +238 -0
- package/src/implementations/http/hono-rpc/types.ts +16 -0
- package/src/implementations/http/hono-stream/README.md +12 -12
- package/src/implementations/http/hono-stream/index.test.ts +1768 -0
- package/src/implementations/http/hono-stream/index.ts +456 -0
- package/src/implementations/http/hono-stream/types.ts +20 -0
- package/src/implementations/types.ts +174 -0
- package/src/index.test.ts +1185 -0
- package/src/index.ts +522 -0
- package/src/schema/compute-schema.test.ts +128 -0
- package/src/schema/compute-schema.ts +88 -0
- package/src/schema/extract-json-schema.test.ts +25 -0
- package/src/schema/extract-json-schema.ts +15 -0
- package/src/schema/parser.test.ts +182 -0
- package/src/schema/parser.ts +215 -0
- package/src/schema/resolve-schema-lib.test.ts +19 -0
- package/src/schema/resolve-schema-lib.ts +29 -0
- package/src/schema/types.ts +20 -0
- package/src/stack-utils.test.ts +94 -0
- package/src/stack-utils.ts +129 -0
- package/agent_config/claude-code/skills/review/SKILL.md +0 -53
- package/docs/superpowers/plans/2026-03-30-client-codegen.md +0 -2833
- package/docs/superpowers/specs/2026-03-30-client-codegen-design.md +0 -632
- /package/agent_config/claude-code/skills/{guide → ts-procedures}/patterns.md +0 -0
- /package/agent_config/claude-code/skills/{review → ts-procedures-review}/checklist.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/express-rpc.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/hono-api.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/hono-rpc.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/hono-stream.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/procedure.md +0 -0
- /package/agent_config/claude-code/skills/{scaffold → ts-procedures-scaffold}/templates/stream-procedure.md +0 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
+
import { runBeforeRequest, runAfterResponse, runOnError } from './hooks.js'
|
|
3
|
+
import type {
|
|
4
|
+
BeforeRequestContext,
|
|
5
|
+
AfterResponseContext,
|
|
6
|
+
ErrorContext,
|
|
7
|
+
ClientHooks,
|
|
8
|
+
} from './types.js'
|
|
9
|
+
|
|
10
|
+
// ── helpers ───────────────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
function makeBeforeCtx(overrides?: Partial<BeforeRequestContext>): BeforeRequestContext {
|
|
13
|
+
return {
|
|
14
|
+
procedureName: 'TestProcedure',
|
|
15
|
+
scope: 'test',
|
|
16
|
+
request: { url: 'https://example.com', method: 'GET' },
|
|
17
|
+
...overrides,
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function makeAfterCtx(overrides?: Partial<AfterResponseContext>): AfterResponseContext {
|
|
22
|
+
return {
|
|
23
|
+
procedureName: 'TestProcedure',
|
|
24
|
+
scope: 'test',
|
|
25
|
+
request: { url: 'https://example.com', method: 'GET' },
|
|
26
|
+
response: { status: 200, headers: {}, body: null },
|
|
27
|
+
...overrides,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function makeErrorCtx(overrides?: Partial<ErrorContext>): ErrorContext {
|
|
32
|
+
return {
|
|
33
|
+
procedureName: 'TestProcedure',
|
|
34
|
+
scope: 'test',
|
|
35
|
+
request: { url: 'https://example.com', method: 'GET' },
|
|
36
|
+
error: new Error('something went wrong'),
|
|
37
|
+
...overrides,
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ── runBeforeRequest ──────────────────────────────────────
|
|
42
|
+
|
|
43
|
+
describe('runBeforeRequest', () => {
|
|
44
|
+
it('runs global hook then local hook in order', async () => {
|
|
45
|
+
const order: string[] = []
|
|
46
|
+
const globalHooks: ClientHooks = {
|
|
47
|
+
onBeforeRequest: (ctx) => { order.push('global'); return ctx },
|
|
48
|
+
}
|
|
49
|
+
const localHooks: ClientHooks = {
|
|
50
|
+
onBeforeRequest: (ctx) => { order.push('local'); return ctx },
|
|
51
|
+
}
|
|
52
|
+
const ctx = makeBeforeCtx()
|
|
53
|
+
await runBeforeRequest(ctx, globalHooks, localHooks)
|
|
54
|
+
expect(order).toEqual(['global', 'local'])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('passes the (possibly mutated) context from global to local', async () => {
|
|
58
|
+
const globalHooks: ClientHooks = {
|
|
59
|
+
onBeforeRequest: (ctx) => ({
|
|
60
|
+
...ctx,
|
|
61
|
+
request: { ...ctx.request, url: 'https://mutated.example.com' },
|
|
62
|
+
}),
|
|
63
|
+
}
|
|
64
|
+
let receivedUrl = ''
|
|
65
|
+
const localHooks: ClientHooks = {
|
|
66
|
+
onBeforeRequest: (ctx) => { receivedUrl = ctx.request.url; return ctx },
|
|
67
|
+
}
|
|
68
|
+
const ctx = makeBeforeCtx()
|
|
69
|
+
await runBeforeRequest(ctx, globalHooks, localHooks)
|
|
70
|
+
expect(receivedUrl).toBe('https://mutated.example.com')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('returns the final mutated context', async () => {
|
|
74
|
+
const globalHooks: ClientHooks = {
|
|
75
|
+
onBeforeRequest: (ctx) => ({ ...ctx, scope: 'mutated-by-global' }),
|
|
76
|
+
}
|
|
77
|
+
const localHooks: ClientHooks = {
|
|
78
|
+
onBeforeRequest: (ctx) => ({ ...ctx, scope: ctx.scope + '-then-local' }),
|
|
79
|
+
}
|
|
80
|
+
const ctx = makeBeforeCtx()
|
|
81
|
+
const result = await runBeforeRequest(ctx, globalHooks, localHooks)
|
|
82
|
+
expect(result.scope).toBe('mutated-by-global-then-local')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('handles async hooks', async () => {
|
|
86
|
+
const order: string[] = []
|
|
87
|
+
const globalHooks: ClientHooks = {
|
|
88
|
+
onBeforeRequest: async (ctx) => { order.push('global'); return ctx },
|
|
89
|
+
}
|
|
90
|
+
const localHooks: ClientHooks = {
|
|
91
|
+
onBeforeRequest: async (ctx) => { order.push('local'); return ctx },
|
|
92
|
+
}
|
|
93
|
+
await runBeforeRequest(makeBeforeCtx(), globalHooks, localHooks)
|
|
94
|
+
expect(order).toEqual(['global', 'local'])
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('skips undefined global hook gracefully', async () => {
|
|
98
|
+
const localHooks: ClientHooks = {
|
|
99
|
+
onBeforeRequest: (ctx) => ({ ...ctx, scope: 'local-only' }),
|
|
100
|
+
}
|
|
101
|
+
const result = await runBeforeRequest(makeBeforeCtx(), {}, localHooks)
|
|
102
|
+
expect(result.scope).toBe('local-only')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it('skips undefined local hook gracefully', async () => {
|
|
106
|
+
const globalHooks: ClientHooks = {
|
|
107
|
+
onBeforeRequest: (ctx) => ({ ...ctx, scope: 'global-only' }),
|
|
108
|
+
}
|
|
109
|
+
const result = await runBeforeRequest(makeBeforeCtx(), globalHooks, undefined)
|
|
110
|
+
expect(result.scope).toBe('global-only')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('skips both hooks gracefully when neither defined', async () => {
|
|
114
|
+
const ctx = makeBeforeCtx()
|
|
115
|
+
const result = await runBeforeRequest(ctx, {}, undefined)
|
|
116
|
+
expect(result).toEqual(ctx)
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
// ── runAfterResponse ──────────────────────────────────────
|
|
121
|
+
|
|
122
|
+
describe('runAfterResponse', () => {
|
|
123
|
+
it('runs global then local hook', async () => {
|
|
124
|
+
const order: string[] = []
|
|
125
|
+
const globalHooks: ClientHooks = { onAfterResponse: () => { order.push('global') } }
|
|
126
|
+
const localHooks: ClientHooks = { onAfterResponse: () => { order.push('local') } }
|
|
127
|
+
await runAfterResponse(makeAfterCtx(), globalHooks, localHooks)
|
|
128
|
+
expect(order).toEqual(['global', 'local'])
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('handles async hooks', async () => {
|
|
132
|
+
const order: string[] = []
|
|
133
|
+
const globalHooks: ClientHooks = {
|
|
134
|
+
onAfterResponse: async () => { order.push('global') },
|
|
135
|
+
}
|
|
136
|
+
const localHooks: ClientHooks = {
|
|
137
|
+
onAfterResponse: async () => { order.push('local') },
|
|
138
|
+
}
|
|
139
|
+
await runAfterResponse(makeAfterCtx(), globalHooks, localHooks)
|
|
140
|
+
expect(order).toEqual(['global', 'local'])
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('skips undefined hooks gracefully', async () => {
|
|
144
|
+
// Should not throw
|
|
145
|
+
await runAfterResponse(makeAfterCtx(), {}, undefined)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('returns void', async () => {
|
|
149
|
+
const result = await runAfterResponse(makeAfterCtx(), {}, undefined)
|
|
150
|
+
expect(result).toBeUndefined()
|
|
151
|
+
})
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
// ── runOnError ────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
describe('runOnError', () => {
|
|
157
|
+
it('runs global then local hook', async () => {
|
|
158
|
+
const order: string[] = []
|
|
159
|
+
const globalHooks: ClientHooks = { onError: () => { order.push('global') } }
|
|
160
|
+
const localHooks: ClientHooks = { onError: () => { order.push('local') } }
|
|
161
|
+
await runOnError(makeErrorCtx(), globalHooks, localHooks)
|
|
162
|
+
expect(order).toEqual(['global', 'local'])
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('handles async hooks', async () => {
|
|
166
|
+
const order: string[] = []
|
|
167
|
+
const globalHooks: ClientHooks = { onError: async () => { order.push('global') } }
|
|
168
|
+
const localHooks: ClientHooks = { onError: async () => { order.push('local') } }
|
|
169
|
+
await runOnError(makeErrorCtx(), globalHooks, localHooks)
|
|
170
|
+
expect(order).toEqual(['global', 'local'])
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('skips undefined hooks gracefully', async () => {
|
|
174
|
+
await runOnError(makeErrorCtx(), {}, undefined)
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
it('returns void', async () => {
|
|
178
|
+
const result = await runOnError(makeErrorCtx(), {}, undefined)
|
|
179
|
+
expect(result).toBeUndefined()
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it('receives the error in context', async () => {
|
|
183
|
+
const receivedErrors: unknown[] = []
|
|
184
|
+
const globalHooks: ClientHooks = {
|
|
185
|
+
onError: (ctx) => { receivedErrors.push(ctx.error) },
|
|
186
|
+
}
|
|
187
|
+
const err = new Error('boom')
|
|
188
|
+
await runOnError(makeErrorCtx({ error: err }), globalHooks, undefined)
|
|
189
|
+
expect(receivedErrors[0]).toBe(err)
|
|
190
|
+
})
|
|
191
|
+
})
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
+
import { createClient } from './index.js'
|
|
3
|
+
import { ClientRequestError } from './errors.js'
|
|
4
|
+
import type {
|
|
5
|
+
ClientAdapter,
|
|
6
|
+
AdapterRequest,
|
|
7
|
+
AdapterResponse,
|
|
8
|
+
AdapterStreamResponse,
|
|
9
|
+
ClientHooks,
|
|
10
|
+
CallDescriptor,
|
|
11
|
+
StreamDescriptor,
|
|
12
|
+
ClientInstance,
|
|
13
|
+
} from './types.js'
|
|
14
|
+
|
|
15
|
+
// ── helpers ───────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
async function* makeAsyncIterable<T>(items: T[]): AsyncIterable<T> {
|
|
18
|
+
for (const item of items) {
|
|
19
|
+
yield item
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function makeAdapter(
|
|
24
|
+
responseBody: unknown = { ok: true },
|
|
25
|
+
streamItems: unknown[] = []
|
|
26
|
+
): ClientAdapter {
|
|
27
|
+
return {
|
|
28
|
+
request: vi.fn(async (_req: AdapterRequest): Promise<AdapterResponse> => ({
|
|
29
|
+
status: 200,
|
|
30
|
+
headers: {},
|
|
31
|
+
body: responseBody,
|
|
32
|
+
})),
|
|
33
|
+
stream: vi.fn(async (_req: AdapterRequest): Promise<AdapterStreamResponse> => ({
|
|
34
|
+
status: 200,
|
|
35
|
+
headers: {},
|
|
36
|
+
body: makeAsyncIterable(streamItems),
|
|
37
|
+
})),
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function makeCallDescriptor(overrides?: Partial<CallDescriptor>): CallDescriptor {
|
|
42
|
+
return {
|
|
43
|
+
name: 'GetUser',
|
|
44
|
+
scope: 'users',
|
|
45
|
+
path: '/users',
|
|
46
|
+
method: 'GET',
|
|
47
|
+
kind: 'rpc',
|
|
48
|
+
params: { id: '1' },
|
|
49
|
+
...overrides,
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function makeStreamDescriptor(overrides?: Partial<StreamDescriptor>): StreamDescriptor {
|
|
54
|
+
return {
|
|
55
|
+
name: 'WatchUsers',
|
|
56
|
+
scope: 'users',
|
|
57
|
+
path: '/stream/users',
|
|
58
|
+
method: 'POST',
|
|
59
|
+
kind: 'stream',
|
|
60
|
+
streamMode: 'sse',
|
|
61
|
+
params: {},
|
|
62
|
+
...overrides,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ── createClient ──────────────────────────────────────────
|
|
67
|
+
|
|
68
|
+
describe('createClient', () => {
|
|
69
|
+
it('creates a client with typed scope callables', () => {
|
|
70
|
+
const adapter = makeAdapter()
|
|
71
|
+
|
|
72
|
+
const client = createClient({
|
|
73
|
+
adapter,
|
|
74
|
+
basePath: 'https://api.example.com',
|
|
75
|
+
scopes: (instance: ClientInstance) => ({
|
|
76
|
+
users: {
|
|
77
|
+
getUser: (id: string) =>
|
|
78
|
+
instance.call<{ id: string; name: string }>(
|
|
79
|
+
makeCallDescriptor({ params: { id } }),
|
|
80
|
+
),
|
|
81
|
+
},
|
|
82
|
+
}),
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
expect(client).toHaveProperty('users')
|
|
86
|
+
expect(client.users).toHaveProperty('getUser')
|
|
87
|
+
expect(typeof client.users.getUser).toBe('function')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('executes procedure calls through the adapter', async () => {
|
|
91
|
+
const adapter = makeAdapter({ id: '1', name: 'Alice' })
|
|
92
|
+
|
|
93
|
+
const client = createClient({
|
|
94
|
+
adapter,
|
|
95
|
+
basePath: 'https://api.example.com',
|
|
96
|
+
scopes: (instance: ClientInstance) => ({
|
|
97
|
+
users: {
|
|
98
|
+
getUser: (id: string) =>
|
|
99
|
+
instance.call<{ id: string; name: string }>(
|
|
100
|
+
makeCallDescriptor({ params: { id } }),
|
|
101
|
+
),
|
|
102
|
+
},
|
|
103
|
+
}),
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
const result = await client.users.getUser('1')
|
|
107
|
+
expect(adapter.request).toHaveBeenCalledOnce()
|
|
108
|
+
expect(result).toEqual({ id: '1', name: 'Alice' })
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('throws ClientRequestError on non-2xx from scope callable', async () => {
|
|
112
|
+
const adapter: ClientAdapter = {
|
|
113
|
+
request: vi.fn(async (): Promise<AdapterResponse> => ({
|
|
114
|
+
status: 404,
|
|
115
|
+
headers: {},
|
|
116
|
+
body: { message: 'Not Found' },
|
|
117
|
+
})),
|
|
118
|
+
stream: vi.fn(async (): Promise<AdapterStreamResponse> => ({
|
|
119
|
+
status: 200,
|
|
120
|
+
headers: {},
|
|
121
|
+
body: makeAsyncIterable([]),
|
|
122
|
+
})),
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const client = createClient({
|
|
126
|
+
adapter,
|
|
127
|
+
basePath: 'https://api.example.com',
|
|
128
|
+
scopes: (instance: ClientInstance) => ({
|
|
129
|
+
users: {
|
|
130
|
+
getUser: (id: string) =>
|
|
131
|
+
instance.call<unknown>(makeCallDescriptor({ params: { id } })),
|
|
132
|
+
},
|
|
133
|
+
}),
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
await expect(client.users.getUser('99')).rejects.toThrow(ClientRequestError)
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('applies global hooks to all calls', async () => {
|
|
140
|
+
const capturedHeaders: Record<string, string>[] = []
|
|
141
|
+
const adapter: ClientAdapter = {
|
|
142
|
+
request: vi.fn(async (req: AdapterRequest): Promise<AdapterResponse> => {
|
|
143
|
+
capturedHeaders.push(req.headers ?? {})
|
|
144
|
+
return { status: 200, headers: {}, body: {} }
|
|
145
|
+
}),
|
|
146
|
+
stream: vi.fn(async (): Promise<AdapterStreamResponse> => ({
|
|
147
|
+
status: 200,
|
|
148
|
+
headers: {},
|
|
149
|
+
body: makeAsyncIterable([]),
|
|
150
|
+
})),
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const globalHooks: ClientHooks = {
|
|
154
|
+
onBeforeRequest: (ctx) => ({
|
|
155
|
+
...ctx,
|
|
156
|
+
request: { ...ctx.request, headers: { 'x-api-key': 'secret' } },
|
|
157
|
+
}),
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const client = createClient({
|
|
161
|
+
adapter,
|
|
162
|
+
basePath: 'https://api.example.com',
|
|
163
|
+
hooks: globalHooks,
|
|
164
|
+
scopes: (instance: ClientInstance) => ({
|
|
165
|
+
users: {
|
|
166
|
+
getUser: () => instance.call<unknown>(makeCallDescriptor()),
|
|
167
|
+
},
|
|
168
|
+
}),
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
await client.users.getUser()
|
|
172
|
+
expect(capturedHeaders[0]?.['x-api-key']).toBe('secret')
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it('streaming works — SSE yield and return via client.stream()', async () => {
|
|
176
|
+
const sseItems = [
|
|
177
|
+
{ data: { n: 1 }, event: 'tick' },
|
|
178
|
+
{ data: { n: 2 }, event: 'tick' },
|
|
179
|
+
{ data: { total: 2 }, event: 'return' },
|
|
180
|
+
]
|
|
181
|
+
const adapter = makeAdapter({}, sseItems)
|
|
182
|
+
|
|
183
|
+
const client = createClient({
|
|
184
|
+
adapter,
|
|
185
|
+
basePath: 'https://api.example.com',
|
|
186
|
+
scopes: (instance: ClientInstance) => ({
|
|
187
|
+
updates: {
|
|
188
|
+
watch: () => instance.stream<{ n: number }, { total: number }>(
|
|
189
|
+
makeStreamDescriptor({ streamMode: 'sse' })
|
|
190
|
+
),
|
|
191
|
+
},
|
|
192
|
+
}),
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
const stream = client.updates.watch()
|
|
196
|
+
|
|
197
|
+
const yielded: { n: number }[] = []
|
|
198
|
+
for await (const item of stream) {
|
|
199
|
+
yielded.push(item)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
expect(yielded).toEqual([{ n: 1 }, { n: 2 }])
|
|
203
|
+
await expect(stream.result).resolves.toEqual({ total: 2 })
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
it('streaming works — text mode yields raw chunks', async () => {
|
|
207
|
+
const chunks = ['line1\n', 'line2\n']
|
|
208
|
+
const adapter = makeAdapter({}, chunks)
|
|
209
|
+
|
|
210
|
+
const client = createClient({
|
|
211
|
+
adapter,
|
|
212
|
+
basePath: 'https://api.example.com',
|
|
213
|
+
scopes: (instance: ClientInstance) => ({
|
|
214
|
+
logs: {
|
|
215
|
+
tail: () => instance.stream<string, void>(
|
|
216
|
+
makeStreamDescriptor({ streamMode: 'text', name: 'TailLogs', scope: 'logs' })
|
|
217
|
+
),
|
|
218
|
+
},
|
|
219
|
+
}),
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
const stream = client.logs.tail()
|
|
223
|
+
const received: string[] = []
|
|
224
|
+
for await (const chunk of stream) {
|
|
225
|
+
received.push(chunk)
|
|
226
|
+
}
|
|
227
|
+
expect(received).toEqual(chunks)
|
|
228
|
+
await expect(stream.result).resolves.toBeUndefined()
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('stream() is synchronous — returns TypedStream immediately (no await needed)', () => {
|
|
232
|
+
const adapter = makeAdapter({}, [])
|
|
233
|
+
|
|
234
|
+
const client = createClient({
|
|
235
|
+
adapter,
|
|
236
|
+
basePath: 'https://api.example.com',
|
|
237
|
+
scopes: (instance: ClientInstance) => ({
|
|
238
|
+
updates: {
|
|
239
|
+
watch: () => instance.stream<string, void>(makeStreamDescriptor()),
|
|
240
|
+
},
|
|
241
|
+
}),
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
// Must be synchronous — no await
|
|
245
|
+
const stream = client.updates.watch()
|
|
246
|
+
expect(typeof stream[Symbol.asyncIterator]).toBe('function')
|
|
247
|
+
expect(stream).toHaveProperty('result')
|
|
248
|
+
expect(stream.result).toBeInstanceOf(Promise)
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
it('applies global hooks to streams', async () => {
|
|
252
|
+
const capturedHeaders: Record<string, string>[] = []
|
|
253
|
+
const adapter: ClientAdapter = {
|
|
254
|
+
request: vi.fn(async (): Promise<never> => { throw new Error('not expected') }),
|
|
255
|
+
stream: vi.fn(async (req: AdapterRequest): Promise<AdapterStreamResponse> => {
|
|
256
|
+
capturedHeaders.push(req.headers ?? {})
|
|
257
|
+
return { status: 200, headers: {}, body: makeAsyncIterable([]) }
|
|
258
|
+
}),
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const globalHooks: ClientHooks = {
|
|
262
|
+
onBeforeRequest: (ctx) => ({
|
|
263
|
+
...ctx,
|
|
264
|
+
request: { ...ctx.request, headers: { 'x-stream-key': 'stream-secret' } },
|
|
265
|
+
}),
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const client = createClient({
|
|
269
|
+
adapter,
|
|
270
|
+
basePath: 'https://api.example.com',
|
|
271
|
+
hooks: globalHooks,
|
|
272
|
+
scopes: (instance: ClientInstance) => ({
|
|
273
|
+
updates: {
|
|
274
|
+
watch: () => instance.stream<string, void>(makeStreamDescriptor()),
|
|
275
|
+
},
|
|
276
|
+
}),
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
const stream = client.updates.watch()
|
|
280
|
+
for await (const _ of stream) { /* drain */ }
|
|
281
|
+
|
|
282
|
+
expect(capturedHeaders[0]?.['x-stream-key']).toBe('stream-secret')
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
it('barrel exports: re-exports ClientRequestError', async () => {
|
|
286
|
+
// This verifies the exports are accessible — the import at the top covers this
|
|
287
|
+
const { ClientRequestError: CRE } = await import('./index.js')
|
|
288
|
+
expect(CRE).toBeDefined()
|
|
289
|
+
})
|
|
290
|
+
})
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { interpolatePath, buildAdapterRequest } from './request-builder.js'
|
|
3
|
+
import { ClientPathParamError } from './errors.js'
|
|
4
|
+
import type { CallDescriptor } from './types.js'
|
|
5
|
+
|
|
6
|
+
// ── interpolatePath ───────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
describe('interpolatePath', () => {
|
|
9
|
+
it('returns path unchanged when no params are present', () => {
|
|
10
|
+
expect(interpolatePath('/users', {}, 'GetUsers')).toBe('/users')
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('replaces a single :param with the provided value', () => {
|
|
14
|
+
expect(interpolatePath('/users/:id', { id: '42' }, 'GetUser')).toBe('/users/42')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('replaces multiple :params in one path', () => {
|
|
18
|
+
expect(
|
|
19
|
+
interpolatePath('/orgs/:orgId/users/:userId', { orgId: 'acme', userId: '7' }, 'GetUser')
|
|
20
|
+
).toBe('/orgs/acme/users/7')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('URI-encodes param values', () => {
|
|
24
|
+
expect(interpolatePath('/users/:name', { name: 'hello world' }, 'GetUser')).toBe(
|
|
25
|
+
'/users/hello%20world'
|
|
26
|
+
)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('throws ClientPathParamError when a required param is missing', () => {
|
|
30
|
+
expect(() => interpolatePath('/users/:id', {}, 'GetUser')).toThrow(ClientPathParamError)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('throws with descriptive message containing the param name and path', () => {
|
|
34
|
+
expect(() => interpolatePath('/users/:id', {}, 'GetUser')).toThrow(/id/)
|
|
35
|
+
expect(() => interpolatePath('/users/:id', {}, 'GetUser')).toThrow(/\/users\/:id/)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('is a no-op when path has no :params', () => {
|
|
39
|
+
expect(interpolatePath('/static/path', {}, 'Foo')).toBe('/static/path')
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
// ── buildAdapterRequest ───────────────────────────────────
|
|
44
|
+
|
|
45
|
+
describe('buildAdapterRequest', () => {
|
|
46
|
+
const base: Omit<CallDescriptor, 'kind' | 'params'> = {
|
|
47
|
+
name: 'GetUser',
|
|
48
|
+
scope: 'users',
|
|
49
|
+
path: '/users/:id',
|
|
50
|
+
method: 'GET',
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
it('RPC: sends flat params as JSON body', () => {
|
|
54
|
+
const descriptor: CallDescriptor = {
|
|
55
|
+
...base,
|
|
56
|
+
path: '/rpc/GetUser',
|
|
57
|
+
kind: 'rpc',
|
|
58
|
+
params: { userId: '42', extra: true },
|
|
59
|
+
}
|
|
60
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
61
|
+
expect(req.url).toBe('https://api.example.com/rpc/GetUser')
|
|
62
|
+
expect(req.method).toBe('GET')
|
|
63
|
+
expect(req.body).toEqual({ userId: '42', extra: true })
|
|
64
|
+
expect(req.headers).toBeUndefined()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('stream: sends flat params as JSON body (same as RPC)', () => {
|
|
68
|
+
const descriptor: CallDescriptor = {
|
|
69
|
+
...base,
|
|
70
|
+
path: '/stream/Updates',
|
|
71
|
+
kind: 'stream',
|
|
72
|
+
params: { filter: 'active' },
|
|
73
|
+
}
|
|
74
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
75
|
+
expect(req.url).toBe('https://api.example.com/stream/Updates')
|
|
76
|
+
expect(req.body).toEqual({ filter: 'active' })
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('API: interpolates pathParams into URL', () => {
|
|
80
|
+
const descriptor: CallDescriptor = {
|
|
81
|
+
...base,
|
|
82
|
+
path: '/users/:id',
|
|
83
|
+
kind: 'api',
|
|
84
|
+
params: { pathParams: { id: '99' } },
|
|
85
|
+
}
|
|
86
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
87
|
+
expect(req.url).toBe('https://api.example.com/users/99')
|
|
88
|
+
expect(req.body).toBeUndefined()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('API: appends query params to the URL', () => {
|
|
92
|
+
const descriptor: CallDescriptor = {
|
|
93
|
+
...base,
|
|
94
|
+
path: '/users',
|
|
95
|
+
kind: 'api',
|
|
96
|
+
params: { query: { role: 'admin', active: 'true' } },
|
|
97
|
+
}
|
|
98
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
99
|
+
expect(req.url).toContain('role=admin')
|
|
100
|
+
expect(req.url).toContain('active=true')
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('API: sets body from body channel', () => {
|
|
104
|
+
const descriptor: CallDescriptor = {
|
|
105
|
+
...base,
|
|
106
|
+
path: '/users',
|
|
107
|
+
method: 'POST',
|
|
108
|
+
kind: 'api',
|
|
109
|
+
params: { body: { name: 'Alice', age: 30 } },
|
|
110
|
+
}
|
|
111
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
112
|
+
expect(req.body).toEqual({ name: 'Alice', age: 30 })
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('API: merges headers channel into request headers', () => {
|
|
116
|
+
const descriptor: CallDescriptor = {
|
|
117
|
+
...base,
|
|
118
|
+
path: '/users',
|
|
119
|
+
kind: 'api',
|
|
120
|
+
params: { headers: { 'x-tenant-id': 'tenant-123' } },
|
|
121
|
+
}
|
|
122
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
123
|
+
expect(req.headers?.['x-tenant-id']).toBe('tenant-123')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it('API GET with query only — no body', () => {
|
|
127
|
+
const descriptor: CallDescriptor = {
|
|
128
|
+
...base,
|
|
129
|
+
path: '/search',
|
|
130
|
+
method: 'GET',
|
|
131
|
+
kind: 'api',
|
|
132
|
+
params: { query: { q: 'typescript' } },
|
|
133
|
+
}
|
|
134
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
135
|
+
expect(req.url).toContain('q=typescript')
|
|
136
|
+
expect(req.body).toBeUndefined()
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('API: combines pathParams, query, body, and headers together', () => {
|
|
140
|
+
const descriptor: CallDescriptor = {
|
|
141
|
+
...base,
|
|
142
|
+
path: '/orgs/:orgId/users/:userId',
|
|
143
|
+
method: 'PUT',
|
|
144
|
+
kind: 'api',
|
|
145
|
+
params: {
|
|
146
|
+
pathParams: { orgId: 'acme', userId: '7' },
|
|
147
|
+
query: { version: '2' },
|
|
148
|
+
body: { name: 'Bob' },
|
|
149
|
+
headers: { 'x-request-id': 'req-xyz' },
|
|
150
|
+
},
|
|
151
|
+
}
|
|
152
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
153
|
+
expect(req.url).toContain('/orgs/acme/users/7')
|
|
154
|
+
expect(req.url).toContain('version=2')
|
|
155
|
+
expect(req.body).toEqual({ name: 'Bob' })
|
|
156
|
+
expect(req.headers?.['x-request-id']).toBe('req-xyz')
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('RPC with keys named "body" and "query" is treated as flat — NOT structured', () => {
|
|
160
|
+
const descriptor: CallDescriptor = {
|
|
161
|
+
...base,
|
|
162
|
+
path: '/rpc/DoThing',
|
|
163
|
+
kind: 'rpc',
|
|
164
|
+
params: { body: 'literal-body-value', query: 'literal-query-value' },
|
|
165
|
+
}
|
|
166
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com')
|
|
167
|
+
// The entire params object goes to body — no query string appended
|
|
168
|
+
expect(req.body).toEqual({ body: 'literal-body-value', query: 'literal-query-value' })
|
|
169
|
+
expect(req.url).toBe('https://api.example.com/rpc/DoThing')
|
|
170
|
+
// Should NOT have a query string derived from params.query
|
|
171
|
+
expect(req.url).not.toContain('?')
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
it('prepends basePath to the URL', () => {
|
|
175
|
+
const descriptor: CallDescriptor = {
|
|
176
|
+
...base,
|
|
177
|
+
path: '/users',
|
|
178
|
+
kind: 'rpc',
|
|
179
|
+
params: {},
|
|
180
|
+
}
|
|
181
|
+
const req = buildAdapterRequest(descriptor, 'https://api.example.com/v1')
|
|
182
|
+
expect(req.url).toBe('https://api.example.com/v1/users')
|
|
183
|
+
})
|
|
184
|
+
})
|