@noverax/courier-sveltekit 0.3.1 → 0.5.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 CHANGED
@@ -92,9 +92,9 @@ npm install @noverax/courier-sveltekit @noverax/courier @sveltejs/kit
92
92
 
93
93
  ## Usage
94
94
 
95
- ```ts
95
+ ```ts side="server"
96
96
  // src/hooks.server.ts
97
- import { registerSources } from "@noverax/courier"
97
+ import { registerSources } from "@noverax/courier/server"
98
98
  import { sources } from "$lib/api"
99
99
 
100
100
  registerSources(sources)
@@ -127,20 +127,53 @@ In `vite dev`, a chunked body larger than the limit resets the connection instea
127
127
 
128
128
  ## With Frontend
129
129
 
130
- Pair with `createProxyCaller` on the client:
130
+ Add the routes plugin beside SvelteKit's, so every page — and the server that renders it — gets the routes its proxy client may call, built from `courier.config.ts`.
131
+
132
+ The plugin runs on `@noverax/courier-build`, which only the build needs, so install it as a dev dependency:
133
+
134
+ ```bash
135
+ npm install --save-dev @noverax/courier-build
136
+ ```
137
+
138
+ Then add it:
139
+
140
+ ```ts
141
+ // vite.config.ts
142
+ import { sveltekit } from "@sveltejs/kit/vite"
143
+ import { courierRoutes } from "@noverax/courier-sveltekit/plugin"
144
+ import { defineConfig } from "vite"
145
+
146
+ export default defineConfig({ plugins: [sveltekit(), courierRoutes()] })
147
+ ```
148
+
149
+ Register the config once, so `routes` is typed from it:
131
150
 
132
151
  ```ts
133
- import { createProxyCaller } from "@noverax/courier"
134
- import { apiRoutes } from "./courier.routes"
152
+ // src/courier-env.d.ts
153
+ import type config from "../courier.config"
154
+
155
+ declare module "@noverax/courier/routes" {
156
+ interface Register {
157
+ config: typeof config
158
+ }
159
+ }
160
+ ```
161
+
162
+ Then pair with `createProxyCaller` on the client:
163
+
164
+ ```ts side="page"
165
+ import { createProxyCaller } from "@noverax/courier/client"
166
+ import { routes } from "@noverax/courier/routes"
135
167
 
136
- // The routes Courier writes from the endpoints you mark .expose():
137
- // what a page may call, under the key the source has in your `sources` map.
138
- const caller = createProxyCaller(apiRoutes, {
168
+ // What a page may call of the source registered as `api` in your `sources` map:
169
+ // the endpoints you mark .expose(), and how each is reached.
170
+ const caller = createProxyCaller(routes.api, {
139
171
  proxyPath: { http: "/api/request" },
140
172
  })
141
- const users = await caller.users.getAll()
142
173
  ```
143
174
 
175
+ A project that would rather keep the routes in its tree writes them with `npx @noverax/courier-cli routes`, and gives the caller `apiRoutes` from `./courier.routes` in place of `routes.api`.
176
+
144
177
  <br />
145
178
 
146
179
  ## Cancelled calls
@@ -153,10 +186,10 @@ When the browser cancels a call — an aborted upload, a query it no longer need
153
186
 
154
187
  An API that keeps the user's session describes it in `defineApi`, and every call a hook, a `load` function, a form action or an endpoint makes carries the session of the request being served — the cookies the session names and nothing else of your application's — once the request can be found:
155
188
 
156
- ```ts
189
+ ```ts side="server"
157
190
  // src/hooks.server.ts — SvelteKit 2.20 and later
158
191
  import { getRequestEvent } from "$app/server"
159
- import { registerRequestContext } from "@noverax/courier"
192
+ import { registerRequestContext } from "@noverax/courier/server"
160
193
  import { svelteKitRequestContext } from "@noverax/courier-sveltekit"
161
194
 
162
195
  registerRequestContext(svelteKitRequestContext(getRequestEvent))
@@ -187,6 +220,10 @@ The request SvelteKit 2.20 and later is serving, for `registerRequestContext`.
187
220
 
188
221
  From `@noverax/courier-sveltekit/handle`: a `handle` hook that serves every request with its session at hand.
189
222
 
223
+ ### `courierRoutes(options?)`
224
+
225
+ From `@noverax/courier-sveltekit/plugin`: the Vite plugin that builds the routes a page's proxy client may call from `courier.config.ts`, in place of `@noverax/courier/routes`. `config` names the config file when it is not the one found from the root.
226
+
190
227
  <br />
191
228
 
192
229
  ## Part of Courier
package/lib/handle.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { t as SvelteKitServerEvent } from "./request-context-CXZTuX53.js";
2
- //#region src/handle.d.ts
1
+ import { t as SvelteKitServerEvent } from "./request-context-C96e5HoI.js";
3
2
  /** A SvelteKit `handle` hook, as far as it reads its input. */
4
3
  export type RequestContextHandle = <TEvent extends SvelteKitServerEvent, TResponse>(input: {
5
4
  event: TEvent;
@@ -21,5 +20,4 @@ export type RequestContextHandle = <TEvent extends SvelteKitServerEvent, TRespon
21
20
  * export const handle = requestContextHandle();
22
21
  * ```
23
22
  */
24
- export declare function requestContextHandle(): RequestContextHandle;
25
- //#endregion
23
+ export declare function requestContextHandle(): RequestContextHandle;
package/lib/handle.js CHANGED
@@ -1,6 +1,6 @@
1
- import { t as svelteKitIncomingRequest } from "./request-context-UtZzeQWD.js";
1
+ import { t as svelteKitIncomingRequest } from "./request-context-Cy7IqxdJ.js";
2
2
  import { withIncomingRequest } from "@noverax/courier/node";
3
- //#region src/handle.ts
3
+ // src/handle.ts
4
4
  /**
5
5
  * A `handle` hook that serves every request with its session and headers at
6
6
  * hand: each call the request's hooks, `load` functions, form actions and
@@ -20,5 +20,4 @@ import { withIncomingRequest } from "@noverax/courier/node";
20
20
  function requestContextHandle() {
21
21
  return ({ event, resolve }) => withIncomingRequest(svelteKitIncomingRequest(event), () => resolve(event));
22
22
  }
23
- //#endregion
24
23
  export { requestContextHandle };
package/lib/index.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { n as svelteKitIncomingRequest, r as svelteKitRequestContext, t as SvelteKitServerEvent } from "./request-context-CXZTuX53.js";
2
- import { ClientConnection, ConnectResult, HandlerConfig } from "@noverax/courier";
3
- //#region src/index.d.ts
1
+ import { n as svelteKitIncomingRequest, r as svelteKitRequestContext, t as SvelteKitServerEvent } from "./request-context-C96e5HoI.js";
2
+ import { ClientConnection, ConnectResult, HandlerConfig } from "@noverax/courier/server";
4
3
  /**
5
4
  * The parts of SvelteKit's `RequestEvent` the handler reads. `platform.req`
6
5
  * is the Node request `@sveltejs/adapter-node` passes along.
@@ -47,5 +46,4 @@ export interface SvelteKitRequestEvent {
47
46
  * ```
48
47
  */
49
48
  export declare function courierHandler<TConnect extends ConnectResult = ConnectResult>(config?: HandlerConfig<TConnect>): (event: SvelteKitRequestEvent) => Promise<Response>;
50
- //#endregion
51
49
  export { type SvelteKitServerEvent, svelteKitIncomingRequest, svelteKitRequestContext };
package/lib/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { n as svelteKitRequestContext, r as svelteKitClientAddress, t as svelteKitIncomingRequest } from "./request-context-UtZzeQWD.js";
2
- import { createHandler, handleWhileConnected } from "@noverax/courier";
1
+ import { n as svelteKitRequestContext, r as svelteKitClientAddress, t as svelteKitIncomingRequest } from "./request-context-Cy7IqxdJ.js";
3
2
  import { announceWhenRegistered } from "@noverax/courier/announce";
4
3
  import { rememberPeerAddress, serverOriginOf } from "@noverax/courier/internal";
5
- //#region src/index.ts
4
+ import { createHandler, handleWhileConnected } from "@noverax/courier/server";
5
+ // src/index.ts
6
6
  /**
7
7
  * Create a SvelteKit request handler for the courier proxy.
8
8
  *
@@ -46,5 +46,4 @@ function courierHandler(config) {
46
46
  return handleWhileConnected(handle, event.request, event.platform?.req?.socket);
47
47
  };
48
48
  }
49
- //#endregion
50
49
  export { courierHandler, svelteKitIncomingRequest, svelteKitRequestContext };
@@ -0,0 +1,2 @@
1
+ import { CourierRoutesOptions, courierRoutes } from "@noverax/courier-build/vite";
2
+ export { type CourierRoutesOptions, courierRoutes };
package/lib/plugin.js ADDED
@@ -0,0 +1 @@
1
+ export { courierRoutes } from "@noverax/courier-build/vite";
@@ -1,5 +1,4 @@
1
- import { IncomingRequest, RequestContextProvider } from "@noverax/courier";
2
- //#region src/request-context.d.ts
1
+ import { IncomingRequest, RequestContextProvider } from "@noverax/courier/server";
3
2
  /** A cookie's options, as SvelteKit's `cookies.set()` takes them. */
4
3
  interface SvelteKitCookieOptions {
5
4
  path: string;
@@ -34,7 +33,7 @@ interface SvelteKitServerEvent {
34
33
  * ```ts
35
34
  * // src/hooks.server.ts
36
35
  * import { getRequestEvent } from "$app/server";
37
- * import { registerRequestContext } from "@noverax/courier";
36
+ * import { registerRequestContext } from "@noverax/courier/server";
38
37
  * import { svelteKitRequestContext } from "@noverax/courier-sveltekit";
39
38
  *
40
39
  * registerRequestContext(svelteKitRequestContext(getRequestEvent));
@@ -55,5 +54,4 @@ declare function svelteKitRequestContext(getRequestEvent: () => SvelteKitServerE
55
54
  * ```
56
55
  */
57
56
  declare function svelteKitIncomingRequest(event: SvelteKitServerEvent): IncomingRequest;
58
- //#endregion
59
57
  export { svelteKitIncomingRequest as n, svelteKitRequestContext as r, SvelteKitServerEvent as t };
@@ -1,6 +1,6 @@
1
- import { createLogger } from "@noverax/courier";
2
1
  import { forwardedChain } from "@noverax/courier/internal";
3
- //#region src/client-address.ts
2
+ import { createLogger } from "@noverax/courier/server";
3
+ // src/client-address.ts
4
4
  const log$1 = createLogger("SvelteKit");
5
5
  /**
6
6
  * The address SvelteKit's adapter reports a request came from, where it
@@ -22,8 +22,7 @@ function svelteKitClientAddress(event) {
22
22
  return;
23
23
  }
24
24
  }
25
- //#endregion
26
- //#region src/request-context.ts
25
+ // src/request-context.ts
27
26
  const log = createLogger("SvelteKit");
28
27
  const providers = /* @__PURE__ */ new WeakMap();
29
28
  /**
@@ -38,7 +37,7 @@ const providers = /* @__PURE__ */ new WeakMap();
38
37
  * ```ts
39
38
  * // src/hooks.server.ts
40
39
  * import { getRequestEvent } from "$app/server";
41
- * import { registerRequestContext } from "@noverax/courier";
40
+ * import { registerRequestContext } from "@noverax/courier/server";
42
41
  * import { svelteKitRequestContext } from "@noverax/courier-sveltekit";
43
42
  *
44
43
  * registerRequestContext(svelteKitRequestContext(getRequestEvent));
@@ -111,5 +110,4 @@ function optionsOf(cookie) {
111
110
  function lower(value) {
112
111
  return value.toLowerCase();
113
112
  }
114
- //#endregion
115
113
  export { svelteKitRequestContext as n, svelteKitClientAddress as r, svelteKitIncomingRequest as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noverax/courier-sveltekit",
3
- "version": "0.3.1",
3
+ "version": "0.5.0",
4
4
  "description": "SvelteKit adapter for Courier: an endpoint that proxies typed calls to any backend, with the user's session in load functions and form actions",
5
5
  "keywords": [
6
6
  "courier",
@@ -49,12 +49,20 @@
49
49
  "types": "./lib/handle.d.ts",
50
50
  "import": "./lib/handle.js",
51
51
  "default": "./lib/handle.js"
52
+ },
53
+ "./plugin": {
54
+ "types": "./lib/plugin.d.ts",
55
+ "import": "./lib/plugin.js",
56
+ "default": "./lib/plugin.js"
52
57
  }
53
58
  },
54
59
  "typesVersions": {
55
60
  "*": {
56
61
  "handle": [
57
62
  "./lib/handle.d.ts"
63
+ ],
64
+ "plugin": [
65
+ "./lib/plugin.d.ts"
58
66
  ]
59
67
  }
60
68
  },
@@ -72,9 +80,20 @@
72
80
  "access": "public"
73
81
  },
74
82
  "peerDependencies": {
75
- "@noverax/courier": "^0.3.1"
83
+ "@noverax/courier": "^0.5.0",
84
+ "@noverax/courier-build": "^0.5.0",
85
+ "vite": ">=5.0.0"
86
+ },
87
+ "peerDependenciesMeta": {
88
+ "@noverax/courier-build": {
89
+ "optional": true
90
+ },
91
+ "vite": {
92
+ "optional": true
93
+ }
76
94
  },
77
95
  "devDependencies": {
78
- "@noverax/courier": "*"
96
+ "@noverax/courier": "*",
97
+ "@noverax/courier-build": "*"
79
98
  }
80
99
  }