wirejs-resources 0.1.177 → 0.1.179

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.
@@ -0,0 +1,6 @@
1
+ export type ApiGetter = () => any | Promise<any>;
2
+ export type RequestScope = {
3
+ context: any;
4
+ getApi?: ApiGetter;
5
+ api?: any;
6
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { RequestScope } from './request-scope-types.js';
2
+ export declare function runWithRequestScope<T>(scope: RequestScope, run: () => T): T;
3
+ export declare function getRequestScope(): RequestScope | undefined;
4
+ export declare function callApiFromRequestScope(method: string[], args: any[]): Promise<{
5
+ readonly handled: false;
6
+ readonly data?: undefined;
7
+ } | {
8
+ readonly handled: true;
9
+ readonly data: any;
10
+ }>;
@@ -0,0 +1,55 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ import { requiresContext } from './context.js';
3
+ const requestScope = new AsyncLocalStorage();
4
+ console.log('request scope created');
5
+ export function runWithRequestScope(scope, run) {
6
+ return requestScope.run(scope, run);
7
+ }
8
+ export function getRequestScope() {
9
+ return requestScope.getStore();
10
+ }
11
+ async function resolveApi(scope) {
12
+ if (scope.api) {
13
+ return scope.api;
14
+ }
15
+ if (!scope.getApi) {
16
+ return undefined;
17
+ }
18
+ scope.api = await scope.getApi();
19
+ return scope.api;
20
+ }
21
+ async function invokeApiMethod(api, method, args, context, withExplicitContextArg) {
22
+ console.log(`calling ${method.join('.')} on `, Object.entries(api));
23
+ const [scope, ...rest] = method;
24
+ if (!scope || !api || (typeof api !== 'object' && typeof api !== 'function')) {
25
+ throw new Error(`Invalid API method path: ${method.join('.')}`);
26
+ }
27
+ if (rest.length === 0) {
28
+ const target = api[scope];
29
+ if (requiresContext(api[scope])) {
30
+ return target(context, ...(withExplicitContextArg ? args.slice(1) : args));
31
+ }
32
+ return target(...args);
33
+ }
34
+ return invokeApiMethod(api[scope], rest, args, context, withExplicitContextArg);
35
+ }
36
+ export async function callApiFromRequestScope(method, args) {
37
+ const scope = getRequestScope();
38
+ if (!scope) {
39
+ return {
40
+ handled: false,
41
+ };
42
+ }
43
+ const withExplicitContextArg = args[0] === scope.context;
44
+ const api = await resolveApi(scope);
45
+ if (!api) {
46
+ return {
47
+ handled: false,
48
+ };
49
+ }
50
+ const data = await invokeApiMethod(api, method, args, scope.context, withExplicitContextArg);
51
+ return {
52
+ handled: true,
53
+ data,
54
+ };
55
+ }
@@ -1,4 +1,13 @@
1
1
  async function callApi(INTERNAL_API_URL, method, ...args) {
2
+ console.log('callApi from wirejs-resources', method, ...args);
3
+ const directResponse = typeof globalThis.callApiFromRequestScope === 'function' ?
4
+ await globalThis.callApiFromRequestScope(method, [...args]) :
5
+ { handled: false };
6
+ if (directResponse.handled) {
7
+ console.log({ handled: true, directResponse });
8
+ return directResponse.data;
9
+ }
10
+ console.log({ handled: false, directResponse });
2
11
  function isNode() {
3
12
  return typeof args[0]?.cookies?.getAll === 'function';
4
13
  }
package/dist/config.d.ts CHANGED
@@ -5,4 +5,38 @@ export type DeploymentConfig = {
5
5
  bundleNodeModules?: string[];
6
6
  bundleFormat?: 'cjs' | 'esm';
7
7
  bundleMinify?: boolean;
8
+ /**
9
+ * Function URL auth mode for the API Lambda.
10
+ *
11
+ * - `aws-iam` (default): CloudFront must sign origin requests (OAC)
12
+ * - `none`: Function URL does not require SigV4 (useful for CI smoke tests)
13
+ */
14
+ functionUrlAuthType?: 'aws-iam' | 'none';
15
+ /**
16
+ * Optional list of custom domain names to use for hosting.
17
+ * Supported by `wirejs-deploy-cdk`. When provided, the deployment
18
+ * will configure CloudFront with these domain names and output DNS
19
+ * configuration instructions.
20
+ */
21
+ domainNames?: string[];
22
+ /**
23
+ * Optional mapping of branch name to custom domain(s).
24
+ *
25
+ * Example:
26
+ * {
27
+ * main: 'app.example.com',
28
+ * staging: ['staging.example.com', 'preview.example.com'],
29
+ * '*': '*.example.com'
30
+ * }
31
+ *
32
+ * Template behavior:
33
+ * - Any `*` in a selected domain value is replaced with the current
34
+ * branch id slug (lowercase, non-alphanumeric => `-`).
35
+ * - Example: BRANCH_ID='feature/login' + '*.example.com' =>
36
+ * 'feature-login.example.com'.
37
+ *
38
+ * Branch resolution is handled by deployment providers (for CDK, using
39
+ * BRANCH_ID / GITHUB_REF_NAME).
40
+ */
41
+ domainsByBranch?: Record<string, string | string[]>;
8
42
  };
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export * from './services/oidc-providers.js';
4
4
  export { CookieJar } from './adapters/cookie-jar.js';
5
5
  export { SignedCookie } from './adapters/signed-cookie.js';
6
6
  export { withContext, requiresContext, Context, ContextWrapped } from './adapters/context.js';
7
+ export { runWithRequestScope, getRequestScope, callApiFromRequestScope, } from './adapters/request-scope.js';
7
8
  export { Resource } from './resource.js';
8
9
  export { overrides } from './overrides.js';
9
10
  export { Setting } from './resources/setting.js';
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ export * from './services/oidc-providers.js';
4
4
  export { CookieJar } from './adapters/cookie-jar.js';
5
5
  export { SignedCookie } from './adapters/signed-cookie.js';
6
6
  export { withContext, requiresContext, Context } from './adapters/context.js';
7
+ export { runWithRequestScope, getRequestScope, callApiFromRequestScope, } from './adapters/request-scope.js';
7
8
  export { Resource } from './resource.js';
8
9
  export { overrides } from './overrides.js';
9
10
  export { Setting } from './resources/setting.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-resources",
3
- "version": "0.1.177",
3
+ "version": "0.1.179",
4
4
  "description": "Basic services and server-side resources for wirejs apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",