zero-com 1.13.2 → 1.13.3
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 +15 -0
- package/lib/runtime.d.ts +1 -0
- package/lib/runtime.js +10 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -229,6 +229,21 @@ When called outside `handle()`, the function executes directly with no context.
|
|
|
229
229
|
Error: context() called outside of a server function
|
|
230
230
|
```
|
|
231
231
|
|
|
232
|
+
Use `runWithContext` to provide a context in those cases:
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
import { runWithContext } from 'zero-com'
|
|
236
|
+
|
|
237
|
+
const authorize = async (credentials) => {
|
|
238
|
+
return runWithContext({}, async () => {
|
|
239
|
+
const user = await getUserByCredentials(credentials.email, credentials.password)
|
|
240
|
+
return user ?? null
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
`runWithContext` accepts any object as the context value, which will be returned by `context()` inside any server function called within the callback.
|
|
246
|
+
|
|
232
247
|
When called inside `handle()`, context is propagated automatically to the function and any nested `func()` calls, same as the normal RPC path.
|
|
233
248
|
|
|
234
249
|
## File boundary rule
|
package/lib/runtime.d.ts
CHANGED
|
@@ -11,4 +11,5 @@ declare global {
|
|
|
11
11
|
export declare function context<T = unknown>(): T;
|
|
12
12
|
export declare function func<F extends (...args: any[]) => any>(fn: F): F;
|
|
13
13
|
export declare const handle: (funcId: string, ctx: any, args: any[]) => any;
|
|
14
|
+
export declare const runWithContext: <T>(ctx: any, fn: () => T) => T;
|
|
14
15
|
export declare const call: (fn: (funcId: string, args: any[]) => any) => void;
|
package/lib/runtime.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.call = exports.handle = void 0;
|
|
3
|
+
exports.call = exports.runWithContext = exports.handle = void 0;
|
|
4
4
|
exports.context = context;
|
|
5
5
|
exports.func = func;
|
|
6
6
|
// Context storage - only available on server (Node.js)
|
|
@@ -78,6 +78,15 @@ const handle = (funcId, ctx, args) => {
|
|
|
78
78
|
return storage.run(ctx, () => fn(...args));
|
|
79
79
|
};
|
|
80
80
|
exports.handle = handle;
|
|
81
|
+
// Run a callback within a context, making context() available inside it.
|
|
82
|
+
// Use this in server-only code that does not go through handle() (e.g. auth callbacks).
|
|
83
|
+
const runWithContext = (ctx, fn) => {
|
|
84
|
+
const storage = getContextStorage();
|
|
85
|
+
if (!storage)
|
|
86
|
+
throw new Error('runWithContext() is only available on the server');
|
|
87
|
+
return storage.run(ctx, fn);
|
|
88
|
+
};
|
|
89
|
+
exports.runWithContext = runWithContext;
|
|
81
90
|
// Client calls this to set up transport (overrides default server-side behavior)
|
|
82
91
|
// In production mode: transformed by plugin to assignment
|
|
83
92
|
const call = (fn) => {
|