mates-fullstack 1.0.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/README.md +311 -0
- package/dist/arctic-auth.d.ts +101 -0
- package/dist/arctic-auth.d.ts.map +1 -0
- package/dist/arctic-auth.js +538 -0
- package/dist/arctic-auth.js.map +1 -0
- package/dist/asset-manifest.d.ts +14 -0
- package/dist/asset-manifest.d.ts.map +1 -0
- package/dist/asset-manifest.js +102 -0
- package/dist/asset-manifest.js.map +1 -0
- package/dist/browser.d.ts +18 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +25 -0
- package/dist/browser.js.map +1 -0
- package/dist/build-esbuild.d.ts +29 -0
- package/dist/build-esbuild.d.ts.map +1 -0
- package/dist/build-esbuild.js +699 -0
- package/dist/build-esbuild.js.map +1 -0
- package/dist/build-prod.d.ts +126 -0
- package/dist/build-prod.d.ts.map +1 -0
- package/dist/build-prod.js +1014 -0
- package/dist/build-prod.js.map +1 -0
- package/dist/cli-new.d.ts +14 -0
- package/dist/cli-new.d.ts.map +1 -0
- package/dist/cli-new.js +637 -0
- package/dist/cli-new.js.map +1 -0
- package/dist/client.d.ts +43 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +130 -0
- package/dist/client.js.map +1 -0
- package/dist/cors.d.ts +16 -0
- package/dist/cors.d.ts.map +1 -0
- package/dist/cors.js +60 -0
- package/dist/cors.js.map +1 -0
- package/dist/ctx.d.ts +78 -0
- package/dist/ctx.d.ts.map +1 -0
- package/dist/ctx.js +280 -0
- package/dist/ctx.js.map +1 -0
- package/dist/dev-watcher.d.ts +23 -0
- package/dist/dev-watcher.d.ts.map +1 -0
- package/dist/dev-watcher.js +136 -0
- package/dist/dev-watcher.js.map +1 -0
- package/dist/docs-generator.d.ts +69 -0
- package/dist/docs-generator.d.ts.map +1 -0
- package/dist/docs-generator.js +557 -0
- package/dist/docs-generator.js.map +1 -0
- package/dist/docs-page.d.ts +20 -0
- package/dist/docs-page.d.ts.map +1 -0
- package/dist/docs-page.js +1152 -0
- package/dist/docs-page.js.map +1 -0
- package/dist/download.d.ts +78 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +202 -0
- package/dist/download.js.map +1 -0
- package/dist/env-loader.d.ts +76 -0
- package/dist/env-loader.d.ts.map +1 -0
- package/dist/env-loader.js +213 -0
- package/dist/env-loader.js.map +1 -0
- package/dist/errors.d.ts +146 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +386 -0
- package/dist/errors.js.map +1 -0
- package/dist/head.d.ts +31 -0
- package/dist/head.d.ts.map +1 -0
- package/dist/head.js +245 -0
- package/dist/head.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/dist/internal-prefixes.d.ts +16 -0
- package/dist/internal-prefixes.d.ts.map +1 -0
- package/dist/internal-prefixes.js +16 -0
- package/dist/internal-prefixes.js.map +1 -0
- package/dist/internal.d.ts +25 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +25 -0
- package/dist/internal.js.map +1 -0
- package/dist/jwt.d.ts +166 -0
- package/dist/jwt.d.ts.map +1 -0
- package/dist/jwt.js +261 -0
- package/dist/jwt.js.map +1 -0
- package/dist/log.d.ts +44 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +66 -0
- package/dist/log.js.map +1 -0
- package/dist/logger.d.ts +76 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +138 -0
- package/dist/logger.js.map +1 -0
- package/dist/main-runner.d.ts +59 -0
- package/dist/main-runner.d.ts.map +1 -0
- package/dist/main-runner.js +157 -0
- package/dist/main-runner.js.map +1 -0
- package/dist/mates-auth.d.ts +82 -0
- package/dist/mates-auth.d.ts.map +1 -0
- package/dist/mates-auth.js +323 -0
- package/dist/mates-auth.js.map +1 -0
- package/dist/middleware.d.ts +30 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +67 -0
- package/dist/middleware.js.map +1 -0
- package/dist/project-resolver.d.ts +102 -0
- package/dist/project-resolver.d.ts.map +1 -0
- package/dist/project-resolver.js +271 -0
- package/dist/project-resolver.js.map +1 -0
- package/dist/rate-limit.d.ts +37 -0
- package/dist/rate-limit.d.ts.map +1 -0
- package/dist/rate-limit.js +109 -0
- package/dist/rate-limit.js.map +1 -0
- package/dist/redirect.d.ts +84 -0
- package/dist/redirect.d.ts.map +1 -0
- package/dist/redirect.js +105 -0
- package/dist/redirect.js.map +1 -0
- package/dist/renderer.d.ts +91 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +630 -0
- package/dist/renderer.js.map +1 -0
- package/dist/request-logger.d.ts +12 -0
- package/dist/request-logger.d.ts.map +1 -0
- package/dist/request-logger.js +55 -0
- package/dist/request-logger.js.map +1 -0
- package/dist/rest.d.ts +25 -0
- package/dist/rest.d.ts.map +1 -0
- package/dist/rest.js +93 -0
- package/dist/rest.js.map +1 -0
- package/dist/router.d.ts +71 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +222 -0
- package/dist/router.js.map +1 -0
- package/dist/rpc-registry.d.ts +84 -0
- package/dist/rpc-registry.d.ts.map +1 -0
- package/dist/rpc-registry.js +271 -0
- package/dist/rpc-registry.js.map +1 -0
- package/dist/rpc-runner.d.ts +82 -0
- package/dist/rpc-runner.d.ts.map +1 -0
- package/dist/rpc-runner.js +564 -0
- package/dist/rpc-runner.js.map +1 -0
- package/dist/sanitize.d.ts +61 -0
- package/dist/sanitize.d.ts.map +1 -0
- package/dist/sanitize.js +193 -0
- package/dist/sanitize.js.map +1 -0
- package/dist/security-headers.d.ts +114 -0
- package/dist/security-headers.d.ts.map +1 -0
- package/dist/security-headers.js +121 -0
- package/dist/security-headers.js.map +1 -0
- package/dist/server-fn.d.ts +323 -0
- package/dist/server-fn.d.ts.map +1 -0
- package/dist/server-fn.js +373 -0
- package/dist/server-fn.js.map +1 -0
- package/dist/server-public.d.ts +13 -0
- package/dist/server-public.d.ts.map +1 -0
- package/dist/server-public.js +12 -0
- package/dist/server-public.js.map +1 -0
- package/dist/server-timeout.d.ts +38 -0
- package/dist/server-timeout.d.ts.map +1 -0
- package/dist/server-timeout.js +46 -0
- package/dist/server-timeout.js.map +1 -0
- package/dist/server.d.ts +100 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1218 -0
- package/dist/server.js.map +1 -0
- package/dist/socket-router.d.ts +153 -0
- package/dist/socket-router.d.ts.map +1 -0
- package/dist/socket-router.js +612 -0
- package/dist/socket-router.js.map +1 -0
- package/dist/sso.d.ts +90 -0
- package/dist/sso.d.ts.map +1 -0
- package/dist/sso.js +261 -0
- package/dist/sso.js.map +1 -0
- package/dist/ssr-context.d.ts +49 -0
- package/dist/ssr-context.d.ts.map +1 -0
- package/dist/ssr-context.js +85 -0
- package/dist/ssr-context.js.map +1 -0
- package/dist/ssr-globals.d.ts +32 -0
- package/dist/ssr-globals.d.ts.map +1 -0
- package/dist/ssr-globals.js +1010 -0
- package/dist/ssr-globals.js.map +1 -0
- package/dist/ssr-template.d.ts +73 -0
- package/dist/ssr-template.d.ts.map +1 -0
- package/dist/ssr-template.js +507 -0
- package/dist/ssr-template.js.map +1 -0
- package/dist/stack-mapper.d.ts +25 -0
- package/dist/stack-mapper.d.ts.map +1 -0
- package/dist/stack-mapper.js +139 -0
- package/dist/stack-mapper.js.map +1 -0
- package/dist/stream.d.ts +89 -0
- package/dist/stream.d.ts.map +1 -0
- package/dist/stream.js +299 -0
- package/dist/stream.js.map +1 -0
- package/dist/upload.d.ts +69 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +110 -0
- package/dist/upload.js.map +1 -0
- package/dist/validate.d.ts +58 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +89 -0
- package/dist/validate.js.map +1 -0
- package/dist/verify-package.d.ts +3 -0
- package/dist/verify-package.d.ts.map +1 -0
- package/dist/verify-package.js +128 -0
- package/dist/verify-package.js.map +1 -0
- package/package.json +79 -0
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mates-ssr — server-fn.ts
|
|
3
|
+
*
|
|
4
|
+
* `serverFn` is the only API surface a developer touches to define a server
|
|
5
|
+
* function. It is a near-identity wrapper on the server and is NEVER executed
|
|
6
|
+
* in the browser — the Bun build plugin (`serverFnPlugin` in cli.ts) intercepts
|
|
7
|
+
* every import of a file inside `src/api/` during `Bun.build()` and replaces
|
|
8
|
+
* the entire module with lightweight RPC stubs before Bun ever tries to resolve
|
|
9
|
+
* the file's own imports (db, secrets, fs, etc.).
|
|
10
|
+
*
|
|
11
|
+
* ─── Server behaviour ────────────────────────────────────────────────────────
|
|
12
|
+
*
|
|
13
|
+
* import { serverFn } from "mates-ssr";
|
|
14
|
+
* export const getUsers = serverFn(async (filter?: string) => {
|
|
15
|
+
* return db.query("SELECT * FROM users WHERE name LIKE ?").all(filter);
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* On the server:
|
|
19
|
+
* - serverFn() is called once at module-load time.
|
|
20
|
+
* - It marks the function with metadata (__isMatesServerFn__, __fnName__)
|
|
21
|
+
* so the fn-registry can index it during startup scanning.
|
|
22
|
+
* - It returns the original async function unchanged.
|
|
23
|
+
* - When called during an SSR render pass (isSSR() === true) the function
|
|
24
|
+
* executes directly, in-process, with zero network overhead.
|
|
25
|
+
* - When called from a server-side API handler context the function also
|
|
26
|
+
* executes directly.
|
|
27
|
+
*
|
|
28
|
+
* ─── Client behaviour ────────────────────────────────────────────────────────
|
|
29
|
+
*
|
|
30
|
+
* The Bun plugin rewrites the entire src/api/*.ts module before bundling.
|
|
31
|
+
* What the browser receives is:
|
|
32
|
+
*
|
|
33
|
+
* import { __callServerFn } from "mates-ssr/client";
|
|
34
|
+
* export const getUsers = (...args) => __callServerFn("users", "getUsers", args);
|
|
35
|
+
* export const createUser = (...args) => __callServerFn("users", "createUser", args);
|
|
36
|
+
*
|
|
37
|
+
* `serverFn()` itself is never present in client.js.
|
|
38
|
+
* The original function body (db queries, secrets, file I/O) is never present
|
|
39
|
+
* in client.js.
|
|
40
|
+
* The imports of the api file (db, auth, etc.) are never resolved by Bun for
|
|
41
|
+
* the browser target — the plugin replaces the file before resolution.
|
|
42
|
+
*
|
|
43
|
+
* ─── Type safety ─────────────────────────────────────────────────────────────
|
|
44
|
+
*
|
|
45
|
+
* serverFn() is generic and preserves the full signature of the wrapped
|
|
46
|
+
* function:
|
|
47
|
+
*
|
|
48
|
+
* export const getUser = serverFn(async (id: number): Promise<User> => { … });
|
|
49
|
+
* // ^─────── type: (id: number) => Promise<User>
|
|
50
|
+
*
|
|
51
|
+
* The client stub generated by the plugin also accepts the same argument
|
|
52
|
+
* types because TypeScript resolves the import from the original .ts source
|
|
53
|
+
* file (which has the real types) — only the runtime implementation is
|
|
54
|
+
* replaced in the browser bundle. Types are never stripped by the plugin.
|
|
55
|
+
*
|
|
56
|
+
* ─── Error handling ──────────────────────────────────────────────────────────
|
|
57
|
+
*
|
|
58
|
+
* Errors thrown inside a serverFn are:
|
|
59
|
+
*
|
|
60
|
+
* On the server (direct call):
|
|
61
|
+
* - Propagated as normal JS exceptions.
|
|
62
|
+
* - The caller (asyncAction, onMount, etc.) handles them via try/catch
|
|
63
|
+
* or the error atom of asyncAction.
|
|
64
|
+
*
|
|
65
|
+
* On the client (RPC call via __callServerFn):
|
|
66
|
+
* - The RPC endpoint catches the error and returns:
|
|
67
|
+
* HTTP 500 { "error": "message", "code": "SERVER_FN_ERROR" }
|
|
68
|
+
* - __callServerFn re-throws as a plain Error with the server's message.
|
|
69
|
+
* - The caller handles it identically to a direct error.
|
|
70
|
+
*
|
|
71
|
+
* ─── Constraints ─────────────────────────────────────────────────────────────
|
|
72
|
+
*
|
|
73
|
+
* 1. serverFn MUST be used at the top level of a src/api/ file — not inside
|
|
74
|
+
* another function, class, or conditional. The plugin uses a static regex
|
|
75
|
+
* scan to extract export names; dynamic usage is not detected.
|
|
76
|
+
*
|
|
77
|
+
* 2. Arguments MUST be JSON-serialisable. The RPC layer uses JSON.stringify
|
|
78
|
+
* on the client and JSON.parse on the server. Dates, Sets, Maps, and class
|
|
79
|
+
* instances are not supported without a custom serialiser.
|
|
80
|
+
*
|
|
81
|
+
* 3. Return values MUST be JSON-serialisable for the same reason.
|
|
82
|
+
*
|
|
83
|
+
* 4. serverFn files must live under the configured apiDir (default: src/api/).
|
|
84
|
+
* Files outside that directory are not intercepted by the plugin and will
|
|
85
|
+
* not have their server-only imports stripped.
|
|
86
|
+
*
|
|
87
|
+
* 5. Do NOT import serverFn files from client-only code that is NOT also
|
|
88
|
+
* imported through a src/api/ path. If you import a serverFn file
|
|
89
|
+
* directly in src/client.ts (bypassing the api/ directory convention)
|
|
90
|
+
* the plugin will still intercept it by path, but this is a footgun —
|
|
91
|
+
* keep all server function definitions inside src/api/.
|
|
92
|
+
*/
|
|
93
|
+
export declare const SERVER_FN_MARKER: unique symbol;
|
|
94
|
+
/** Any async function — the constraint serverFn imposes on its argument. */
|
|
95
|
+
export type AnyAsyncFn = (...args: any[]) => Promise<any>;
|
|
96
|
+
/**
|
|
97
|
+
* Context injected as the SECOND argument to every server function.
|
|
98
|
+
*
|
|
99
|
+
* Convention: (input, { auth, roles, custom }: ServerContext)
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* export async function createPost(
|
|
103
|
+
* { content }: { content: string },
|
|
104
|
+
* { auth }: ServerContext,
|
|
105
|
+
* ) {
|
|
106
|
+
* checkAuth(auth);
|
|
107
|
+
* return PostsDAL.create(auth.sub, content);
|
|
108
|
+
* }
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* // No input
|
|
112
|
+
* export async function getFeed(_: void, { auth }: ServerContext) {
|
|
113
|
+
* checkAuth(auth);
|
|
114
|
+
* }
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* // Custom context from onRequest hook
|
|
118
|
+
* export async function getItems(_: void, { auth, custom }: ServerContext) {
|
|
119
|
+
* const tenant = custom.tenant;
|
|
120
|
+
* }
|
|
121
|
+
*/
|
|
122
|
+
export interface ServerContext {
|
|
123
|
+
/**
|
|
124
|
+
* Verified JWT payload for the current request.
|
|
125
|
+
* null if the request has no valid auth token.
|
|
126
|
+
*/
|
|
127
|
+
auth: Record<string, any> | null;
|
|
128
|
+
/**
|
|
129
|
+
* Role helpers derived from the auth payload.
|
|
130
|
+
*/
|
|
131
|
+
roles: {
|
|
132
|
+
/** Returns true if the user has at least one of the given roles */
|
|
133
|
+
has(...roles: string[]): boolean;
|
|
134
|
+
/** Returns true if the user has ALL of the given roles */
|
|
135
|
+
hasAll(...roles: string[]): boolean;
|
|
136
|
+
/** Returns the raw roles array */
|
|
137
|
+
list(): string[];
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* The raw incoming Request object.
|
|
141
|
+
* Escape hatch for reading headers, URL, etc. not covered by other helpers.
|
|
142
|
+
*/
|
|
143
|
+
req: Request | null;
|
|
144
|
+
/**
|
|
145
|
+
* Custom data set by onRequest hooks via RequestContext.set().
|
|
146
|
+
* Use this to pass per-request data (tenant, feature flags, etc.) into server functions.
|
|
147
|
+
*/
|
|
148
|
+
custom: Record<string, any>;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Run a function with the given ServerContext available via getContext().
|
|
152
|
+
* Uses AsyncLocalStorage so concurrent requests are properly isolated.
|
|
153
|
+
* @internal — called by fn-registry's callServerFn
|
|
154
|
+
*/
|
|
155
|
+
export declare function runWithServerContext<T>(ctx: ServerContext, fn: () => T): T;
|
|
156
|
+
/**
|
|
157
|
+
* Set the server context for the current function call.
|
|
158
|
+
* @internal — called by fn-registry's callServerFn
|
|
159
|
+
* @deprecated Use runWithServerContext() instead for async safety.
|
|
160
|
+
* Kept for backward compatibility.
|
|
161
|
+
*/
|
|
162
|
+
export declare function setCurrentServerContext(_ctx: ServerContext | null): void;
|
|
163
|
+
/**
|
|
164
|
+
* Get the current server context.
|
|
165
|
+
* Used inside server functions to access auth, roles, and the raw request.
|
|
166
|
+
*
|
|
167
|
+
* The framework wraps each server function call in `runWithServerContext()`,
|
|
168
|
+
* which sets up an AsyncLocalStorage scope. `getContext()` reads from
|
|
169
|
+
* that scope — fully isolated per request even under high concurrency.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* export async function getUsers(filter: string) {
|
|
173
|
+
* const { auth } = getContext();
|
|
174
|
+
* checkAuth(auth);
|
|
175
|
+
* return UsersDAL.getAll(filter);
|
|
176
|
+
* }
|
|
177
|
+
*/
|
|
178
|
+
export declare function getContext(): ServerContext;
|
|
179
|
+
/**
|
|
180
|
+
* Strips the trailing ServerContext parameter from a server function type,
|
|
181
|
+
* producing the type as seen by client callers.
|
|
182
|
+
*
|
|
183
|
+
* The framework always injects ServerContext — client callers never pass it.
|
|
184
|
+
* This type helper makes that explicit in shared type definitions.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* // Server:
|
|
188
|
+
* export async function getUsers(filter: string, ctx = {} as ServerContext): Promise<User[]> { }
|
|
189
|
+
*
|
|
190
|
+
* // Shared types file:
|
|
191
|
+
* export type GetUsersFn = ClientFn<typeof getUsers>;
|
|
192
|
+
* // GetUsersFn = (filter: string) => Promise<User[]>
|
|
193
|
+
*/
|
|
194
|
+
export type ClientFn<T extends (...args: any[]) => any> = T extends (...args: infer Args) => infer Return ? Args extends [...infer Rest, ServerContext?] ? (...args: Rest) => Return : (...args: Args) => Return : never;
|
|
195
|
+
/**
|
|
196
|
+
* Throw this from any server function to send a specific HTTP status,
|
|
197
|
+
* error message, and optional structured data to the client.
|
|
198
|
+
*
|
|
199
|
+
* The client receives an Error with .status, .code, and .data properties.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* // 404 — not found
|
|
203
|
+
* throw new ServerError(404, "Post not found");
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* // 403 — forbidden with extra context
|
|
207
|
+
* throw new ServerError(403, "Access denied", {
|
|
208
|
+
* code: "POST_PRIVATE",
|
|
209
|
+
* postId: id,
|
|
210
|
+
* });
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* // 400 — validation / business rule
|
|
214
|
+
* throw new ServerError(400, "Title too long", {
|
|
215
|
+
* code: "TITLE_TOO_LONG",
|
|
216
|
+
* max: 200,
|
|
217
|
+
* actual: body.title.length,
|
|
218
|
+
* });
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* // 409 — conflict
|
|
222
|
+
* throw new ServerError(409, "Email already registered", {
|
|
223
|
+
* code: "EMAIL_TAKEN",
|
|
224
|
+
* });
|
|
225
|
+
*/
|
|
226
|
+
export declare class ServerError extends Error {
|
|
227
|
+
/** HTTP status code sent to the client */
|
|
228
|
+
readonly status: number;
|
|
229
|
+
/** Machine-readable error code. Defaults to HTTP status name if not provided. */
|
|
230
|
+
readonly code: string;
|
|
231
|
+
/** Extra structured data sent alongside the error. Never throws — always JSON-serializable. */
|
|
232
|
+
readonly data: Record<string, unknown>;
|
|
233
|
+
constructor(status: number, message: string, data?: Record<string, unknown> & {
|
|
234
|
+
code?: string;
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* A server function — the return type of serverFn().
|
|
239
|
+
*
|
|
240
|
+
* On the server : identical to the original function type T.
|
|
241
|
+
* On the client : at runtime this is the RPC stub, but TypeScript still sees
|
|
242
|
+
* type T because it reads the .ts source, not the bundle.
|
|
243
|
+
*
|
|
244
|
+
* The marker properties are non-enumerable and invisible to callers.
|
|
245
|
+
*/
|
|
246
|
+
export type ServerFn<T extends AnyAsyncFn> = T & {
|
|
247
|
+
readonly [SERVER_FN_MARKER]: true;
|
|
248
|
+
/** Module-relative name used by the registry and RPC endpoint. */
|
|
249
|
+
readonly __fnName__: string;
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Wrap an async function as a server function.
|
|
253
|
+
*
|
|
254
|
+
* On the server the original function is returned as-is (with metadata).
|
|
255
|
+
* In the client bundle the entire module is replaced by the build plugin —
|
|
256
|
+
* this wrapper is never present in browser code.
|
|
257
|
+
*
|
|
258
|
+
* @param fn The async function to mark as server-only.
|
|
259
|
+
* @param name Optional explicit name. When omitted the function's `.name`
|
|
260
|
+
* property is used. Explicit names are useful for minified builds
|
|
261
|
+
* or when the inferred name would be ambiguous.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* export const getUsers = serverFn(async (filter?: string) => {
|
|
265
|
+
* return db.query("SELECT * FROM users WHERE name LIKE ?").all(filter);
|
|
266
|
+
* });
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* export const createUser = serverFn(
|
|
270
|
+
* async (data: { name: string; email: string }) => {
|
|
271
|
+
* if (!data.email) throw new Error("email is required");
|
|
272
|
+
* return db.query("INSERT INTO users (name,email) VALUES (?,?) RETURNING *")
|
|
273
|
+
* .get(data.name, data.email);
|
|
274
|
+
* }
|
|
275
|
+
* );
|
|
276
|
+
*/
|
|
277
|
+
export declare function serverFn<T extends AnyAsyncFn>(fn: T, name?: string): ServerFn<T>;
|
|
278
|
+
/**
|
|
279
|
+
* Returns true when `value` is a function wrapped with serverFn().
|
|
280
|
+
*
|
|
281
|
+
* Useful for the fn-registry scanner and for defensive checks inside the
|
|
282
|
+
* RPC endpoint handler.
|
|
283
|
+
*/
|
|
284
|
+
export declare function isServerFn(value: unknown): value is ServerFn<AnyAsyncFn>;
|
|
285
|
+
/**
|
|
286
|
+
* Retrieve the original HTTP Request for the current request context.
|
|
287
|
+
*
|
|
288
|
+
* Returns null when called outside a request context (e.g. from a client-side
|
|
289
|
+
* asyncAction call via the RPC stub).
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* export const getProfile = serverFn(async () => {
|
|
293
|
+
* const req = getServerContext();
|
|
294
|
+
* const token = req?.headers.get("authorization");
|
|
295
|
+
* if (!token) throw new Error("Unauthorized");
|
|
296
|
+
* return verifyToken(token);
|
|
297
|
+
* });
|
|
298
|
+
*/
|
|
299
|
+
export declare function getServerContext(): Request | null;
|
|
300
|
+
/**
|
|
301
|
+
* Convenience helper — read a cookie from the current request context.
|
|
302
|
+
*
|
|
303
|
+
* Returns null when called on the client or when the cookie is absent.
|
|
304
|
+
*
|
|
305
|
+
* @example
|
|
306
|
+
* export const getSession = serverFn(async () => {
|
|
307
|
+
* const sessionId = getCookie("session_id");
|
|
308
|
+
* if (!sessionId) throw new Error("Not authenticated");
|
|
309
|
+
* return sessionStore.get(sessionId);
|
|
310
|
+
* });
|
|
311
|
+
*/
|
|
312
|
+
export declare function getCookie(name: string): string | null;
|
|
313
|
+
/**
|
|
314
|
+
* Convenience helper — read a request header from the current request context.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* export const getUser = serverFn(async () => {
|
|
318
|
+
* const auth = getHeader("authorization");
|
|
319
|
+
* return verifyJWT(auth);
|
|
320
|
+
* });
|
|
321
|
+
*/
|
|
322
|
+
export declare function getHeader(name: string): string | null;
|
|
323
|
+
//# sourceMappingURL=server-fn.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-fn.d.ts","sourceRoot":"","sources":["../src/server-fn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2FG;AAMH,eAAO,MAAM,gBAAgB,eAA2B,CAAC;AAIzD,4EAA4E;AAC5E,MAAM,MAAM,UAAU,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;IAEjC;;OAE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