@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.
- package/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/cjs/browser.js +489 -0
- package/dist/cjs/handleRes.js +49 -0
- package/dist/cjs/node.js +531 -0
- package/dist/cjs/qs.js +39 -0
- package/dist/cjs/requestContext.js +94 -0
- package/dist/cjs/transport.js +213 -0
- package/dist/cjs/types.js +53 -0
- package/dist/cjs/utiles.js +52 -0
- package/dist/esm/browser.mjs +366 -0
- package/dist/esm/handleRes.mjs +15 -0
- package/dist/esm/node.mjs +405 -0
- package/dist/esm/qs.mjs +1 -0
- package/dist/esm/requestContext.mjs +51 -0
- package/dist/esm/transport.mjs +179 -0
- package/dist/esm/types.mjs +10 -0
- package/dist/esm/utiles.mjs +18 -0
- package/dist/esm-node/browser.mjs +367 -0
- package/dist/esm-node/handleRes.mjs +16 -0
- package/dist/esm-node/node.mjs +406 -0
- package/dist/esm-node/qs.mjs +2 -0
- package/dist/esm-node/requestContext.mjs +52 -0
- package/dist/esm-node/transport.mjs +180 -0
- package/dist/esm-node/types.mjs +11 -0
- package/dist/esm-node/utiles.mjs +19 -0
- package/dist/types/browser.d.ts +28 -0
- package/dist/types/handleRes.d.ts +2 -0
- package/dist/types/node.d.ts +29 -0
- package/dist/types/qs.d.ts +1 -0
- package/dist/types/requestContext.d.ts +18 -0
- package/dist/types/transport.d.ts +12 -0
- package/dist/types/types.d.ts +167 -0
- package/dist/types/utiles.d.ts +5 -0
- package/package.json +86 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const getUploadPayload = (args)=>{
|
|
2
|
+
const payload = 'object' == typeof args[args.length - 1] ? args[args.length - 1] : {};
|
|
3
|
+
const files = payload.files;
|
|
4
|
+
if (!files) throw new Error('no files');
|
|
5
|
+
const formdata = new FormData();
|
|
6
|
+
for (const [key, value] of Object.entries(files))if (value instanceof FileList) for(let i = 0; i < value.length; i++){
|
|
7
|
+
const file = value.item(i);
|
|
8
|
+
if (file) formdata.append(key, file);
|
|
9
|
+
}
|
|
10
|
+
else formdata.append(key, value);
|
|
11
|
+
const body = formdata;
|
|
12
|
+
return {
|
|
13
|
+
body,
|
|
14
|
+
headers: payload.headers,
|
|
15
|
+
params: payload.params
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export { getUploadPayload };
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { compile } from "path-to-regexp";
|
|
3
|
+
import { stringify } from "qs";
|
|
4
|
+
import { handleRes } from "./handleRes.mjs";
|
|
5
|
+
import { executeWithResilience } from "./transport.mjs";
|
|
6
|
+
import { BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS, BFF_ENVELOPE_HEADER, BFF_OPERATION_CONTEXT_DETAIL_HEADER, BFF_OPERATION_CONTEXT_HEADER } from "./types.mjs";
|
|
7
|
+
import { getUploadPayload } from "./utiles.mjs";
|
|
8
|
+
export * from "./requestContext.mjs";
|
|
9
|
+
export * from "./types.mjs";
|
|
10
|
+
const realRequest = new Map();
|
|
11
|
+
const realAllowedHeaders = new Map();
|
|
12
|
+
const realRequireEnvelope = new Map();
|
|
13
|
+
const realAllowCrossOriginEnvelope = new Map();
|
|
14
|
+
const realTransportResilience = new Map();
|
|
15
|
+
const realIdentityBinding = new Map();
|
|
16
|
+
const realOperationContract = new Map();
|
|
17
|
+
const domainMap = new Map();
|
|
18
|
+
const isEmptyDomain = (domain)=>'string' != typeof domain || '' === domain.trim();
|
|
19
|
+
const TRACEPARENT_HEADER = 'traceparent';
|
|
20
|
+
const OPERATION_CONTEXT_DETAIL_HEADER = BFF_OPERATION_CONTEXT_DETAIL_HEADER;
|
|
21
|
+
const TRACEPARENT_REGEX = /^00-([0-9a-f]{32})-([0-9a-f]{16})-[0-9a-f]{2}$/i;
|
|
22
|
+
const readProcessEnv = (key)=>{
|
|
23
|
+
if ("u" < typeof process || void 0 === process.env || 'string' != typeof process.env[key]) return;
|
|
24
|
+
return process.env[key];
|
|
25
|
+
};
|
|
26
|
+
const isStrictDefaultRequestIdEnabled = ()=>'true' === readProcessEnv('MODERN_BFF_STRICT_DEFAULT_REQUEST_ID');
|
|
27
|
+
const isSecuredRequestId = (requestId)=>'default' !== requestId || isStrictDefaultRequestIdEnabled();
|
|
28
|
+
const firstHeaderValue = (value)=>Array.isArray(value) ? value[0] : value;
|
|
29
|
+
const findHeaderKey = (headers, header)=>{
|
|
30
|
+
const normalized = header.toLowerCase();
|
|
31
|
+
return Object.keys(headers).find((key)=>key.toLowerCase() === normalized);
|
|
32
|
+
};
|
|
33
|
+
const readHeader = (headers, header)=>{
|
|
34
|
+
const key = findHeaderKey(headers, header);
|
|
35
|
+
return 'string' == typeof key ? headers[key] : void 0;
|
|
36
|
+
};
|
|
37
|
+
const writeHeader = (headers, header, value)=>{
|
|
38
|
+
if (void 0 === value) return;
|
|
39
|
+
const key = findHeaderKey(headers, header);
|
|
40
|
+
if ('string' == typeof key && key !== header) delete headers[key];
|
|
41
|
+
headers[header] = value;
|
|
42
|
+
};
|
|
43
|
+
const deleteHeader = (headers, header)=>{
|
|
44
|
+
const key = findHeaderKey(headers, header);
|
|
45
|
+
if ('string' == typeof key) delete headers[key];
|
|
46
|
+
};
|
|
47
|
+
const toOrigin = (value)=>{
|
|
48
|
+
if (!value) return;
|
|
49
|
+
try {
|
|
50
|
+
return new URL(value).origin;
|
|
51
|
+
} catch (error) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const parseTraceparent = (value)=>{
|
|
56
|
+
const traceparent = firstHeaderValue(value);
|
|
57
|
+
if ('string' != typeof traceparent) return;
|
|
58
|
+
const match = traceparent.trim().match(TRACEPARENT_REGEX);
|
|
59
|
+
if (!match) return;
|
|
60
|
+
const [, traceId, spanId] = match;
|
|
61
|
+
if (!traceId || !spanId) return;
|
|
62
|
+
return {
|
|
63
|
+
traceId: traceId.toLowerCase(),
|
|
64
|
+
spanId: spanId.toLowerCase()
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
const extractPathParamNames = (path)=>Array.from(path.matchAll(/:([A-Za-z0-9_]+)/g)).flatMap(([, key])=>key ? [
|
|
68
|
+
key
|
|
69
|
+
] : []);
|
|
70
|
+
const originFetch = (...params)=>{
|
|
71
|
+
const [url, init] = params;
|
|
72
|
+
if (init?.method?.toLowerCase() === 'get') init.body = void 0;
|
|
73
|
+
return fetch(url, init).then(handleRes);
|
|
74
|
+
};
|
|
75
|
+
const buildOperationContext = ({ requestId, method, path, operationContext, traceparent })=>{
|
|
76
|
+
const routePath = operationContext?.routePath || path;
|
|
77
|
+
const operationMethod = (operationContext?.method || method || 'GET').toUpperCase();
|
|
78
|
+
const rawOperationId = operationContext?.operationId || `${operationMethod}:${routePath}`;
|
|
79
|
+
const operationId = rawOperationId.startsWith(`${requestId}:`) ? rawOperationId : `${requestId}:${rawOperationId}`;
|
|
80
|
+
const traceparentValue = operationContext?.traceparent || ('string' == typeof firstHeaderValue(traceparent) ? String(firstHeaderValue(traceparent)) : void 0);
|
|
81
|
+
const parsedTraceContext = operationContext?.traceId && operationContext?.spanId ? {
|
|
82
|
+
traceId: operationContext.traceId,
|
|
83
|
+
spanId: operationContext.spanId
|
|
84
|
+
} : parseTraceparent(traceparentValue);
|
|
85
|
+
return {
|
|
86
|
+
requestId,
|
|
87
|
+
operationId,
|
|
88
|
+
routePath,
|
|
89
|
+
method: operationMethod,
|
|
90
|
+
...operationContext?.schemaHash ? {
|
|
91
|
+
schemaHash: operationContext.schemaHash
|
|
92
|
+
} : {},
|
|
93
|
+
...'number' == typeof operationContext?.operationVersion ? {
|
|
94
|
+
operationVersion: operationContext.operationVersion
|
|
95
|
+
} : {},
|
|
96
|
+
...traceparentValue ? {
|
|
97
|
+
traceparent: traceparentValue
|
|
98
|
+
} : {},
|
|
99
|
+
...parsedTraceContext ? {
|
|
100
|
+
traceId: parsedTraceContext.traceId,
|
|
101
|
+
spanId: parsedTraceContext.spanId
|
|
102
|
+
} : {}
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
class ProducerClientNotInitializedError extends Error {
|
|
106
|
+
constructor(requestId){
|
|
107
|
+
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';
|
|
108
|
+
this.name = 'ProducerClientNotInitializedError';
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
class ProducerDomainNotConfiguredError extends Error {
|
|
112
|
+
constructor(requestId){
|
|
113
|
+
super(`Producer client "${requestId}" must provide setDomain() during configure().`), this.code = 'BFF_PRODUCER_DOMAIN_NOT_CONFIGURED';
|
|
114
|
+
this.name = 'ProducerDomainNotConfiguredError';
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
class CrossOriginEnvelopePolicyError extends Error {
|
|
118
|
+
constructor(requestId, sourceOrigin, targetOrigin){
|
|
119
|
+
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';
|
|
120
|
+
this.name = 'CrossOriginEnvelopePolicyError';
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
class IdentityBindingViolationError extends Error {
|
|
124
|
+
constructor(violation){
|
|
125
|
+
super(`Identity header "${violation.header}" for producer "${violation.requestId}" was rejected by server-derived identity binding.`), this.code = 'BFF_IDENTITY_BINDING_VIOLATION';
|
|
126
|
+
this.name = 'IdentityBindingViolationError';
|
|
127
|
+
this.violation = violation;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
class OperationContractViolationError extends Error {
|
|
131
|
+
constructor(violation){
|
|
132
|
+
super(`Operation contract violation "${violation.reason}" for producer "${violation.requestId}" operation "${violation.operationId}".`), this.code = 'BFF_OPERATION_CONTRACT_VIOLATION';
|
|
133
|
+
this.name = 'OperationContractViolationError';
|
|
134
|
+
this.violation = violation;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const validateOperationContract = (requestId, contextPayload)=>{
|
|
138
|
+
const operationContract = realOperationContract.get(requestId);
|
|
139
|
+
const operationContractEnabled = operationContract?.enabled ?? isSecuredRequestId(requestId);
|
|
140
|
+
if (!operationContractEnabled) return;
|
|
141
|
+
const strict = operationContract?.strict ?? true;
|
|
142
|
+
const requireSchemaHash = operationContract?.requireSchemaHash ?? true;
|
|
143
|
+
const requireOperationVersion = operationContract?.requireOperationVersion ?? true;
|
|
144
|
+
const maybeReportViolation = (reason)=>{
|
|
145
|
+
const violation = {
|
|
146
|
+
requestId,
|
|
147
|
+
target: 'browser',
|
|
148
|
+
operationId: contextPayload.operationId,
|
|
149
|
+
routePath: contextPayload.routePath,
|
|
150
|
+
method: contextPayload.method,
|
|
151
|
+
schemaHash: 'string' == typeof contextPayload.schemaHash ? contextPayload.schemaHash : void 0,
|
|
152
|
+
operationVersion: 'number' == typeof contextPayload.operationVersion ? contextPayload.operationVersion : void 0,
|
|
153
|
+
reason
|
|
154
|
+
};
|
|
155
|
+
operationContract?.onViolation?.(violation);
|
|
156
|
+
if (strict) throw new OperationContractViolationError(violation);
|
|
157
|
+
};
|
|
158
|
+
if (requireSchemaHash && 'string' != typeof contextPayload.schemaHash) maybeReportViolation('missing_schema_hash');
|
|
159
|
+
if (requireOperationVersion && 'number' != typeof contextPayload.operationVersion) maybeReportViolation('missing_operation_version');
|
|
160
|
+
};
|
|
161
|
+
const getConfiguredRequest = (requestId, fallback)=>{
|
|
162
|
+
const configuredRequest = realRequest.get(requestId);
|
|
163
|
+
if (configuredRequest) return configuredRequest;
|
|
164
|
+
if ('default' !== requestId) throw new ProducerClientNotInitializedError(requestId);
|
|
165
|
+
return fallback;
|
|
166
|
+
};
|
|
167
|
+
const configure = (options)=>{
|
|
168
|
+
const { request, interceptor, allowedHeaders, transport, requireEnvelope, allowCrossOriginEnvelope, identityBinding, operationContract, setDomain, requestId = 'default' } = options;
|
|
169
|
+
const hasExistingDomain = domainMap.has(requestId);
|
|
170
|
+
if ('default' !== requestId && !setDomain && !hasExistingDomain) throw new ProducerDomainNotConfiguredError(requestId);
|
|
171
|
+
let configuredRequest = request || originFetch;
|
|
172
|
+
if (interceptor && !request) configuredRequest = interceptor(fetch);
|
|
173
|
+
if (Array.isArray(allowedHeaders)) realAllowedHeaders.set(requestId, allowedHeaders);
|
|
174
|
+
if (transport && 'object' == typeof transport) realTransportResilience.set(requestId, transport);
|
|
175
|
+
if (identityBinding && 'object' == typeof identityBinding) realIdentityBinding.set(requestId, identityBinding);
|
|
176
|
+
if (operationContract && 'object' == typeof operationContract) realOperationContract.set(requestId, operationContract);
|
|
177
|
+
if ('boolean' == typeof requireEnvelope) realRequireEnvelope.set(requestId, requireEnvelope);
|
|
178
|
+
if ('boolean' == typeof allowCrossOriginEnvelope || 'function' == typeof allowCrossOriginEnvelope) realAllowCrossOriginEnvelope.set(requestId, allowCrossOriginEnvelope);
|
|
179
|
+
if (setDomain) {
|
|
180
|
+
const resolvedDomain = setDomain({
|
|
181
|
+
target: 'browser',
|
|
182
|
+
requestId
|
|
183
|
+
});
|
|
184
|
+
if ('default' !== requestId && isEmptyDomain(resolvedDomain)) throw new ProducerDomainNotConfiguredError(requestId);
|
|
185
|
+
if ('string' == typeof resolvedDomain) domainMap.set(requestId, resolvedDomain);
|
|
186
|
+
}
|
|
187
|
+
realRequest.set(requestId, configuredRequest);
|
|
188
|
+
};
|
|
189
|
+
const normalizeRequestOptions = (...args)=>{
|
|
190
|
+
if ('object' == typeof args[0] && null !== args[0]) return args[0];
|
|
191
|
+
const [path, method, port, httpMethodDecider, fetch1, requestId, operationContext] = args;
|
|
192
|
+
return {
|
|
193
|
+
path,
|
|
194
|
+
method,
|
|
195
|
+
port,
|
|
196
|
+
httpMethodDecider,
|
|
197
|
+
fetch: fetch1,
|
|
198
|
+
requestId,
|
|
199
|
+
operationContext
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
const createRequest = (...args)=>{
|
|
203
|
+
const { path, method, port, httpMethodDecider = 'functionName', fetch: fetch1 = originFetch, domain, requestId = 'default', operationContext } = normalizeRequestOptions(...args);
|
|
204
|
+
const getFinalPath = compile(path, {
|
|
205
|
+
encode: encodeURIComponent
|
|
206
|
+
});
|
|
207
|
+
const keyNames = extractPathParamNames(path);
|
|
208
|
+
const sender = async (...args)=>{
|
|
209
|
+
const fetcher = getConfiguredRequest(requestId, fetch1);
|
|
210
|
+
let body;
|
|
211
|
+
let finalURL;
|
|
212
|
+
let headers;
|
|
213
|
+
if ('inputParams' === httpMethodDecider) {
|
|
214
|
+
finalURL = path;
|
|
215
|
+
body = JSON.stringify({
|
|
216
|
+
args
|
|
217
|
+
});
|
|
218
|
+
headers = {
|
|
219
|
+
'Content-Type': 'application/json'
|
|
220
|
+
};
|
|
221
|
+
} else {
|
|
222
|
+
const payload = 'object' == typeof args[args.length - 1] ? args[args.length - 1] : {};
|
|
223
|
+
payload.params = payload.params || {};
|
|
224
|
+
const requestParams = args[0];
|
|
225
|
+
if ('object' == typeof requestParams && requestParams.params) {
|
|
226
|
+
const { params } = requestParams;
|
|
227
|
+
keyNames.forEach((keyName)=>{
|
|
228
|
+
payload.params[keyName] = params[keyName];
|
|
229
|
+
});
|
|
230
|
+
} else keyNames.forEach((keyName, index)=>{
|
|
231
|
+
payload.params[keyName] = args[index];
|
|
232
|
+
});
|
|
233
|
+
const finalPath = getFinalPath(payload.params);
|
|
234
|
+
finalURL = payload.query ? `${finalPath}?${stringify(payload.query)}` : finalPath;
|
|
235
|
+
headers = payload.headers ? {
|
|
236
|
+
...payload.headers
|
|
237
|
+
} : {};
|
|
238
|
+
const identityBinding = realIdentityBinding.get(requestId);
|
|
239
|
+
const identityBindingEnabled = identityBinding?.enabled ?? isSecuredRequestId(requestId);
|
|
240
|
+
const identityBindingStrict = identityBinding?.strict ?? isSecuredRequestId(requestId);
|
|
241
|
+
const protectedIdentityHeaders = (identityBinding?.protectedHeaders || BFF_DEFAULT_PROTECTED_IDENTITY_HEADERS).map((header)=>header.toLowerCase());
|
|
242
|
+
if (identityBindingEnabled) {
|
|
243
|
+
const derivedIdentityHeaders = {};
|
|
244
|
+
const customDerivedHeaders = identityBinding?.deriveHeaders?.({
|
|
245
|
+
requestId,
|
|
246
|
+
target: 'browser',
|
|
247
|
+
incomingHeaders: {},
|
|
248
|
+
protectedHeaders: [
|
|
249
|
+
...protectedIdentityHeaders
|
|
250
|
+
]
|
|
251
|
+
});
|
|
252
|
+
if (customDerivedHeaders && 'object' == typeof customDerivedHeaders) for (const header of protectedIdentityHeaders){
|
|
253
|
+
const customValue = readHeader(customDerivedHeaders, header);
|
|
254
|
+
if (void 0 !== customValue) writeHeader(derivedIdentityHeaders, header, customValue);
|
|
255
|
+
}
|
|
256
|
+
for (const header of protectedIdentityHeaders){
|
|
257
|
+
const attemptedValue = readHeader(headers, header);
|
|
258
|
+
if (void 0 === attemptedValue) continue;
|
|
259
|
+
const violation = {
|
|
260
|
+
requestId,
|
|
261
|
+
target: 'browser',
|
|
262
|
+
header,
|
|
263
|
+
attemptedValue,
|
|
264
|
+
derivedValue: readHeader(derivedIdentityHeaders, header),
|
|
265
|
+
reason: identityBindingStrict ? 'client_override_rejected' : 'client_override_blocked'
|
|
266
|
+
};
|
|
267
|
+
identityBinding?.onViolation?.(violation);
|
|
268
|
+
if (identityBindingStrict) throw new IdentityBindingViolationError(violation);
|
|
269
|
+
deleteHeader(headers, header);
|
|
270
|
+
}
|
|
271
|
+
Object.keys(derivedIdentityHeaders).forEach((header)=>{
|
|
272
|
+
writeHeader(headers, header, derivedIdentityHeaders[header]);
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
body = payload.data && 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
|
|
276
|
+
if (payload.data) {
|
|
277
|
+
headers['Content-Type'] = 'application/json';
|
|
278
|
+
body = 'object' == typeof payload.data ? JSON.stringify(payload.data) : payload.body;
|
|
279
|
+
} else if (payload.body) {
|
|
280
|
+
headers['Content-Type'] = 'text/plain';
|
|
281
|
+
body = payload.body;
|
|
282
|
+
} else if (payload.formData) body = payload.formData;
|
|
283
|
+
else if (payload.formUrlencoded) {
|
|
284
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
285
|
+
body = 'object' != typeof payload.formUrlencoded || payload.formUrlencoded instanceof URLSearchParams ? payload.formUrlencoded : stringify(payload.formUrlencoded);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
headers.accept = "application/json,*/*;q=0.8";
|
|
289
|
+
const configDomain = domainMap.get(requestId);
|
|
290
|
+
if ('default' !== requestId && isEmptyDomain(configDomain)) throw new ProducerDomainNotConfiguredError(requestId);
|
|
291
|
+
finalURL = `${configDomain || ''}${finalURL}`;
|
|
292
|
+
const shouldRequireEnvelope = realRequireEnvelope.get(requestId) ?? isSecuredRequestId(requestId);
|
|
293
|
+
if (shouldRequireEnvelope) {
|
|
294
|
+
const sourceOrigin = "u" > typeof window ? window.location.origin : void 0;
|
|
295
|
+
const targetOrigin = toOrigin(finalURL);
|
|
296
|
+
const traceContext = parseTraceparent(readHeader(headers, TRACEPARENT_HEADER));
|
|
297
|
+
const isCrossOrigin = Boolean(sourceOrigin) && Boolean(targetOrigin) && sourceOrigin !== targetOrigin;
|
|
298
|
+
if (isCrossOrigin) {
|
|
299
|
+
const policy = realAllowCrossOriginEnvelope.get(requestId);
|
|
300
|
+
const isAllowed = 'function' == typeof policy ? policy({
|
|
301
|
+
requestId,
|
|
302
|
+
sourceOrigin,
|
|
303
|
+
targetOrigin,
|
|
304
|
+
target: 'browser'
|
|
305
|
+
}) : true === policy;
|
|
306
|
+
if (!isAllowed) throw new CrossOriginEnvelopePolicyError(requestId, sourceOrigin, targetOrigin);
|
|
307
|
+
}
|
|
308
|
+
headers[BFF_ENVELOPE_HEADER] = JSON.stringify({
|
|
309
|
+
requestId,
|
|
310
|
+
target: 'browser',
|
|
311
|
+
timestamp: Date.now(),
|
|
312
|
+
sourceOrigin,
|
|
313
|
+
targetOrigin,
|
|
314
|
+
...traceContext ? {
|
|
315
|
+
traceId: traceContext.traceId,
|
|
316
|
+
spanId: traceContext.spanId
|
|
317
|
+
} : {}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
if (isSecuredRequestId(requestId)) {
|
|
321
|
+
if (void 0 === readHeader(headers, TRACEPARENT_HEADER) && operationContext?.traceparent) writeHeader(headers, TRACEPARENT_HEADER, operationContext.traceparent);
|
|
322
|
+
const contextPayload = buildOperationContext({
|
|
323
|
+
requestId,
|
|
324
|
+
method,
|
|
325
|
+
path,
|
|
326
|
+
operationContext,
|
|
327
|
+
traceparent: readHeader(headers, TRACEPARENT_HEADER)
|
|
328
|
+
});
|
|
329
|
+
validateOperationContract(requestId, contextPayload);
|
|
330
|
+
if (void 0 === readHeader(headers, BFF_OPERATION_CONTEXT_HEADER)) writeHeader(headers, BFF_OPERATION_CONTEXT_HEADER, contextPayload.operationId);
|
|
331
|
+
writeHeader(headers, OPERATION_CONTEXT_DETAIL_HEADER, JSON.stringify(contextPayload));
|
|
332
|
+
}
|
|
333
|
+
return executeWithResilience({
|
|
334
|
+
requestId,
|
|
335
|
+
target: 'browser',
|
|
336
|
+
method,
|
|
337
|
+
url: finalURL,
|
|
338
|
+
init: {
|
|
339
|
+
method,
|
|
340
|
+
body,
|
|
341
|
+
headers
|
|
342
|
+
},
|
|
343
|
+
fetcher,
|
|
344
|
+
transport: realTransportResilience.get(requestId)
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
return sender;
|
|
348
|
+
};
|
|
349
|
+
const createUploader = ({ path, domain, requestId = 'default' })=>{
|
|
350
|
+
const getFinalPath = compile(path, {
|
|
351
|
+
encode: encodeURIComponent
|
|
352
|
+
});
|
|
353
|
+
const sender = (...args)=>{
|
|
354
|
+
const fetcher = getConfiguredRequest(requestId, originFetch);
|
|
355
|
+
const { body, headers, params } = getUploadPayload(args);
|
|
356
|
+
const finalPath = getFinalPath(params);
|
|
357
|
+
const configDomain = domainMap.get(requestId);
|
|
358
|
+
const finalURL = `${configDomain || domain || ''}${finalPath}`;
|
|
359
|
+
return fetcher(finalURL, {
|
|
360
|
+
method: 'POST',
|
|
361
|
+
body,
|
|
362
|
+
headers
|
|
363
|
+
});
|
|
364
|
+
};
|
|
365
|
+
return sender;
|
|
366
|
+
};
|
|
367
|
+
export { CrossOriginEnvelopePolicyError, IdentityBindingViolationError, OperationContractViolationError, ProducerClientNotInitializedError, ProducerDomainNotConfiguredError, configure, createRequest, createUploader };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const handleRes = async (res)=>{
|
|
3
|
+
const contentType = res.headers.get('content-type');
|
|
4
|
+
if (!res.ok) {
|
|
5
|
+
const data = await res.json();
|
|
6
|
+
res.data = data;
|
|
7
|
+
throw res;
|
|
8
|
+
}
|
|
9
|
+
if (contentType?.includes('application/json') || contentType?.includes('text/json')) return res.json();
|
|
10
|
+
if (contentType?.includes('text/html') || contentType?.includes('text/plain')) return res.text();
|
|
11
|
+
if ((contentType?.includes('application/x-www-form-urlencoded') || contentType?.includes('multipart/form-data')) && res instanceof Response) return res.formData();
|
|
12
|
+
if (contentType?.includes('application/octet-stream')) return res.arrayBuffer();
|
|
13
|
+
if (contentType?.includes('image/png')) return res;
|
|
14
|
+
return res.text();
|
|
15
|
+
};
|
|
16
|
+
export { handleRes };
|