lixnet 1.5.0 → 1.5.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/README.md +23 -10
- package/dist/exports.d.ts +1 -1
- package/dist/lib/router.d.ts +22 -0
- package/dist/lib/server.d.ts +21 -8
- package/dist/lib/server.js +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -41,7 +41,7 @@ It’s designed to be minimal: no codegen, no schema registry, no client bundler
|
|
|
41
41
|
|
|
42
42
|
- [Quickstart (HTTP RPC)](#quickstart-http-rpc)
|
|
43
43
|
- [Framework adapters (server wiring)](#framework-adapters-server-wiring)
|
|
44
|
-
- [Inferred contract (`server.route`)](#inferred-contract-serverroute)
|
|
44
|
+
- [Inferred contract (`routes` / `server.route`)](#inferred-contract-routes--serverroute)
|
|
45
45
|
- [Server: `LixnetServer`](#server-lixnetserver)
|
|
46
46
|
- [Errors](#errors-lixneterror)
|
|
47
47
|
- [CORS](#cors)
|
|
@@ -175,9 +175,9 @@ await client.call(
|
|
|
175
175
|
);
|
|
176
176
|
```
|
|
177
177
|
|
|
178
|
-
## Inferred contract (`server.route`)
|
|
178
|
+
## Inferred contract (`routes` / `server.route`)
|
|
179
179
|
|
|
180
|
-
Instead of writing an `Events` type by hand, chain `.route(...)`
|
|
180
|
+
Instead of writing an `Events` type by hand, pass your events as `routes` (or chain `.route(...)`) and let the client infer everything from `typeof server`. Experimental: `Events` + `on()` keeps working and both can be used side by side.
|
|
181
181
|
|
|
182
182
|
```ts
|
|
183
183
|
// cloudflare/ioServer.ts (worker only)
|
|
@@ -186,18 +186,31 @@ import { LixnetServer } from "lixnet";
|
|
|
186
186
|
import live from "./(functions)/live";
|
|
187
187
|
|
|
188
188
|
export default function createRpcServer(options: { defaultHeaders?: Record<string, string> } = {}) {
|
|
189
|
-
return new LixnetServer(
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
189
|
+
return new LixnetServer({
|
|
190
|
+
...options,
|
|
191
|
+
routes: {
|
|
192
|
+
helloThere: {
|
|
193
|
+
schema: z.object({ name: z.string() }),
|
|
194
|
+
handler: ({ name }) => ({ message: `Hello ${name}` }), // name: string, from the schema
|
|
195
|
+
},
|
|
196
|
+
// no schema: input is the handler's parameter type minus request/response
|
|
197
|
+
live: { handler: live },
|
|
198
|
+
},
|
|
199
|
+
});
|
|
196
200
|
}
|
|
197
201
|
|
|
198
202
|
export type RpcServer = ReturnType<typeof createRpcServer>;
|
|
199
203
|
```
|
|
200
204
|
|
|
205
|
+
`events` is an alias of `routes` (same types and behaviour) for codebases that still use the events naming; pass one or the other.
|
|
206
|
+
|
|
207
|
+
`.route(name, { schema?, handler })` does the same one route at a time and can be chained after `routes`, e.g. to add events conditionally:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
new LixnetServer({ routes: { helloThere: { ... } } })
|
|
211
|
+
.route("live", { handler: live });
|
|
212
|
+
```
|
|
213
|
+
|
|
201
214
|
```ts
|
|
202
215
|
// cloudflare/io.ts (browser)
|
|
203
216
|
import type { RpcServer } from "./ioServer"; // type-only: erased at build time
|
package/dist/exports.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { LixnetResponse } from "./lib/util/response";
|
|
|
7
7
|
import { LixnetError, LIXNET_ERROR_STATUS } from "./lib/error";
|
|
8
8
|
export { LixnetServer, LixnetClient, LixnetPeer, LixnetStreamServer, LixnetStreamClient, LixnetResponse, LixnetError, LIXNET_ERROR_STATUS, };
|
|
9
9
|
export type { LixnetErrorCode, LixnetIssue } from "./lib/error";
|
|
10
|
-
export type { LixnetRoute, LixnetRoutes, InferLixnetEvents } from "./lib/router";
|
|
10
|
+
export type { LixnetRoute, LixnetRoutes, LixnetRouteDefinitions, LixnetRoutesFromDefinitions, InferLixnetEvents, } from "./lib/router";
|
|
11
11
|
export type { LixnetCookies, LixnetHeaders, LixnetRequest, ReadonlyHeaders, ReadonlyRequestCookies, RequestCookie, } from "./lib/util/request";
|
|
12
12
|
export type { LXN_ServerClient_EventType, LXNServerHandler, FunctionInput, LXN_ServerClient_Request, LixnetServerInjections, LXNStreamEmitter, LXNCallArgs, } from "./lib/types";
|
|
13
13
|
export type { LXNStreamCallOptions } from "./lib/streamclient";
|
package/dist/lib/router.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from "./standardschema";
|
|
2
|
+
import type { LixnetServerInjections } from "./types";
|
|
1
3
|
/**
|
|
2
4
|
* Type-level description of a route added with `LixnetServer.route(...)`: the input the
|
|
3
5
|
* client sends and the output it receives.
|
|
@@ -16,3 +18,23 @@ export type InferLixnetEvents<T> = T extends {
|
|
|
16
18
|
} ? {
|
|
17
19
|
[K in keyof TRoutes]: (input: TRoutes[K]["input"]) => TRoutes[K]["output"];
|
|
18
20
|
} : T;
|
|
21
|
+
type RouteHandlerInput<TSchema> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> & LixnetServerInjections : any;
|
|
22
|
+
/**
|
|
23
|
+
* The `routes` constructor option: `{ name: { schema?, handler } }`. Each handler is typed from
|
|
24
|
+
* its own schema; without one, annotate the handler's parameter.
|
|
25
|
+
*/
|
|
26
|
+
export type LixnetRouteDefinitions<TSchemas> = {
|
|
27
|
+
[K in keyof TSchemas]: {
|
|
28
|
+
schema?: TSchemas[K];
|
|
29
|
+
handler: (input: RouteHandlerInput<TSchemas[K]>) => unknown;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
/** Turns the `routes` option's type into routes, the same shape `.route(...)` records. */
|
|
33
|
+
export type LixnetRoutesFromDefinitions<TDefs> = {
|
|
34
|
+
[K in keyof TDefs]: TDefs[K] extends {
|
|
35
|
+
handler: (input: infer TParams) => infer TOutput;
|
|
36
|
+
} ? LixnetRoute<TDefs[K] extends {
|
|
37
|
+
schema: infer TSchema extends StandardSchemaV1;
|
|
38
|
+
} ? StandardSchemaV1.InferInput<TSchema> : Omit<TParams, keyof LixnetServerInjections>, Awaited<TOutput>> : never;
|
|
39
|
+
};
|
|
40
|
+
export {};
|
package/dist/lib/server.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { FunctionInput, FunctionOutput, LXNEventEntries, LXNEventSchema, LX
|
|
|
3
3
|
import { LixnetResponse } from "./util/response";
|
|
4
4
|
import { type LixnetRequest } from "./util/request";
|
|
5
5
|
import type { StandardSchemaV1 } from "./standardschema";
|
|
6
|
-
import type { LixnetRoute, LixnetRoutes } from "./router";
|
|
6
|
+
import type { LixnetRoute, LixnetRouteDefinitions, LixnetRoutes, LixnetRoutesFromDefinitions } from "./router";
|
|
7
7
|
import type { LixnetServerInjections } from "./types";
|
|
8
8
|
type LXNServerEventInput<Events extends LXN_ServerClient_EventType, TName extends keyof Events> = {
|
|
9
9
|
event: TName;
|
|
@@ -13,13 +13,26 @@ type LXNServerEventInput<Events extends LXN_ServerClient_EventType, TName extend
|
|
|
13
13
|
}, FunctionOutput<Events[TName]>>;
|
|
14
14
|
schema?: LXNEventSchema<Events, TName>;
|
|
15
15
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Routes with inferred types, as `{ name: { schema?, handler } }`: the same as calling
|
|
18
|
+
* `.route(name, ...)` for each. `events` is an alias of `routes`; pass one or the other.
|
|
19
|
+
*/
|
|
20
|
+
type LixnetServerRoutesOption<TRouteDefinitions> = {
|
|
21
|
+
routes?: TRouteDefinitions;
|
|
22
|
+
events?: never;
|
|
23
|
+
} | {
|
|
24
|
+
events?: TRouteDefinitions;
|
|
25
|
+
routes?: never;
|
|
26
|
+
};
|
|
27
|
+
export default class LixnetServer<Events extends LXN_ServerClient_EventType = LXN_ServerClient_EventType, Routes extends LixnetRoutes = {}, TSchemas extends Record<string, unknown> = {}, TDefs extends Record<string, {
|
|
28
|
+
handler: (input: any) => unknown;
|
|
29
|
+
}> = {}> extends LixnetServerBase<LXNServerEventInput<Events, keyof Events>> {
|
|
30
|
+
/** Type-only: routes from the `routes` option and `.route(...)`, read by `InferLixnetEvents`. */
|
|
31
|
+
readonly "~routes": Routes & LixnetRoutesFromDefinitions<TDefs>;
|
|
19
32
|
private formatter;
|
|
20
|
-
constructor({ formatter, ...options }?: LixnetServerBaseOptions & {
|
|
33
|
+
constructor({ formatter, routes, events, ...options }?: LixnetServerBaseOptions & {
|
|
21
34
|
formatter?: (this: LixnetResponse) => Response;
|
|
22
|
-
});
|
|
35
|
+
} & LixnetServerRoutesOption<TDefs & LixnetRouteDefinitions<TSchemas>>);
|
|
23
36
|
/**
|
|
24
37
|
* Adds an event whose types are inferred, so no `Events` contract is needed. Chain calls and
|
|
25
38
|
* give the client `typeof server` through an `import type`.
|
|
@@ -29,12 +42,12 @@ export default class LixnetServer<Events extends LXN_ServerClient_EventType = LX
|
|
|
29
42
|
route<K extends string, TSchema extends StandardSchemaV1, TOutput>(event: K, route: {
|
|
30
43
|
schema: TSchema;
|
|
31
44
|
handler: (input: StandardSchemaV1.InferOutput<TSchema> & LixnetServerInjections) => TOutput;
|
|
32
|
-
}): LixnetServer<Events, Routes & Record<K, LixnetRoute<StandardSchemaV1.InferInput<TSchema>, Awaited<TOutput
|
|
45
|
+
}): LixnetServer<Events, Routes & Record<K, LixnetRoute<StandardSchemaV1.InferInput<TSchema>, Awaited<TOutput>>>, TSchemas, TDefs>;
|
|
33
46
|
/** Without a schema, the input is the handler's parameter type minus `request`/`response`. */
|
|
34
47
|
route<K extends string, TParams, TOutput>(event: K, route: {
|
|
35
48
|
schema?: undefined;
|
|
36
49
|
handler: (input: TParams) => TOutput;
|
|
37
|
-
}): LixnetServer<Events, Routes & Record<K, LixnetRoute<Omit<TParams, keyof LixnetServerInjections>, Awaited<TOutput
|
|
50
|
+
}): LixnetServer<Events, Routes & Record<K, LixnetRoute<Omit<TParams, keyof LixnetServerInjections>, Awaited<TOutput>>>, TSchemas, TDefs>;
|
|
38
51
|
on<K extends keyof Events & string>(input: LXNServerEventInput<Events, K> | LXNEventEntries<Events, {
|
|
39
52
|
[P in keyof Events & string]: LXNServerEventInput<Events, P>;
|
|
40
53
|
}>): void;
|
package/dist/lib/server.js
CHANGED
|
@@ -4,9 +4,12 @@ import { createLixnetRequest } from "./util/request";
|
|
|
4
4
|
import getDefaultFormatter from "./util/getDefaultFormatter";
|
|
5
5
|
export default class LixnetServer extends LixnetServerBase {
|
|
6
6
|
formatter;
|
|
7
|
-
constructor({ formatter, ...options } = {}) {
|
|
7
|
+
constructor({ formatter, routes, events, ...options } = {}) {
|
|
8
8
|
super(options);
|
|
9
9
|
this.formatter = formatter ?? getDefaultFormatter(this.defaultHeaders);
|
|
10
|
+
for (const [event, route] of Object.entries(routes ?? events ?? {})) {
|
|
11
|
+
this.register({ ...route, event });
|
|
12
|
+
}
|
|
10
13
|
}
|
|
11
14
|
route(event, route) {
|
|
12
15
|
this.register({ ...route, event });
|