@ts-pf/http 0.7.0 → 1.0.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/README.md +9 -9
- package/dist/codec.d.ts +1 -1
- package/dist/codec.js +1 -1
- package/dist/content-type.d.ts +1 -1
- package/dist/content-type.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/rpc.d.ts +1 -1
- package/package.json +3 -3
- package/skills/ts-pf-http/SKILL.md +8 -8
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @ts-pf/http
|
|
2
2
|
|
|
3
|
-
HTTP wire helpers for [ts-pf](https://github.com/crobinson42/ts-pf#readme): `
|
|
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
|
|
|
@@ -13,7 +13,7 @@ npm i @ts-pf/contract @ts-pf/server @ts-pf/server-http @ts-pf/client @ts-pf/clie
|
|
|
13
13
|
## Use
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
|
-
import { httpStatus,
|
|
16
|
+
import { httpStatus, JsonCodec, joinProcedurePath, PROTOCOL_HEADER } from '@ts-pf/http'
|
|
17
17
|
import { PFError, PROTOCOL_VERSION } from '@ts-pf/protocol'
|
|
18
18
|
|
|
19
19
|
// Call a ts-pf endpoint with plain fetch — what curl or a non-TS client sends.
|
|
@@ -29,22 +29,22 @@ const envelope = await res.json()
|
|
|
29
29
|
|
|
30
30
|
// What FetchHandler does with a thrown PFError: body from the codec, status
|
|
31
31
|
// from the table (VALIDATION 422, NOT_FOUND 404, INTERNAL 500, …).
|
|
32
|
-
const codec = new
|
|
32
|
+
const codec = new JsonCodec() // the default RpcCodec; FetchHandler / FetchLink take { codec }
|
|
33
33
|
httpStatus(new PFError({ code: 'NOT_FOUND', status: 404 })) // 404
|
|
34
34
|
```
|
|
35
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 }`. Set `RpcCodec.streams`: `true` when `encodeSuccess` encodes an `AsyncIterable` output (as a `ReadableStream` body) and `decodeRequest` yields an `AsyncIterable` input, `false` when it cannot (`
|
|
36
|
+
A custom body encoding implements `RpcCodec` (`encode` returns `{ contentType, body }`; `decode` takes `{ contentType, text(), formData(), body() }`) and goes on `FetchHandler` / `FetchLink` via `{ codec }`. Set `RpcCodec.streams`: `true` when `encodeSuccess` encodes an `AsyncIterable` output (as a `ReadableStream` body) and `decodeRequest` yields an `AsyncIterable` input, `false` when it cannot (`JsonCodec` and `MultipartCodec` are `false`; `StreamCodec` and `SseCodec` are `true`). `FetchHandler` answers a stream procedure on a `streams: false` codec with `BAD_REQUEST` before the body is read; with the flag unset it only finds out after the handler returns. First-party codecs: `@ts-pf/file` (`MultipartCodec`) and `@ts-pf/stream` (`StreamCodec`, `SseCodec`). The message-transport analog is `@ts-pf/message`.
|
|
37
37
|
|
|
38
38
|
### Wrapping codecs
|
|
39
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`). `
|
|
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
41
|
|
|
42
42
|
```ts
|
|
43
|
-
import { type EnvelopeTextCodec, isJsonContentType,
|
|
43
|
+
import { type EnvelopeTextCodec, isJsonContentType, JsonCodec, jsonBodySource, mimeType } from '@ts-pf/http'
|
|
44
44
|
|
|
45
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
|
|
47
|
-
const inner: EnvelopeTextCodec = new
|
|
46
|
+
isJsonContentType(null) // true — a missing Content-Type is treated as JSON, like JsonCodec does
|
|
47
|
+
const inner: EnvelopeTextCodec = new JsonCodec()
|
|
48
48
|
const line = (await inner.encodeSuccess({ id: 1 })).body // a string, by type
|
|
49
49
|
await inner.decodeResponse(jsonBodySource(line)) // { ok: true, output: { id: 1 } }
|
|
50
50
|
```
|
|
@@ -55,7 +55,7 @@ Wire spec: [`PROTOCOL.md`](https://github.com/crobinson42/ts-pf/blob/main/packag
|
|
|
55
55
|
|
|
56
56
|
## API
|
|
57
57
|
|
|
58
|
-
- `
|
|
58
|
+
- `JsonCodec`, `RpcCodec` (optional `streams` flag), `RpcEncodedBody`, `RpcBodySource`
|
|
59
59
|
- `EnvelopeTextCodec`, `RpcEncodedText` (the `inner` role of `MultipartCodec` / `StreamCodec` / `SseCodec`)
|
|
60
60
|
- `mimeType`, `isJsonContentType`, `jsonBodySource`
|
|
61
61
|
- `PROTOCOL_HEADER`, `joinProcedurePath`, `parseProcedurePath`
|
package/dist/codec.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type RpcRequest, type RpcResponse } from '@ts-pf/protocol';
|
|
2
2
|
import type { EnvelopeTextCodec, RpcBodySource, RpcEncodedText } from './rpc.js';
|
|
3
|
-
export declare class
|
|
3
|
+
export declare class JsonCodec implements EnvelopeTextCodec {
|
|
4
4
|
readonly streams: boolean;
|
|
5
5
|
encodeRequest(req: RpcRequest): RpcEncodedText;
|
|
6
6
|
decodeRequest(source: RpcBodySource): Promise<RpcRequest>;
|
package/dist/codec.js
CHANGED
package/dist/content-type.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
export declare function mimeType(contentType: string | null | undefined): string;
|
|
7
7
|
/**
|
|
8
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 `
|
|
9
|
+
* with no `Content-Type` is still treated as JSON, as `JsonCodec` does).
|
|
10
10
|
*/
|
|
11
11
|
export declare function isJsonContentType(contentType: string | null | undefined): boolean;
|
|
12
12
|
//# sourceMappingURL=content-type.d.ts.map
|
package/dist/content-type.js
CHANGED
|
@@ -11,7 +11,7 @@ export function mimeType(contentType) {
|
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
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 `
|
|
14
|
+
* with no `Content-Type` is still treated as JSON, as `JsonCodec` does).
|
|
15
15
|
*/
|
|
16
16
|
export function isJsonContentType(contentType) {
|
|
17
17
|
const type = mimeType(contentType);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { jsonBodySource } from './body-source.js';
|
|
2
|
-
export {
|
|
2
|
+
export { JsonCodec } from './codec.js';
|
|
3
3
|
export { isJsonContentType, mimeType } from './content-type.js';
|
|
4
4
|
export { httpStatus, PROTOCOL_HTTP_STATUS } from './http-status.js';
|
|
5
5
|
export { joinProcedurePath, parseProcedurePath } from './path.js';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { jsonBodySource } from './body-source.js';
|
|
2
|
-
export {
|
|
2
|
+
export { JsonCodec } from './codec.js';
|
|
3
3
|
export { isJsonContentType, mimeType } from './content-type.js';
|
|
4
4
|
export { httpStatus, PROTOCOL_HTTP_STATUS } from './http-status.js';
|
|
5
5
|
export { joinProcedurePath, parseProcedurePath } from './path.js';
|
package/dist/rpc.d.ts
CHANGED
|
@@ -37,7 +37,7 @@ export interface RpcCodec {
|
|
|
37
37
|
* An `RpcCodec` whose encoded bodies are strings. This is the `inner` role of
|
|
38
38
|
* the wrapping codecs (`MultipartCodec`, `StreamCodec`, `SseCodec`): they
|
|
39
39
|
* embed one envelope per multipart part, JSONL line, or SSE event, so the
|
|
40
|
-
* inner codec must give them text, not a `Blob` or a stream. `
|
|
40
|
+
* inner codec must give them text, not a `Blob` or a stream. `JsonCodec`
|
|
41
41
|
* implements it. Handlers and links still take any `RpcCodec`.
|
|
42
42
|
*/
|
|
43
43
|
export interface EnvelopeTextCodec extends RpcCodec {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-pf/http",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "HTTP wire helpers:
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "HTTP wire helpers: JsonCodec, RpcCodec, PROTOCOL_HEADER, path helpers, httpStatus",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ts-pf",
|
|
7
7
|
"typescript",
|
|
@@ -50,6 +50,6 @@
|
|
|
50
50
|
"lint": "biome check src tests"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@ts-pf/protocol": "0.
|
|
53
|
+
"@ts-pf/protocol": "^1.0.0"
|
|
54
54
|
}
|
|
55
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 —
|
|
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
|
|
@@ -14,23 +14,23 @@ Link for agents: `npx skills experimental_sync -y`
|
|
|
14
14
|
## Do
|
|
15
15
|
|
|
16
16
|
```ts
|
|
17
|
-
import {
|
|
17
|
+
import { JsonCodec, PROTOCOL_HEADER, httpStatus, joinProcedurePath } from '@ts-pf/http'
|
|
18
18
|
|
|
19
|
-
const codec = new
|
|
19
|
+
const codec = new JsonCodec()
|
|
20
20
|
joinProcedurePath('/rpc', ['planet', 'find']) // '/rpc/planet/find'
|
|
21
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
|
-
`
|
|
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
26
|
|
|
27
|
-
A custom `RpcCodec` sets `streams`: `true` when it encodes an `AsyncIterable` output as a `ReadableStream` body and decodes an `AsyncIterable` input, `false` when it cannot (`
|
|
27
|
+
A custom `RpcCodec` sets `streams`: `true` when it encodes an `AsyncIterable` output as a `ReadableStream` body and decodes an `AsyncIterable` input, `false` when it cannot (`JsonCodec` / `MultipartCodec` are `false`, `StreamCodec` / `SseCodec` `true`). On `streams: false`, `FetchHandler` answers a stream procedure with `BAD_REQUEST` before the body is read.
|
|
28
28
|
|
|
29
|
-
Those wrappers take `inner?: EnvelopeTextCodec` — an `RpcCodec` whose encoded bodies are strings (`RpcEncodedText`), because they embed one envelope per part / line / event. `
|
|
29
|
+
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).
|
|
30
30
|
|
|
31
31
|
## API
|
|
32
32
|
|
|
33
|
-
- `
|
|
33
|
+
- `JsonCodec`, `RpcCodec` (`streams?: boolean`), `RpcEncodedBody`, `RpcBodySource`
|
|
34
34
|
- `EnvelopeTextCodec`, `RpcEncodedText`
|
|
35
35
|
- `mimeType`, `isJsonContentType`, `jsonBodySource`
|
|
36
36
|
- `PROTOCOL_HEADER`, `joinProcedurePath`, `parseProcedurePath`
|
|
@@ -39,7 +39,7 @@ Those wrappers take `inner?: EnvelopeTextCodec` — an `RpcCodec` whose encoded
|
|
|
39
39
|
## Pair with
|
|
40
40
|
|
|
41
41
|
- Handler/link: `ts-pf-server-http` / `ts-pf-client-http` (they take `{ codec }`)
|
|
42
|
-
- Other bodies: `ts-pf-file` / `ts-pf-stream`
|
|
42
|
+
- Other bodies: `ts-pf-file` / `ts-pf-stream` (JSONL and SSE)
|
|
43
43
|
|
|
44
44
|
## Don't
|
|
45
45
|
|