@bleedingdev/modern-js-plugin-bff 3.2.0-ultramodern.120 → 3.2.0-ultramodern.122
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/dist/cjs/runtime/data-platform/index.js +2 -13
- package/dist/cjs/runtime/effect/adapter.js +78 -9
- package/dist/cjs/runtime/effect/edge.js +13 -17
- package/dist/cjs/runtime/effect/endpoint-contracts.js +130 -0
- package/dist/cjs/runtime/effect/handler.js +50 -5
- package/dist/cjs/runtime/effect/module.js +16 -7
- package/dist/cjs/runtime/effect/operation-context.js +1 -13
- package/dist/cjs/runtime/effect-client/runtime.js +266 -0
- package/dist/cjs/runtime/hono/adapter.js +21 -9
- package/dist/cjs/runtime/safe-failure.js +83 -0
- package/dist/cjs/utils/clientGenerator.js +4 -4
- package/dist/cjs/utils/crossProjectServerPolicy.js +104 -0
- package/dist/cjs/utils/effectClientGenerator.js +90 -483
- package/dist/esm/runtime/data-platform/index.mjs +2 -13
- package/dist/esm/runtime/effect/adapter.mjs +78 -9
- package/dist/esm/runtime/effect/edge.mjs +2 -9
- package/dist/esm/runtime/effect/endpoint-contracts.mjs +68 -0
- package/dist/esm/runtime/effect/handler.mjs +36 -4
- package/dist/esm/runtime/effect/module.mjs +17 -8
- package/dist/esm/runtime/effect/operation-context.mjs +1 -13
- package/dist/esm/runtime/effect-client/runtime.mjs +228 -0
- package/dist/esm/runtime/hono/adapter.mjs +21 -9
- package/dist/esm/runtime/safe-failure.mjs +45 -0
- package/dist/esm/utils/clientGenerator.mjs +5 -5
- package/dist/esm/utils/crossProjectServerPolicy.mjs +50 -0
- package/dist/esm/utils/effectClientGenerator.mjs +88 -484
- package/dist/esm-node/runtime/data-platform/index.mjs +2 -13
- package/dist/esm-node/runtime/effect/adapter.mjs +78 -9
- package/dist/esm-node/runtime/effect/edge.mjs +2 -9
- package/dist/esm-node/runtime/effect/endpoint-contracts.mjs +69 -0
- package/dist/esm-node/runtime/effect/handler.mjs +36 -4
- package/dist/esm-node/runtime/effect/module.mjs +17 -8
- package/dist/esm-node/runtime/effect/operation-context.mjs +1 -13
- package/dist/esm-node/runtime/effect-client/runtime.mjs +229 -0
- package/dist/esm-node/runtime/hono/adapter.mjs +21 -9
- package/dist/esm-node/runtime/safe-failure.mjs +46 -0
- package/dist/esm-node/utils/clientGenerator.mjs +5 -5
- package/dist/esm-node/utils/crossProjectServerPolicy.mjs +52 -0
- package/dist/esm-node/utils/effectClientGenerator.mjs +88 -484
- package/dist/types/runtime/effect/adapter.d.ts +25 -0
- package/dist/types/runtime/effect/endpoint-contracts.d.ts +62 -0
- package/dist/types/runtime/effect/handler.d.ts +30 -0
- package/dist/types/runtime/effect/module.d.ts +21 -1
- package/dist/types/runtime/effect-client/runtime.d.ts +71 -0
- package/dist/types/runtime/hono/adapter.d.ts +3 -0
- package/dist/types/runtime/safe-failure.d.ts +1 -0
- package/dist/types/utils/crossProjectServerPolicy.d.ts +35 -0
- package/dist/types/utils/effectClientGenerator.d.ts +15 -1
- package/package.json +24 -12
|
@@ -37,6 +37,7 @@ var __webpack_require__ = {};
|
|
|
37
37
|
var __webpack_exports__ = {};
|
|
38
38
|
__webpack_require__.r(__webpack_exports__);
|
|
39
39
|
__webpack_require__.d(__webpack_exports__, {
|
|
40
|
+
generateEffectClient: ()=>generateEffectClient,
|
|
40
41
|
generateEffectClientCode: ()=>generateEffectClientCode,
|
|
41
42
|
renderEffectClientDeclaration: ()=>renderEffectClientDeclaration,
|
|
42
43
|
resolveEffectEntryFile: ()=>resolveEffectEntryFile
|
|
@@ -45,6 +46,7 @@ const bff_core_namespaceObject = require("@modern-js/bff-core");
|
|
|
45
46
|
const utils_namespaceObject = require("@modern-js/utils");
|
|
46
47
|
const external_path_namespaceObject = require("path");
|
|
47
48
|
var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
|
|
49
|
+
const endpoint_contracts_js_namespaceObject = require("../runtime/effect/endpoint-contracts.js");
|
|
48
50
|
const JS_OR_TS_EXTS = [
|
|
49
51
|
'.js',
|
|
50
52
|
'.jsx',
|
|
@@ -56,18 +58,11 @@ const JS_OR_TS_EXTS = [
|
|
|
56
58
|
'.cts'
|
|
57
59
|
];
|
|
58
60
|
const DEFAULT_REQUEST_CREATOR = '@modern-js/plugin-bff/client';
|
|
59
|
-
const
|
|
61
|
+
const EFFECT_CLIENT_RUNTIME_IMPORT = '@modern-js/plugin-bff/effect-client-runtime';
|
|
60
62
|
let httpApiRuntimePromise;
|
|
61
63
|
function isRecord(value) {
|
|
62
64
|
return 'object' == typeof value && null !== value;
|
|
63
65
|
}
|
|
64
|
-
function ensureLeadingSlash(pathname) {
|
|
65
|
-
return pathname.startsWith('/') ? pathname : `/${pathname}`;
|
|
66
|
-
}
|
|
67
|
-
function normalizePrefix(prefix) {
|
|
68
|
-
if ('/' === prefix) return '';
|
|
69
|
-
return ensureLeadingSlash(prefix || '/api');
|
|
70
|
-
}
|
|
71
66
|
function isAbsoluteUrl(value) {
|
|
72
67
|
try {
|
|
73
68
|
new URL(value);
|
|
@@ -79,32 +74,22 @@ function isAbsoluteUrl(value) {
|
|
|
79
74
|
function resolveBatchEndpoint(prefix, endpoint) {
|
|
80
75
|
const value = endpoint || '/_data/batch';
|
|
81
76
|
if (isAbsoluteUrl(value)) return value;
|
|
82
|
-
const normalizedPrefix =
|
|
83
|
-
const normalizedEndpoint = ensureLeadingSlash(value);
|
|
77
|
+
const normalizedPrefix = (0, endpoint_contracts_js_namespaceObject.normalizeEffectPrefix)(prefix);
|
|
78
|
+
const normalizedEndpoint = (0, endpoint_contracts_js_namespaceObject.ensureLeadingSlash)(value);
|
|
84
79
|
if (!normalizedPrefix) return normalizedEndpoint;
|
|
85
80
|
if (normalizedEndpoint === normalizedPrefix || normalizedEndpoint.startsWith(`${normalizedPrefix}/`)) return normalizedEndpoint;
|
|
86
81
|
return `${normalizedPrefix}${'/' === normalizedEndpoint ? '' : normalizedEndpoint}`;
|
|
87
82
|
}
|
|
88
|
-
function
|
|
89
|
-
const normalizedPrefix = normalizePrefix(prefix);
|
|
90
|
-
const normalizedEndpointPath = ensureLeadingSlash(endpointPath);
|
|
91
|
-
const finalEndpointPath = '/' === normalizedEndpointPath ? '' : endpointPath;
|
|
92
|
-
if (!normalizedPrefix && !finalEndpointPath) return '/';
|
|
93
|
-
return `${normalizedPrefix}${finalEndpointPath || ''}`;
|
|
94
|
-
}
|
|
95
|
-
function toSafeIdentifier(name) {
|
|
96
|
-
const sanitized = name.replace(/[^a-zA-Z0-9_$]/g, '_');
|
|
97
|
-
if (!sanitized) return '_';
|
|
98
|
-
if (/^[0-9]/.test(sanitized)) return `_${sanitized}`;
|
|
99
|
-
return sanitized;
|
|
100
|
-
}
|
|
101
|
-
function getPackageName(appDir) {
|
|
83
|
+
function getPackageInfo(appDir) {
|
|
102
84
|
try {
|
|
103
85
|
const packageJsonPath = external_path_default().resolve(appDir, './package.json');
|
|
104
86
|
const packageJson = utils_namespaceObject.fs.readJSONSync(packageJsonPath);
|
|
105
|
-
return
|
|
87
|
+
return {
|
|
88
|
+
name: packageJson.name,
|
|
89
|
+
version: packageJson.version
|
|
90
|
+
};
|
|
106
91
|
} catch {
|
|
107
|
-
return;
|
|
92
|
+
return {};
|
|
108
93
|
}
|
|
109
94
|
}
|
|
110
95
|
async function getHttpApiRuntime() {
|
|
@@ -127,472 +112,66 @@ async function getHttpApiRuntime() {
|
|
|
127
112
|
})();
|
|
128
113
|
return httpApiRuntimePromise;
|
|
129
114
|
}
|
|
130
|
-
function resolveApiId(api) {
|
|
131
|
-
const fallback = 'EffectHttpApi';
|
|
132
|
-
const maybeApi = api;
|
|
133
|
-
if ('identifier' in maybeApi && 'string' == typeof maybeApi.identifier && maybeApi.identifier) return maybeApi.identifier;
|
|
134
|
-
return fallback;
|
|
135
|
-
}
|
|
136
|
-
function collectEffectEndpoints(httpApiRuntime, api, prefix) {
|
|
137
|
-
const endpoints = [];
|
|
138
|
-
const apiId = resolveApiId(api);
|
|
139
|
-
httpApiRuntime.reflect(api, {
|
|
140
|
-
onGroup: ()=>{},
|
|
141
|
-
onEndpoint: ({ group, endpoint })=>{
|
|
142
|
-
endpoints.push({
|
|
143
|
-
apiId,
|
|
144
|
-
groupName: String(group.identifier),
|
|
145
|
-
endpointName: String(endpoint.name),
|
|
146
|
-
method: String(endpoint.method).toUpperCase(),
|
|
147
|
-
routePath: getRoutePath(prefix, String(endpoint.path))
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
return endpoints.sort((a, b)=>{
|
|
152
|
-
if (a.groupName === b.groupName) return a.endpointName.localeCompare(b.endpointName);
|
|
153
|
-
return a.groupName.localeCompare(b.groupName);
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
115
|
async function loadEffectApi(resourcePath) {
|
|
157
116
|
const httpApiRuntime = await getHttpApiRuntime();
|
|
158
117
|
const mod = await (0, utils_namespaceObject.compatibleRequire)(resourcePath, false);
|
|
159
|
-
|
|
160
|
-
if (isRecord(mod) && isRecord(mod.default) && httpApiRuntime.isHttpApi(mod.default.api)) return mod.default.api;
|
|
161
|
-
if (isRecord(mod) && 'function' == typeof mod.default && 0 === mod.default.length) {
|
|
162
|
-
const output = await mod.default();
|
|
163
|
-
if (isRecord(output) && httpApiRuntime.isHttpApi(output.api)) return output.api;
|
|
164
|
-
}
|
|
165
|
-
return null;
|
|
118
|
+
return (0, endpoint_contracts_js_namespaceObject.extractHttpApiFromModule)(mod, httpApiRuntime.isHttpApi);
|
|
166
119
|
}
|
|
167
120
|
function renderEffectClientCode(endpoints, options) {
|
|
168
|
-
const senderDeclarations = [];
|
|
169
|
-
const operationDeclarations = [];
|
|
170
|
-
const callerDeclarations = [];
|
|
171
|
-
const groupedCallers = {};
|
|
172
|
-
const groupedOperations = {};
|
|
173
121
|
const requestCreator = options.requestCreator || DEFAULT_REQUEST_CREATOR;
|
|
174
|
-
const dataPlatformImport = DEFAULT_DATA_PLATFORM_IMPORT;
|
|
175
122
|
const httpMethodDecider = options.httpMethodDecider || 'functionName';
|
|
176
|
-
const
|
|
177
|
-
const packageName =
|
|
123
|
+
const packageInfo = getPackageInfo(options.appDir);
|
|
124
|
+
const packageName = packageInfo.name;
|
|
178
125
|
const dataPlatformAppNamespace = packageName || 'unknown-app';
|
|
179
126
|
const requestId = 'bundle' === options.target ? packageName || process.env.npm_package_name : void 0;
|
|
180
127
|
const normalizedRequestId = requestId || 'default';
|
|
181
|
-
const operationVersion =
|
|
182
|
-
const schemaHash = (0, bff_core_namespaceObject.createOperationSchemaHash)((0, bff_core_namespaceObject.createOperationEntries)(endpoints.map((endpoint)=>({
|
|
183
|
-
name: endpoint.endpointName,
|
|
184
|
-
httpMethod: endpoint.method,
|
|
185
|
-
routePath: endpoint.routePath
|
|
186
|
-
}))), normalizedRequestId);
|
|
128
|
+
const operationVersion = (0, bff_core_namespaceObject.deriveOperationVersion)(packageInfo.version);
|
|
187
129
|
const batchConfig = options.dataPlatformBatch;
|
|
188
130
|
const batchEndpoint = resolveBatchEndpoint(options.prefix, batchConfig?.endpoint);
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
appNamespace: dataPlatformAppNamespace,
|
|
222
|
-
apiId: endpoint.apiId,
|
|
223
|
-
group: endpoint.groupName,
|
|
224
|
-
endpoint: endpoint.endpointName,
|
|
225
|
-
operationId,
|
|
226
|
-
routePath: endpoint.routePath,
|
|
227
|
-
method: endpoint.method,
|
|
228
|
-
operationVersion,
|
|
229
|
-
schemaHash,
|
|
230
|
-
version: operationVersion
|
|
231
|
-
})};`);
|
|
232
|
-
callerDeclarations.push(`const ${callVar} = (request = {}) => ${senderVar}(__prepareEffectRequest(${JSON.stringify(endpoint.method)}, ${JSON.stringify(endpoint.routePath)}, ${operationVar}, request));`);
|
|
233
|
-
groupedCallers[endpoint.groupName] ??= [];
|
|
234
|
-
groupedCallers[endpoint.groupName].push({
|
|
235
|
-
endpointName: endpoint.endpointName,
|
|
236
|
-
callVar
|
|
237
|
-
});
|
|
238
|
-
groupedOperations[endpoint.groupName] ??= [];
|
|
239
|
-
groupedOperations[endpoint.groupName].push({
|
|
240
|
-
endpointName: endpoint.endpointName,
|
|
241
|
-
operationVar
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
const groupObjectEntries = Object.entries(groupedCallers).map(([groupName, groupCallers])=>{
|
|
245
|
-
const endpointEntries = groupCallers.map((caller)=>`${JSON.stringify(caller.endpointName)}: ${caller.callVar}`).join(', ');
|
|
246
|
-
return `${JSON.stringify(groupName)}: { ${endpointEntries} }`;
|
|
247
|
-
});
|
|
248
|
-
const clientObject = groupObjectEntries.length ? `{
|
|
249
|
-
${groupObjectEntries.join(',\n ')}
|
|
250
|
-
}` : '{}';
|
|
251
|
-
const operationManifestEntries = Object.entries(groupedOperations).map(([groupName, groupOperations])=>{
|
|
252
|
-
const endpointEntries = groupOperations.map((operation)=>`${JSON.stringify(operation.endpointName)}: ${operation.operationVar}`).join(', ');
|
|
253
|
-
return `${JSON.stringify(groupName)}: { ${endpointEntries} }`;
|
|
254
|
-
});
|
|
255
|
-
const operationManifestObject = operationManifestEntries.length ? `{
|
|
256
|
-
${operationManifestEntries.join(',\n ')}
|
|
257
|
-
}` : '{}';
|
|
131
|
+
const manifest = {
|
|
132
|
+
endpoints: endpoints.map((endpoint)=>({
|
|
133
|
+
apiId: endpoint.apiId,
|
|
134
|
+
group: endpoint.groupName,
|
|
135
|
+
endpoint: endpoint.endpointName,
|
|
136
|
+
method: endpoint.method,
|
|
137
|
+
routePath: endpoint.routePath,
|
|
138
|
+
schemaHash: (0, endpoint_contracts_js_namespaceObject.createEffectEndpointContractHash)(endpoint, normalizedRequestId),
|
|
139
|
+
operationVersion
|
|
140
|
+
}))
|
|
141
|
+
};
|
|
142
|
+
const config = {
|
|
143
|
+
appNamespace: dataPlatformAppNamespace,
|
|
144
|
+
...requestId ? {
|
|
145
|
+
requestId
|
|
146
|
+
} : {},
|
|
147
|
+
port: options.port,
|
|
148
|
+
useEnvPort: 'server' === options.target,
|
|
149
|
+
defaultOrigin: `http://localhost:${String(options.port)}`,
|
|
150
|
+
httpMethodDecider,
|
|
151
|
+
batch: {
|
|
152
|
+
enabled: batchConfig?.enabled ?? true,
|
|
153
|
+
endpoint: batchEndpoint,
|
|
154
|
+
flushIntervalMs: batchConfig?.flushIntervalMs ?? 8,
|
|
155
|
+
maxBatchSize: batchConfig?.maxBatchSize ?? 16,
|
|
156
|
+
maxBatchBytes: batchConfig?.maxBatchBytes ?? 65536,
|
|
157
|
+
requestTimeoutMs: batchConfig?.requestTimeoutMs ?? 10000,
|
|
158
|
+
allowedMethods: batchConfig?.allowedMethods && batchConfig.allowedMethods.length > 0 ? batchConfig.allowedMethods : [
|
|
159
|
+
'GET'
|
|
160
|
+
]
|
|
161
|
+
}
|
|
162
|
+
};
|
|
258
163
|
return `import * as __requestRuntime from ${JSON.stringify(requestCreator)};
|
|
259
|
-
import {
|
|
260
|
-
createDataBatchTransport,
|
|
261
|
-
DEFAULT_DATA_BATCH_HEADER,
|
|
262
|
-
DEFAULT_DATA_ENVELOPE_HEADER,
|
|
263
|
-
createRequestEnvelope,
|
|
264
|
-
encodeRequestEnvelopeHeader,
|
|
265
|
-
} from ${JSON.stringify(dataPlatformImport)};
|
|
266
|
-
|
|
267
|
-
const createRequest = __requestRuntime.createRequest;
|
|
268
|
-
const __configureRequest =
|
|
269
|
-
typeof __requestRuntime.configure === 'function'
|
|
270
|
-
? __requestRuntime.configure
|
|
271
|
-
: undefined;
|
|
272
|
-
const __createRequestContextHeaders =
|
|
273
|
-
typeof __requestRuntime.createRequestContextHeaders === 'function'
|
|
274
|
-
? __requestRuntime.createRequestContextHeaders
|
|
275
|
-
: undefined;
|
|
276
|
-
|
|
277
|
-
const __METHODS_WITHOUT_BODY = new Set(['GET', 'DELETE', 'HEAD', 'OPTIONS']);
|
|
278
|
-
const __DATA_REQUEST_MODES = new Set(['cache-first', 'stale-while-revalidate', 'network-only']);
|
|
279
|
-
const __DATA_MUTATION_MODES = new Set(['optimistic', 'pessimistic', 'fire-and-forget']);
|
|
280
|
-
const __DEFAULT_APP_NAMESPACE = ${JSON.stringify(dataPlatformAppNamespace)};
|
|
281
|
-
const __DEFAULT_ORIGIN = 'http://localhost:${String(options.port)}';
|
|
282
|
-
const __DEFAULT_BATCH_CONFIG = ${batchConfigCode};
|
|
283
|
-
const __REQUEST_ID = ${requestId ? JSON.stringify(requestId) : 'undefined'};
|
|
284
|
-
const __RUNTIME_FETCH =
|
|
285
|
-
typeof fetch === 'function' ? fetch.bind(globalThis) : undefined;
|
|
286
|
-
|
|
287
|
-
if (__REQUEST_ID && __configureRequest) {
|
|
288
|
-
const __configurePayload = {
|
|
289
|
-
requestId: __REQUEST_ID,
|
|
290
|
-
requireEnvelope: true,
|
|
291
|
-
identityBinding: {
|
|
292
|
-
enabled: true,
|
|
293
|
-
strict: true,
|
|
294
|
-
},
|
|
295
|
-
operationContract: {
|
|
296
|
-
enabled: true,
|
|
297
|
-
strict: true,
|
|
298
|
-
requireSchemaHash: true,
|
|
299
|
-
requireOperationVersion: true,
|
|
300
|
-
},
|
|
301
|
-
setDomain: () => {
|
|
302
|
-
if (
|
|
303
|
-
typeof window !== 'undefined' &&
|
|
304
|
-
window.location &&
|
|
305
|
-
typeof window.location.origin === 'string' &&
|
|
306
|
-
window.location.origin
|
|
307
|
-
) {
|
|
308
|
-
return window.location.origin;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
if (
|
|
312
|
-
typeof globalThis !== 'undefined' &&
|
|
313
|
-
globalThis.location &&
|
|
314
|
-
typeof globalThis.location.origin === 'string' &&
|
|
315
|
-
globalThis.location.origin
|
|
316
|
-
) {
|
|
317
|
-
return globalThis.location.origin;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
return __DEFAULT_ORIGIN;
|
|
321
|
-
},
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
if (__DEFAULT_BATCH_CONFIG.enabled !== false && __RUNTIME_FETCH) {
|
|
325
|
-
__configurePayload.request = createDataBatchTransport({
|
|
326
|
-
fetch: __RUNTIME_FETCH,
|
|
327
|
-
endpoint: __DEFAULT_BATCH_CONFIG.endpoint,
|
|
328
|
-
flushIntervalMs: __DEFAULT_BATCH_CONFIG.flushIntervalMs,
|
|
329
|
-
maxBatchSize: __DEFAULT_BATCH_CONFIG.maxBatchSize,
|
|
330
|
-
maxBatchBytes: __DEFAULT_BATCH_CONFIG.maxBatchBytes,
|
|
331
|
-
requestTimeoutMs: __DEFAULT_BATCH_CONFIG.requestTimeoutMs,
|
|
332
|
-
allowedMethods: __DEFAULT_BATCH_CONFIG.allowedMethods,
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
__configureRequest(__configurePayload);
|
|
337
|
-
}
|
|
164
|
+
import { createGeneratedEffectClient } from ${JSON.stringify(EFFECT_CLIENT_RUNTIME_IMPORT)};
|
|
338
165
|
|
|
339
|
-
const
|
|
340
|
-
const __stringOrUndefined = value =>
|
|
341
|
-
typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
342
|
-
const __isDataRequestMode = value =>
|
|
343
|
-
typeof value === 'string' && __DATA_REQUEST_MODES.has(value);
|
|
344
|
-
const __isDataMutationMode = value =>
|
|
345
|
-
typeof value === 'string' && __DATA_MUTATION_MODES.has(value);
|
|
346
|
-
const __normalizeOrigin = value => {
|
|
347
|
-
if (typeof value !== 'string' || value.length === 0) {
|
|
348
|
-
return undefined;
|
|
349
|
-
}
|
|
350
|
-
try {
|
|
351
|
-
return new URL(value).origin;
|
|
352
|
-
} catch {
|
|
353
|
-
return undefined;
|
|
354
|
-
}
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
const __normalizeRequest = (method, request = {}) => {
|
|
358
|
-
if (!__isRecord(request)) {
|
|
359
|
-
return {};
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
const payload = { ...request };
|
|
363
|
-
|
|
364
|
-
if (__isRecord(request.path) && !__isRecord(payload.params)) {
|
|
365
|
-
payload.params = request.path;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
if (__isRecord(request.urlParams) && !__isRecord(payload.query)) {
|
|
369
|
-
payload.query = request.urlParams;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
if (__isRecord(request.headers) && !__isRecord(payload.headers)) {
|
|
373
|
-
payload.headers = request.headers;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if ('payload' in request && request.payload !== undefined) {
|
|
377
|
-
if (request.payload instanceof FormData && !('formData' in payload)) {
|
|
378
|
-
payload.formData = request.payload;
|
|
379
|
-
} else if (__METHODS_WITHOUT_BODY.has(method)) {
|
|
380
|
-
if (__isRecord(request.payload)) {
|
|
381
|
-
payload.query = __isRecord(payload.query)
|
|
382
|
-
? { ...payload.query, ...request.payload }
|
|
383
|
-
: request.payload;
|
|
384
|
-
} else if (!('body' in payload)) {
|
|
385
|
-
payload.body = request.payload;
|
|
386
|
-
}
|
|
387
|
-
} else if (__isRecord(request.payload) && !('data' in payload)) {
|
|
388
|
-
payload.data = request.payload;
|
|
389
|
-
} else if (!('body' in payload)) {
|
|
390
|
-
payload.body = request.payload;
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
return payload;
|
|
395
|
-
};
|
|
166
|
+
const __manifest = ${JSON.stringify(manifest, null, 2)};
|
|
396
167
|
|
|
397
|
-
const
|
|
398
|
-
if (
|
|
399
|
-
typeof window !== 'undefined' &&
|
|
400
|
-
window.location &&
|
|
401
|
-
typeof window.location.origin === 'string' &&
|
|
402
|
-
window.location.origin
|
|
403
|
-
) {
|
|
404
|
-
return window.location.origin;
|
|
405
|
-
}
|
|
168
|
+
const __config = ${JSON.stringify(config, null, 2)};
|
|
406
169
|
|
|
407
|
-
|
|
408
|
-
typeof globalThis !== 'undefined' &&
|
|
409
|
-
globalThis.location &&
|
|
410
|
-
typeof globalThis.location.origin === 'string' &&
|
|
411
|
-
globalThis.location.origin
|
|
412
|
-
) {
|
|
413
|
-
return globalThis.location.origin;
|
|
414
|
-
}
|
|
170
|
+
const __generated = createGeneratedEffectClient(__manifest, __config, __requestRuntime);
|
|
415
171
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
const __resolveTargetOrigin = dataPlatform => {
|
|
420
|
-
const explicitTargetOrigin =
|
|
421
|
-
__stringOrUndefined(dataPlatform.targetOrigin) ||
|
|
422
|
-
__stringOrUndefined(dataPlatform.endpointOrigin);
|
|
423
|
-
if (explicitTargetOrigin) {
|
|
424
|
-
return explicitTargetOrigin;
|
|
425
|
-
}
|
|
426
|
-
return __DEFAULT_ORIGIN;
|
|
427
|
-
};
|
|
428
|
-
|
|
429
|
-
const __shouldAttachEnvelopeHeader = dataPlatform => {
|
|
430
|
-
if (dataPlatform.allowCrossOriginEnvelope === true) {
|
|
431
|
-
return true;
|
|
432
|
-
}
|
|
433
|
-
const currentOrigin = __normalizeOrigin(__resolveOrigin());
|
|
434
|
-
const targetOrigin = __normalizeOrigin(__resolveTargetOrigin(dataPlatform));
|
|
435
|
-
if (!currentOrigin || !targetOrigin) {
|
|
436
|
-
return true;
|
|
437
|
-
}
|
|
438
|
-
return currentOrigin === targetOrigin;
|
|
439
|
-
};
|
|
440
|
-
|
|
441
|
-
const __toEnvelopeInput = normalizedRequest => {
|
|
442
|
-
if (!__isRecord(normalizedRequest)) {
|
|
443
|
-
return {};
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
const payload = {};
|
|
447
|
-
if (__isRecord(normalizedRequest.params)) {
|
|
448
|
-
payload.path = normalizedRequest.params;
|
|
449
|
-
}
|
|
450
|
-
if (__isRecord(normalizedRequest.query)) {
|
|
451
|
-
payload.query = normalizedRequest.query;
|
|
452
|
-
}
|
|
453
|
-
if ('data' in normalizedRequest && normalizedRequest.data !== undefined) {
|
|
454
|
-
payload.data = normalizedRequest.data;
|
|
455
|
-
}
|
|
456
|
-
if ('body' in normalizedRequest && normalizedRequest.body !== undefined) {
|
|
457
|
-
payload.body = normalizedRequest.body;
|
|
458
|
-
}
|
|
459
|
-
if (
|
|
460
|
-
typeof FormData !== 'undefined' &&
|
|
461
|
-
normalizedRequest.formData instanceof FormData
|
|
462
|
-
) {
|
|
463
|
-
payload.formData = Array.from(normalizedRequest.formData.entries()).map(
|
|
464
|
-
([key, value]) => [key, String(value)],
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
if (
|
|
468
|
-
typeof URLSearchParams !== 'undefined' &&
|
|
469
|
-
normalizedRequest.formUrlencoded instanceof URLSearchParams
|
|
470
|
-
) {
|
|
471
|
-
payload.formUrlencoded = normalizedRequest.formUrlencoded.toString();
|
|
472
|
-
}
|
|
473
|
-
return payload;
|
|
474
|
-
};
|
|
475
|
-
|
|
476
|
-
const createEffectRequestContext = requestContext => {
|
|
477
|
-
if (!__isRecord(requestContext)) {
|
|
478
|
-
return {};
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
const headers = __createRequestContextHeaders
|
|
482
|
-
? __createRequestContextHeaders(requestContext)
|
|
483
|
-
: {};
|
|
484
|
-
|
|
485
|
-
return {
|
|
486
|
-
...requestContext,
|
|
487
|
-
headers,
|
|
488
|
-
};
|
|
489
|
-
};
|
|
490
|
-
|
|
491
|
-
const __applyRequestContext = (normalizedRequest, request = {}) => {
|
|
492
|
-
if (!__isRecord(request) || !__isRecord(request.requestContext)) {
|
|
493
|
-
return normalizedRequest;
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
const requestContext = createEffectRequestContext(request.requestContext);
|
|
497
|
-
const requestHeaders = __isRecord(requestContext.headers)
|
|
498
|
-
? requestContext.headers
|
|
499
|
-
: {};
|
|
500
|
-
|
|
501
|
-
if (Object.keys(requestHeaders).length === 0) {
|
|
502
|
-
return normalizedRequest;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
return {
|
|
506
|
-
...normalizedRequest,
|
|
507
|
-
headers: {
|
|
508
|
-
...requestHeaders,
|
|
509
|
-
...(__isRecord(normalizedRequest.headers) ? normalizedRequest.headers : {}),
|
|
510
|
-
},
|
|
511
|
-
};
|
|
512
|
-
};
|
|
513
|
-
|
|
514
|
-
const __prepareEffectRequest = (method, routePath, operation, request = {}) => {
|
|
515
|
-
const normalizedRequest = __applyRequestContext(
|
|
516
|
-
__normalizeRequest(method, request),
|
|
517
|
-
request,
|
|
518
|
-
);
|
|
519
|
-
const dataPlatform = __isRecord(request) && __isRecord(request.dataPlatform)
|
|
520
|
-
? request.dataPlatform
|
|
521
|
-
: {};
|
|
522
|
-
const strictEnvelope =
|
|
523
|
-
dataPlatform.requireEnvelope === true || dataPlatform.strict === true;
|
|
524
|
-
|
|
525
|
-
if (!strictEnvelope && !__shouldAttachEnvelopeHeader(dataPlatform)) {
|
|
526
|
-
return normalizedRequest;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
try {
|
|
530
|
-
const namespace =
|
|
531
|
-
__stringOrUndefined(dataPlatform.appNamespace) || __DEFAULT_APP_NAMESPACE;
|
|
532
|
-
const origin = __stringOrUndefined(dataPlatform.origin) || __resolveOrigin();
|
|
533
|
-
const envelope = createRequestEnvelope({
|
|
534
|
-
operation: {
|
|
535
|
-
...operation,
|
|
536
|
-
appNamespace: namespace,
|
|
537
|
-
},
|
|
538
|
-
scope: {
|
|
539
|
-
appNamespace: namespace,
|
|
540
|
-
origin,
|
|
541
|
-
tenantId: __stringOrUndefined(dataPlatform.tenantId),
|
|
542
|
-
userId: __stringOrUndefined(dataPlatform.userId),
|
|
543
|
-
sessionId: __stringOrUndefined(dataPlatform.sessionId),
|
|
544
|
-
},
|
|
545
|
-
requestInput: {
|
|
546
|
-
method,
|
|
547
|
-
routePath,
|
|
548
|
-
payload: __toEnvelopeInput(normalizedRequest),
|
|
549
|
-
},
|
|
550
|
-
requestMode: __isDataRequestMode(dataPlatform.requestMode)
|
|
551
|
-
? dataPlatform.requestMode
|
|
552
|
-
: undefined,
|
|
553
|
-
mutationMode: __isDataMutationMode(dataPlatform.mutationMode)
|
|
554
|
-
? dataPlatform.mutationMode
|
|
555
|
-
: undefined,
|
|
556
|
-
selectionPlan: __isRecord(dataPlatform.selectionPlan)
|
|
557
|
-
? dataPlatform.selectionPlan
|
|
558
|
-
: undefined,
|
|
559
|
-
traceContext: __isRecord(dataPlatform.traceContext)
|
|
560
|
-
? dataPlatform.traceContext
|
|
561
|
-
: undefined,
|
|
562
|
-
requireTraceContext: dataPlatform.requireTraceContext === true,
|
|
563
|
-
});
|
|
564
|
-
|
|
565
|
-
const headerName =
|
|
566
|
-
__stringOrUndefined(dataPlatform.envelopeHeader) ||
|
|
567
|
-
DEFAULT_DATA_ENVELOPE_HEADER;
|
|
568
|
-
const headers = __isRecord(normalizedRequest.headers)
|
|
569
|
-
? { ...normalizedRequest.headers }
|
|
570
|
-
: {};
|
|
571
|
-
|
|
572
|
-
if (dataPlatform.batch === false) {
|
|
573
|
-
headers[DEFAULT_DATA_BATCH_HEADER] = 'off';
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
headers[headerName] = encodeRequestEnvelopeHeader(envelope);
|
|
577
|
-
|
|
578
|
-
return {
|
|
579
|
-
...normalizedRequest,
|
|
580
|
-
headers,
|
|
581
|
-
};
|
|
582
|
-
} catch (error) {
|
|
583
|
-
if (strictEnvelope) {
|
|
584
|
-
throw error;
|
|
585
|
-
}
|
|
586
|
-
return normalizedRequest;
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
|
|
590
|
-
${senderDeclarations.join('\n')}
|
|
591
|
-
${operationDeclarations.join('\n')}
|
|
592
|
-
${callerDeclarations.join('\n')}
|
|
593
|
-
|
|
594
|
-
const client = ${clientObject};
|
|
595
|
-
const operationManifest = ${operationManifestObject};
|
|
172
|
+
const client = __generated.client;
|
|
173
|
+
const operationManifest = __generated.operationManifest;
|
|
174
|
+
const createEffectRequestContext = __generated.createEffectRequestContext;
|
|
596
175
|
const effectBffModule = {
|
|
597
176
|
client,
|
|
598
177
|
operationManifest,
|
|
@@ -603,7 +182,23 @@ export { client, createEffectRequestContext, operationManifest };
|
|
|
603
182
|
export default effectBffModule;
|
|
604
183
|
`;
|
|
605
184
|
}
|
|
606
|
-
function
|
|
185
|
+
function renderClientShape(endpoints, valueType) {
|
|
186
|
+
if (0 === endpoints.length) return `Record<string, Record<string, ${valueType}>>`;
|
|
187
|
+
const groups = new Map();
|
|
188
|
+
for (const endpoint of endpoints){
|
|
189
|
+
const group = groups.get(endpoint.groupName) || [];
|
|
190
|
+
group.push(endpoint.endpointName);
|
|
191
|
+
groups.set(endpoint.groupName, group);
|
|
192
|
+
}
|
|
193
|
+
const groupEntries = [
|
|
194
|
+
...groups.entries()
|
|
195
|
+
].map(([groupName, names])=>{
|
|
196
|
+
const endpointEntries = names.map((name)=>` ${JSON.stringify(name)}: ${valueType};`).join('\n');
|
|
197
|
+
return ` ${JSON.stringify(groupName)}: {\n${endpointEntries}\n };`;
|
|
198
|
+
});
|
|
199
|
+
return `{\n${groupEntries.join('\n')}\n}`;
|
|
200
|
+
}
|
|
201
|
+
function renderEffectClientDeclaration(endpoints = []) {
|
|
607
202
|
return `export type EffectClientOperation = (
|
|
608
203
|
request?: unknown,
|
|
609
204
|
) => Promise<unknown>;
|
|
@@ -649,30 +244,40 @@ export type EffectRequestContext = {
|
|
|
649
244
|
traceId?: string;
|
|
650
245
|
spanId?: string;
|
|
651
246
|
};
|
|
247
|
+
export type GeneratedEffectClient = ${renderClientShape(endpoints, 'EffectClientOperation')};
|
|
248
|
+
export type GeneratedEffectOperationManifest = ${renderClientShape(endpoints, "EffectOperationDescriptor")};
|
|
652
249
|
|
|
653
|
-
export declare const client:
|
|
250
|
+
export declare const client: GeneratedEffectClient;
|
|
654
251
|
export declare const createEffectRequestContext: (
|
|
655
252
|
requestContext: Record<string, unknown>,
|
|
656
253
|
) => EffectRequestContext;
|
|
657
|
-
export declare const operationManifest:
|
|
254
|
+
export declare const operationManifest: GeneratedEffectOperationManifest;
|
|
658
255
|
declare const effectBffModule: {
|
|
659
|
-
client:
|
|
256
|
+
client: GeneratedEffectClient;
|
|
660
257
|
createEffectRequestContext: typeof createEffectRequestContext;
|
|
661
|
-
operationManifest:
|
|
258
|
+
operationManifest: GeneratedEffectOperationManifest;
|
|
662
259
|
};
|
|
663
260
|
|
|
664
261
|
export default effectBffModule;
|
|
665
262
|
`;
|
|
666
263
|
}
|
|
667
|
-
async function
|
|
264
|
+
async function generateEffectClient(options) {
|
|
668
265
|
const api = await loadEffectApi(options.resourcePath);
|
|
669
266
|
if (!api) {
|
|
670
267
|
utils_namespaceObject.logger.warn(`[BFF][Effect] Failed to generate client for ${options.resourcePath}: unable to resolve exported HttpApi.`);
|
|
671
268
|
return null;
|
|
672
269
|
}
|
|
673
270
|
const httpApiRuntime = await getHttpApiRuntime();
|
|
674
|
-
const endpoints = collectEffectEndpoints(httpApiRuntime, api, options.prefix);
|
|
675
|
-
return
|
|
271
|
+
const endpoints = (0, endpoint_contracts_js_namespaceObject.collectEffectEndpoints)(httpApiRuntime.reflect, api, options.prefix);
|
|
272
|
+
return {
|
|
273
|
+
code: renderEffectClientCode(endpoints, options),
|
|
274
|
+
declaration: renderEffectClientDeclaration(endpoints),
|
|
275
|
+
endpoints
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
async function generateEffectClientCode(options) {
|
|
279
|
+
const artifacts = await generateEffectClient(options);
|
|
280
|
+
return artifacts ? artifacts.code : null;
|
|
676
281
|
}
|
|
677
282
|
function resolveEffectEntryFile(options) {
|
|
678
283
|
const { appDir, apiDir, effectEntry } = options;
|
|
@@ -681,10 +286,12 @@ function resolveEffectEntryFile(options) {
|
|
|
681
286
|
if (external_path_default().extname(entryWithoutExt)) return utils_namespaceObject.fs.existsSync(entryWithoutExt) ? entryWithoutExt : void 0;
|
|
682
287
|
return (0, utils_namespaceObject.findExists)(JS_OR_TS_EXTS.map((ext)=>`${entryWithoutExt}${ext}`));
|
|
683
288
|
}
|
|
289
|
+
exports.generateEffectClient = __webpack_exports__.generateEffectClient;
|
|
684
290
|
exports.generateEffectClientCode = __webpack_exports__.generateEffectClientCode;
|
|
685
291
|
exports.renderEffectClientDeclaration = __webpack_exports__.renderEffectClientDeclaration;
|
|
686
292
|
exports.resolveEffectEntryFile = __webpack_exports__.resolveEffectEntryFile;
|
|
687
293
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
294
|
+
"generateEffectClient",
|
|
688
295
|
"generateEffectClientCode",
|
|
689
296
|
"renderEffectClientDeclaration",
|
|
690
297
|
"resolveEffectEntryFile"
|