@bleedingdev/modern-js-plugin-bff-extensions 3.9.0-ultramodern.5 → 3.9.0-ultramodern.7
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/README.md +6 -3
- package/dist/cjs/backend-federation/edge-runtime.js +3 -15
- package/dist/cjs/backend-federation/edge.js +2 -3
- package/dist/cjs/backend-federation/identity.js +17 -23
- package/dist/cjs/backend-federation/load.js +1 -11
- package/dist/cjs/backend-federation/validate-loaded.js +2 -4
- package/dist/cjs/backend-federation-manifest/load.js +9 -7
- package/dist/cjs/backend-federation-manifest/validation.js +4 -4
- package/dist/cjs/{client-generator → effect-source-loader}/built-entry.js +1 -1
- package/dist/cjs/{client-generator/runtime.js → effect-source-loader/http-api-runtime.js} +12 -20
- package/dist/cjs/effect-source-loader/index.js +14 -6
- package/dist/cjs/effect-source-loader/operation-contracts.js +83 -0
- package/dist/cjs/effect-source-loader/rspack-loader.js +1 -17
- package/dist/cjs/effect-source-loader/worker-runtime-wrapper.js +6 -21
- package/dist/esm/backend-federation/edge-runtime.mjs +2 -11
- package/dist/esm/backend-federation/edge.mjs +3 -4
- package/dist/esm/backend-federation/identity.mjs +17 -23
- package/dist/esm/backend-federation/load.mjs +1 -11
- package/dist/esm/backend-federation/validate-loaded.mjs +2 -4
- package/dist/esm/backend-federation-manifest/load.mjs +9 -7
- package/dist/esm/backend-federation-manifest/validation.mjs +4 -4
- package/dist/esm/{client-generator → effect-source-loader}/built-entry.mjs +1 -1
- package/dist/esm/effect-source-loader/http-api-runtime.mjs +19 -0
- package/dist/esm/effect-source-loader/index.mjs +4 -2
- package/dist/esm/effect-source-loader/operation-contracts.mjs +45 -0
- package/dist/esm/effect-source-loader/rspack-loader.mjs +2 -18
- package/dist/esm/effect-source-loader/worker-runtime-wrapper.mjs +4 -16
- package/dist/esm-node/backend-federation/edge-runtime.mjs +2 -11
- package/dist/esm-node/backend-federation/edge.mjs +3 -4
- package/dist/esm-node/backend-federation/identity.mjs +17 -23
- package/dist/esm-node/backend-federation/load.mjs +1 -11
- package/dist/esm-node/backend-federation/validate-loaded.mjs +2 -4
- package/dist/esm-node/backend-federation-manifest/load.mjs +9 -7
- package/dist/esm-node/backend-federation-manifest/validation.mjs +4 -4
- package/dist/esm-node/{client-generator → effect-source-loader}/built-entry.mjs +1 -1
- package/dist/esm-node/effect-source-loader/http-api-runtime.mjs +20 -0
- package/dist/esm-node/effect-source-loader/index.mjs +4 -2
- package/dist/esm-node/effect-source-loader/operation-contracts.mjs +46 -0
- package/dist/esm-node/effect-source-loader/rspack-loader.mjs +2 -18
- package/dist/esm-node/effect-source-loader/worker-runtime-wrapper.mjs +4 -16
- package/dist/types/backend-federation/edge-runtime.d.ts +0 -5
- package/dist/types/backend-federation/edge.d.ts +0 -2
- package/dist/types/backend-federation/identity.d.ts +2 -11
- package/dist/types/backend-federation/load.d.ts +1 -7
- package/dist/types/backend-federation/node.d.ts +1 -3
- package/dist/types/backend-federation/types.d.ts +0 -6
- package/dist/types/backend-federation/validate-loaded.d.ts +1 -2
- package/dist/types/backend-federation-manifest/metadata.d.ts +0 -6
- package/dist/types/effect-source-loader/index.d.ts +4 -2
- package/dist/types/effect-source-loader/operation-contracts.d.ts +8 -0
- package/dist/types/effect-source-loader/rspack-loader.d.ts +0 -14
- package/dist/types/effect-source-loader/worker-runtime-wrapper.d.ts +4 -22
- package/package.json +10 -22
- package/src/backend-federation/edge-runtime.ts +2 -13
- package/src/backend-federation/edge.ts +8 -16
- package/src/backend-federation/identity.ts +9 -27
- package/src/backend-federation/load.ts +6 -33
- package/src/backend-federation/node.ts +0 -11
- package/src/backend-federation/types.ts +0 -6
- package/src/backend-federation/validate-loaded.ts +2 -7
- package/src/backend-federation-manifest/load.ts +20 -14
- package/src/backend-federation-manifest/metadata.ts +0 -6
- package/src/backend-federation-manifest/validation.ts +9 -12
- package/src/{client-generator → effect-source-loader}/built-entry.ts +1 -3
- package/src/effect-source-loader/http-api-runtime.ts +39 -0
- package/src/effect-source-loader/index.ts +7 -5
- package/src/effect-source-loader/operation-contracts.ts +72 -0
- package/src/effect-source-loader/rspack-loader.ts +3 -33
- package/src/effect-source-loader/worker-runtime-wrapper.ts +4 -46
- package/dist/cjs/backend-federation/legacy-warning.js +0 -40
- package/dist/cjs/client-generator/generator.js +0 -73
- package/dist/cjs/client-generator/index.js +0 -54
- package/dist/cjs/client-generator/rendering.js +0 -253
- package/dist/cjs/client-generator/types.js +0 -18
- package/dist/esm/backend-federation/legacy-warning.mjs +0 -2
- package/dist/esm/client-generator/generator.mjs +0 -32
- package/dist/esm/client-generator/index.mjs +0 -3
- package/dist/esm/client-generator/rendering.mjs +0 -209
- package/dist/esm/client-generator/runtime.mjs +0 -27
- package/dist/esm/client-generator/types.mjs +0 -0
- package/dist/esm-node/backend-federation/legacy-warning.mjs +0 -3
- package/dist/esm-node/client-generator/generator.mjs +0 -33
- package/dist/esm-node/client-generator/index.mjs +0 -4
- package/dist/esm-node/client-generator/rendering.mjs +0 -210
- package/dist/esm-node/client-generator/runtime.mjs +0 -29
- package/dist/esm-node/client-generator/types.mjs +0 -1
- package/dist/types/backend-federation/legacy-warning.d.ts +0 -1
- package/dist/types/client-generator/generator.d.ts +0 -3
- package/dist/types/client-generator/index.d.ts +0 -4
- package/dist/types/client-generator/rendering.d.ts +0 -24
- package/dist/types/client-generator/types.d.ts +0 -33
- package/src/backend-federation/legacy-warning.ts +0 -2
- package/src/client-generator/generator.ts +0 -52
- package/src/client-generator/index.ts +0 -10
- package/src/client-generator/rendering.ts +0 -307
- package/src/client-generator/runtime.ts +0 -69
- package/src/client-generator/types.ts +0 -35
- /package/dist/cjs/{client-generator → effect-source-loader}/paths.js +0 -0
- /package/dist/esm/{client-generator → effect-source-loader}/paths.mjs +0 -0
- /package/dist/esm-node/{client-generator → effect-source-loader}/paths.mjs +0 -0
- /package/dist/types/{client-generator → effect-source-loader}/built-entry.d.ts +0 -0
- /package/dist/types/{client-generator/runtime.d.ts → effect-source-loader/http-api-runtime.d.ts} +0 -0
- /package/dist/types/{client-generator → effect-source-loader}/paths.d.ts +0 -0
- /package/src/{client-generator → effect-source-loader}/paths.ts +0 -0
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
2
|
-
import { createEffectEndpointContractHash, ensureLeadingSlash, normalizeEffectPrefix, toOperationContractSources } from "@modern-js/bff-effect/effect";
|
|
3
|
-
import { buildOperationContractMap, deriveOperationVersion } from "@modern-js/server-runtime-extensions/bff-policy/node";
|
|
4
|
-
import { fs, upath } from "@modern-js/utils";
|
|
5
|
-
const DEFAULT_REQUEST_CREATOR = '@modern-js/plugin-bff/client';
|
|
6
|
-
const EFFECT_CLIENT_RUNTIME_IMPORT = '@modern-js/bff-effect/effect-client-runtime';
|
|
7
|
-
function isAbsoluteUrl(value) {
|
|
8
|
-
try {
|
|
9
|
-
new URL(value);
|
|
10
|
-
return true;
|
|
11
|
-
} catch {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function resolveBatchEndpoint(prefix, endpoint) {
|
|
16
|
-
const value = void 0 === endpoint || '' === endpoint ? '/_data/batch' : endpoint;
|
|
17
|
-
if (isAbsoluteUrl(value)) return value;
|
|
18
|
-
const normalizedPrefix = normalizeEffectPrefix(prefix);
|
|
19
|
-
const normalizedEndpoint = ensureLeadingSlash(value);
|
|
20
|
-
if ('' === normalizedPrefix) return normalizedEndpoint;
|
|
21
|
-
if (normalizedEndpoint === normalizedPrefix || normalizedEndpoint.startsWith(`${normalizedPrefix}/`)) return normalizedEndpoint;
|
|
22
|
-
return `${normalizedPrefix}${'/' === normalizedEndpoint ? '' : normalizedEndpoint}`;
|
|
23
|
-
}
|
|
24
|
-
function getPackageInfo(resourcePath, appDir, onDependency) {
|
|
25
|
-
for (const startDir of [
|
|
26
|
-
upath.dirname(resourcePath),
|
|
27
|
-
appDir
|
|
28
|
-
]){
|
|
29
|
-
let current = upath.resolve(startDir);
|
|
30
|
-
for(let depth = 0; depth < 32; depth += 1){
|
|
31
|
-
const packageJsonPath = upath.join(current, 'package.json');
|
|
32
|
-
if (fs.existsSync(packageJsonPath)) {
|
|
33
|
-
onDependency?.(packageJsonPath);
|
|
34
|
-
try {
|
|
35
|
-
const packageJson = fs.readJSONSync(packageJsonPath);
|
|
36
|
-
return {
|
|
37
|
-
name: packageJson.name,
|
|
38
|
-
version: packageJson.version
|
|
39
|
-
};
|
|
40
|
-
} catch {
|
|
41
|
-
return {};
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
const parent = upath.dirname(current);
|
|
45
|
-
if (parent === current) break;
|
|
46
|
-
current = parent;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return {};
|
|
50
|
-
}
|
|
51
|
-
function renderEffectClientCode(generation) {
|
|
52
|
-
const { config, manifest } = generation;
|
|
53
|
-
return `import * as __requestRuntime from ${JSON.stringify(generation.requestCreator)};
|
|
54
|
-
import { createGeneratedEffectClient } from ${JSON.stringify(EFFECT_CLIENT_RUNTIME_IMPORT)};
|
|
55
|
-
|
|
56
|
-
const __manifest = ${JSON.stringify(manifest, null, 2)};
|
|
57
|
-
|
|
58
|
-
const __config = ${JSON.stringify(config, null, 2)};
|
|
59
|
-
|
|
60
|
-
const __generated = createGeneratedEffectClient(__manifest, __config, __requestRuntime);
|
|
61
|
-
|
|
62
|
-
const client = __generated.client;
|
|
63
|
-
const operationManifest = __generated.operationManifest;
|
|
64
|
-
const createEffectRequestContext = __generated.createEffectRequestContext;
|
|
65
|
-
const apiModule = {
|
|
66
|
-
client,
|
|
67
|
-
operationManifest,
|
|
68
|
-
createEffectRequestContext,
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
export { client, createEffectRequestContext, operationManifest };
|
|
72
|
-
export default apiModule;
|
|
73
|
-
`;
|
|
74
|
-
}
|
|
75
|
-
function createEffectClientGeneration(endpoints, options) {
|
|
76
|
-
const requestCreator = options.requestCreator || DEFAULT_REQUEST_CREATOR;
|
|
77
|
-
const httpMethodDecider = options.httpMethodDecider || 'functionName';
|
|
78
|
-
const packageInfo = getPackageInfo(options.resourcePath, options.appDir, options.onDependency);
|
|
79
|
-
const packageName = packageInfo.name;
|
|
80
|
-
const dataPlatformAppNamespace = void 0 === packageName || '' === packageName ? 'unknown-app' : packageName;
|
|
81
|
-
const requestId = 'bundle' === options.target ? void 0 !== options.requestId && '' !== options.requestId.trim() ? options.requestId.trim() : void 0 !== packageName && '' !== packageName ? packageName : void 0 : void 0;
|
|
82
|
-
const normalizedRequestId = void 0 === requestId || '' === requestId ? 'default' : requestId;
|
|
83
|
-
const operationVersion = deriveOperationVersion(packageInfo.version);
|
|
84
|
-
const batchConfig = options.dataPlatformBatch;
|
|
85
|
-
const batchEndpoint = resolveBatchEndpoint(options.prefix, batchConfig?.endpoint);
|
|
86
|
-
const manifest = {
|
|
87
|
-
endpoints: endpoints.map((endpoint)=>({
|
|
88
|
-
apiId: endpoint.apiId,
|
|
89
|
-
group: endpoint.groupName,
|
|
90
|
-
endpoint: endpoint.endpointName,
|
|
91
|
-
method: endpoint.method,
|
|
92
|
-
routePath: endpoint.routePath,
|
|
93
|
-
schemaHash: createEffectEndpointContractHash(endpoint, normalizedRequestId),
|
|
94
|
-
operationVersion
|
|
95
|
-
}))
|
|
96
|
-
};
|
|
97
|
-
const config = {
|
|
98
|
-
appNamespace: dataPlatformAppNamespace,
|
|
99
|
-
...void 0 !== requestId && '' !== requestId ? {
|
|
100
|
-
requestId
|
|
101
|
-
} : {},
|
|
102
|
-
port: options.port,
|
|
103
|
-
useEnvPort: 'server' === options.target,
|
|
104
|
-
defaultOrigin: `http://localhost:${String(options.port)}`,
|
|
105
|
-
httpMethodDecider,
|
|
106
|
-
batch: {
|
|
107
|
-
enabled: batchConfig?.enabled ?? true,
|
|
108
|
-
endpoint: batchEndpoint,
|
|
109
|
-
flushIntervalMs: batchConfig?.flushIntervalMs ?? 8,
|
|
110
|
-
maxBatchSize: batchConfig?.maxBatchSize ?? 16,
|
|
111
|
-
maxBatchBytes: batchConfig?.maxBatchBytes ?? 65536,
|
|
112
|
-
requestTimeoutMs: batchConfig?.requestTimeoutMs ?? 10000,
|
|
113
|
-
allowedMethods: batchConfig?.allowedMethods !== void 0 && batchConfig.allowedMethods.length > 0 ? batchConfig.allowedMethods : [
|
|
114
|
-
'GET'
|
|
115
|
-
]
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
return {
|
|
119
|
-
config,
|
|
120
|
-
manifest,
|
|
121
|
-
operationContracts: buildOperationContractMap({
|
|
122
|
-
handlers: toOperationContractSources(endpoints),
|
|
123
|
-
requestId: normalizedRequestId,
|
|
124
|
-
operationVersion
|
|
125
|
-
}),
|
|
126
|
-
operationVersion,
|
|
127
|
-
requestCreator,
|
|
128
|
-
requestId: normalizedRequestId
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
function renderClientShape(endpoints, valueType) {
|
|
132
|
-
if (0 === endpoints.length) return `Record<string, Record<string, ${valueType}>>`;
|
|
133
|
-
const groups = new Map();
|
|
134
|
-
for (const endpoint of endpoints){
|
|
135
|
-
const group = groups.get(endpoint.groupName) || [];
|
|
136
|
-
group.push(endpoint.endpointName);
|
|
137
|
-
groups.set(endpoint.groupName, group);
|
|
138
|
-
}
|
|
139
|
-
const groupEntries = [
|
|
140
|
-
...groups.entries()
|
|
141
|
-
].map(([groupName, names])=>{
|
|
142
|
-
const endpointEntries = names.map((name)=>` ${JSON.stringify(name)}: ${valueType};`).join('\n');
|
|
143
|
-
return ` ${JSON.stringify(groupName)}: {\n${endpointEntries}\n };`;
|
|
144
|
-
});
|
|
145
|
-
return `{\n${groupEntries.join('\n')}\n}`;
|
|
146
|
-
}
|
|
147
|
-
function renderEffectClientDeclaration(endpoints = []) {
|
|
148
|
-
return `export type EffectClientOperation = (
|
|
149
|
-
request?: unknown,
|
|
150
|
-
) => Promise<unknown>;
|
|
151
|
-
export type EffectClientGroup = Record<string, EffectClientOperation>;
|
|
152
|
-
export type EffectClient = Record<string, EffectClientGroup>;
|
|
153
|
-
export type EffectOperationDescriptor = {
|
|
154
|
-
appNamespace: string;
|
|
155
|
-
apiId: string;
|
|
156
|
-
group: string;
|
|
157
|
-
endpoint: string;
|
|
158
|
-
operationId: string;
|
|
159
|
-
routePath: string;
|
|
160
|
-
method: string;
|
|
161
|
-
operationVersion: number;
|
|
162
|
-
schemaHash: string;
|
|
163
|
-
version: number;
|
|
164
|
-
};
|
|
165
|
-
export type EffectOperationManifest = Record<
|
|
166
|
-
string,
|
|
167
|
-
Record<string, EffectOperationDescriptor>
|
|
168
|
-
>;
|
|
169
|
-
export type EffectOperationContext = {
|
|
170
|
-
requestId?: string;
|
|
171
|
-
operationId?: string;
|
|
172
|
-
routePath?: string;
|
|
173
|
-
method?: string;
|
|
174
|
-
schemaHash?: string;
|
|
175
|
-
operationVersion?: number;
|
|
176
|
-
locale?: string;
|
|
177
|
-
traceparent?: string;
|
|
178
|
-
traceId?: string;
|
|
179
|
-
spanId?: string;
|
|
180
|
-
source?: string;
|
|
181
|
-
scope?: Record<string, unknown>;
|
|
182
|
-
sessionClaims?: Record<string, unknown>;
|
|
183
|
-
attributes?: Record<string, unknown>;
|
|
184
|
-
};
|
|
185
|
-
export type EffectRequestContext = {
|
|
186
|
-
headers?: Record<string, string>;
|
|
187
|
-
locale?: string;
|
|
188
|
-
operationContext?: EffectOperationContext;
|
|
189
|
-
traceparent?: string;
|
|
190
|
-
traceId?: string;
|
|
191
|
-
spanId?: string;
|
|
192
|
-
};
|
|
193
|
-
export type GeneratedEffectClient = ${renderClientShape(endpoints, 'EffectClientOperation')};
|
|
194
|
-
export type GeneratedEffectOperationManifest = ${renderClientShape(endpoints, "EffectOperationDescriptor")};
|
|
195
|
-
|
|
196
|
-
export declare const client: GeneratedEffectClient;
|
|
197
|
-
export declare const createEffectRequestContext: (
|
|
198
|
-
requestContext: Record<string, unknown>,
|
|
199
|
-
) => EffectRequestContext;
|
|
200
|
-
export declare const operationManifest: GeneratedEffectOperationManifest;
|
|
201
|
-
declare const apiModule: {
|
|
202
|
-
client: GeneratedEffectClient;
|
|
203
|
-
createEffectRequestContext: typeof createEffectRequestContext;
|
|
204
|
-
operationManifest: GeneratedEffectOperationManifest;
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
export default apiModule;
|
|
208
|
-
`;
|
|
209
|
-
}
|
|
210
|
-
export { createEffectClientGeneration, renderEffectClientCode, renderEffectClientDeclaration };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import __rslib_shim_module__ from "node:module";
|
|
2
|
-
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
-
import { extractHttpApiFromModule } from "@modern-js/bff-effect/effect";
|
|
4
|
-
import { compatibleRequire, upath } from "@modern-js/utils";
|
|
5
|
-
import { loadEffectSourceModule } from "../effect-source-loader/loader.mjs";
|
|
6
|
-
let httpApiRuntimePromise;
|
|
7
|
-
function isRecord(value) {
|
|
8
|
-
return 'object' == typeof value && null !== value;
|
|
9
|
-
}
|
|
10
|
-
function isHttpApiRuntime(value) {
|
|
11
|
-
return isRecord(value) && 'function' == typeof value.isHttpApi && 'function' == typeof value.reflect;
|
|
12
|
-
}
|
|
13
|
-
function getHttpApiRuntime() {
|
|
14
|
-
if (void 0 === httpApiRuntimePromise) httpApiRuntimePromise = compatibleRequire('effect/unstable/httpapi', false).catch((error)=>{
|
|
15
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
16
|
-
if (!message.includes("Cannot find module 'effect/unstable/httpapi'")) throw error;
|
|
17
|
-
const effectPackageJson = require.resolve('effect/package.json');
|
|
18
|
-
const effectHttpApiRuntimePath = upath.join(upath.dirname(effectPackageJson), 'dist', 'unstable', 'httpapi', 'index.js');
|
|
19
|
-
return compatibleRequire(effectHttpApiRuntimePath, false);
|
|
20
|
-
}).then((mod)=>{
|
|
21
|
-
if (isRecord(mod) && isHttpApiRuntime(mod.HttpApi)) return mod.HttpApi;
|
|
22
|
-
throw new Error('[BFF][Effect] Unable to resolve HttpApi runtime from effect/unstable/httpapi.');
|
|
23
|
-
});
|
|
24
|
-
return httpApiRuntimePromise;
|
|
25
|
-
}
|
|
26
|
-
function loadEffectApi(options) {
|
|
27
|
-
return getHttpApiRuntime().then((httpApiRuntime)=>loadEffectSourceModule(options).then((mod)=>extractHttpApiFromModule(mod, httpApiRuntime.isHttpApi)));
|
|
28
|
-
}
|
|
29
|
-
export { getHttpApiRuntime, loadEffectApi };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import "node:module";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const LEGACY_LOAD_WARNING = "[BFF][Effect] loadBackendFederatedEffectApi was called without an expected delivery-unit identity (expected.unitId + expected.buildMarker). Identity-less public backend loads are deprecated (ADR-0019/MV-G23) and will be rejected in a future major.";
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { EffectClientCodegenOptions, GeneratedEffectClientArtifacts } from './types.js';
|
|
2
|
-
export declare function generateEffectClient(options: EffectClientCodegenOptions): Promise<GeneratedEffectClientArtifacts | null>;
|
|
3
|
-
export declare function generateEffectClientCode(options: EffectClientCodegenOptions): Promise<string | null>;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { bundleBuiltEffectEntryForNode, resolveEffectEntryPaths, } from './built-entry.js';
|
|
2
|
-
export { generateEffectClient, generateEffectClientCode } from './generator.js';
|
|
3
|
-
export { resolveEffectEntryFile } from './paths.js';
|
|
4
|
-
export type { EffectClientCodegenOptions, GeneratedEffectClientArtifacts, } from './types.js';
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { type EffectEndpointMeta } from '@modern-js/bff-effect/effect';
|
|
2
|
-
import { type OperationContractMap } from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
3
|
-
import type { EffectClientCodegenOptions } from './types.js';
|
|
4
|
-
export type EffectClientGeneration = {
|
|
5
|
-
config: Record<string, unknown>;
|
|
6
|
-
manifest: {
|
|
7
|
-
endpoints: Array<{
|
|
8
|
-
apiId: string;
|
|
9
|
-
group: string;
|
|
10
|
-
endpoint: string;
|
|
11
|
-
method: string;
|
|
12
|
-
routePath: string;
|
|
13
|
-
schemaHash: string;
|
|
14
|
-
operationVersion: number;
|
|
15
|
-
}>;
|
|
16
|
-
};
|
|
17
|
-
operationContracts: OperationContractMap;
|
|
18
|
-
operationVersion: number;
|
|
19
|
-
requestCreator: string;
|
|
20
|
-
requestId: string;
|
|
21
|
-
};
|
|
22
|
-
export declare function renderEffectClientCode(generation: EffectClientGeneration): string;
|
|
23
|
-
export declare function createEffectClientGeneration(endpoints: EffectEndpointMeta[], options: EffectClientCodegenOptions): EffectClientGeneration;
|
|
24
|
-
export declare function renderEffectClientDeclaration(endpoints?: EffectEndpointMeta[]): string;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { EffectEndpointMeta } from '@modern-js/bff-effect/effect';
|
|
2
|
-
import type { OperationContractMap } from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
3
|
-
import type { HttpMethodDecider } from '@modern-js/types';
|
|
4
|
-
export type EffectClientCodegenOptions = {
|
|
5
|
-
appDir: string;
|
|
6
|
-
apiDir: string;
|
|
7
|
-
resourcePath: string;
|
|
8
|
-
prefix: string;
|
|
9
|
-
port: number;
|
|
10
|
-
target?: string;
|
|
11
|
-
/** Producer identity used by cross-project clients and server contracts. */
|
|
12
|
-
requestId?: string;
|
|
13
|
-
requestCreator?: string;
|
|
14
|
-
httpMethodDecider?: HttpMethodDecider;
|
|
15
|
-
onDependency?: (dependency: string) => void;
|
|
16
|
-
dataPlatformBatch?: {
|
|
17
|
-
enabled?: boolean;
|
|
18
|
-
endpoint?: string;
|
|
19
|
-
flushIntervalMs?: number;
|
|
20
|
-
maxBatchSize?: number;
|
|
21
|
-
maxBatchBytes?: number;
|
|
22
|
-
requestTimeoutMs?: number;
|
|
23
|
-
allowedMethods?: string[];
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
export type GeneratedEffectClientArtifacts = {
|
|
27
|
-
code: string;
|
|
28
|
-
declaration: string;
|
|
29
|
-
endpoints: EffectEndpointMeta[];
|
|
30
|
-
operationContracts: OperationContractMap;
|
|
31
|
-
operationVersion: number;
|
|
32
|
-
requestId: string;
|
|
33
|
-
};
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export const LEGACY_LOAD_WARNING =
|
|
2
|
-
'[BFF][Effect] loadBackendFederatedEffectApi was called without an expected delivery-unit identity (expected.unitId + expected.buildMarker). Identity-less public backend loads are deprecated (ADR-0019/MV-G23) and will be rejected in a future major.';
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { collectEffectEndpoints } from '@modern-js/bff-effect/effect';
|
|
2
|
-
import { logger } from '@modern-js/utils';
|
|
3
|
-
import {
|
|
4
|
-
createEffectClientGeneration,
|
|
5
|
-
renderEffectClientCode,
|
|
6
|
-
renderEffectClientDeclaration,
|
|
7
|
-
} from './rendering';
|
|
8
|
-
import { getHttpApiRuntime, loadEffectApi } from './runtime';
|
|
9
|
-
import type {
|
|
10
|
-
EffectClientCodegenOptions,
|
|
11
|
-
GeneratedEffectClientArtifacts,
|
|
12
|
-
} from './types';
|
|
13
|
-
|
|
14
|
-
export function generateEffectClient(
|
|
15
|
-
options: EffectClientCodegenOptions,
|
|
16
|
-
): Promise<GeneratedEffectClientArtifacts | null> {
|
|
17
|
-
return loadEffectApi({
|
|
18
|
-
appDir: options.appDir,
|
|
19
|
-
resourcePath: options.resourcePath,
|
|
20
|
-
onDependency: options.onDependency,
|
|
21
|
-
}).then(api => {
|
|
22
|
-
if (api === null) {
|
|
23
|
-
logger.warn(
|
|
24
|
-
`[BFF][Effect] Failed to generate client for ${options.resourcePath}: unable to resolve exported HttpApi.`,
|
|
25
|
-
);
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return getHttpApiRuntime().then(httpApiRuntime => {
|
|
30
|
-
const endpoints = collectEffectEndpoints(
|
|
31
|
-
httpApiRuntime.reflect,
|
|
32
|
-
api,
|
|
33
|
-
options.prefix,
|
|
34
|
-
);
|
|
35
|
-
const generation = createEffectClientGeneration(endpoints, options);
|
|
36
|
-
return {
|
|
37
|
-
code: renderEffectClientCode(generation),
|
|
38
|
-
declaration: renderEffectClientDeclaration(endpoints),
|
|
39
|
-
endpoints,
|
|
40
|
-
operationContracts: generation.operationContracts,
|
|
41
|
-
operationVersion: generation.operationVersion,
|
|
42
|
-
requestId: generation.requestId,
|
|
43
|
-
};
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function generateEffectClientCode(options: EffectClientCodegenOptions) {
|
|
49
|
-
return generateEffectClient(options).then(artifacts =>
|
|
50
|
-
artifacts === null ? null : artifacts.code,
|
|
51
|
-
);
|
|
52
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export {
|
|
2
|
-
bundleBuiltEffectEntryForNode,
|
|
3
|
-
resolveEffectEntryPaths,
|
|
4
|
-
} from './built-entry';
|
|
5
|
-
export { generateEffectClient, generateEffectClientCode } from './generator';
|
|
6
|
-
export { resolveEffectEntryFile } from './paths';
|
|
7
|
-
export type {
|
|
8
|
-
EffectClientCodegenOptions,
|
|
9
|
-
GeneratedEffectClientArtifacts,
|
|
10
|
-
} from './types';
|
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createEffectEndpointContractHash,
|
|
3
|
-
type EffectEndpointMeta,
|
|
4
|
-
ensureLeadingSlash,
|
|
5
|
-
normalizeEffectPrefix,
|
|
6
|
-
toOperationContractSources,
|
|
7
|
-
} from '@modern-js/bff-effect/effect';
|
|
8
|
-
import {
|
|
9
|
-
buildOperationContractMap,
|
|
10
|
-
deriveOperationVersion,
|
|
11
|
-
type OperationContractMap,
|
|
12
|
-
} from '@modern-js/server-runtime-extensions/bff-policy/node';
|
|
13
|
-
import { fs, upath as path } from '@modern-js/utils';
|
|
14
|
-
import type { EffectClientCodegenOptions } from './types';
|
|
15
|
-
|
|
16
|
-
const DEFAULT_REQUEST_CREATOR = '@modern-js/plugin-bff/client';
|
|
17
|
-
const EFFECT_CLIENT_RUNTIME_IMPORT =
|
|
18
|
-
'@modern-js/bff-effect/effect-client-runtime';
|
|
19
|
-
|
|
20
|
-
function isAbsoluteUrl(value: string) {
|
|
21
|
-
try {
|
|
22
|
-
new URL(value);
|
|
23
|
-
return true;
|
|
24
|
-
} catch {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function resolveBatchEndpoint(prefix: string, endpoint: string | undefined) {
|
|
30
|
-
const value =
|
|
31
|
-
endpoint === undefined || endpoint === '' ? '/_data/batch' : endpoint;
|
|
32
|
-
if (isAbsoluteUrl(value)) {
|
|
33
|
-
return value;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const normalizedPrefix = normalizeEffectPrefix(prefix);
|
|
37
|
-
const normalizedEndpoint = ensureLeadingSlash(value);
|
|
38
|
-
if (normalizedPrefix === '') {
|
|
39
|
-
return normalizedEndpoint;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
normalizedEndpoint === normalizedPrefix ||
|
|
44
|
-
normalizedEndpoint.startsWith(`${normalizedPrefix}/`)
|
|
45
|
-
) {
|
|
46
|
-
return normalizedEndpoint;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return `${normalizedPrefix}${normalizedEndpoint === '/' ? '' : normalizedEndpoint}`;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function getPackageInfo(
|
|
53
|
-
resourcePath: string,
|
|
54
|
-
appDir: string,
|
|
55
|
-
onDependency?: (dependency: string) => void,
|
|
56
|
-
): { name?: string; version?: string } {
|
|
57
|
-
for (const startDir of [path.dirname(resourcePath), appDir]) {
|
|
58
|
-
let current = path.resolve(startDir);
|
|
59
|
-
for (let depth = 0; depth < 32; depth += 1) {
|
|
60
|
-
const packageJsonPath = path.join(current, 'package.json');
|
|
61
|
-
if (fs.existsSync(packageJsonPath)) {
|
|
62
|
-
onDependency?.(packageJsonPath);
|
|
63
|
-
try {
|
|
64
|
-
const packageJson = fs.readJSONSync(packageJsonPath) as {
|
|
65
|
-
name?: string;
|
|
66
|
-
version?: string;
|
|
67
|
-
};
|
|
68
|
-
return { name: packageJson.name, version: packageJson.version };
|
|
69
|
-
} catch {
|
|
70
|
-
return {};
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const parent = path.dirname(current);
|
|
74
|
-
if (parent === current) {
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
current = parent;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return {};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export type EffectClientGeneration = {
|
|
84
|
-
config: Record<string, unknown>;
|
|
85
|
-
manifest: {
|
|
86
|
-
endpoints: Array<{
|
|
87
|
-
apiId: string;
|
|
88
|
-
group: string;
|
|
89
|
-
endpoint: string;
|
|
90
|
-
method: string;
|
|
91
|
-
routePath: string;
|
|
92
|
-
schemaHash: string;
|
|
93
|
-
operationVersion: number;
|
|
94
|
-
}>;
|
|
95
|
-
};
|
|
96
|
-
operationContracts: OperationContractMap;
|
|
97
|
-
operationVersion: number;
|
|
98
|
-
requestCreator: string;
|
|
99
|
-
requestId: string;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
export function renderEffectClientCode(generation: EffectClientGeneration) {
|
|
103
|
-
const { config, manifest } = generation;
|
|
104
|
-
return `import * as __requestRuntime from ${JSON.stringify(
|
|
105
|
-
generation.requestCreator,
|
|
106
|
-
)};
|
|
107
|
-
import { createGeneratedEffectClient } from ${JSON.stringify(
|
|
108
|
-
EFFECT_CLIENT_RUNTIME_IMPORT,
|
|
109
|
-
)};
|
|
110
|
-
|
|
111
|
-
const __manifest = ${JSON.stringify(manifest, null, 2)};
|
|
112
|
-
|
|
113
|
-
const __config = ${JSON.stringify(config, null, 2)};
|
|
114
|
-
|
|
115
|
-
const __generated = createGeneratedEffectClient(__manifest, __config, __requestRuntime);
|
|
116
|
-
|
|
117
|
-
const client = __generated.client;
|
|
118
|
-
const operationManifest = __generated.operationManifest;
|
|
119
|
-
const createEffectRequestContext = __generated.createEffectRequestContext;
|
|
120
|
-
const apiModule = {
|
|
121
|
-
client,
|
|
122
|
-
operationManifest,
|
|
123
|
-
createEffectRequestContext,
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
export { client, createEffectRequestContext, operationManifest };
|
|
127
|
-
export default apiModule;
|
|
128
|
-
`;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function createEffectClientGeneration(
|
|
132
|
-
endpoints: EffectEndpointMeta[],
|
|
133
|
-
options: EffectClientCodegenOptions,
|
|
134
|
-
): EffectClientGeneration {
|
|
135
|
-
const requestCreator = options.requestCreator || DEFAULT_REQUEST_CREATOR;
|
|
136
|
-
const httpMethodDecider = options.httpMethodDecider || 'functionName';
|
|
137
|
-
const packageInfo = getPackageInfo(
|
|
138
|
-
options.resourcePath,
|
|
139
|
-
options.appDir,
|
|
140
|
-
options.onDependency,
|
|
141
|
-
);
|
|
142
|
-
const packageName = packageInfo.name;
|
|
143
|
-
const dataPlatformAppNamespace =
|
|
144
|
-
packageName === undefined || packageName === ''
|
|
145
|
-
? 'unknown-app'
|
|
146
|
-
: packageName;
|
|
147
|
-
const requestId =
|
|
148
|
-
options.target === 'bundle'
|
|
149
|
-
? options.requestId !== undefined && options.requestId.trim() !== ''
|
|
150
|
-
? options.requestId.trim()
|
|
151
|
-
: packageName !== undefined && packageName !== ''
|
|
152
|
-
? packageName
|
|
153
|
-
: undefined
|
|
154
|
-
: undefined;
|
|
155
|
-
const normalizedRequestId =
|
|
156
|
-
requestId === undefined || requestId === '' ? 'default' : requestId;
|
|
157
|
-
const operationVersion = deriveOperationVersion(packageInfo.version);
|
|
158
|
-
const batchConfig = options.dataPlatformBatch;
|
|
159
|
-
const batchEndpoint = resolveBatchEndpoint(
|
|
160
|
-
options.prefix,
|
|
161
|
-
batchConfig?.endpoint,
|
|
162
|
-
);
|
|
163
|
-
|
|
164
|
-
const manifest = {
|
|
165
|
-
endpoints: endpoints.map(endpoint => ({
|
|
166
|
-
apiId: endpoint.apiId,
|
|
167
|
-
group: endpoint.groupName,
|
|
168
|
-
endpoint: endpoint.endpointName,
|
|
169
|
-
method: endpoint.method,
|
|
170
|
-
routePath: endpoint.routePath,
|
|
171
|
-
schemaHash: createEffectEndpointContractHash(
|
|
172
|
-
endpoint,
|
|
173
|
-
normalizedRequestId,
|
|
174
|
-
),
|
|
175
|
-
operationVersion,
|
|
176
|
-
})),
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
const config = {
|
|
180
|
-
appNamespace: dataPlatformAppNamespace,
|
|
181
|
-
...(requestId !== undefined && requestId !== '' ? { requestId } : {}),
|
|
182
|
-
port: options.port,
|
|
183
|
-
useEnvPort: options.target === 'server',
|
|
184
|
-
defaultOrigin: `http://localhost:${String(options.port)}`,
|
|
185
|
-
httpMethodDecider,
|
|
186
|
-
batch: {
|
|
187
|
-
enabled: batchConfig?.enabled ?? true,
|
|
188
|
-
endpoint: batchEndpoint,
|
|
189
|
-
flushIntervalMs: batchConfig?.flushIntervalMs ?? 8,
|
|
190
|
-
maxBatchSize: batchConfig?.maxBatchSize ?? 16,
|
|
191
|
-
maxBatchBytes: batchConfig?.maxBatchBytes ?? 64 * 1024,
|
|
192
|
-
requestTimeoutMs: batchConfig?.requestTimeoutMs ?? 10_000,
|
|
193
|
-
allowedMethods:
|
|
194
|
-
batchConfig?.allowedMethods !== undefined &&
|
|
195
|
-
batchConfig.allowedMethods.length > 0
|
|
196
|
-
? batchConfig.allowedMethods
|
|
197
|
-
: ['GET'],
|
|
198
|
-
},
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
return {
|
|
202
|
-
config,
|
|
203
|
-
manifest,
|
|
204
|
-
operationContracts: buildOperationContractMap({
|
|
205
|
-
handlers: toOperationContractSources(endpoints),
|
|
206
|
-
requestId: normalizedRequestId,
|
|
207
|
-
operationVersion,
|
|
208
|
-
}),
|
|
209
|
-
operationVersion,
|
|
210
|
-
requestCreator,
|
|
211
|
-
requestId: normalizedRequestId,
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function renderClientShape(
|
|
216
|
-
endpoints: EffectEndpointMeta[],
|
|
217
|
-
valueType: string,
|
|
218
|
-
): string {
|
|
219
|
-
if (endpoints.length === 0) {
|
|
220
|
-
return `Record<string, Record<string, ${valueType}>>`;
|
|
221
|
-
}
|
|
222
|
-
const groups = new Map<string, string[]>();
|
|
223
|
-
for (const endpoint of endpoints) {
|
|
224
|
-
const group = groups.get(endpoint.groupName) || [];
|
|
225
|
-
group.push(endpoint.endpointName);
|
|
226
|
-
groups.set(endpoint.groupName, group);
|
|
227
|
-
}
|
|
228
|
-
const groupEntries = [...groups.entries()].map(([groupName, names]) => {
|
|
229
|
-
const endpointEntries = names
|
|
230
|
-
.map(name => ` ${JSON.stringify(name)}: ${valueType};`)
|
|
231
|
-
.join('\n');
|
|
232
|
-
return ` ${JSON.stringify(groupName)}: {\n${endpointEntries}\n };`;
|
|
233
|
-
});
|
|
234
|
-
return `{\n${groupEntries.join('\n')}\n}`;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export function renderEffectClientDeclaration(
|
|
238
|
-
endpoints: EffectEndpointMeta[] = [],
|
|
239
|
-
) {
|
|
240
|
-
return `export type EffectClientOperation = (
|
|
241
|
-
request?: unknown,
|
|
242
|
-
) => Promise<unknown>;
|
|
243
|
-
export type EffectClientGroup = Record<string, EffectClientOperation>;
|
|
244
|
-
export type EffectClient = Record<string, EffectClientGroup>;
|
|
245
|
-
export type EffectOperationDescriptor = {
|
|
246
|
-
appNamespace: string;
|
|
247
|
-
apiId: string;
|
|
248
|
-
group: string;
|
|
249
|
-
endpoint: string;
|
|
250
|
-
operationId: string;
|
|
251
|
-
routePath: string;
|
|
252
|
-
method: string;
|
|
253
|
-
operationVersion: number;
|
|
254
|
-
schemaHash: string;
|
|
255
|
-
version: number;
|
|
256
|
-
};
|
|
257
|
-
export type EffectOperationManifest = Record<
|
|
258
|
-
string,
|
|
259
|
-
Record<string, EffectOperationDescriptor>
|
|
260
|
-
>;
|
|
261
|
-
export type EffectOperationContext = {
|
|
262
|
-
requestId?: string;
|
|
263
|
-
operationId?: string;
|
|
264
|
-
routePath?: string;
|
|
265
|
-
method?: string;
|
|
266
|
-
schemaHash?: string;
|
|
267
|
-
operationVersion?: number;
|
|
268
|
-
locale?: string;
|
|
269
|
-
traceparent?: string;
|
|
270
|
-
traceId?: string;
|
|
271
|
-
spanId?: string;
|
|
272
|
-
source?: string;
|
|
273
|
-
scope?: Record<string, unknown>;
|
|
274
|
-
sessionClaims?: Record<string, unknown>;
|
|
275
|
-
attributes?: Record<string, unknown>;
|
|
276
|
-
};
|
|
277
|
-
export type EffectRequestContext = {
|
|
278
|
-
headers?: Record<string, string>;
|
|
279
|
-
locale?: string;
|
|
280
|
-
operationContext?: EffectOperationContext;
|
|
281
|
-
traceparent?: string;
|
|
282
|
-
traceId?: string;
|
|
283
|
-
spanId?: string;
|
|
284
|
-
};
|
|
285
|
-
export type GeneratedEffectClient = ${renderClientShape(
|
|
286
|
-
endpoints,
|
|
287
|
-
'EffectClientOperation',
|
|
288
|
-
)};
|
|
289
|
-
export type GeneratedEffectOperationManifest = ${renderClientShape(
|
|
290
|
-
endpoints,
|
|
291
|
-
'EffectOperationDescriptor',
|
|
292
|
-
)};
|
|
293
|
-
|
|
294
|
-
export declare const client: GeneratedEffectClient;
|
|
295
|
-
export declare const createEffectRequestContext: (
|
|
296
|
-
requestContext: Record<string, unknown>,
|
|
297
|
-
) => EffectRequestContext;
|
|
298
|
-
export declare const operationManifest: GeneratedEffectOperationManifest;
|
|
299
|
-
declare const apiModule: {
|
|
300
|
-
client: GeneratedEffectClient;
|
|
301
|
-
createEffectRequestContext: typeof createEffectRequestContext;
|
|
302
|
-
operationManifest: GeneratedEffectOperationManifest;
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
export default apiModule;
|
|
306
|
-
`;
|
|
307
|
-
}
|