@bleedingdev/modern-js-bff-effect 3.9.0-ultramodern.2 → 3.9.0-ultramodern.21

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.
Files changed (142) hide show
  1. package/README.md +20 -4
  2. package/dist/cjs/assembly.js +50 -0
  3. package/dist/cjs/{effect-client/guards.js → data-platform/batch/options.js} +13 -9
  4. package/dist/cjs/data-platform/batch/queue.js +48 -83
  5. package/dist/cjs/data-platform/trace.js +2 -2
  6. package/dist/cjs/effect/context.js +28 -4
  7. package/dist/cjs/effect/edge-dispatcher.js +12 -12
  8. package/dist/cjs/effect/edge.js +36 -3
  9. package/dist/cjs/effect/endpoint-contracts.js +3 -4
  10. package/dist/cjs/effect/entry-shape.js +40 -12
  11. package/dist/cjs/effect/generated-entry.js +57 -0
  12. package/dist/cjs/effect/handler/batch-handler.js +7 -7
  13. package/dist/cjs/effect/handler/batch.js +8 -10
  14. package/dist/cjs/effect/handler/definition.js +1 -21
  15. package/dist/cjs/effect/index.js +8 -8
  16. package/dist/cjs/effect/module.js +8 -4
  17. package/dist/cjs/effect/operation-context.js +10 -14
  18. package/dist/cjs/effect/rpcSerialization.js +0 -2
  19. package/dist/cjs/effect-client/cross-project.js +89 -0
  20. package/dist/cjs/effect-client/index.js +5 -3
  21. package/dist/cjs/microvertical-api.js +120 -0
  22. package/dist/esm/assembly.mjs +12 -0
  23. package/dist/esm/data-platform/batch/options.mjs +10 -0
  24. package/dist/esm/data-platform/batch/queue.mjs +48 -83
  25. package/dist/esm/data-platform/trace.mjs +1 -1
  26. package/dist/esm/effect/context.mjs +21 -6
  27. package/dist/esm/effect/edge-dispatcher.mjs +6 -6
  28. package/dist/esm/effect/edge.mjs +3 -0
  29. package/dist/esm/effect/endpoint-contracts.mjs +2 -3
  30. package/dist/esm/effect/entry-shape-registry.mjs +2 -2
  31. package/dist/esm/effect/entry-shape.mjs +29 -6
  32. package/dist/esm/effect/generated-entry.mjs +19 -0
  33. package/dist/esm/effect/handler/batch-handler.mjs +8 -8
  34. package/dist/esm/effect/handler/batch.mjs +9 -8
  35. package/dist/esm/effect/handler/definition.mjs +1 -21
  36. package/dist/esm/effect/index.mjs +1 -1
  37. package/dist/esm/effect/module.mjs +8 -4
  38. package/dist/esm/effect/operation-context.mjs +3 -4
  39. package/dist/esm/effect/rpcSerialization.mjs +0 -2
  40. package/dist/esm/effect-client/cross-project.mjs +51 -0
  41. package/dist/esm/effect-client/index.mjs +4 -2
  42. package/dist/esm/microvertical-api.mjs +67 -0
  43. package/dist/esm-node/assembly.mjs +13 -0
  44. package/dist/esm-node/data-platform/batch/options.mjs +11 -0
  45. package/dist/esm-node/data-platform/batch/queue.mjs +48 -83
  46. package/dist/esm-node/data-platform/trace.mjs +1 -1
  47. package/dist/esm-node/effect/context.mjs +21 -6
  48. package/dist/esm-node/effect/edge-dispatcher.mjs +6 -6
  49. package/dist/esm-node/effect/edge.mjs +3 -0
  50. package/dist/esm-node/effect/endpoint-contracts.mjs +2 -3
  51. package/dist/esm-node/effect/entry-shape.mjs +29 -6
  52. package/dist/esm-node/effect/generated-entry.mjs +20 -0
  53. package/dist/esm-node/effect/handler/batch-handler.mjs +8 -8
  54. package/dist/esm-node/effect/handler/batch.mjs +9 -8
  55. package/dist/esm-node/effect/handler/definition.mjs +1 -21
  56. package/dist/esm-node/effect/index.mjs +1 -1
  57. package/dist/esm-node/effect/module.mjs +8 -4
  58. package/dist/esm-node/effect/operation-context.mjs +3 -4
  59. package/dist/esm-node/effect/rpcSerialization.mjs +0 -2
  60. package/dist/esm-node/effect-client/cross-project.mjs +52 -0
  61. package/dist/esm-node/effect-client/index.mjs +4 -2
  62. package/dist/esm-node/microvertical-api.mjs +68 -0
  63. package/dist/types/assembly.d.ts +10 -0
  64. package/dist/types/data-platform/batch/options.d.ts +7 -0
  65. package/dist/types/data-platform/batch/queue.d.ts +0 -1
  66. package/dist/types/effect/context.d.ts +15 -2
  67. package/dist/types/effect/edge-dispatcher.d.ts +3 -3
  68. package/dist/types/effect/edge.d.ts +3 -0
  69. package/dist/types/effect/endpoint-contracts.d.ts +2 -20
  70. package/dist/types/effect/entry-shape.d.ts +16 -2
  71. package/dist/types/effect/generated-entry.d.ts +7 -0
  72. package/dist/types/effect/handler/batch.d.ts +7 -2
  73. package/dist/types/effect/handler/types.d.ts +2 -17
  74. package/dist/types/effect/handler.d.ts +1 -1
  75. package/dist/types/effect/module.d.ts +2 -1
  76. package/dist/types/effect/operation-context.d.ts +1 -2
  77. package/dist/types/effect-client/cross-project.d.ts +10 -0
  78. package/dist/types/effect-client/index.d.ts +7 -4
  79. package/dist/types/microvertical-api.d.ts +65 -0
  80. package/package.json +47 -42
  81. package/src/assembly.ts +43 -0
  82. package/src/data-platform/batch/options.ts +20 -0
  83. package/src/data-platform/batch/queue.ts +63 -92
  84. package/src/data-platform/trace.ts +1 -1
  85. package/src/effect/context.ts +64 -12
  86. package/src/effect/edge-dispatcher.ts +14 -11
  87. package/src/effect/edge.ts +6 -0
  88. package/src/effect/endpoint-contracts.ts +3 -24
  89. package/src/effect/entry-shape.ts +105 -10
  90. package/src/effect/generated-entry.ts +36 -0
  91. package/src/effect/handler/batch-handler.ts +27 -28
  92. package/src/effect/handler/batch.ts +9 -13
  93. package/src/effect/handler/definition.ts +0 -45
  94. package/src/effect/handler/http.ts +2 -2
  95. package/src/effect/handler/types.ts +3 -32
  96. package/src/effect/handler.ts +0 -1
  97. package/src/effect/index.ts +1 -1
  98. package/src/effect/module.ts +11 -4
  99. package/src/effect/operation-context.ts +2 -6
  100. package/src/effect/rpcSerialization.ts +0 -2
  101. package/src/effect-client/cross-project.ts +104 -0
  102. package/src/effect-client/index.ts +17 -7
  103. package/src/microvertical-api.ts +102 -0
  104. package/dist/cjs/effect/edge-context.js +0 -61
  105. package/dist/cjs/effect-client/batch.js +0 -73
  106. package/dist/cjs/effect-client/envelope.js +0 -145
  107. package/dist/cjs/effect-client/request-context.js +0 -64
  108. package/dist/cjs/effect-client/request.js +0 -66
  109. package/dist/cjs/effect-client/runtime.js +0 -100
  110. package/dist/cjs/effect-client/types.js +0 -18
  111. package/dist/esm/effect/edge-context.mjs +0 -14
  112. package/dist/esm/effect-client/batch.mjs +0 -35
  113. package/dist/esm/effect-client/envelope.mjs +0 -104
  114. package/dist/esm/effect-client/guards.mjs +0 -3
  115. package/dist/esm/effect-client/request-context.mjs +0 -23
  116. package/dist/esm/effect-client/request.mjs +0 -28
  117. package/dist/esm/effect-client/runtime.mjs +0 -62
  118. package/dist/esm/effect-client/types.mjs +0 -0
  119. package/dist/esm-node/effect/edge-context.mjs +0 -15
  120. package/dist/esm-node/effect-client/batch.mjs +0 -36
  121. package/dist/esm-node/effect-client/envelope.mjs +0 -105
  122. package/dist/esm-node/effect-client/guards.mjs +0 -4
  123. package/dist/esm-node/effect-client/request-context.mjs +0 -24
  124. package/dist/esm-node/effect-client/request.mjs +0 -29
  125. package/dist/esm-node/effect-client/runtime.mjs +0 -63
  126. package/dist/esm-node/effect-client/types.mjs +0 -1
  127. package/dist/types/effect/edge-context.d.ts +0 -5
  128. package/dist/types/effect-client/batch.d.ts +0 -2
  129. package/dist/types/effect-client/envelope.d.ts +0 -15
  130. package/dist/types/effect-client/guards.d.ts +0 -2
  131. package/dist/types/effect-client/request-context.d.ts +0 -3
  132. package/dist/types/effect-client/request.d.ts +0 -1
  133. package/dist/types/effect-client/runtime.d.ts +0 -5
  134. package/dist/types/effect-client/types.d.ts +0 -68
  135. package/src/effect/edge-context.ts +0 -51
  136. package/src/effect-client/batch.ts +0 -50
  137. package/src/effect-client/envelope.ts +0 -238
  138. package/src/effect-client/guards.ts +0 -6
  139. package/src/effect-client/request-context.ts +0 -54
  140. package/src/effect-client/request.ts +0 -51
  141. package/src/effect-client/runtime.ts +0 -122
  142. package/src/effect-client/types.ts +0 -84
@@ -1,29 +0,0 @@
1
- import "node:module";
2
- import { isRecord } from "./guards.mjs";
3
- const METHODS_WITHOUT_BODY = new Set([
4
- 'GET',
5
- 'DELETE',
6
- 'HEAD',
7
- 'OPTIONS'
8
- ]);
9
- const normalizeRequest = (method, request)=>{
10
- if (!isRecord(request)) return {};
11
- const payload = {
12
- ...request
13
- };
14
- if (isRecord(request.path) && !isRecord(payload.params)) payload.params = request.path;
15
- if (isRecord(request.urlParams) && !isRecord(payload.query)) payload.query = request.urlParams;
16
- if (isRecord(request.headers) && !isRecord(payload.headers)) payload.headers = request.headers;
17
- if ('payload' in request && void 0 !== request.payload) if ("u" > typeof FormData && request.payload instanceof FormData && !('formData' in payload)) payload.formData = request.payload;
18
- else if (METHODS_WITHOUT_BODY.has(method)) {
19
- if (isRecord(request.payload)) payload.query = isRecord(payload.query) ? {
20
- ...payload.query,
21
- ...request.payload
22
- } : request.payload;
23
- else if (!('body' in payload)) payload.body = request.payload;
24
- } else if (!isRecord(request.payload) || 'data' in payload) {
25
- if (!('body' in payload)) payload.body = request.payload;
26
- } else payload.data = request.payload;
27
- return payload;
28
- };
29
- export { normalizeRequest };
@@ -1,63 +0,0 @@
1
- import "node:module";
2
- import { configureGeneratedEffectClientRuntime } from "./batch.mjs";
3
- import { prepareEffectRequest } from "./envelope.mjs";
4
- import { createEffectRequestContext as external_request_context_mjs_createEffectRequestContext } from "./request-context.mjs";
5
- const createGeneratedEffectClient = (manifest, config, requestRuntime)=>{
6
- const createRequest = requestRuntime.createRequest;
7
- const configureRequest = 'function' == typeof requestRuntime.configure ? requestRuntime.configure : void 0;
8
- const createRequestContextHeaders = 'function' == typeof requestRuntime.createRequestContextHeaders ? requestRuntime.createRequestContextHeaders : void 0;
9
- const httpMethodDecider = config.httpMethodDecider || 'functionName';
10
- const port = config.useEnvPort && "u" > typeof process && process.env && process.env.PORT ? process.env.PORT : config.port;
11
- configureGeneratedEffectClientRuntime(config, configureRequest);
12
- const createEffectRequestContext = (requestContext)=>external_request_context_mjs_createEffectRequestContext(createRequestContextHeaders, requestContext);
13
- const client = {};
14
- const operationManifest = {};
15
- for (const endpoint of manifest.endpoints){
16
- const operationId = `${endpoint.method}:${endpoint.routePath}`;
17
- const operation = {
18
- appNamespace: config.appNamespace,
19
- apiId: endpoint.apiId,
20
- group: endpoint.group,
21
- endpoint: endpoint.endpoint,
22
- operationId,
23
- routePath: endpoint.routePath,
24
- method: endpoint.method,
25
- operationVersion: endpoint.operationVersion,
26
- schemaHash: endpoint.schemaHash,
27
- version: endpoint.operationVersion
28
- };
29
- const sender = createRequest({
30
- path: endpoint.routePath,
31
- method: endpoint.method,
32
- port,
33
- operationContext: {
34
- operationId,
35
- routePath: endpoint.routePath,
36
- method: endpoint.method,
37
- schemaHash: endpoint.schemaHash,
38
- operationVersion: endpoint.operationVersion
39
- },
40
- httpMethodDecider,
41
- ...config.requestId ? {
42
- requestId: config.requestId
43
- } : {}
44
- });
45
- const call = (request = {})=>sender(prepareEffectRequest({
46
- endpoint,
47
- operation,
48
- request,
49
- config,
50
- createEffectRequestContext
51
- }));
52
- client[endpoint.group] ??= {};
53
- client[endpoint.group][endpoint.endpoint] = call;
54
- operationManifest[endpoint.group] ??= {};
55
- operationManifest[endpoint.group][endpoint.endpoint] = operation;
56
- }
57
- return {
58
- client,
59
- operationManifest,
60
- createEffectRequestContext
61
- };
62
- };
63
- export { createGeneratedEffectClient };
@@ -1 +0,0 @@
1
- import "node:module";
@@ -1,5 +0,0 @@
1
- import { type EffectContext } from './operation-context.js';
2
- export { type CreateEffectOperationContextOptions, createEffectOperationContext, type EffectContext, } from './operation-context.js';
3
- export declare const runWithEffectContext: <T>(context: EffectContext, cb: () => T) => T;
4
- export declare const useEffectContext: () => EffectContext;
5
- export declare const useOperationContext: () => import("@modern-js/create-request").OperationContext;
@@ -1,2 +0,0 @@
1
- import type { EffectRequestRuntime, GeneratedEffectClientConfig } from './types.js';
2
- export declare const configureGeneratedEffectClientRuntime: (config: GeneratedEffectClientConfig, configureRequest: EffectRequestRuntime['configure'] | undefined) => void;
@@ -1,15 +0,0 @@
1
- import type { EffectOperationDescriptor, EffectRequestContext, EffectRequestContextInput, GeneratedEffectClientConfig, GeneratedEffectEndpoint } from './types.js';
2
- /**
3
- * Resolves the current page origin with a configurable fallback. Single
4
- * implementation shared by the configure() bootstrap and the data envelope
5
- * (the generated template previously duplicated data-platform's
6
- * `resolveRuntimeOrigin` with a different fallback).
7
- */
8
- export declare const resolveOrigin: (defaultOrigin: string) => string;
9
- export declare const prepareEffectRequest: (options: {
10
- endpoint: GeneratedEffectEndpoint;
11
- operation: EffectOperationDescriptor;
12
- request: unknown;
13
- config: GeneratedEffectClientConfig;
14
- createEffectRequestContext: (requestContext: EffectRequestContextInput) => EffectRequestContext;
15
- }) => Record<string, unknown>;
@@ -1,2 +0,0 @@
1
- export declare const isRecord: (value: unknown) => value is Record<string, unknown>;
2
- export declare const stringOrUndefined: (value: unknown) => string | undefined;
@@ -1,3 +0,0 @@
1
- import type { EffectRequestContext, EffectRequestContextInput, EffectRequestRuntime } from './types.js';
2
- export declare const createEffectRequestContext: (createRequestContextHeaders: EffectRequestRuntime['createRequestContextHeaders'], requestContext: EffectRequestContextInput) => EffectRequestContext;
3
- export declare const applyRequestContext: (normalizedRequest: Record<string, unknown>, request: unknown, createRequestContext: (requestContext: EffectRequestContextInput) => EffectRequestContext) => Record<string, unknown>;
@@ -1 +0,0 @@
1
- export declare const normalizeRequest: (method: string, request: unknown) => Record<string, unknown>;
@@ -1,5 +0,0 @@
1
- import type { EffectRequestRuntime, GeneratedEffectClientConfig, GeneratedEffectClientModule, GeneratedEffectEndpoint } from './types.js';
2
- export type { EffectClient, EffectClientGroup, EffectClientOperation, EffectOperationDescriptor, EffectOperationManifest, EffectRequestContext, EffectRequestContextInput, EffectRequestRuntime, GeneratedEffectBatchConfig, GeneratedEffectClientConfig, GeneratedEffectClientModule, GeneratedEffectEndpoint, } from './types.js';
3
- export declare const createGeneratedEffectClient: (manifest: {
4
- endpoints: GeneratedEffectEndpoint[];
5
- }, config: GeneratedEffectClientConfig, requestRuntime: EffectRequestRuntime) => GeneratedEffectClientModule;
@@ -1,68 +0,0 @@
1
- export type GeneratedEffectEndpoint = {
2
- apiId: string;
3
- group: string;
4
- endpoint: string;
5
- method: string;
6
- routePath: string;
7
- /** Per-endpoint operation contract hash (bff-core hash). */
8
- schemaHash: string;
9
- operationVersion: number;
10
- };
11
- export type GeneratedEffectBatchConfig = {
12
- enabled: boolean;
13
- endpoint: string;
14
- flushIntervalMs: number;
15
- maxBatchSize: number;
16
- maxBatchBytes: number;
17
- requestTimeoutMs: number;
18
- allowedMethods: string[];
19
- };
20
- export type GeneratedEffectClientConfig = {
21
- appNamespace: string;
22
- /** Cross-project producer id; absent for same-project clients. */
23
- requestId?: string;
24
- port: number;
25
- /** Resolve the port from process.env.PORT first (server bundles). */
26
- useEnvPort?: boolean;
27
- defaultOrigin: string;
28
- httpMethodDecider?: string;
29
- batch: GeneratedEffectBatchConfig;
30
- };
31
- export type EffectOperationDescriptor = {
32
- appNamespace: string;
33
- apiId: string;
34
- group: string;
35
- endpoint: string;
36
- operationId: string;
37
- routePath: string;
38
- method: string;
39
- operationVersion: number;
40
- schemaHash: string;
41
- version: number;
42
- };
43
- export type EffectClientOperation = (request?: unknown) => Promise<unknown>;
44
- export type EffectClientGroup = Record<string, EffectClientOperation>;
45
- export type EffectClient = Record<string, EffectClientGroup>;
46
- export type EffectOperationManifest = Record<string, Record<string, EffectOperationDescriptor>>;
47
- export type EffectRequestContextInput = Record<string, unknown>;
48
- export type EffectRequestContext = {
49
- headers: Record<string, string>;
50
- } & Record<string, unknown>;
51
- /** Structural slice of `@modern-js/create-request` the runtime relies on. */
52
- export type EffectRequestRuntime = {
53
- createRequest: (options: {
54
- path: string;
55
- method: string;
56
- port: number | string;
57
- operationContext: Record<string, unknown>;
58
- httpMethodDecider: string;
59
- requestId?: string;
60
- }) => (...args: unknown[]) => Promise<unknown>;
61
- configure?: (options: Record<string, unknown>) => void;
62
- createRequestContextHeaders?: (input: EffectRequestContextInput) => Record<string, string>;
63
- };
64
- export type GeneratedEffectClientModule = {
65
- client: EffectClient;
66
- operationManifest: EffectOperationManifest;
67
- createEffectRequestContext: (requestContext: EffectRequestContextInput) => EffectRequestContext;
68
- };
@@ -1,51 +0,0 @@
1
- // @effect-diagnostics strictBooleanExpressions:off
2
- import {
3
- type EffectContext,
4
- type EffectContextStorage,
5
- kEffectContextStorage,
6
- } from './operation-context';
7
-
8
- export {
9
- type CreateEffectOperationContextOptions,
10
- createEffectOperationContext,
11
- type EffectContext,
12
- } from './operation-context';
13
-
14
- type EffectContextStorageConstructor = new () => EffectContextStorage;
15
-
16
- const globalStore = globalThis as typeof globalThis & {
17
- [kEffectContextStorage]?: EffectContextStorage;
18
- process?: {
19
- getBuiltinModule?: (id: string) => unknown;
20
- };
21
- };
22
-
23
- const asyncHooks = globalStore.process?.getBuiltinModule?.(
24
- 'node:async_hooks',
25
- ) as { AsyncLocalStorage?: EffectContextStorageConstructor } | undefined;
26
- const AsyncLocalStorage = asyncHooks?.AsyncLocalStorage;
27
- if (typeof AsyncLocalStorage !== 'function') {
28
- throw new Error(
29
- '[BFF][Effect] The edge runtime must provide AsyncLocalStorage. Enable Node.js compatibility or the nodejs_als compatibility flag.',
30
- );
31
- }
32
-
33
- const effectContextStorage =
34
- globalStore[kEffectContextStorage] ??
35
- (globalStore[kEffectContextStorage] = new AsyncLocalStorage());
36
-
37
- export const runWithEffectContext = <T>(
38
- context: EffectContext,
39
- cb: () => T,
40
- ): T => effectContextStorage.run(context, cb);
41
-
42
- export const useEffectContext = (): EffectContext => {
43
- const context = effectContextStorage.getStore();
44
- if (!context) {
45
- throw new Error(`Can't call useEffectContext out of Effect runtime scope`);
46
- }
47
-
48
- return context;
49
- };
50
-
51
- export const useOperationContext = () => useEffectContext().operationContext;
@@ -1,50 +0,0 @@
1
- // @effect-diagnostics processEnv:off strictBooleanExpressions:off
2
- import { createDataBatchTransport } from '../data-platform';
3
- import { resolveOrigin } from './envelope';
4
- import type {
5
- EffectRequestRuntime,
6
- GeneratedEffectClientConfig,
7
- } from './types';
8
-
9
- const resolveRuntimeFetch = (): typeof fetch | undefined =>
10
- typeof fetch === 'function' ? fetch.bind(globalThis) : undefined;
11
-
12
- export const configureGeneratedEffectClientRuntime = (
13
- config: GeneratedEffectClientConfig,
14
- configureRequest: EffectRequestRuntime['configure'] | undefined,
15
- ): void => {
16
- const defaultOrigin = config.defaultOrigin;
17
-
18
- if (config.requestId && configureRequest) {
19
- const configurePayload: Record<string, unknown> = {
20
- requestId: config.requestId,
21
- requireEnvelope: true,
22
- identityBinding: {
23
- enabled: true,
24
- strict: true,
25
- },
26
- operationContract: {
27
- enabled: true,
28
- strict: true,
29
- requireSchemaHash: true,
30
- requireOperationVersion: true,
31
- },
32
- setDomain: () => resolveOrigin(defaultOrigin),
33
- };
34
-
35
- const runtimeFetch = resolveRuntimeFetch();
36
- if (config.batch.enabled !== false && runtimeFetch) {
37
- configurePayload.request = createDataBatchTransport({
38
- fetch: runtimeFetch,
39
- endpoint: config.batch.endpoint,
40
- flushIntervalMs: config.batch.flushIntervalMs,
41
- maxBatchSize: config.batch.maxBatchSize,
42
- maxBatchBytes: config.batch.maxBatchBytes,
43
- requestTimeoutMs: config.batch.requestTimeoutMs,
44
- allowedMethods: config.batch.allowedMethods,
45
- });
46
- }
47
-
48
- configureRequest(configurePayload);
49
- }
50
- };
@@ -1,238 +0,0 @@
1
- // @effect-diagnostics processEnv:off strictBooleanExpressions:off
2
- import {
3
- createRequestEnvelope,
4
- DEFAULT_DATA_BATCH_HEADER,
5
- DEFAULT_DATA_ENVELOPE_HEADER,
6
- encodeRequestEnvelopeHeader,
7
- type RequestEnvelope,
8
- type SelectionPlan,
9
- type TraceContext,
10
- } from '../data-platform';
11
- import { isRecord, stringOrUndefined } from './guards';
12
- import { normalizeRequest } from './request';
13
- import { applyRequestContext } from './request-context';
14
- import type {
15
- EffectOperationDescriptor,
16
- EffectRequestContext,
17
- EffectRequestContextInput,
18
- GeneratedEffectClientConfig,
19
- GeneratedEffectEndpoint,
20
- } from './types';
21
-
22
- const DATA_REQUEST_MODES = new Set([
23
- 'cache-first',
24
- 'stale-while-revalidate',
25
- 'network-only',
26
- ]);
27
- const DATA_MUTATION_MODES = new Set([
28
- 'optimistic',
29
- 'pessimistic',
30
- 'fire-and-forget',
31
- ]);
32
-
33
- const isDataRequestMode = (value: unknown) =>
34
- typeof value === 'string' && DATA_REQUEST_MODES.has(value);
35
-
36
- const isDataMutationMode = (value: unknown) =>
37
- typeof value === 'string' && DATA_MUTATION_MODES.has(value);
38
-
39
- const normalizeOrigin = (value: unknown): string | undefined => {
40
- if (typeof value !== 'string' || value.length === 0) {
41
- return undefined;
42
- }
43
- try {
44
- return new URL(value).origin;
45
- } catch {
46
- return undefined;
47
- }
48
- };
49
-
50
- const resolveRuntimeFetch = (): typeof fetch | undefined =>
51
- typeof fetch === 'function' ? fetch.bind(globalThis) : undefined;
52
-
53
- /**
54
- * Resolves the current page origin with a configurable fallback. Single
55
- * implementation shared by the configure() bootstrap and the data envelope
56
- * (the generated template previously duplicated data-platform's
57
- * `resolveRuntimeOrigin` with a different fallback).
58
- */
59
-
60
- export const resolveOrigin = (defaultOrigin: string): string => {
61
- if (
62
- typeof window !== 'undefined' &&
63
- window.location &&
64
- typeof window.location.origin === 'string' &&
65
- window.location.origin
66
- ) {
67
- return window.location.origin;
68
- }
69
-
70
- const globalLocation = (
71
- globalThis as { location?: { origin?: unknown } } | undefined
72
- )?.location;
73
- if (
74
- globalLocation &&
75
- typeof globalLocation.origin === 'string' &&
76
- globalLocation.origin
77
- ) {
78
- return globalLocation.origin;
79
- }
80
-
81
- return defaultOrigin;
82
- };
83
-
84
- const resolveTargetOrigin = (
85
- dataPlatform: Record<string, unknown>,
86
- defaultOrigin: string,
87
- ): string => {
88
- const explicitTargetOrigin =
89
- stringOrUndefined(dataPlatform.targetOrigin) ||
90
- stringOrUndefined(dataPlatform.endpointOrigin);
91
- if (explicitTargetOrigin) {
92
- return explicitTargetOrigin;
93
- }
94
- return defaultOrigin;
95
- };
96
-
97
- const shouldAttachEnvelopeHeader = (
98
- dataPlatform: Record<string, unknown>,
99
- defaultOrigin: string,
100
- ): boolean => {
101
- if (dataPlatform.allowCrossOriginEnvelope === true) {
102
- return true;
103
- }
104
- const currentOrigin = normalizeOrigin(resolveOrigin(defaultOrigin));
105
- const targetOrigin = normalizeOrigin(
106
- resolveTargetOrigin(dataPlatform, defaultOrigin),
107
- );
108
- if (!currentOrigin || !targetOrigin) {
109
- return true;
110
- }
111
- return currentOrigin === targetOrigin;
112
- };
113
-
114
- const toEnvelopeInput = (
115
- normalizedRequest: Record<string, unknown>,
116
- ): Record<string, unknown> => {
117
- const payload: Record<string, unknown> = {};
118
- if (isRecord(normalizedRequest.params)) {
119
- payload.path = normalizedRequest.params;
120
- }
121
- if (isRecord(normalizedRequest.query)) {
122
- payload.query = normalizedRequest.query;
123
- }
124
- if ('data' in normalizedRequest && normalizedRequest.data !== undefined) {
125
- payload.data = normalizedRequest.data;
126
- }
127
- if ('body' in normalizedRequest && normalizedRequest.body !== undefined) {
128
- payload.body = normalizedRequest.body;
129
- }
130
- if (
131
- typeof FormData !== 'undefined' &&
132
- normalizedRequest.formData instanceof FormData
133
- ) {
134
- payload.formData = Array.from(normalizedRequest.formData.entries()).map(
135
- ([key, value]) => [key, String(value)],
136
- );
137
- }
138
- if (
139
- typeof URLSearchParams !== 'undefined' &&
140
- normalizedRequest.formUrlencoded instanceof URLSearchParams
141
- ) {
142
- payload.formUrlencoded = normalizedRequest.formUrlencoded.toString();
143
- }
144
- return payload;
145
- };
146
-
147
- export const prepareEffectRequest = (options: {
148
- endpoint: GeneratedEffectEndpoint;
149
- operation: EffectOperationDescriptor;
150
- request: unknown;
151
- config: GeneratedEffectClientConfig;
152
- createEffectRequestContext: (
153
- requestContext: EffectRequestContextInput,
154
- ) => EffectRequestContext;
155
- }): Record<string, unknown> => {
156
- const { endpoint, operation, request, config, createEffectRequestContext } =
157
- options;
158
- const defaultOrigin = config.defaultOrigin;
159
- const normalizedRequest = applyRequestContext(
160
- normalizeRequest(endpoint.method, request),
161
- request,
162
- createEffectRequestContext,
163
- );
164
- const dataPlatform =
165
- isRecord(request) && isRecord(request.dataPlatform)
166
- ? request.dataPlatform
167
- : {};
168
- const strictEnvelope =
169
- dataPlatform.requireEnvelope === true || dataPlatform.strict === true;
170
-
171
- if (
172
- !strictEnvelope &&
173
- !shouldAttachEnvelopeHeader(dataPlatform, defaultOrigin)
174
- ) {
175
- return normalizedRequest;
176
- }
177
-
178
- try {
179
- const namespace =
180
- stringOrUndefined(dataPlatform.appNamespace) || config.appNamespace;
181
- const origin =
182
- stringOrUndefined(dataPlatform.origin) || resolveOrigin(defaultOrigin);
183
- const envelope: RequestEnvelope = createRequestEnvelope({
184
- operation: {
185
- ...operation,
186
- appNamespace: namespace,
187
- },
188
- scope: {
189
- appNamespace: namespace,
190
- origin,
191
- tenantId: stringOrUndefined(dataPlatform.tenantId),
192
- userId: stringOrUndefined(dataPlatform.userId),
193
- sessionId: stringOrUndefined(dataPlatform.sessionId),
194
- },
195
- requestInput: {
196
- method: endpoint.method,
197
- routePath: endpoint.routePath,
198
- payload: toEnvelopeInput(normalizedRequest),
199
- },
200
- requestMode: isDataRequestMode(dataPlatform.requestMode)
201
- ? (dataPlatform.requestMode as RequestEnvelope['requestMode'])
202
- : undefined,
203
- mutationMode: isDataMutationMode(dataPlatform.mutationMode)
204
- ? (dataPlatform.mutationMode as RequestEnvelope['mutationMode'])
205
- : undefined,
206
- selectionPlan: isRecord(dataPlatform.selectionPlan)
207
- ? (dataPlatform.selectionPlan as SelectionPlan)
208
- : undefined,
209
- traceContext: isRecord(dataPlatform.traceContext)
210
- ? (dataPlatform.traceContext as unknown as TraceContext)
211
- : undefined,
212
- requireTraceContext: dataPlatform.requireTraceContext === true,
213
- });
214
-
215
- const headerName =
216
- stringOrUndefined(dataPlatform.envelopeHeader) ||
217
- DEFAULT_DATA_ENVELOPE_HEADER;
218
- const headers = isRecord(normalizedRequest.headers)
219
- ? { ...normalizedRequest.headers }
220
- : {};
221
-
222
- if (dataPlatform.batch === false) {
223
- headers[DEFAULT_DATA_BATCH_HEADER] = 'off';
224
- }
225
-
226
- headers[headerName] = encodeRequestEnvelopeHeader(envelope);
227
-
228
- return {
229
- ...normalizedRequest,
230
- headers,
231
- };
232
- } catch (error) {
233
- if (strictEnvelope) {
234
- throw error;
235
- }
236
- return normalizedRequest;
237
- }
238
- };
@@ -1,6 +0,0 @@
1
- // @effect-diagnostics processEnv:off strictBooleanExpressions:off
2
- export const isRecord = (value: unknown): value is Record<string, unknown> =>
3
- typeof value === 'object' && value !== null;
4
-
5
- export const stringOrUndefined = (value: unknown): string | undefined =>
6
- typeof value === 'string' && value.length > 0 ? value : undefined;
@@ -1,54 +0,0 @@
1
- // @effect-diagnostics processEnv:off strictBooleanExpressions:off
2
- import { isRecord } from './guards';
3
- import type {
4
- EffectRequestContext,
5
- EffectRequestContextInput,
6
- EffectRequestRuntime,
7
- } from './types';
8
-
9
- export const createEffectRequestContext = (
10
- createRequestContextHeaders: EffectRequestRuntime['createRequestContextHeaders'],
11
- requestContext: EffectRequestContextInput,
12
- ): EffectRequestContext => {
13
- if (!isRecord(requestContext)) {
14
- return {} as EffectRequestContext;
15
- }
16
-
17
- const headers = createRequestContextHeaders
18
- ? createRequestContextHeaders(requestContext)
19
- : {};
20
-
21
- return {
22
- ...requestContext,
23
- headers,
24
- };
25
- };
26
-
27
- export const applyRequestContext = (
28
- normalizedRequest: Record<string, unknown>,
29
- request: unknown,
30
- createRequestContext: (
31
- requestContext: EffectRequestContextInput,
32
- ) => EffectRequestContext,
33
- ): Record<string, unknown> => {
34
- if (!isRecord(request) || !isRecord(request.requestContext)) {
35
- return normalizedRequest;
36
- }
37
-
38
- const requestContext = createRequestContext(request.requestContext);
39
- const requestHeaders = isRecord(requestContext.headers)
40
- ? requestContext.headers
41
- : {};
42
-
43
- if (Object.keys(requestHeaders).length === 0) {
44
- return normalizedRequest;
45
- }
46
-
47
- return {
48
- ...normalizedRequest,
49
- headers: {
50
- ...(isRecord(normalizedRequest.headers) ? normalizedRequest.headers : {}),
51
- ...requestHeaders,
52
- },
53
- };
54
- };
@@ -1,51 +0,0 @@
1
- // @effect-diagnostics processEnv:off strictBooleanExpressions:off
2
- import { isRecord } from './guards';
3
-
4
- const METHODS_WITHOUT_BODY = new Set(['GET', 'DELETE', 'HEAD', 'OPTIONS']);
5
-
6
- export const normalizeRequest = (
7
- method: string,
8
- request: unknown,
9
- ): Record<string, unknown> => {
10
- if (!isRecord(request)) {
11
- return {};
12
- }
13
-
14
- const payload: Record<string, unknown> = { ...request };
15
-
16
- if (isRecord(request.path) && !isRecord(payload.params)) {
17
- payload.params = request.path;
18
- }
19
-
20
- if (isRecord(request.urlParams) && !isRecord(payload.query)) {
21
- payload.query = request.urlParams;
22
- }
23
-
24
- if (isRecord(request.headers) && !isRecord(payload.headers)) {
25
- payload.headers = request.headers;
26
- }
27
-
28
- if ('payload' in request && request.payload !== undefined) {
29
- if (
30
- typeof FormData !== 'undefined' &&
31
- request.payload instanceof FormData &&
32
- !('formData' in payload)
33
- ) {
34
- payload.formData = request.payload;
35
- } else if (METHODS_WITHOUT_BODY.has(method)) {
36
- if (isRecord(request.payload)) {
37
- payload.query = isRecord(payload.query)
38
- ? { ...payload.query, ...request.payload }
39
- : request.payload;
40
- } else if (!('body' in payload)) {
41
- payload.body = request.payload;
42
- }
43
- } else if (isRecord(request.payload) && !('data' in payload)) {
44
- payload.data = request.payload;
45
- } else if (!('body' in payload)) {
46
- payload.body = request.payload;
47
- }
48
- }
49
-
50
- return payload;
51
- };