@defjs/core 0.3.3 → 0.4.0-beta.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 +1 -1
- package/README.md +85 -2
- package/index.d.ts +1645 -189
- package/index.js +5638 -852
- package/index.min.js +44 -1
- package/package.json +14 -12
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,5 +1,88 @@
|
|
|
1
|
-
|
|
1
|
+
# @defjs/core
|
|
2
|
+
|
|
3
|
+
Type-safe HTTP, SSE, and WebSocket commands for TypeScript.
|
|
4
|
+
|
|
5
|
+
This README describes the package version that contains it. Defjs is still pre-1.0, so use the documentation and release notes published for the exact version installed in your application.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun add @defjs/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The package is ESM and declares no runtime dependencies. Its tarball retains this `README.md` and the repository `LICENSE`; repository-wide guides and examples remain outside the package.
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { createClient, defineRequest, struct, withEndpoint } from '@defjs/core'
|
|
19
|
+
|
|
20
|
+
const client = createClient(withEndpoint('https://api.example.com'))
|
|
21
|
+
|
|
22
|
+
const getUser = defineRequest({
|
|
23
|
+
method: 'GET',
|
|
24
|
+
path: '/users/:id',
|
|
25
|
+
input: struct.object({
|
|
26
|
+
path: struct.object({ id: struct.number() }),
|
|
27
|
+
}),
|
|
28
|
+
output: struct.object({ id: struct.number(), name: struct.string() }),
|
|
29
|
+
error: struct.object({ message: struct.string() }),
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const [error, user] = await client.execute(getUser({ path: { id: 1 } }))
|
|
33
|
+
|
|
34
|
+
if (error) {
|
|
35
|
+
console.error(String(error), error.code)
|
|
36
|
+
} else {
|
|
37
|
+
console.log(user.id, user.name)
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`output` is one Struct for the 2xx body; `error` is one Struct for everything else. `ok` is the only fork, and only one side ever decodes. Omit a side and that body is never read.
|
|
42
|
+
|
|
43
|
+
## Core Boundaries
|
|
44
|
+
|
|
45
|
+
- Commands are typed values created by `defineRequest`, `defineEventStream`, and `defineWebSocket`.
|
|
46
|
+
- Structs validate request input and response data at runtime. A `builder` places parsed input onto `path`, `query`, `headers`, and `body`. Omit it when the input struct already has that shape. `.alias(name)` is the wire name for a typed value.
|
|
47
|
+
- Expected failures are returned as native `Fault` instances in an error-first tuple; custom interceptors and application callbacks can still throw. A `Fault` is discriminated by one closed set of `code` values whose prefix is the class — `HTTP_STATUS`, `REQ_*`, `NET_*`, `RES_*`, `EXT_*`, `CAP_*`, `ENV_UNSUPPORTED`. `String(error)` is directly loggable, while `code` and the variant metadata remain enumerable for structured logs.
|
|
48
|
+
- Faults carry their underlying value on the native `Error` cause chain. Narrow `error.cause instanceof StructError` before calling Struct-only helpers such as `format()`, `flatten()`, or `prettify()`.
|
|
49
|
+
- Server clients that capture cookies, authorization, tenant data, or user data must be created inside the owning request scope.
|
|
50
|
+
- Ordinary HTTP work is request-scoped and is bounded with its execute-time timeout or `AbortSignal`; `Client` is not `AsyncDisposable` and has no global `dispose()` lifecycle.
|
|
51
|
+
- Returned SSE and WebSocket handles are `AsyncDisposable`, so `await using` waits for Defjs-owned teardown. SSE disposal stops Defjs reading/reconnect work and releases its reader lock, but does not wait forever for a provider-controlled `cancel()` promise.
|
|
52
|
+
- WebSocket `closed` is the logical terminal result. Its disposer uses a one-second bounded teardown and may reject with `TimeoutError`; it cannot prove physical TCP closure. Existing `close()` and `closed` APIs remain available for manual lifecycle control.
|
|
53
|
+
- A timeout or cancellation does not prove that a server did not receive a write. Preserve operation identity and use an application/server idempotency contract before replaying writes.
|
|
54
|
+
- OpenAPI generation, full SDK generation, query caching, and GraphQL protocol handling are not included in this release. Define contracts by hand or compose purpose-built tools at the application boundary.
|
|
55
|
+
|
|
56
|
+
For client-local tests, inject a Fetch-compatible function with `withHTTPHandle(...)`. This keeps request interception scoped to one client and still exercises command building, interceptors, status dispatch, and response validation.
|
|
57
|
+
|
|
58
|
+
### WebSocket messages
|
|
59
|
+
|
|
60
|
+
`incoming` and `outgoing` are each one struct. `session.send` encodes that struct (aliases included) and sends JSON. A frame that is not valid JSON, or does not match `incoming`, is reported to `session.onRuntimeError` and is not delivered. Use `struct.discriminatedUnion` when one socket carries several message kinds. Static subprotocols belong on `protocols`.
|
|
61
|
+
|
|
62
|
+
## Runtime Notes
|
|
63
|
+
|
|
64
|
+
Repository development, testing, building, packaging, and publishing use Bun `1.4.0`. The package uses standard Web APIs such as `fetch`, `Request`, `Response`, `Headers`, streams, and `WebSocket` according to the transports enabled by the application. The packed-consumer gate typechecks and executes the published artifact with Bun `1.4.0`.
|
|
65
|
+
|
|
66
|
+
The repository-validated and supported minimum compiler/lib contract uses `target: 'ES2022'` with `lib: ['ES2022', 'ESNext.Disposable', 'DOM', 'DOM.Iterable']`. The gate is pinned to TypeScript 7; older compiler versions are not promised. This supported set is verified together rather than claiming that every entry is independently forced by one declaration, and it supplies types only — not runtime Web API polyfills.
|
|
67
|
+
|
|
68
|
+
**SSE reconnect behavior change:** without `withSSEReconnect(...)`, SSE no longer retries network/stream-read failures. Pass `withSSEReconnect({ attempts: 3 })` (or another reviewed budget) to retry those failures.
|
|
69
|
+
|
|
70
|
+
When using a checkout through `file:` or a workspace link, run `bun run build` in the checkout before compiling or running the external consumer. The checkout manifest points to generated `dist/` files; published packages already contain those files.
|
|
71
|
+
|
|
72
|
+
After compiling an application to ESM, the repository-tested command is:
|
|
73
|
+
|
|
74
|
+
```sh
|
|
75
|
+
bun run dist/index.js
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Browser applications use their bundler and the platform Fetch stream and WebSocket capabilities required by the transports they enable.
|
|
2
79
|
|
|
3
80
|
## Documentation
|
|
4
81
|
|
|
5
|
-
|
|
82
|
+
Guides live on the documentation site, not in the published tarball.
|
|
83
|
+
|
|
84
|
+
- [Getting started](https://defjs.org/guide/getting-started)
|
|
85
|
+
- [Client](https://defjs.org/core/client)
|
|
86
|
+
- [HTTP](https://defjs.org/core/http)
|
|
87
|
+
- [SSE](https://defjs.org/core/sse)
|
|
88
|
+
- [WebSocket](https://defjs.org/core/web-socket)
|