@bleedingdev/modern-js-plugin-bff 3.2.0-ultramodern.99 → 3.4.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/dist/cjs/cli.js +9 -5
- package/dist/cjs/constants.js +13 -9
- package/dist/cjs/index.js +9 -5
- package/dist/cjs/loader.js +9 -5
- package/dist/cjs/runtime/create-request/index.js +9 -5
- package/dist/cjs/runtime/data-platform/index.js +11 -18
- package/dist/cjs/runtime/effect/adapter.js +87 -14
- package/dist/cjs/runtime/effect/context.js +9 -5
- package/dist/cjs/runtime/effect/edge.js +26 -26
- package/dist/cjs/runtime/effect/endpoint-contracts.js +130 -0
- package/dist/cjs/runtime/effect/handler.js +107 -63
- package/dist/cjs/runtime/effect/index.js +28 -16
- package/dist/cjs/runtime/effect/module.js +71 -35
- package/dist/cjs/runtime/effect/operation-context.js +10 -18
- package/dist/cjs/runtime/effect-client/index.js +10 -6
- package/dist/cjs/runtime/effect-client/runtime.js +266 -0
- package/dist/cjs/runtime/hono/adapter.js +30 -14
- package/dist/cjs/runtime/hono/index.js +9 -5
- package/dist/cjs/runtime/hono/operators.js +9 -5
- package/dist/cjs/runtime/safe-failure.js +83 -0
- package/dist/cjs/server.js +9 -5
- package/dist/cjs/utils/clientGenerator.js +13 -9
- package/dist/cjs/utils/createHonoRoutes.js +9 -5
- package/dist/cjs/utils/crossProjectApiPlugin.js +9 -5
- package/dist/cjs/utils/crossProjectServerPolicy.js +104 -0
- package/dist/cjs/utils/effectClientGenerator.js +99 -488
- package/dist/cjs/utils/pluginGenerator.js +9 -5
- package/dist/cjs/utils/runtimeGenerator.js +9 -5
- 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 +41 -8
- package/dist/esm/runtime/effect/index.mjs +2 -0
- package/dist/esm/runtime/effect/module.mjs +63 -31
- package/dist/esm/runtime/effect/operation-context.mjs +1 -13
- package/dist/esm/runtime/effect-client/index.mjs +1 -1
- 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 +41 -8
- package/dist/esm-node/runtime/effect/index.mjs +2 -0
- package/dist/esm-node/runtime/effect/module.mjs +63 -31
- package/dist/esm-node/runtime/effect/operation-context.mjs +1 -13
- package/dist/esm-node/runtime/effect-client/index.mjs +1 -1
- 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/context.d.ts +1 -1
- package/dist/types/runtime/effect/endpoint-contracts.d.ts +62 -0
- package/dist/types/runtime/effect/handler.d.ts +37 -4
- package/dist/types/runtime/effect/index.d.ts +1 -0
- package/dist/types/runtime/effect/module.d.ts +22 -2
- 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/server.d.ts +1 -1
- package/dist/types/utils/createHonoRoutes.d.ts +3 -3
- package/dist/types/utils/crossProjectServerPolicy.d.ts +35 -0
- package/dist/types/utils/effectClientGenerator.d.ts +16 -2
- package/package.json +40 -27
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { DEFAULT_DATA_BATCH_HEADER, DEFAULT_DATA_ENVELOPE_HEADER, createDataBatchTransport, createRequestEnvelope, encodeRequestEnvelopeHeader } from "../data-platform/index.mjs";
|
|
2
|
+
const METHODS_WITHOUT_BODY = new Set([
|
|
3
|
+
'GET',
|
|
4
|
+
'DELETE',
|
|
5
|
+
'HEAD',
|
|
6
|
+
'OPTIONS'
|
|
7
|
+
]);
|
|
8
|
+
const DATA_REQUEST_MODES = new Set([
|
|
9
|
+
'cache-first',
|
|
10
|
+
'stale-while-revalidate',
|
|
11
|
+
'network-only'
|
|
12
|
+
]);
|
|
13
|
+
const DATA_MUTATION_MODES = new Set([
|
|
14
|
+
'optimistic',
|
|
15
|
+
'pessimistic',
|
|
16
|
+
'fire-and-forget'
|
|
17
|
+
]);
|
|
18
|
+
const isRecord = (value)=>'object' == typeof value && null !== value;
|
|
19
|
+
const stringOrUndefined = (value)=>'string' == typeof value && value.length > 0 ? value : void 0;
|
|
20
|
+
const isDataRequestMode = (value)=>'string' == typeof value && DATA_REQUEST_MODES.has(value);
|
|
21
|
+
const isDataMutationMode = (value)=>'string' == typeof value && DATA_MUTATION_MODES.has(value);
|
|
22
|
+
const normalizeOrigin = (value)=>{
|
|
23
|
+
if ('string' != typeof value || 0 === value.length) return;
|
|
24
|
+
try {
|
|
25
|
+
return new URL(value).origin;
|
|
26
|
+
} catch {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const resolveRuntimeFetch = ()=>'function' == typeof fetch ? fetch.bind(globalThis) : void 0;
|
|
31
|
+
const resolveOrigin = (defaultOrigin)=>{
|
|
32
|
+
if ("u" > typeof window && window.location && 'string' == typeof window.location.origin && window.location.origin) return window.location.origin;
|
|
33
|
+
const globalLocation = globalThis?.location;
|
|
34
|
+
if (globalLocation && 'string' == typeof globalLocation.origin && globalLocation.origin) return globalLocation.origin;
|
|
35
|
+
return defaultOrigin;
|
|
36
|
+
};
|
|
37
|
+
const normalizeRequest = (method, request)=>{
|
|
38
|
+
if (!isRecord(request)) return {};
|
|
39
|
+
const payload = {
|
|
40
|
+
...request
|
|
41
|
+
};
|
|
42
|
+
if (isRecord(request.path) && !isRecord(payload.params)) payload.params = request.path;
|
|
43
|
+
if (isRecord(request.urlParams) && !isRecord(payload.query)) payload.query = request.urlParams;
|
|
44
|
+
if (isRecord(request.headers) && !isRecord(payload.headers)) payload.headers = request.headers;
|
|
45
|
+
if ('payload' in request && void 0 !== request.payload) if ("u" > typeof FormData && request.payload instanceof FormData && !('formData' in payload)) payload.formData = request.payload;
|
|
46
|
+
else if (METHODS_WITHOUT_BODY.has(method)) {
|
|
47
|
+
if (isRecord(request.payload)) payload.query = isRecord(payload.query) ? {
|
|
48
|
+
...payload.query,
|
|
49
|
+
...request.payload
|
|
50
|
+
} : request.payload;
|
|
51
|
+
else if (!('body' in payload)) payload.body = request.payload;
|
|
52
|
+
} else if (!isRecord(request.payload) || 'data' in payload) {
|
|
53
|
+
if (!('body' in payload)) payload.body = request.payload;
|
|
54
|
+
} else payload.data = request.payload;
|
|
55
|
+
return payload;
|
|
56
|
+
};
|
|
57
|
+
const resolveTargetOrigin = (dataPlatform, defaultOrigin)=>{
|
|
58
|
+
const explicitTargetOrigin = stringOrUndefined(dataPlatform.targetOrigin) || stringOrUndefined(dataPlatform.endpointOrigin);
|
|
59
|
+
if (explicitTargetOrigin) return explicitTargetOrigin;
|
|
60
|
+
return defaultOrigin;
|
|
61
|
+
};
|
|
62
|
+
const shouldAttachEnvelopeHeader = (dataPlatform, defaultOrigin)=>{
|
|
63
|
+
if (true === dataPlatform.allowCrossOriginEnvelope) return true;
|
|
64
|
+
const currentOrigin = normalizeOrigin(resolveOrigin(defaultOrigin));
|
|
65
|
+
const targetOrigin = normalizeOrigin(resolveTargetOrigin(dataPlatform, defaultOrigin));
|
|
66
|
+
if (!currentOrigin || !targetOrigin) return true;
|
|
67
|
+
return currentOrigin === targetOrigin;
|
|
68
|
+
};
|
|
69
|
+
const toEnvelopeInput = (normalizedRequest)=>{
|
|
70
|
+
const payload = {};
|
|
71
|
+
if (isRecord(normalizedRequest.params)) payload.path = normalizedRequest.params;
|
|
72
|
+
if (isRecord(normalizedRequest.query)) payload.query = normalizedRequest.query;
|
|
73
|
+
if ('data' in normalizedRequest && void 0 !== normalizedRequest.data) payload.data = normalizedRequest.data;
|
|
74
|
+
if ('body' in normalizedRequest && void 0 !== normalizedRequest.body) payload.body = normalizedRequest.body;
|
|
75
|
+
if ("u" > typeof FormData && normalizedRequest.formData instanceof FormData) payload.formData = Array.from(normalizedRequest.formData.entries()).map(([key, value])=>[
|
|
76
|
+
key,
|
|
77
|
+
String(value)
|
|
78
|
+
]);
|
|
79
|
+
if ("u" > typeof URLSearchParams && normalizedRequest.formUrlencoded instanceof URLSearchParams) payload.formUrlencoded = normalizedRequest.formUrlencoded.toString();
|
|
80
|
+
return payload;
|
|
81
|
+
};
|
|
82
|
+
const createGeneratedEffectClient = (manifest, config, requestRuntime)=>{
|
|
83
|
+
const createRequest = requestRuntime.createRequest;
|
|
84
|
+
const configureRequest = 'function' == typeof requestRuntime.configure ? requestRuntime.configure : void 0;
|
|
85
|
+
const createRequestContextHeaders = 'function' == typeof requestRuntime.createRequestContextHeaders ? requestRuntime.createRequestContextHeaders : void 0;
|
|
86
|
+
const defaultOrigin = config.defaultOrigin;
|
|
87
|
+
const httpMethodDecider = config.httpMethodDecider || 'functionName';
|
|
88
|
+
const port = config.useEnvPort && "u" > typeof process && process.env && process.env.PORT ? process.env.PORT : config.port;
|
|
89
|
+
if (config.requestId && configureRequest) {
|
|
90
|
+
const configurePayload = {
|
|
91
|
+
requestId: config.requestId,
|
|
92
|
+
requireEnvelope: true,
|
|
93
|
+
identityBinding: {
|
|
94
|
+
enabled: true,
|
|
95
|
+
strict: true
|
|
96
|
+
},
|
|
97
|
+
operationContract: {
|
|
98
|
+
enabled: true,
|
|
99
|
+
strict: true,
|
|
100
|
+
requireSchemaHash: true,
|
|
101
|
+
requireOperationVersion: true
|
|
102
|
+
},
|
|
103
|
+
setDomain: ()=>resolveOrigin(defaultOrigin)
|
|
104
|
+
};
|
|
105
|
+
const runtimeFetch = resolveRuntimeFetch();
|
|
106
|
+
if (false !== config.batch.enabled && runtimeFetch) configurePayload.request = createDataBatchTransport({
|
|
107
|
+
fetch: runtimeFetch,
|
|
108
|
+
endpoint: config.batch.endpoint,
|
|
109
|
+
flushIntervalMs: config.batch.flushIntervalMs,
|
|
110
|
+
maxBatchSize: config.batch.maxBatchSize,
|
|
111
|
+
maxBatchBytes: config.batch.maxBatchBytes,
|
|
112
|
+
requestTimeoutMs: config.batch.requestTimeoutMs,
|
|
113
|
+
allowedMethods: config.batch.allowedMethods
|
|
114
|
+
});
|
|
115
|
+
configureRequest(configurePayload);
|
|
116
|
+
}
|
|
117
|
+
const createEffectRequestContext = (requestContext)=>{
|
|
118
|
+
if (!isRecord(requestContext)) return {};
|
|
119
|
+
const headers = createRequestContextHeaders ? createRequestContextHeaders(requestContext) : {};
|
|
120
|
+
return {
|
|
121
|
+
...requestContext,
|
|
122
|
+
headers
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
const applyRequestContext = (normalizedRequest, request)=>{
|
|
126
|
+
if (!isRecord(request) || !isRecord(request.requestContext)) return normalizedRequest;
|
|
127
|
+
const requestContext = createEffectRequestContext(request.requestContext);
|
|
128
|
+
const requestHeaders = isRecord(requestContext.headers) ? requestContext.headers : {};
|
|
129
|
+
if (0 === Object.keys(requestHeaders).length) return normalizedRequest;
|
|
130
|
+
return {
|
|
131
|
+
...normalizedRequest,
|
|
132
|
+
headers: {
|
|
133
|
+
...requestHeaders,
|
|
134
|
+
...isRecord(normalizedRequest.headers) ? normalizedRequest.headers : {}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
const prepareEffectRequest = (endpoint, operation, request)=>{
|
|
139
|
+
const normalizedRequest = applyRequestContext(normalizeRequest(endpoint.method, request), request);
|
|
140
|
+
const dataPlatform = isRecord(request) && isRecord(request.dataPlatform) ? request.dataPlatform : {};
|
|
141
|
+
const strictEnvelope = true === dataPlatform.requireEnvelope || true === dataPlatform.strict;
|
|
142
|
+
if (!strictEnvelope && !shouldAttachEnvelopeHeader(dataPlatform, defaultOrigin)) return normalizedRequest;
|
|
143
|
+
try {
|
|
144
|
+
const namespace = stringOrUndefined(dataPlatform.appNamespace) || config.appNamespace;
|
|
145
|
+
const origin = stringOrUndefined(dataPlatform.origin) || resolveOrigin(defaultOrigin);
|
|
146
|
+
const envelope = createRequestEnvelope({
|
|
147
|
+
operation: {
|
|
148
|
+
...operation,
|
|
149
|
+
appNamespace: namespace
|
|
150
|
+
},
|
|
151
|
+
scope: {
|
|
152
|
+
appNamespace: namespace,
|
|
153
|
+
origin,
|
|
154
|
+
tenantId: stringOrUndefined(dataPlatform.tenantId),
|
|
155
|
+
userId: stringOrUndefined(dataPlatform.userId),
|
|
156
|
+
sessionId: stringOrUndefined(dataPlatform.sessionId)
|
|
157
|
+
},
|
|
158
|
+
requestInput: {
|
|
159
|
+
method: endpoint.method,
|
|
160
|
+
routePath: endpoint.routePath,
|
|
161
|
+
payload: toEnvelopeInput(normalizedRequest)
|
|
162
|
+
},
|
|
163
|
+
requestMode: isDataRequestMode(dataPlatform.requestMode) ? dataPlatform.requestMode : void 0,
|
|
164
|
+
mutationMode: isDataMutationMode(dataPlatform.mutationMode) ? dataPlatform.mutationMode : void 0,
|
|
165
|
+
selectionPlan: isRecord(dataPlatform.selectionPlan) ? dataPlatform.selectionPlan : void 0,
|
|
166
|
+
traceContext: isRecord(dataPlatform.traceContext) ? dataPlatform.traceContext : void 0,
|
|
167
|
+
requireTraceContext: true === dataPlatform.requireTraceContext
|
|
168
|
+
});
|
|
169
|
+
const headerName = stringOrUndefined(dataPlatform.envelopeHeader) || DEFAULT_DATA_ENVELOPE_HEADER;
|
|
170
|
+
const headers = isRecord(normalizedRequest.headers) ? {
|
|
171
|
+
...normalizedRequest.headers
|
|
172
|
+
} : {};
|
|
173
|
+
if (false === dataPlatform.batch) headers[DEFAULT_DATA_BATCH_HEADER] = 'off';
|
|
174
|
+
headers[headerName] = encodeRequestEnvelopeHeader(envelope);
|
|
175
|
+
return {
|
|
176
|
+
...normalizedRequest,
|
|
177
|
+
headers
|
|
178
|
+
};
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (strictEnvelope) throw error;
|
|
181
|
+
return normalizedRequest;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
const client = {};
|
|
185
|
+
const operationManifest = {};
|
|
186
|
+
for (const endpoint of manifest.endpoints){
|
|
187
|
+
const operationId = `${endpoint.method}:${endpoint.routePath}`;
|
|
188
|
+
const operation = {
|
|
189
|
+
appNamespace: config.appNamespace,
|
|
190
|
+
apiId: endpoint.apiId,
|
|
191
|
+
group: endpoint.group,
|
|
192
|
+
endpoint: endpoint.endpoint,
|
|
193
|
+
operationId,
|
|
194
|
+
routePath: endpoint.routePath,
|
|
195
|
+
method: endpoint.method,
|
|
196
|
+
operationVersion: endpoint.operationVersion,
|
|
197
|
+
schemaHash: endpoint.schemaHash,
|
|
198
|
+
version: endpoint.operationVersion
|
|
199
|
+
};
|
|
200
|
+
const sender = createRequest({
|
|
201
|
+
path: endpoint.routePath,
|
|
202
|
+
method: endpoint.method,
|
|
203
|
+
port,
|
|
204
|
+
operationContext: {
|
|
205
|
+
operationId,
|
|
206
|
+
routePath: endpoint.routePath,
|
|
207
|
+
method: endpoint.method,
|
|
208
|
+
schemaHash: endpoint.schemaHash,
|
|
209
|
+
operationVersion: endpoint.operationVersion
|
|
210
|
+
},
|
|
211
|
+
httpMethodDecider,
|
|
212
|
+
...config.requestId ? {
|
|
213
|
+
requestId: config.requestId
|
|
214
|
+
} : {}
|
|
215
|
+
});
|
|
216
|
+
const call = (request = {})=>sender(prepareEffectRequest(endpoint, operation, request));
|
|
217
|
+
client[endpoint.group] ??= {};
|
|
218
|
+
client[endpoint.group][endpoint.endpoint] = call;
|
|
219
|
+
operationManifest[endpoint.group] ??= {};
|
|
220
|
+
operationManifest[endpoint.group][endpoint.endpoint] = operation;
|
|
221
|
+
}
|
|
222
|
+
return {
|
|
223
|
+
client,
|
|
224
|
+
operationManifest,
|
|
225
|
+
createEffectRequestContext
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
export { createGeneratedEffectClient };
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Hono, run } from "@modern-js/server-core";
|
|
2
2
|
import { isProd, logger } from "@modern-js/utils";
|
|
3
3
|
import createHonoRoutes from "../../utils/createHonoRoutes.mjs";
|
|
4
|
+
import { checkCrossProjectPolicyResponse, resolveAdapterCrossProjectPolicy } from "../../utils/crossProjectServerPolicy.mjs";
|
|
5
|
+
import { createSafeFailureResponse } from "../safe-failure.mjs";
|
|
4
6
|
const before = [
|
|
5
7
|
'custom-server-hook',
|
|
6
8
|
'custom-server-middleware',
|
|
@@ -18,6 +20,7 @@ class HonoAdapter {
|
|
|
18
20
|
this.apiMiddleware = [];
|
|
19
21
|
this.apiServer = null;
|
|
20
22
|
this.isHono = true;
|
|
23
|
+
this.prefix = '/api';
|
|
21
24
|
this.setHandlers = async ()=>{
|
|
22
25
|
if (!this.isHono) return;
|
|
23
26
|
const { apiHandlerInfos } = this.api.getServerContext();
|
|
@@ -30,6 +33,22 @@ class HonoAdapter {
|
|
|
30
33
|
order: 'post',
|
|
31
34
|
before: before
|
|
32
35
|
}));
|
|
36
|
+
this.crossProjectPolicy = resolveAdapterCrossProjectPolicy(this.api, apiHandlerInfos || []);
|
|
37
|
+
if (this.crossProjectPolicy) {
|
|
38
|
+
const policyMiddleware = async (c, next)=>{
|
|
39
|
+
const denial = checkCrossProjectPolicyResponse(c.req.header(), this.crossProjectPolicy);
|
|
40
|
+
if (denial) return denial;
|
|
41
|
+
await next();
|
|
42
|
+
};
|
|
43
|
+
this.apiMiddleware.unshift({
|
|
44
|
+
name: 'bff-cross-project-policy',
|
|
45
|
+
path: `${this.prefix}/*`,
|
|
46
|
+
method: 'all',
|
|
47
|
+
handler: policyMiddleware,
|
|
48
|
+
order: 'post',
|
|
49
|
+
before: before
|
|
50
|
+
});
|
|
51
|
+
}
|
|
33
52
|
};
|
|
34
53
|
this.registerApiRoutes = async ()=>{
|
|
35
54
|
if (!this.isHono) return;
|
|
@@ -66,15 +85,7 @@ class HonoAdapter {
|
|
|
66
85
|
} catch (configError) {
|
|
67
86
|
logger.error(`Error in serverConfig.onError handler: ${configError}`);
|
|
68
87
|
}
|
|
69
|
-
|
|
70
|
-
return new Response(JSON.stringify({
|
|
71
|
-
message: err instanceof Error ? err.message : '[BFF] Internal Server Error'
|
|
72
|
-
}), {
|
|
73
|
-
status,
|
|
74
|
-
headers: {
|
|
75
|
-
'content-type': 'application/json; charset=utf-8'
|
|
76
|
-
}
|
|
77
|
-
});
|
|
88
|
+
return createSafeFailureResponse(err);
|
|
78
89
|
});
|
|
79
90
|
};
|
|
80
91
|
this.registerMiddleware = async (options)=>{
|
|
@@ -84,6 +95,7 @@ class HonoAdapter {
|
|
|
84
95
|
this.isHono = false;
|
|
85
96
|
return;
|
|
86
97
|
}
|
|
98
|
+
this.prefix = prefix || this.prefix;
|
|
87
99
|
const { middlewares: globalMiddlewares } = this.api.getServerContext();
|
|
88
100
|
await this.setHandlers();
|
|
89
101
|
if (isProd()) globalMiddlewares.push(...this.apiMiddleware);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const SAFE_FAILURE_MESSAGES = {
|
|
2
|
+
500: 'Internal Server Error',
|
|
3
|
+
503: 'Service Unavailable'
|
|
4
|
+
};
|
|
5
|
+
const SAFE_FAILURE_CODES = {
|
|
6
|
+
500: 'INTERNAL_SERVER_ERROR',
|
|
7
|
+
503: 'SERVICE_UNAVAILABLE'
|
|
8
|
+
};
|
|
9
|
+
const readErrorProperty = (error, key)=>{
|
|
10
|
+
if ('object' != typeof error || null === error || !(key in error)) return;
|
|
11
|
+
return error[key];
|
|
12
|
+
};
|
|
13
|
+
const normalizeFailureStatus = (value)=>{
|
|
14
|
+
if ('number' != typeof value || !Number.isInteger(value)) return;
|
|
15
|
+
return value >= 400 && value <= 599 ? value : void 0;
|
|
16
|
+
};
|
|
17
|
+
const normalizeRetryAfter = (value)=>{
|
|
18
|
+
if ('number' == typeof value && Number.isFinite(value) && value >= 0) return String(Math.ceil(value));
|
|
19
|
+
if ('string' == typeof value) {
|
|
20
|
+
const trimmed = value.trim();
|
|
21
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
22
|
+
}
|
|
23
|
+
if (value instanceof Date) return value.toUTCString();
|
|
24
|
+
};
|
|
25
|
+
const createSafeFailureResponse = (error)=>{
|
|
26
|
+
const status = normalizeFailureStatus(readErrorProperty(error, 'status')) ?? normalizeFailureStatus(readErrorProperty(error, 'statusCode')) ?? 500;
|
|
27
|
+
const retryAfter = 503 === status ? normalizeRetryAfter(readErrorProperty(error, 'retryAfter')) ?? normalizeRetryAfter(readErrorProperty(error, 'retryAfterSeconds')) ?? normalizeRetryAfter('number' == typeof readErrorProperty(error, 'retryAfterMs') ? readErrorProperty(error, 'retryAfterMs') / 1000 : void 0) : void 0;
|
|
28
|
+
const body = {
|
|
29
|
+
success: false,
|
|
30
|
+
error: {
|
|
31
|
+
code: SAFE_FAILURE_CODES[status] ?? 'REQUEST_FAILED',
|
|
32
|
+
message: SAFE_FAILURE_MESSAGES[status] ?? 'Request failed',
|
|
33
|
+
status
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const headers = {
|
|
37
|
+
'content-type': 'application/json; charset=utf-8'
|
|
38
|
+
};
|
|
39
|
+
if (void 0 !== retryAfter) headers['Retry-After'] = retryAfter;
|
|
40
|
+
return new Response(JSON.stringify(body), {
|
|
41
|
+
status,
|
|
42
|
+
headers
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
export { createSafeFailureResponse };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { generateClient } from "@modern-js/bff-core";
|
|
2
2
|
import { fs, logger } from "@modern-js/utils";
|
|
3
3
|
import path from "path";
|
|
4
|
-
import {
|
|
4
|
+
import { generateEffectClient, resolveEffectEntryFile } from "./effectClientGenerator.mjs";
|
|
5
5
|
const API_DIR = 'api';
|
|
6
6
|
const PLUGIN_DIR = 'plugin';
|
|
7
7
|
const RUNTIME_DIR = 'runtime';
|
|
@@ -262,7 +262,7 @@ async function clientGenerator(draftOptions) {
|
|
|
262
262
|
source: effectSource,
|
|
263
263
|
relativeDistPath: draftOptions.relativeDistPath
|
|
264
264
|
});
|
|
265
|
-
const
|
|
265
|
+
const effectClientArtifacts = await generateEffectClient({
|
|
266
266
|
appDir: draftOptions.appDir,
|
|
267
267
|
apiDir: draftOptions.apiDir,
|
|
268
268
|
resourcePath: effectEntryFile,
|
|
@@ -273,10 +273,10 @@ async function clientGenerator(draftOptions) {
|
|
|
273
273
|
httpMethodDecider: draftOptions.httpMethodDecider,
|
|
274
274
|
dataPlatformBatch: draftOptions.effectDataPlatformBatch
|
|
275
275
|
});
|
|
276
|
-
if (
|
|
276
|
+
if (effectClientArtifacts) {
|
|
277
277
|
const targetTypeFile = effectFileDetails.targetDir.replace(/\.js$/, '.d.ts');
|
|
278
|
-
await writeTargetFile(effectFileDetails.absTargetDir,
|
|
279
|
-
await writeTargetFile(path.resolve(targetTypeFile),
|
|
278
|
+
await writeTargetFile(effectFileDetails.absTargetDir, effectClientArtifacts.code);
|
|
279
|
+
await writeTargetFile(path.resolve(targetTypeFile), effectClientArtifacts.declaration);
|
|
280
280
|
generatedSourceList.push(effectFileDetails);
|
|
281
281
|
}
|
|
282
282
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { checkCrossProjectPolicy, deriveOperationVersion, resolveCrossProjectPolicy } from "@modern-js/bff-core";
|
|
2
|
+
import path from "path";
|
|
3
|
+
const readNearestPackageVersion = (startDir)=>{
|
|
4
|
+
if (!startDir) return;
|
|
5
|
+
let current = path.resolve(startDir);
|
|
6
|
+
for(let depth = 0; depth < 10; depth += 1){
|
|
7
|
+
try {
|
|
8
|
+
const packageJson = require(path.join(current, 'package.json'));
|
|
9
|
+
if ('string' == typeof packageJson.version) return packageJson.version;
|
|
10
|
+
} catch {}
|
|
11
|
+
const parent = path.dirname(current);
|
|
12
|
+
if (parent === current) break;
|
|
13
|
+
current = parent;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const resolveAdapterCrossProjectPolicy = (api, handlers)=>{
|
|
17
|
+
const bff = api.getServerConfig()?.bff;
|
|
18
|
+
const { apiDirectory, appDirectory } = api.getServerContext();
|
|
19
|
+
return resolveCrossProjectPolicy({
|
|
20
|
+
crossProjectPolicy: bff?.crossProjectPolicy,
|
|
21
|
+
handlers,
|
|
22
|
+
requestId: bff?.requestId,
|
|
23
|
+
isCrossProjectServer: bff?.isCrossProjectServer,
|
|
24
|
+
operationVersion: deriveOperationVersion(readNearestPackageVersion(apiDirectory) ?? readNearestPackageVersion(appDirectory))
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
const DENIAL_HEADERS = {
|
|
28
|
+
'content-type': 'application/json; charset=utf-8'
|
|
29
|
+
};
|
|
30
|
+
const checkCrossProjectPolicyResponse = (headers, policy)=>{
|
|
31
|
+
if (!policy?.enabled) return null;
|
|
32
|
+
const denial = checkCrossProjectPolicy(headers, policy);
|
|
33
|
+
if (!denial) return null;
|
|
34
|
+
return new Response(JSON.stringify(denial.body), {
|
|
35
|
+
status: denial.status,
|
|
36
|
+
headers: DENIAL_HEADERS
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
const toHeaderRecord = (headers)=>{
|
|
40
|
+
const record = {};
|
|
41
|
+
headers.forEach((value, key)=>{
|
|
42
|
+
record[key] = value;
|
|
43
|
+
});
|
|
44
|
+
return record;
|
|
45
|
+
};
|
|
46
|
+
const checkCrossProjectPolicyForRequest = (request, policy)=>{
|
|
47
|
+
if (!policy?.enabled) return null;
|
|
48
|
+
return checkCrossProjectPolicyResponse(toHeaderRecord(request.headers), policy);
|
|
49
|
+
};
|
|
50
|
+
export { checkCrossProjectPolicyForRequest, checkCrossProjectPolicyResponse, resolveAdapterCrossProjectPolicy };
|