@redmix/context 0.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Redmix
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # Context
2
+
3
+ ## About
4
+
5
+ This package contains code for the global context used on the API side of a
6
+ Redwood application. It's automatically available in services, auth functions
7
+ and custom functions.
8
+
9
+ ## Serveful environments
10
+
11
+ In serverful environments with Fastify the global context is injected by a
12
+ Fastify `onRequest` hook.
13
+
14
+ ## Serverless environments
15
+
16
+ Babel is used to automatically wrap functions with code that makes the context
17
+ available.
@@ -0,0 +1,14 @@
1
+ export interface GlobalContext extends Record<string, unknown> {
2
+ }
3
+ export declare const createContextProxy: (target: GlobalContext) => GlobalContext;
4
+ export declare let context: GlobalContext;
5
+ /**
6
+ * Set the contents of the global context object.
7
+ *
8
+ * This completely replaces the existing context values such as currentUser.
9
+ *
10
+ * If you wish to extend the context simply use the `context` object directly,
11
+ * such as `context.magicNumber = 1`, or `setContext({ ...context, magicNumber: 1 })`
12
+ */
13
+ export declare const setContext: (newContext: GlobalContext) => GlobalContext;
14
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/context.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,aAAc,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAAG;AAEjE,eAAO,MAAM,kBAAkB,WAAY,aAAa,kBAevD,CAAA;AAED,eAAO,IAAI,OAAO,EAAE,aAAsC,CAAA;AAE1D;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,eAAgB,aAAa,KAAG,aAWtD,CAAA"}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var context_exports = {};
20
+ __export(context_exports, {
21
+ context: () => context,
22
+ createContextProxy: () => createContextProxy,
23
+ setContext: () => setContext
24
+ });
25
+ module.exports = __toCommonJS(context_exports);
26
+ var import_store = require("./store.js");
27
+ const createContextProxy = (target) => {
28
+ return new Proxy(target, {
29
+ get: (_target, property) => {
30
+ const store = (0, import_store.getAsyncStoreInstance)().getStore();
31
+ const ctx = store?.get("context") || {};
32
+ return ctx[property];
33
+ },
34
+ set: (_target, property, newVal) => {
35
+ const store = (0, import_store.getAsyncStoreInstance)().getStore();
36
+ const ctx = store?.get("context") || {};
37
+ ctx[property] = newVal;
38
+ store?.set("context", ctx);
39
+ return true;
40
+ }
41
+ });
42
+ };
43
+ let context = createContextProxy({});
44
+ const setContext = (newContext) => {
45
+ context = createContextProxy(newContext);
46
+ const store = (0, import_store.getAsyncStoreInstance)().getStore();
47
+ store?.set("context", newContext);
48
+ return context;
49
+ };
50
+ // Annotate the CommonJS export names for ESM import in node:
51
+ 0 && (module.exports = {
52
+ context,
53
+ createContextProxy,
54
+ setContext
55
+ });
@@ -0,0 +1,5 @@
1
+ import type { GlobalContext } from './context.js';
2
+ declare global {
3
+ const context: GlobalContext;
4
+ }
5
+ //# sourceMappingURL=global.api-auto-imports.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"global.api-auto-imports.d.ts","sourceRoot":"","sources":["../../src/global.api-auto-imports.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,CAAC,MAAM,CAAC;IACb,MAAM,OAAO,EAAE,aAAa,CAAA;CAC7B"}
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,4 @@
1
+ export * from './context.js';
2
+ import './global.api-auto-imports.js';
3
+ export * from './global.api-auto-imports.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAG5B,OAAO,8BAA8B,CAAA;AACrC,cAAc,8BAA8B,CAAA"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+ var index_exports = {};
17
+ module.exports = __toCommonJS(index_exports);
18
+ __reExport(index_exports, require("./context.js"), module.exports);
19
+ var import_global_api_auto_imports = require("./global.api-auto-imports.js");
20
+ __reExport(index_exports, require("./global.api-auto-imports.js"), module.exports);
21
+ // Annotate the CommonJS export names for ESM import in node:
22
+ 0 && (module.exports = {
23
+ ...require("./context.js"),
24
+ ...require("./global.api-auto-imports.js")
25
+ });
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,9 @@
1
+ import { AsyncLocalStorage } from 'async_hooks';
2
+ import type { GlobalContext } from './context.js';
3
+ /**
4
+ * This returns a AsyncLocalStorage instance, not the actual store.
5
+ * Should not be used by Redwood apps directly. The framework handles
6
+ * this.
7
+ */
8
+ export declare const getAsyncStoreInstance: () => AsyncLocalStorage<Map<string, GlobalContext>>;
9
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAIjD;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,qDAKjC,CAAA"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var store_exports = {};
20
+ __export(store_exports, {
21
+ getAsyncStoreInstance: () => getAsyncStoreInstance
22
+ });
23
+ module.exports = __toCommonJS(store_exports);
24
+ var import_async_hooks = require("async_hooks");
25
+ let CONTEXT_STORAGE;
26
+ const getAsyncStoreInstance = () => {
27
+ if (!CONTEXT_STORAGE) {
28
+ CONTEXT_STORAGE = new import_async_hooks.AsyncLocalStorage();
29
+ }
30
+ return CONTEXT_STORAGE;
31
+ };
32
+ // Annotate the CommonJS export names for ESM import in node:
33
+ 0 && (module.exports = {
34
+ getAsyncStoreInstance
35
+ });
@@ -0,0 +1,14 @@
1
+ export interface GlobalContext extends Record<string, unknown> {
2
+ }
3
+ export declare const createContextProxy: (target: GlobalContext) => GlobalContext;
4
+ export declare let context: GlobalContext;
5
+ /**
6
+ * Set the contents of the global context object.
7
+ *
8
+ * This completely replaces the existing context values such as currentUser.
9
+ *
10
+ * If you wish to extend the context simply use the `context` object directly,
11
+ * such as `context.magicNumber = 1`, or `setContext({ ...context, magicNumber: 1 })`
12
+ */
13
+ export declare const setContext: (newContext: GlobalContext) => GlobalContext;
14
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,aAAc,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAAG;AAEjE,eAAO,MAAM,kBAAkB,WAAY,aAAa,kBAevD,CAAA;AAED,eAAO,IAAI,OAAO,EAAE,aAAsC,CAAA;AAE1D;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,eAAgB,aAAa,KAAG,aAWtD,CAAA"}
@@ -0,0 +1,29 @@
1
+ import { getAsyncStoreInstance } from "./store.js";
2
+ const createContextProxy = (target) => {
3
+ return new Proxy(target, {
4
+ get: (_target, property) => {
5
+ const store = getAsyncStoreInstance().getStore();
6
+ const ctx = store?.get("context") || {};
7
+ return ctx[property];
8
+ },
9
+ set: (_target, property, newVal) => {
10
+ const store = getAsyncStoreInstance().getStore();
11
+ const ctx = store?.get("context") || {};
12
+ ctx[property] = newVal;
13
+ store?.set("context", ctx);
14
+ return true;
15
+ }
16
+ });
17
+ };
18
+ let context = createContextProxy({});
19
+ const setContext = (newContext) => {
20
+ context = createContextProxy(newContext);
21
+ const store = getAsyncStoreInstance().getStore();
22
+ store?.set("context", newContext);
23
+ return context;
24
+ };
25
+ export {
26
+ context,
27
+ createContextProxy,
28
+ setContext
29
+ };
@@ -0,0 +1,5 @@
1
+ import type { GlobalContext } from './context.js';
2
+ declare global {
3
+ const context: GlobalContext;
4
+ }
5
+ //# sourceMappingURL=global.api-auto-imports.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"global.api-auto-imports.d.ts","sourceRoot":"","sources":["../src/global.api-auto-imports.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,CAAC,MAAM,CAAC;IACb,MAAM,OAAO,EAAE,aAAa,CAAA;CAC7B"}
File without changes
@@ -0,0 +1,4 @@
1
+ export * from './context.js';
2
+ import './global.api-auto-imports.js';
3
+ export * from './global.api-auto-imports.js';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA;AAG5B,OAAO,8BAA8B,CAAA;AACrC,cAAc,8BAA8B,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./context.js";
2
+ import "./global.api-auto-imports.js";
3
+ export * from "./global.api-auto-imports.js";
@@ -0,0 +1,9 @@
1
+ import { AsyncLocalStorage } from 'async_hooks';
2
+ import type { GlobalContext } from './context.js';
3
+ /**
4
+ * This returns a AsyncLocalStorage instance, not the actual store.
5
+ * Should not be used by Redwood apps directly. The framework handles
6
+ * this.
7
+ */
8
+ export declare const getAsyncStoreInstance: () => AsyncLocalStorage<Map<string, GlobalContext>>;
9
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAE/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAIjD;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,qDAKjC,CAAA"}
package/dist/store.js ADDED
@@ -0,0 +1,11 @@
1
+ import { AsyncLocalStorage } from "async_hooks";
2
+ let CONTEXT_STORAGE;
3
+ const getAsyncStoreInstance = () => {
4
+ if (!CONTEXT_STORAGE) {
5
+ CONTEXT_STORAGE = new AsyncLocalStorage();
6
+ }
7
+ return CONTEXT_STORAGE;
8
+ };
9
+ export {
10
+ getAsyncStoreInstance
11
+ };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@redmix/context",
3
+ "version": "0.0.1",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/redmix-run/redmix.git",
7
+ "directory": "packages/context"
8
+ },
9
+ "license": "MIT",
10
+ "type": "module",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ },
17
+ "default": {
18
+ "types": "./dist/cjs/index.d.ts",
19
+ "default": "./dist/cjs/index.js"
20
+ }
21
+ },
22
+ "./dist/store": {
23
+ "import": {
24
+ "types": "./dist/store.d.ts",
25
+ "default": "./dist/store.js"
26
+ },
27
+ "default": {
28
+ "types": "./dist/cjs/store.d.ts",
29
+ "default": "./dist/cjs/store.js"
30
+ }
31
+ }
32
+ },
33
+ "main": "./dist/cjs/index.js",
34
+ "module": "./dist/index.js",
35
+ "types": "./dist/index.d.ts",
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "scripts": {
40
+ "build": "tsx ./build.mts",
41
+ "build:pack": "yarn pack -o redmix-context.tgz",
42
+ "build:types": "tsc --build --verbose ./tsconfig.build.json",
43
+ "build:types-cjs": "tsc --build --verbose ./tsconfig.cjs.json",
44
+ "build:watch": "nodemon --watch src --ext \"js,jsx,ts,tsx\" --ignore dist --exec \"yarn build\"",
45
+ "check:attw": "yarn attw -P",
46
+ "check:package": "concurrently npm:check:attw yarn:publint",
47
+ "prepublishOnly": "NODE_ENV=production yarn build"
48
+ },
49
+ "devDependencies": {
50
+ "@arethetypeswrong/cli": "0.16.4",
51
+ "@redmix/framework-tools": "0.0.1",
52
+ "concurrently": "8.2.2",
53
+ "publint": "0.3.11",
54
+ "tsx": "4.19.3",
55
+ "typescript": "5.6.2"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public"
59
+ },
60
+ "gitHead": "688027c97502c500ebbede9cdc7cc51545a8dcf3"
61
+ }