@bleedingdev/modern-js-create-request 3.2.0-ultramodern.0

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,213 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ executeWithResilience: ()=>executeWithResilience
28
+ });
29
+ const DEFAULT_RETRYABLE_STATUS_CODES = [
30
+ 408,
31
+ 425,
32
+ 429,
33
+ 500,
34
+ 502,
35
+ 503,
36
+ 504
37
+ ];
38
+ const DEFAULT_BASE_DELAY_MS = 100;
39
+ const DEFAULT_MAX_DELAY_MS = 1000;
40
+ const DEFAULT_JITTER_RATIO = 0.1;
41
+ const createTimeoutError = (timeoutMs)=>{
42
+ const error = new Error(`Request timed out after ${timeoutMs}ms`);
43
+ error.name = 'TimeoutError';
44
+ return error;
45
+ };
46
+ const wait = (ms)=>new Promise((resolve)=>{
47
+ const timer = setTimeout(()=>resolve(), ms);
48
+ if ('function' == typeof timer.unref) timer.unref();
49
+ });
50
+ const toStatusCode = (error)=>{
51
+ if (!error || 'object' != typeof error) return;
52
+ const status = error.status;
53
+ if ('number' == typeof status) return status;
54
+ const responseStatus = error.response?.status;
55
+ if ('number' == typeof responseStatus) return responseStatus;
56
+ };
57
+ const isRetryableNetworkError = (error)=>{
58
+ if (!error || 'object' != typeof error) return false;
59
+ const name = error.name;
60
+ if ('AbortError' === name || 'FetchError' === name || 'TimeoutError' === name || 'TypeError' === name) return true;
61
+ const code = error.code;
62
+ return 'ECONNRESET' === code || 'ETIMEDOUT' === code || 'ECONNREFUSED' === code || 'EPIPE' === code;
63
+ };
64
+ const normalizePositiveNumber = (value, fallback)=>'number' == typeof value && Number.isFinite(value) && value > 0 ? value : fallback;
65
+ const normalizeNonNegativeInt = (value, fallback)=>'number' == typeof value && Number.isFinite(value) && value >= 0 ? Math.floor(value) : fallback;
66
+ const normalizeJitterRatio = (value)=>{
67
+ if ('number' != typeof value || !Number.isFinite(value)) return DEFAULT_JITTER_RATIO;
68
+ return Math.max(0, Math.min(1, value));
69
+ };
70
+ const shouldRetryWithDefaults = (statusCode, error, retryableStatusCodes)=>{
71
+ if ('number' == typeof statusCode) return retryableStatusCodes.includes(statusCode);
72
+ return isRetryableNetworkError(error);
73
+ };
74
+ const shouldRetry = (retryOptions, context, retryableStatusCodes)=>{
75
+ if ('function' == typeof retryOptions?.shouldRetry) try {
76
+ return retryOptions.shouldRetry(context);
77
+ } catch (error) {
78
+ return false;
79
+ }
80
+ return shouldRetryWithDefaults(context.statusCode, context.error, retryableStatusCodes);
81
+ };
82
+ const getBackoffMs = (retryOptions, attempt)=>{
83
+ const baseDelayMs = normalizePositiveNumber(retryOptions?.baseDelayMs, DEFAULT_BASE_DELAY_MS);
84
+ const maxDelayMs = normalizePositiveNumber(retryOptions?.maxDelayMs, DEFAULT_MAX_DELAY_MS);
85
+ const jitterRatio = normalizeJitterRatio(retryOptions?.jitterRatio);
86
+ const exponentialDelay = Math.min(maxDelayMs, baseDelayMs * 2 ** Math.max(0, attempt - 1));
87
+ const jitterFactor = 0 === jitterRatio ? 1 : 1 + (2 * Math.random() - 1) * jitterRatio;
88
+ return Math.max(0, Math.floor(exponentialDelay * jitterFactor));
89
+ };
90
+ const emitDegradedEvent = (transport, event)=>{
91
+ if ('function' != typeof transport?.onDegraded) return;
92
+ try {
93
+ transport.onDegraded(event);
94
+ } catch (error) {}
95
+ };
96
+ const withTimeout = async (promise, timeoutMs, signalBuilder)=>{
97
+ if (!timeoutMs || timeoutMs <= 0) {
98
+ const result = await promise;
99
+ return {
100
+ result
101
+ };
102
+ }
103
+ const controller = signalBuilder?.();
104
+ let timeoutTriggered = false;
105
+ let rejectTimeout;
106
+ const timeoutPromise = new Promise((_, reject)=>{
107
+ rejectTimeout = reject;
108
+ });
109
+ const timer = setTimeout(()=>{
110
+ timeoutTriggered = true;
111
+ if (controller) controller.abort();
112
+ rejectTimeout?.(createTimeoutError(timeoutMs));
113
+ }, timeoutMs);
114
+ if ('function' == typeof timer.unref) timer.unref();
115
+ try {
116
+ const result = await Promise.race([
117
+ promise,
118
+ timeoutPromise
119
+ ]);
120
+ return {
121
+ result,
122
+ timeoutTriggered
123
+ };
124
+ } catch (error) {
125
+ if (timeoutTriggered && error?.name === 'AbortError') throw createTimeoutError(timeoutMs);
126
+ throw error;
127
+ } finally{
128
+ clearTimeout(timer);
129
+ }
130
+ };
131
+ const executeWithResilience = async ({ requestId, target, method, url, init, fetcher, transport })=>{
132
+ const retries = normalizeNonNegativeInt(transport?.retry?.retries, 0);
133
+ const timeoutMs = 'number' == typeof transport?.timeoutMs && transport.timeoutMs > 0 ? transport.timeoutMs : void 0;
134
+ const maxAttempts = retries + 1;
135
+ const retryableStatusCodes = transport?.retry?.retryableStatusCodes && transport.retry.retryableStatusCodes.length > 0 ? transport.retry.retryableStatusCodes : DEFAULT_RETRYABLE_STATUS_CODES;
136
+ let attempt = 0;
137
+ while(attempt < maxAttempts){
138
+ attempt += 1;
139
+ const canUseAbortController = "u" > typeof AbortController;
140
+ let controller;
141
+ if (timeoutMs && canUseAbortController) controller = new AbortController();
142
+ const nextInit = controller && !init.signal ? {
143
+ ...init,
144
+ signal: controller.signal
145
+ } : init;
146
+ try {
147
+ const { result } = await withTimeout(fetcher(url, nextInit), timeoutMs, ()=>controller);
148
+ return result;
149
+ } catch (error) {
150
+ const statusCode = toStatusCode(error);
151
+ const decisionContext = {
152
+ requestId,
153
+ target,
154
+ method,
155
+ url,
156
+ attempt,
157
+ maxAttempts,
158
+ error,
159
+ statusCode
160
+ };
161
+ if (error?.name === 'TimeoutError') emitDegradedEvent(transport, {
162
+ requestId,
163
+ target,
164
+ method,
165
+ url,
166
+ reason: 'timeout',
167
+ attempt,
168
+ maxAttempts,
169
+ timeoutMs,
170
+ statusCode,
171
+ error
172
+ });
173
+ const canRetry = attempt < maxAttempts && shouldRetry(transport?.retry, decisionContext, retryableStatusCodes);
174
+ if (!canRetry) {
175
+ if (retries > 0 || error?.name === 'TimeoutError') emitDegradedEvent(transport, {
176
+ requestId,
177
+ target,
178
+ method,
179
+ url,
180
+ reason: 'retry_exhausted',
181
+ attempt,
182
+ maxAttempts,
183
+ timeoutMs,
184
+ statusCode,
185
+ error
186
+ });
187
+ throw error;
188
+ }
189
+ const backoffMs = getBackoffMs(transport?.retry, attempt);
190
+ emitDegradedEvent(transport, {
191
+ requestId,
192
+ target,
193
+ method,
194
+ url,
195
+ reason: 'retry',
196
+ attempt,
197
+ maxAttempts,
198
+ timeoutMs,
199
+ backoffMs,
200
+ statusCode,
201
+ error
202
+ });
203
+ if (backoffMs > 0) await wait(backoffMs);
204
+ }
205
+ }
206
+ };
207
+ exports.executeWithResilience = __webpack_exports__.executeWithResilience;
208
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
209
+ "executeWithResilience"
210
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
211
+ Object.defineProperty(exports, '__esModule', {
212
+ value: true
213
+ });
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS: ()=>BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS,
28
+ BFF_ENVELOPE_HEADER: ()=>BFF_ENVELOPE_HEADER,
29
+ BFF_OPERATION_CONTEXT_DETAIL_HEADER: ()=>BFF_OPERATION_CONTEXT_DETAIL_HEADER,
30
+ BFF_OPERATION_CONTEXT_HEADER: ()=>BFF_OPERATION_CONTEXT_HEADER
31
+ });
32
+ const BFF_ENVELOPE_HEADER = 'x-modernjs-bff-envelope';
33
+ const BFF_OPERATION_CONTEXT_HEADER = 'x-operation-id';
34
+ const BFF_OPERATION_CONTEXT_DETAIL_HEADER = 'x-modernjs-bff-operation-context';
35
+ const BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS = [
36
+ 'x-tenant-id',
37
+ 'x-subject-id',
38
+ 'x-user-id',
39
+ BFF_OPERATION_CONTEXT_HEADER
40
+ ];
41
+ exports.BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS = __webpack_exports__.BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS;
42
+ exports.BFF_ENVELOPE_HEADER = __webpack_exports__.BFF_ENVELOPE_HEADER;
43
+ exports.BFF_OPERATION_CONTEXT_DETAIL_HEADER = __webpack_exports__.BFF_OPERATION_CONTEXT_DETAIL_HEADER;
44
+ exports.BFF_OPERATION_CONTEXT_HEADER = __webpack_exports__.BFF_OPERATION_CONTEXT_HEADER;
45
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
46
+ "BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS",
47
+ "BFF_ENVELOPE_HEADER",
48
+ "BFF_OPERATION_CONTEXT_DETAIL_HEADER",
49
+ "BFF_OPERATION_CONTEXT_HEADER"
50
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
51
+ Object.defineProperty(exports, '__esModule', {
52
+ value: true
53
+ });
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ getUploadPayload: ()=>getUploadPayload
28
+ });
29
+ const getUploadPayload = (args)=>{
30
+ const payload = 'object' == typeof args[args.length - 1] ? args[args.length - 1] : {};
31
+ const files = payload.files;
32
+ if (!files) throw new Error('no files');
33
+ const formdata = new FormData();
34
+ for (const [key, value] of Object.entries(files))if (value instanceof FileList) for(let i = 0; i < value.length; i++){
35
+ const file = value.item(i);
36
+ if (file) formdata.append(key, file);
37
+ }
38
+ else formdata.append(key, value);
39
+ const body = formdata;
40
+ return {
41
+ body,
42
+ headers: payload.headers,
43
+ params: payload.params
44
+ };
45
+ };
46
+ exports.getUploadPayload = __webpack_exports__.getUploadPayload;
47
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
48
+ "getUploadPayload"
49
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
50
+ Object.defineProperty(exports, '__esModule', {
51
+ value: true
52
+ });
@@ -0,0 +1,366 @@
1
+ import { compile } from "path-to-regexp";
2
+ import { stringify } from "qs";
3
+ import { handleRes } from "./handleRes.mjs";
4
+ import { executeWithResilience } from "./transport.mjs";
5
+ import { BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS, BFF_ENVELOPE_HEADER, BFF_OPERATION_CONTEXT_DETAIL_HEADER, BFF_OPERATION_CONTEXT_HEADER } from "./types.mjs";
6
+ import { getUploadPayload } from "./utiles.mjs";
7
+ export * from "./requestContext.mjs";
8
+ export * from "./types.mjs";
9
+ const realRequest = new Map();
10
+ const realAllowedHeaders = new Map();
11
+ const realRequireEnvelope = new Map();
12
+ const realAllowCrossOriginEnvelope = new Map();
13
+ const realTransportResilience = new Map();
14
+ const realIdentityBinding = new Map();
15
+ const realOperationContract = new Map();
16
+ const domainMap = new Map();
17
+ const isEmptyDomain = (domain)=>'string' != typeof domain || '' === domain.trim();
18
+ const TRACEPARENT_HEADER = 'traceparent';
19
+ const OPERATION_CONTEXT_DETAIL_HEADER = BFF_OPERATION_CONTEXT_DETAIL_HEADER;
20
+ const TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/i;
21
+ const readProcessEnv = (key)=>{
22
+ if ("u" < typeof process || void 0 === process.env || 'string' != typeof process.env[key]) return;
23
+ return process.env[key];
24
+ };
25
+ const isStrictDefaultRequestIdEnabled = ()=>'true' === readProcessEnv('MODERN_BFF_STRICT_DEFAULT_REQUEST_ID');
26
+ const isSecuredRequestId = (requestId)=>'default' !== requestId || isStrictDefaultRequestIdEnabled();
27
+ const firstHeaderValue = (value)=>Array.isArray(value) ? value[0] : value;
28
+ const findHeaderKey = (headers, header)=>{
29
+ const normalized = header.toLowerCase();
30
+ return Object.keys(headers).find((key)=>key.toLowerCase() === normalized);
31
+ };
32
+ const readHeader = (headers, header)=>{
33
+ const key = findHeaderKey(headers, header);
34
+ return 'string' == typeof key ? headers[key] : void 0;
35
+ };
36
+ const writeHeader = (headers, header, value)=>{
37
+ if (void 0 === value) return;
38
+ const key = findHeaderKey(headers, header);
39
+ if ('string' == typeof key && key !== header) delete headers[key];
40
+ headers[header] = value;
41
+ };
42
+ const deleteHeader = (headers, header)=>{
43
+ const key = findHeaderKey(headers, header);
44
+ if ('string' == typeof key) delete headers[key];
45
+ };
46
+ const toOrigin = (value)=>{
47
+ if (!value) return;
48
+ try {
49
+ return new URL(value).origin;
50
+ } catch (error) {
51
+ return;
52
+ }
53
+ };
54
+ const parseTraceparent = (value)=>{
55
+ const traceparent = firstHeaderValue(value);
56
+ if ('string' != typeof traceparent) return;
57
+ const match = traceparent.trim().match(TRACEPARENT_REGEX);
58
+ if (!match) return;
59
+ const [, traceId, spanId] = match;
60
+ if (!traceId || !spanId) return;
61
+ return {
62
+ traceId: traceId.toLowerCase(),
63
+ spanId: spanId.toLowerCase()
64
+ };
65
+ };
66
+ const extractPathParamNames = (path)=>Array.from(path.matchAll(/:([A-Za-z0-9_]+)/g)).flatMap(([, key])=>key ? [
67
+ key
68
+ ] : []);
69
+ const originFetch = (...params)=>{
70
+ const [url, init] = params;
71
+ if (init?.method?.toLowerCase() === 'get') init.body = void 0;
72
+ return fetch(url, init).then(handleRes);
73
+ };
74
+ const buildOperationContext = ({ requestId, method, path, operationContext, traceparent })=>{
75
+ const routePath = operationContext?.routePath || path;
76
+ const operationMethod = (operationContext?.method || method || 'GET').toUpperCase();
77
+ const rawOperationId = operationContext?.operationId || `${operationMethod}:${routePath}`;
78
+ const operationId = rawOperationId.startsWith(`${requestId}:`) ? rawOperationId : `${requestId}:${rawOperationId}`;
79
+ const traceparentValue = operationContext?.traceparent || ('string' == typeof firstHeaderValue(traceparent) ? String(firstHeaderValue(traceparent)) : void 0);
80
+ const parsedTraceContext = operationContext?.traceId && operationContext?.spanId ? {
81
+ traceId: operationContext.traceId,
82
+ spanId: operationContext.spanId
83
+ } : parseTraceparent(traceparentValue);
84
+ return {
85
+ requestId,
86
+ operationId,
87
+ routePath,
88
+ method: operationMethod,
89
+ ...operationContext?.schemaHash ? {
90
+ schemaHash: operationContext.schemaHash
91
+ } : {},
92
+ ...'number' == typeof operationContext?.operationVersion ? {
93
+ operationVersion: operationContext.operationVersion
94
+ } : {},
95
+ ...traceparentValue ? {
96
+ traceparent: traceparentValue
97
+ } : {},
98
+ ...parsedTraceContext ? {
99
+ traceId: parsedTraceContext.traceId,
100
+ spanId: parsedTraceContext.spanId
101
+ } : {}
102
+ };
103
+ };
104
+ class ProducerClientNotInitializedError extends Error {
105
+ constructor(requestId){
106
+ super(`Producer client "${requestId}" is not initialized. Call initProducerClient() (or configure()) before using generated APIs for this requestId.`), this.code = 'BFF_PRODUCER_CLIENT_NOT_INITIALIZED';
107
+ this.name = 'ProducerClientNotInitializedError';
108
+ }
109
+ }
110
+ class ProducerDomainNotConfiguredError extends Error {
111
+ constructor(requestId){
112
+ super(`Producer client "${requestId}" must provide setDomain() during configure().`), this.code = 'BFF_PRODUCER_DOMAIN_NOT_CONFIGURED';
113
+ this.name = 'ProducerDomainNotConfiguredError';
114
+ }
115
+ }
116
+ class CrossOriginEnvelopePolicyError extends Error {
117
+ constructor(requestId, sourceOrigin, targetOrigin){
118
+ super(`Cross-origin envelope is not allowed for producer "${requestId}" (${sourceOrigin || 'unknown-origin'} -> ${targetOrigin || 'unknown-origin'}). Configure allowCrossOriginEnvelope to explicitly allow this flow.`), this.code = 'BFF_CROSS_ORIGIN_ENVELOPE_NOT_ALLOWED';
119
+ this.name = 'CrossOriginEnvelopePolicyError';
120
+ }
121
+ }
122
+ class IdentityBindingViolationError extends Error {
123
+ constructor(violation){
124
+ super(`Identity header "${violation.header}" for producer "${violation.requestId}" was rejected by server-derived identity binding.`), this.code = 'BFF_IDENTITY_BINDING_VIOLATION';
125
+ this.name = 'IdentityBindingViolationError';
126
+ this.violation = violation;
127
+ }
128
+ }
129
+ class OperationContractViolationError extends Error {
130
+ constructor(violation){
131
+ super(`Operation contract violation "${violation.reason}" for producer "${violation.requestId}" operation "${violation.operationId}".`), this.code = 'BFF_OPERATION_CONTRACT_VIOLATION';
132
+ this.name = 'OperationContractViolationError';
133
+ this.violation = violation;
134
+ }
135
+ }
136
+ const validateOperationContract = (requestId, contextPayload)=>{
137
+ const operationContract = realOperationContract.get(requestId);
138
+ const operationContractEnabled = operationContract?.enabled ?? isSecuredRequestId(requestId);
139
+ if (!operationContractEnabled) return;
140
+ const strict = operationContract?.strict ?? true;
141
+ const requireSchemaHash = operationContract?.requireSchemaHash ?? true;
142
+ const requireOperationVersion = operationContract?.requireOperationVersion ?? true;
143
+ const maybeReportViolation = (reason)=>{
144
+ const violation = {
145
+ requestId,
146
+ target: 'browser',
147
+ operationId: contextPayload.operationId,
148
+ routePath: contextPayload.routePath,
149
+ method: contextPayload.method,
150
+ schemaHash: 'string' == typeof contextPayload.schemaHash ? contextPayload.schemaHash : void 0,
151
+ operationVersion: 'number' == typeof contextPayload.operationVersion ? contextPayload.operationVersion : void 0,
152
+ reason
153
+ };
154
+ operationContract?.onViolation?.(violation);
155
+ if (strict) throw new OperationContractViolationError(violation);
156
+ };
157
+ if (requireSchemaHash && 'string' != typeof contextPayload.schemaHash) maybeReportViolation('missing_schema_hash');
158
+ if (requireOperationVersion && 'number' != typeof contextPayload.operationVersion) maybeReportViolation('missing_operation_version');
159
+ };
160
+ const getConfiguredRequest = (requestId, fallback)=>{
161
+ const configuredRequest = realRequest.get(requestId);
162
+ if (configuredRequest) return configuredRequest;
163
+ if ('default' !== requestId) throw new ProducerClientNotInitializedError(requestId);
164
+ return fallback;
165
+ };
166
+ const configure = (options)=>{
167
+ const { request, interceptor, allowedHeaders, transport, requireEnvelope, allowCrossOriginEnvelope, identityBinding, operationContract, setDomain, requestId = 'default' } = options;
168
+ const hasExistingDomain = domainMap.has(requestId);
169
+ if ('default' !== requestId && !setDomain && !hasExistingDomain) throw new ProducerDomainNotConfiguredError(requestId);
170
+ let configuredRequest = request || originFetch;
171
+ if (interceptor && !request) configuredRequest = interceptor(fetch);
172
+ if (Array.isArray(allowedHeaders)) realAllowedHeaders.set(requestId, allowedHeaders);
173
+ if (transport && 'object' == typeof transport) realTransportResilience.set(requestId, transport);
174
+ if (identityBinding && 'object' == typeof identityBinding) realIdentityBinding.set(requestId, identityBinding);
175
+ if (operationContract && 'object' == typeof operationContract) realOperationContract.set(requestId, operationContract);
176
+ if ('boolean' == typeof requireEnvelope) realRequireEnvelope.set(requestId, requireEnvelope);
177
+ if ('boolean' == typeof allowCrossOriginEnvelope || 'function' == typeof allowCrossOriginEnvelope) realAllowCrossOriginEnvelope.set(requestId, allowCrossOriginEnvelope);
178
+ if (setDomain) {
179
+ const resolvedDomain = setDomain({
180
+ target: 'browser',
181
+ requestId
182
+ });
183
+ if ('default' !== requestId && isEmptyDomain(resolvedDomain)) throw new ProducerDomainNotConfiguredError(requestId);
184
+ if ('string' == typeof resolvedDomain) domainMap.set(requestId, resolvedDomain);
185
+ }
186
+ realRequest.set(requestId, configuredRequest);
187
+ };
188
+ const normalizeRequestOptions = (...args)=>{
189
+ if ('object' == typeof args[0] && null !== args[0]) return args[0];
190
+ const [path, method, port, httpMethodDecider, fetch1, requestId, operationContext] = args;
191
+ return {
192
+ path,
193
+ method,
194
+ port,
195
+ httpMethodDecider,
196
+ fetch: fetch1,
197
+ requestId,
198
+ operationContext
199
+ };
200
+ };
201
+ const createRequest = (...args)=>{
202
+ const { path, method, port, httpMethodDecider = 'functionName', fetch: fetch1 = originFetch, domain, requestId = 'default', operationContext } = normalizeRequestOptions(...args);
203
+ const getFinalPath = compile(path, {
204
+ encode: encodeURIComponent
205
+ });
206
+ const keyNames = extractPathParamNames(path);
207
+ const sender = async (...args)=>{
208
+ const fetcher = getConfiguredRequest(requestId, fetch1);
209
+ let body;
210
+ let finalURL;
211
+ let headers;
212
+ if ('inputParams' === httpMethodDecider) {
213
+ finalURL = path;
214
+ body = JSON.stringify({
215
+ args
216
+ });
217
+ headers = {
218
+ 'Content-Type': 'application/json'
219
+ };
220
+ } else {
221
+ const payload = 'object' == typeof args[args.length - 1] ? args[args.length - 1] : {};
222
+ payload.params = payload.params || {};
223
+ const requestParams = args[0];
224
+ if ('object' == typeof requestParams && requestParams.params) {
225
+ const { params } = requestParams;
226
+ keyNames.forEach((keyName)=>{
227
+ payload.params[keyName] = params[keyName];
228
+ });
229
+ } else keyNames.forEach((keyName, index)=>{
230
+ payload.params[keyName] = args[index];
231
+ });
232
+ const finalPath = getFinalPath(payload.params);
233
+ finalURL = payload.query ? `${finalPath}?${stringify(payload.query)}` : finalPath;
234
+ headers = payload.headers ? {
235
+ ...payload.headers
236
+ } : {};
237
+ const identityBinding = realIdentityBinding.get(requestId);
238
+ const identityBindingEnabled = identityBinding?.enabled ?? isSecuredRequestId(requestId);
239
+ const identityBindingStrict = identityBinding?.strict ?? isSecuredRequestId(requestId);
240
+ const protectedIdentityHeaders = (identityBinding?.protectedHeaders || BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS).map((header)=>header.toLowerCase());
241
+ if (identityBindingEnabled) {
242
+ const derivedIdentityHeaders = {};
243
+ const customDerivedHeaders = identityBinding?.deriveHeaders?.({
244
+ requestId,
245
+ target: 'browser',
246
+ incomingHeaders: {},
247
+ protectedHeaders: [
248
+ ...protectedIdentityHeaders
249
+ ]
250
+ });
251
+ if (customDerivedHeaders && 'object' == typeof customDerivedHeaders) for (const header of protectedIdentityHeaders){
252
+ const customValue = readHeader(customDerivedHeaders, header);
253
+ if (void 0 !== customValue) writeHeader(derivedIdentityHeaders, header, customValue);
254
+ }
255
+ for (const header of protectedIdentityHeaders){
256
+ const attemptedValue = readHeader(headers, header);
257
+ if (void 0 === attemptedValue) continue;
258
+ const violation = {
259
+ requestId,
260
+ target: 'browser',
261
+ header,
262
+ attemptedValue,
263
+ derivedValue: readHeader(derivedIdentityHeaders, header),
264
+ reason: identityBindingStrict ? 'client_override_rejected' : 'client_override_blocked'
265
+ };
266
+ identityBinding?.onViolation?.(violation);
267
+ if (identityBindingStrict) throw new IdentityBindingViolationError(violation);
268
+ deleteHeader(headers, header);
269
+ }
270
+ Object.keys(derivedIdentityHeaders).forEach((header)=>{
271
+ writeHeader(headers, header, derivedIdentityHeaders[header]);
272
+ });
273
+ }
274
+ body = payload.data && 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
275
+ if (payload.data) {
276
+ headers['Content-Type'] = 'application/json';
277
+ body = 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
278
+ } else if (payload.body) {
279
+ headers['Content-Type'] = 'text/plain';
280
+ body = payload.body;
281
+ } else if (payload.formData) body = payload.formData;
282
+ else if (payload.formUrlencoded) {
283
+ headers['Content-Type'] = 'application/x-www-form-urlencoded';
284
+ body = 'object' != typeof payload.formUrlencoded || payload.formUrlencoded instanceof URLSearchParams ? payload.formUrlencoded : stringify(payload.formUrlencoded);
285
+ }
286
+ }
287
+ headers.accept = "application/json,*/*;q=0.8";
288
+ const configDomain = domainMap.get(requestId);
289
+ if ('default' !== requestId && isEmptyDomain(configDomain)) throw new ProducerDomainNotConfiguredError(requestId);
290
+ finalURL = `${configDomain || ''}${finalURL}`;
291
+ const shouldRequireEnvelope = realRequireEnvelope.get(requestId) ?? isSecuredRequestId(requestId);
292
+ if (shouldRequireEnvelope) {
293
+ const sourceOrigin = "u" > typeof window ? window.location.origin : void 0;
294
+ const targetOrigin = toOrigin(finalURL);
295
+ const traceContext = parseTraceparent(readHeader(headers, TRACEPARENT_HEADER));
296
+ const isCrossOrigin = Boolean(sourceOrigin) && Boolean(targetOrigin) && sourceOrigin !== targetOrigin;
297
+ if (isCrossOrigin) {
298
+ const policy = realAllowCrossOriginEnvelope.get(requestId);
299
+ const isAllowed = 'function' == typeof policy ? policy({
300
+ requestId,
301
+ sourceOrigin,
302
+ targetOrigin,
303
+ target: 'browser'
304
+ }) : true === policy;
305
+ if (!isAllowed) throw new CrossOriginEnvelopePolicyError(requestId, sourceOrigin, targetOrigin);
306
+ }
307
+ headers[BFF_ENVELOPE_HEADER] = JSON.stringify({
308
+ requestId,
309
+ target: 'browser',
310
+ timestamp: Date.now(),
311
+ sourceOrigin,
312
+ targetOrigin,
313
+ ...traceContext ? {
314
+ traceId: traceContext.traceId,
315
+ spanId: traceContext.spanId
316
+ } : {}
317
+ });
318
+ }
319
+ if (isSecuredRequestId(requestId)) {
320
+ if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
321
+ const contextPayload = buildOperationContext({
322
+ requestId,
323
+ method,
324
+ path,
325
+ operationContext,
326
+ traceparent: readHeader(headers, TRACEPARENT_HEADER)
327
+ });
328
+ validateOperationContract(requestId, contextPayload);
329
+ if (void 0 === readHeader(headers, BFF_OPERATION_CONTEXT_HEADER)) writeHeader(headers, BFF_OPERATION_CONTEXT_HEADER, contextPayload.operationId);
330
+ writeHeader(headers, OPERATION_CONTEXT_DETAIL_HEADER, JSON.stringify(contextPayload));
331
+ }
332
+ return executeWithResilience({
333
+ requestId,
334
+ target: 'browser',
335
+ method,
336
+ url: finalURL,
337
+ init: {
338
+ method,
339
+ body,
340
+ headers
341
+ },
342
+ fetcher,
343
+ transport: realTransportResilience.get(requestId)
344
+ });
345
+ };
346
+ return sender;
347
+ };
348
+ const createUploader = ({ path, domain, requestId = 'default' })=>{
349
+ const getFinalPath = compile(path, {
350
+ encode: encodeURIComponent
351
+ });
352
+ const sender = (...args)=>{
353
+ const fetcher = getConfiguredRequest(requestId, originFetch);
354
+ const { body, headers, params } = getUploadPayload(args);
355
+ const finalPath = getFinalPath(params);
356
+ const configDomain = domainMap.get(requestId);
357
+ const finalURL = `${configDomain || domain || ''}${finalPath}`;
358
+ return fetcher(finalURL, {
359
+ method: 'POST',
360
+ body,
361
+ headers
362
+ });
363
+ };
364
+ return sender;
365
+ };
366
+ export { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError, configure, createRequest, createUploader };