@ts-pf/http 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +61 -4
- package/dist/body-source.d.ts +8 -0
- package/dist/body-source.d.ts.map +1 -0
- package/dist/body-source.js +14 -0
- package/dist/body-source.js.map +1 -0
- package/dist/codec.d.ts +5 -5
- package/dist/codec.d.ts.map +1 -1
- package/dist/codec.js +9 -0
- package/dist/codec.js.map +1 -1
- package/dist/content-type.d.ts +12 -0
- package/dist/content-type.d.ts.map +1 -0
- package/dist/content-type.js +20 -0
- package/dist/content-type.js.map +1 -0
- package/dist/http-status.d.ts.map +1 -1
- package/dist/http-status.js +2 -4
- package/dist/http-status.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/rpc.d.ts +20 -0
- package/dist/rpc.d.ts.map +1 -1
- package/package.json +5 -2
- package/skills/ts-pf-http/SKILL.md +8 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ts-pf contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,9 +1,66 @@
|
|
|
1
1
|
# @ts-pf/http
|
|
2
2
|
|
|
3
|
-
HTTP wire helpers for ts-pf: `JSONCodec`, `RpcCodec
|
|
3
|
+
HTTP wire helpers for [ts-pf](https://github.com/crobinson42/ts-pf#readme): `JSONCodec`, the `RpcCodec` interface, `PROTOCOL_HEADER`, `joinProcedurePath` / `parseProcedurePath`, `httpStatus`, and the content-type / body-source helpers the wrapping codecs share. `FetchHandler` (`@ts-pf/server-http`) and `FetchLink` (`@ts-pf/client-http`) are built on these; use them directly for a raw `fetch` call, a custom codec, or your own adapter. Does **not** include `FetchHandler` or `FetchLink`.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Install
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```sh
|
|
8
|
+
npm i @ts-pf/contract @ts-pf/server @ts-pf/server-http @ts-pf/client @ts-pf/client-http zod
|
|
9
|
+
```
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
`@ts-pf/http` comes with `@ts-pf/server-http` / `@ts-pf/client-http`; install it directly when you import from it.
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { httpStatus, JSONCodec, joinProcedurePath, PROTOCOL_HEADER } from '@ts-pf/http'
|
|
17
|
+
import { PFError, PROTOCOL_VERSION } from '@ts-pf/protocol'
|
|
18
|
+
|
|
19
|
+
// Call a ts-pf endpoint with plain fetch — what curl or a non-TS client sends.
|
|
20
|
+
const url = joinProcedurePath('/rpc', ['planet', 'find']) // '/rpc/planet/find'
|
|
21
|
+
const res = await fetch(url, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'content-type': 'application/json', [PROTOCOL_HEADER]: PROTOCOL_VERSION },
|
|
24
|
+
body: JSON.stringify({ input: { id: 1 } }),
|
|
25
|
+
})
|
|
26
|
+
// res.headers.get(PROTOCOL_HEADER) === PROTOCOL_VERSION on every RPC response
|
|
27
|
+
const envelope = await res.json()
|
|
28
|
+
// { ok: true, output } | { ok: false, error: { code, message, data? } }
|
|
29
|
+
|
|
30
|
+
// What FetchHandler does with a thrown PFError: body from the codec, status
|
|
31
|
+
// from the table (VALIDATION 422, NOT_FOUND 404, INTERNAL 500, …).
|
|
32
|
+
const codec = new JSONCodec() // the default RpcCodec; FetchHandler / FetchLink take { codec }
|
|
33
|
+
httpStatus(new PFError({ code: 'NOT_FOUND', status: 404 })) // 404
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
A custom body encoding implements `RpcCodec` (`encode` returns `{ contentType, body }`; `decode` takes `{ contentType, text(), formData(), body() }`) and goes on `FetchHandler` / `FetchLink` via `{ codec }`. First-party codecs: `@ts-pf/file`, `@ts-pf/stream`, `@ts-pf/sse`. The message-transport analog is `@ts-pf/message`.
|
|
37
|
+
|
|
38
|
+
### Wrapping codecs
|
|
39
|
+
|
|
40
|
+
`MultipartCodec`, `StreamCodec`, and `SseCodec` embed one JSON envelope per multipart `rpc` part, JSONL line, or SSE event, so their `inner` codec must produce text. That role is `EnvelopeTextCodec`: an `RpcCodec` whose encoded bodies are `RpcEncodedText` (`body: string`). `JSONCodec` implements it; handlers and links still accept any `RpcCodec`. The helpers they share are exported for your own wrapper:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { type EnvelopeTextCodec, isJsonContentType, JSONCodec, jsonBodySource, mimeType } from '@ts-pf/http'
|
|
44
|
+
|
|
45
|
+
mimeType('application/json; charset=utf-8') // 'application/json' (lower-cased, no parameters; '' when missing)
|
|
46
|
+
isJsonContentType(null) // true — a missing Content-Type is treated as JSON, like JSONCodec does
|
|
47
|
+
const inner: EnvelopeTextCodec = new JSONCodec()
|
|
48
|
+
const line = (await inner.encodeSuccess({ id: 1 })).body // a string, by type
|
|
49
|
+
await inner.decodeResponse(jsonBodySource(line)) // { ok: true, output: { id: 1 } }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
`jsonBodySource(text)` is an `RpcBodySource` over one in-memory envelope (`contentType: 'application/json'`, empty `formData()`, `body()` null).
|
|
53
|
+
|
|
54
|
+
Wire spec: [`PROTOCOL.md`](https://github.com/crobinson42/ts-pf/blob/main/packages/protocol/PROTOCOL.md). Happy path: [root README](https://github.com/crobinson42/ts-pf#readme).
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
- `JSONCodec`, `RpcCodec`, `RpcEncodedBody`, `RpcBodySource`
|
|
59
|
+
- `EnvelopeTextCodec`, `RpcEncodedText` (the `inner` role of `MultipartCodec` / `StreamCodec` / `SseCodec`)
|
|
60
|
+
- `mimeType`, `isJsonContentType`, `jsonBodySource`
|
|
61
|
+
- `PROTOCOL_HEADER`, `joinProcedurePath`, `parseProcedurePath`
|
|
62
|
+
- `httpStatus`, `PROTOCOL_HTTP_STATUS`
|
|
63
|
+
|
|
64
|
+
## Agent skills
|
|
65
|
+
|
|
66
|
+
This package ships [`ts-pf-http`](https://github.com/crobinson42/ts-pf/blob/main/packages/http/skills/ts-pf-http/SKILL.md). Sync with `npx skills experimental_sync -y`.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RpcBodySource } from './rpc.js';
|
|
2
|
+
/**
|
|
3
|
+
* An `RpcBodySource` over one JSON envelope that is already in memory, for
|
|
4
|
+
* handing a JSONL line, an SSE `data:` payload, or a multipart `rpc` part to
|
|
5
|
+
* an inner `EnvelopeTextCodec`. `formData()` is empty and `body()` is `null`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function jsonBodySource(text: string): RpcBodySource;
|
|
8
|
+
//# sourceMappingURL=body-source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-source.d.ts","sourceRoot":"","sources":["../src/body-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAE7C;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,CAO1D"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An `RpcBodySource` over one JSON envelope that is already in memory, for
|
|
3
|
+
* handing a JSONL line, an SSE `data:` payload, or a multipart `rpc` part to
|
|
4
|
+
* an inner `EnvelopeTextCodec`. `formData()` is empty and `body()` is `null`.
|
|
5
|
+
*/
|
|
6
|
+
export function jsonBodySource(text) {
|
|
7
|
+
return {
|
|
8
|
+
contentType: 'application/json',
|
|
9
|
+
text: async () => text,
|
|
10
|
+
formData: async () => new FormData(),
|
|
11
|
+
body: () => null,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=body-source.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-source.js","sourceRoot":"","sources":["../src/body-source.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO;QACL,WAAW,EAAE,kBAAkB;QAC/B,IAAI,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI;QACtB,QAAQ,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI,QAAQ,EAAE;QACpC,IAAI,EAAE,GAAG,EAAE,CAAC,IAAI;KACjB,CAAA;AACH,CAAC"}
|
package/dist/codec.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { type RpcRequest, type RpcResponse } from '@ts-pf/protocol';
|
|
2
|
-
import type {
|
|
3
|
-
export declare class JSONCodec implements
|
|
4
|
-
encodeRequest(req: RpcRequest):
|
|
2
|
+
import type { EnvelopeTextCodec, RpcBodySource, RpcEncodedText } from './rpc.js';
|
|
3
|
+
export declare class JSONCodec implements EnvelopeTextCodec {
|
|
4
|
+
encodeRequest(req: RpcRequest): RpcEncodedText;
|
|
5
5
|
decodeRequest(source: RpcBodySource): Promise<RpcRequest>;
|
|
6
|
-
encodeSuccess<T>(output: T):
|
|
6
|
+
encodeSuccess<T>(output: T): RpcEncodedText;
|
|
7
7
|
encodeFailure(error: {
|
|
8
8
|
code: string;
|
|
9
9
|
message: string;
|
|
10
10
|
data?: unknown;
|
|
11
|
-
}):
|
|
11
|
+
}): RpcEncodedText;
|
|
12
12
|
decodeResponse<T = unknown>(source: RpcBodySource): Promise<RpcResponse<T>>;
|
|
13
13
|
}
|
|
14
14
|
//# sourceMappingURL=codec.d.ts.map
|
package/dist/codec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC5E,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC5E,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AAkBhF,qBAAa,SAAU,YAAW,iBAAiB;IACjD,aAAa,CAAC,GAAG,EAAE,UAAU,GAAG,cAAc;IAOxC,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,UAAU,CAAC;IA8B/D,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,cAAc;IAI3C,aAAa,CAAC,KAAK,EAAE;QACnB,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,IAAI,CAAC,EAAE,OAAO,CAAA;KACf,GAAG,cAAc;IAWZ,cAAc,CAAC,CAAC,GAAG,OAAO,EAC9B,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;CAiC3B"}
|
package/dist/codec.js
CHANGED
|
@@ -33,6 +33,15 @@ export class JSONCodec {
|
|
|
33
33
|
message: 'Request body must be a JSON object',
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
|
+
if (!('input' in parsed) && Object.keys(parsed).length > 0) {
|
|
37
|
+
// `{"name":"x"}` instead of `{"input":{"name":"x"}}` is an invalid
|
|
38
|
+
// envelope, not an undefined input. `{}` and `{"input":null}` are fine.
|
|
39
|
+
throw new PFError({
|
|
40
|
+
code: 'BAD_REQUEST',
|
|
41
|
+
status: 400,
|
|
42
|
+
message: 'Request body must be {"input": ...}',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
36
45
|
const input = parsed.input;
|
|
37
46
|
if (input === undefined || input === null) {
|
|
38
47
|
return { input: undefined };
|
package/dist/codec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../src/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAqC,MAAM,iBAAiB,CAAA;AAG5E,SAAS,SAAS,CAAC,IAAY;IAC7B,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAY,CAAA;IACpC,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,OAAO,CAAC;YAChB,IAAI,EAAE,aAAa;YACnB,MAAM,EAAE,GAAG;YACX,OAAO,EAAE,cAAc;SACxB,CAAC,CAAA;IACJ,CAAC;AACH,CAAC;AAED,SAAS,QAAQ,CAAC,IAAY;IAC5B,OAAO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,OAAO,MAAM,KAAK,QAAQ;YAC1B,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EACrB,CAAC;YACD,MAAM,IAAI,OAAO,CAAC;gBAChB,IAAI,EAAE,aAAa;gBACnB,MAAM,EAAE,GAAG;gBACX,OAAO,EAAE,qCAAqC;aAC/C,CAAC,CAAA;QACJ,CAAC;QACD,MAAM,KAAK,GAAG,MAIb,CAAA;QACD,IAAI,KAAK,CAAC,EAAE,KAAK,IAAI,EAAE,CAAC;YACtB,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,MAAW,EAAE,CAAA;QAChD,CAAC;QACD,IAAI,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,KAAK,CAAC,KAAK,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YACzE,OAAO;gBACL,EAAE,EAAE,KAAK;gBACT,KAAK,EAAE,KAAK,CAAC,KAA0D;aACxE,CAAA;QACH,CAAC;QACD,MAAM,IAAI,OAAO,CAAC;YAChB,IAAI,EAAE,aAAa;YACnB,MAAM,EAAE,GAAG;YACX,OAAO,EAAE,sBAAsB;SAChC,CAAC,CAAA;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The media type of a `Content-Type` header value, lower-cased and without
|
|
3
|
+
* parameters: `'application/json; charset=utf-8'` → `'application/json'`.
|
|
4
|
+
* A missing or empty header gives `''`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function mimeType(contentType: string | null | undefined): string;
|
|
7
|
+
/**
|
|
8
|
+
* True for `application/json` and for a missing header (a bare JSON body
|
|
9
|
+
* with no `Content-Type` is still treated as JSON, as `JSONCodec` does).
|
|
10
|
+
*/
|
|
11
|
+
export declare function isJsonContentType(contentType: string | null | undefined): boolean;
|
|
12
|
+
//# sourceMappingURL=content-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content-type.d.ts","sourceRoot":"","sources":["../src/content-type.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAKvE;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GACrC,OAAO,CAGT"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The media type of a `Content-Type` header value, lower-cased and without
|
|
3
|
+
* parameters: `'application/json; charset=utf-8'` → `'application/json'`.
|
|
4
|
+
* A missing or empty header gives `''`.
|
|
5
|
+
*/
|
|
6
|
+
export function mimeType(contentType) {
|
|
7
|
+
if (contentType == null || contentType === '') {
|
|
8
|
+
return '';
|
|
9
|
+
}
|
|
10
|
+
return contentType.split(';')[0]?.trim().toLowerCase() ?? '';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* True for `application/json` and for a missing header (a bare JSON body
|
|
14
|
+
* with no `Content-Type` is still treated as JSON, as `JSONCodec` does).
|
|
15
|
+
*/
|
|
16
|
+
export function isJsonContentType(contentType) {
|
|
17
|
+
const type = mimeType(contentType);
|
|
18
|
+
return type === '' || type === 'application/json';
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=content-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content-type.js","sourceRoot":"","sources":["../src/content-type.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,WAAsC;IAC7D,IAAI,WAAW,IAAI,IAAI,IAAI,WAAW,KAAK,EAAE,EAAE,CAAC;QAC9C,OAAO,EAAE,CAAA;IACX,CAAC;IACD,OAAO,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,IAAI,EAAE,CAAA;AAC9D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAC/B,WAAsC;IAEtC,MAAM,IAAI,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAA;IAClC,OAAO,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,kBAAkB,CAAA;AACnD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-status.d.ts","sourceRoot":"","sources":["../src/http-status.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAE9C,eAAO,MAAM,oBAAoB;;;;;;;CAOvB,CAAA;
|
|
1
|
+
{"version":3,"file":"http-status.d.ts","sourceRoot":"","sources":["../src/http-status.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAE9C,eAAO,MAAM,oBAAoB;;;;;;;CAOvB,CAAA;AAIV,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAKjD"}
|
package/dist/http-status.js
CHANGED
|
@@ -6,13 +6,11 @@ export const PROTOCOL_HTTP_STATUS = {
|
|
|
6
6
|
PAYLOAD_TOO_LARGE: 413,
|
|
7
7
|
INTERNAL: 500,
|
|
8
8
|
};
|
|
9
|
+
const DEFAULT_HTTP_STATUS = 400;
|
|
9
10
|
export function httpStatus(error) {
|
|
10
|
-
if (error.local === true) {
|
|
11
|
-
return 0;
|
|
12
|
-
}
|
|
13
11
|
if (Object.hasOwn(PROTOCOL_HTTP_STATUS, error.code)) {
|
|
14
12
|
return PROTOCOL_HTTP_STATUS[error.code];
|
|
15
13
|
}
|
|
16
|
-
return error.status;
|
|
14
|
+
return error.status ?? DEFAULT_HTTP_STATUS;
|
|
17
15
|
}
|
|
18
16
|
//# sourceMappingURL=http-status.js.map
|
package/dist/http-status.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-status.js","sourceRoot":"","sources":["../src/http-status.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,WAAW,EAAE,GAAG;IAChB,UAAU,EAAE,GAAG;IACf,SAAS,EAAE,GAAG;IACd,kBAAkB,EAAE,GAAG;IACvB,iBAAiB,EAAE,GAAG;IACtB,QAAQ,EAAE,GAAG;CACL,CAAA;AAEV,MAAM,
|
|
1
|
+
{"version":3,"file":"http-status.js","sourceRoot":"","sources":["../src/http-status.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,WAAW,EAAE,GAAG;IAChB,UAAU,EAAE,GAAG;IACf,SAAS,EAAE,GAAG;IACd,kBAAkB,EAAE,GAAG;IACvB,iBAAiB,EAAE,GAAG;IACtB,QAAQ,EAAE,GAAG;CACL,CAAA;AAEV,MAAM,mBAAmB,GAAG,GAAG,CAAA;AAE/B,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,IAAI,MAAM,CAAC,MAAM,CAAC,oBAAoB,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QACpD,OAAO,oBAAoB,CAAC,KAAK,CAAC,IAAyC,CAAC,CAAA;IAC9E,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,IAAI,mBAAmB,CAAA;AAC5C,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
export { jsonBodySource } from './body-source.js';
|
|
1
2
|
export { JSONCodec } from './codec.js';
|
|
3
|
+
export { isJsonContentType, mimeType } from './content-type.js';
|
|
2
4
|
export { httpStatus, PROTOCOL_HTTP_STATUS } from './http-status.js';
|
|
3
5
|
export { joinProcedurePath, parseProcedurePath } from './path.js';
|
|
4
|
-
export { PROTOCOL_HEADER, type RpcBodySource, type RpcCodec, type RpcEncodedBody, } from './rpc.js';
|
|
6
|
+
export { type EnvelopeTextCodec, PROTOCOL_HEADER, type RpcBodySource, type RpcCodec, type RpcEncodedBody, type RpcEncodedText, } from './rpc.js';
|
|
5
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACnE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AACjE,OAAO,EACL,eAAe,EACf,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,cAAc,GACpB,MAAM,UAAU,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC/D,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACnE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AACjE,OAAO,EACL,KAAK,iBAAiB,EACtB,eAAe,EACf,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,cAAc,EACnB,KAAK,cAAc,GACpB,MAAM,UAAU,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
export { jsonBodySource } from './body-source.js';
|
|
1
2
|
export { JSONCodec } from './codec.js';
|
|
3
|
+
export { isJsonContentType, mimeType } from './content-type.js';
|
|
2
4
|
export { httpStatus, PROTOCOL_HTTP_STATUS } from './http-status.js';
|
|
3
5
|
export { joinProcedurePath, parseProcedurePath } from './path.js';
|
|
4
6
|
export { PROTOCOL_HEADER, } from './rpc.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACnE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AACjE,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC/D,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACnE,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AACjE,OAAO,EAEL,eAAe,GAKhB,MAAM,UAAU,CAAA"}
|
package/dist/rpc.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ export interface RpcEncodedBody {
|
|
|
4
4
|
readonly contentType: string;
|
|
5
5
|
readonly body: string | Blob | FormData | ReadableStream<Uint8Array> | null;
|
|
6
6
|
}
|
|
7
|
+
/** An `RpcEncodedBody` whose body is text: one envelope that fits in a string. */
|
|
8
|
+
export interface RpcEncodedText extends RpcEncodedBody {
|
|
9
|
+
readonly body: string;
|
|
10
|
+
}
|
|
7
11
|
export interface RpcBodySource {
|
|
8
12
|
readonly contentType: string | null;
|
|
9
13
|
text(): Promise<string>;
|
|
@@ -21,4 +25,20 @@ export interface RpcCodec {
|
|
|
21
25
|
}): RpcEncodedBody | Promise<RpcEncodedBody>;
|
|
22
26
|
decodeResponse<T = unknown>(source: RpcBodySource): RpcResponse<T> | Promise<RpcResponse<T>>;
|
|
23
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* An `RpcCodec` whose encoded bodies are strings. This is the `inner` role of
|
|
30
|
+
* the wrapping codecs (`MultipartCodec`, `StreamCodec`, `SseCodec`): they
|
|
31
|
+
* embed one envelope per multipart part, JSONL line, or SSE event, so the
|
|
32
|
+
* inner codec must give them text, not a `Blob` or a stream. `JSONCodec`
|
|
33
|
+
* implements it. Handlers and links still take any `RpcCodec`.
|
|
34
|
+
*/
|
|
35
|
+
export interface EnvelopeTextCodec extends RpcCodec {
|
|
36
|
+
encodeRequest(req: RpcRequest): RpcEncodedText | Promise<RpcEncodedText>;
|
|
37
|
+
encodeSuccess<T>(output: T): RpcEncodedText | Promise<RpcEncodedText>;
|
|
38
|
+
encodeFailure(error: {
|
|
39
|
+
code: string;
|
|
40
|
+
message: string;
|
|
41
|
+
data?: unknown;
|
|
42
|
+
}): RpcEncodedText | Promise<RpcEncodedText>;
|
|
43
|
+
}
|
|
24
44
|
//# sourceMappingURL=rpc.d.ts.map
|
package/dist/rpc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../src/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAE9D,eAAO,MAAM,eAAe,qBAAqB,CAAA;AAEjD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,QAAQ,GAAG,cAAc,CAAC,UAAU,CAAC,GAAG,IAAI,CAAA;CAC5E;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IACnC,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAA;IACvB,QAAQ,IAAI,OAAO,CAAC,QAAQ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7C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-pf/http",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "HTTP wire helpers: JSONCodec, RpcCodec, PROTOCOL_HEADER, path helpers, httpStatus",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ts-pf",
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
"json"
|
|
17
17
|
],
|
|
18
18
|
"license": "MIT",
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22"
|
|
21
|
+
},
|
|
19
22
|
"repository": {
|
|
20
23
|
"type": "git",
|
|
21
24
|
"url": "git+https://github.com/crobinson42/ts-pf.git",
|
|
@@ -47,6 +50,6 @@
|
|
|
47
50
|
"lint": "biome check src tests"
|
|
48
51
|
},
|
|
49
52
|
"dependencies": {
|
|
50
|
-
"@ts-pf/protocol": "0.1
|
|
53
|
+
"@ts-pf/protocol": "0.2.1"
|
|
51
54
|
}
|
|
52
55
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: ts-pf-http
|
|
3
|
-
description: Use when writing or configuring a ts-pf HTTP codec — JSONCodec, RpcCodec, PROTOCOL_HEADER, joinProcedurePath, httpStatus. Triggers: @ts-pf/http, JSONCodec, RpcCodec, PROTOCOL_HTTP_STATUS.
|
|
3
|
+
description: "Use when writing or configuring a ts-pf HTTP codec — JSONCodec, RpcCodec, EnvelopeTextCodec, PROTOCOL_HEADER, joinProcedurePath, httpStatus. Triggers: @ts-pf/http, JSONCodec, RpcCodec, EnvelopeTextCodec, PROTOCOL_HTTP_STATUS."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# @ts-pf/http
|
|
@@ -18,15 +18,19 @@ import { JSONCodec, PROTOCOL_HEADER, httpStatus, joinProcedurePath } from '@ts-p
|
|
|
18
18
|
|
|
19
19
|
const codec = new JSONCodec()
|
|
20
20
|
joinProcedurePath('/rpc', ['planet', 'find']) // '/rpc/planet/find'
|
|
21
|
-
httpStatus(error) // Response.status from PFError
|
|
21
|
+
httpStatus(error) // Response.status from PFError: protocol table, else error.status ?? 400
|
|
22
22
|
// header name: PROTOCOL_HEADER ('x-ts-pf-protocol')
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
`JSONCodec` is the default body. Encode returns `{ contentType, body }`. Decode takes `{ contentType, text(), formData(), body() }`.
|
|
25
|
+
`JSONCodec` is the default body. Encode returns `{ contentType, body }`. Decode takes `{ contentType, text(), formData(), body() }`. `decodeRequest` throws `BAD_REQUEST` for invalid JSON, a non-object body, or an envelope object with keys but no `input` key (`{"id":1}` instead of `{"input":{"id":1}}`); `{}` and `{"input":null}` decode to `input: undefined`. Wrapping codecs (`MultipartCodec`, `StreamCodec`, `SseCodec`) inherit these checks.
|
|
26
|
+
|
|
27
|
+
Those wrappers take `inner?: EnvelopeTextCodec` — an `RpcCodec` whose encoded bodies are strings (`RpcEncodedText`), because they embed one envelope per part / line / event. `JSONCodec` implements it; `FetchHandler` / `FetchLink` still take any `RpcCodec`. Building your own wrapper: `mimeType(contentType)` (media type, lower-cased, no parameters), `isJsonContentType(contentType)` (`application/json` or no header), `jsonBodySource(text)` (an `RpcBodySource` over one in-memory envelope).
|
|
26
28
|
|
|
27
29
|
## API
|
|
28
30
|
|
|
29
31
|
- `JSONCodec`, `RpcCodec`, `RpcEncodedBody`, `RpcBodySource`
|
|
32
|
+
- `EnvelopeTextCodec`, `RpcEncodedText`
|
|
33
|
+
- `mimeType`, `isJsonContentType`, `jsonBodySource`
|
|
30
34
|
- `PROTOCOL_HEADER`, `joinProcedurePath`, `parseProcedurePath`
|
|
31
35
|
- `httpStatus`, `PROTOCOL_HTTP_STATUS`
|
|
32
36
|
|
|
@@ -40,3 +44,4 @@ httpStatus(error) // Response.status from PFError
|
|
|
40
44
|
- `FetchHandler` or `FetchLink` here.
|
|
41
45
|
- Put these exports on `@ts-pf/protocol`.
|
|
42
46
|
- Use this as a WebSocket / stdio framer — that is `ts-pf-message`.
|
|
47
|
+
- Pass a `Blob` / stream-bodied codec as a wrapper's `inner` — it must be an `EnvelopeTextCodec`.
|