@bleedingdev/modern-js-bff-core 3.2.0-ultramodern.12 → 3.2.0-ultramodern.121

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.
Files changed (46) hide show
  1. package/dist/cjs/adapter-kit/index.js +140 -0
  2. package/dist/cjs/adapter-kit/parity.js +546 -0
  3. package/dist/cjs/api.js +9 -5
  4. package/dist/cjs/client/generateClient.js +74 -17
  5. package/dist/cjs/client/index.js +9 -5
  6. package/dist/cjs/client/result.js +13 -9
  7. package/dist/cjs/contracts/eventContracts.js +14 -10
  8. package/dist/cjs/errors/http.js +13 -9
  9. package/dist/cjs/index.js +83 -41
  10. package/dist/cjs/operators/http.js +9 -5
  11. package/dist/cjs/router/constants.js +9 -5
  12. package/dist/cjs/router/index.js +12 -8
  13. package/dist/cjs/router/utils.js +9 -5
  14. package/dist/cjs/security/crossProjectPolicy.js +25 -13
  15. package/dist/cjs/security/operationContracts.js +155 -59
  16. package/dist/cjs/security/resolveCrossProjectPolicy.js +65 -0
  17. package/dist/cjs/types.js +18 -13
  18. package/dist/cjs/utils/alias.js +9 -5
  19. package/dist/cjs/utils/debug.js +9 -5
  20. package/dist/cjs/utils/index.js +12 -8
  21. package/dist/cjs/utils/meta.js +15 -11
  22. package/dist/cjs/utils/storage.js +9 -5
  23. package/dist/cjs/utils/validate.js +9 -5
  24. package/dist/esm/adapter-kit/index.mjs +75 -0
  25. package/dist/esm/adapter-kit/parity.mjs +490 -0
  26. package/dist/esm/client/generateClient.mjs +66 -13
  27. package/dist/esm/index.mjs +2 -0
  28. package/dist/esm/rslib-runtime.mjs +18 -0
  29. package/dist/esm/security/crossProjectPolicy.mjs +10 -2
  30. package/dist/esm/security/operationContracts.mjs +111 -37
  31. package/dist/esm/security/resolveCrossProjectPolicy.mjs +27 -0
  32. package/dist/esm-node/adapter-kit/index.mjs +76 -0
  33. package/dist/esm-node/adapter-kit/parity.mjs +491 -0
  34. package/dist/esm-node/client/generateClient.mjs +66 -13
  35. package/dist/esm-node/index.mjs +2 -0
  36. package/dist/esm-node/rslib-runtime.mjs +19 -0
  37. package/dist/esm-node/security/crossProjectPolicy.mjs +10 -2
  38. package/dist/esm-node/security/operationContracts.mjs +111 -37
  39. package/dist/esm-node/security/resolveCrossProjectPolicy.mjs +28 -0
  40. package/dist/types/adapter-kit/index.d.ts +90 -0
  41. package/dist/types/adapter-kit/parity.d.ts +102 -0
  42. package/dist/types/index.d.ts +2 -0
  43. package/dist/types/security/crossProjectPolicy.d.ts +40 -1
  44. package/dist/types/security/operationContracts.d.ts +60 -4
  45. package/dist/types/security/resolveCrossProjectPolicy.d.ts +48 -0
  46. package/package.json +12 -10
@@ -0,0 +1,75 @@
1
+ import "reflect-metadata";
2
+ import { evaluateCrossProjectPolicy } from "../security/crossProjectPolicy.mjs";
3
+ import { HttpMetadata, HttpMethod } from "../types.mjs";
4
+ import { isInputParamsDeciderHandler, isWithMetaHandler } from "../utils/index.mjs";
5
+ const API_ROUTE_METHODS = {
6
+ [HttpMethod.Get]: 'get',
7
+ [HttpMethod.Post]: 'post',
8
+ [HttpMethod.Put]: 'put',
9
+ [HttpMethod.Delete]: 'delete',
10
+ [HttpMethod.Connect]: 'connect',
11
+ [HttpMethod.Trace]: 'trace',
12
+ [HttpMethod.Patch]: 'patch',
13
+ [HttpMethod.Options]: 'options',
14
+ [HttpMethod.Head]: 'head'
15
+ };
16
+ const toApiRouteMethod = (httpMethod)=>{
17
+ const method = API_ROUTE_METHODS[httpMethod];
18
+ if (!method) throw new Error(`[bff-core] Unsupported HTTP method "${String(httpMethod)}" in API handler info`);
19
+ return method;
20
+ };
21
+ const getRouteMiddlewares = (handler)=>{
22
+ const middlewares = Reflect.getMetadata('middleware', handler);
23
+ return Array.isArray(middlewares) ? middlewares : [];
24
+ };
25
+ const planApiRoutes = (handlerInfos)=>handlerInfos.map(({ routePath, handler, httpMethod })=>({
26
+ method: toApiRouteMethod(httpMethod),
27
+ routePath,
28
+ handler,
29
+ middlewares: getRouteMiddlewares(handler)
30
+ }));
31
+ const HANDLER_WITH_SCHEMA = 'HANDLER_WITH_SCHEMA';
32
+ const isSchemaApiHandler = (handler)=>{
33
+ if ('function' != typeof handler) return false;
34
+ const marked = handler;
35
+ return true === marked[HANDLER_WITH_SCHEMA];
36
+ };
37
+ const getApiHandlerMode = (handler)=>{
38
+ if (isWithMetaHandler(handler)) return 'meta';
39
+ if (isSchemaApiHandler(handler)) return 'schema';
40
+ if (isInputParamsDeciderHandler(handler)) return 'inputParamsDecider';
41
+ return 'plain';
42
+ };
43
+ const mapSchemaHandlerResult = (result)=>{
44
+ if ('HandleSuccess' === result.type) return {
45
+ success: true,
46
+ status: 200,
47
+ body: result.value
48
+ };
49
+ return {
50
+ success: false,
51
+ status: 'InputValidationError' === result.type ? 400 : 500,
52
+ body: result.message
53
+ };
54
+ };
55
+ const getResponseMetaList = (handler)=>{
56
+ const responseMeta = Reflect.getMetadata(HttpMetadata.Response, handler);
57
+ return Array.isArray(responseMeta) ? responseMeta : [];
58
+ };
59
+ const buildPositionalHandlerArgs = (input)=>[
60
+ ...Object.values(input.params),
61
+ input
62
+ ];
63
+ const checkCrossProjectPolicy = (headers, policy)=>{
64
+ const violation = evaluateCrossProjectPolicy(headers, policy);
65
+ if (!violation) return null;
66
+ return {
67
+ status: violation.status,
68
+ body: {
69
+ code: violation.code,
70
+ reason: violation.reason,
71
+ message: violation.message
72
+ }
73
+ };
74
+ };
75
+ export { HANDLER_WITH_SCHEMA, buildPositionalHandlerArgs, checkCrossProjectPolicy, getApiHandlerMode, getResponseMetaList, getRouteMiddlewares, isSchemaApiHandler, mapSchemaHandlerResult, planApiRoutes, toApiRouteMethod };
@@ -0,0 +1,490 @@
1
+ import { buildOperationContractMap } from "../security/operationContracts.mjs";
2
+ import { HttpMethod } from "../types.mjs";
3
+ const PARITY_REQUEST_ID = 'crm';
4
+ const PARITY_PRODUCER_REQUEST_ID = 'crm.producer-a';
5
+ const PARITY_FIXTURE_FILENAME = 'bff-core/adapter-kit/parity-fixture.ts';
6
+ const HANDLER_WITH_SCHEMA = 'HANDLER_WITH_SCHEMA';
7
+ const isRecord = (value)=>Boolean(value) && 'object' == typeof value;
8
+ const createSchemaFixtureHandler = ()=>{
9
+ const handler = (input)=>{
10
+ const data = isRecord(input) && isRecord(input.data) ? input.data : void 0;
11
+ const id = data?.id;
12
+ if ('number' == typeof id) return {
13
+ type: 'HandleSuccess',
14
+ value: {
15
+ id
16
+ }
17
+ };
18
+ if ('boom' === id) return {
19
+ type: 'OutputValidationError',
20
+ message: 'invalid output'
21
+ };
22
+ return {
23
+ type: 'InputValidationError',
24
+ message: 'invalid input'
25
+ };
26
+ };
27
+ return Object.assign(handler, {
28
+ [HANDLER_WITH_SCHEMA]: true
29
+ });
30
+ };
31
+ const createParityApiHandlerInfos = ()=>[
32
+ {
33
+ name: 'getHello',
34
+ httpMethod: HttpMethod.Get,
35
+ routeName: '/hello',
36
+ routePath: '/hello',
37
+ filename: PARITY_FIXTURE_FILENAME,
38
+ handler: ()=>({
39
+ message: 'hello'
40
+ })
41
+ },
42
+ {
43
+ name: 'postHello',
44
+ httpMethod: HttpMethod.Post,
45
+ routeName: '/hello',
46
+ routePath: '/hello',
47
+ filename: PARITY_FIXTURE_FILENAME,
48
+ handler: ()=>'hello'
49
+ },
50
+ {
51
+ name: 'getNothing',
52
+ httpMethod: HttpMethod.Get,
53
+ routeName: '/nothing',
54
+ routePath: '/nothing',
55
+ filename: PARITY_FIXTURE_FILENAME,
56
+ handler: ()=>void 0
57
+ },
58
+ {
59
+ name: 'postEcho',
60
+ httpMethod: HttpMethod.Post,
61
+ routeName: '/echo',
62
+ routePath: '/echo',
63
+ filename: PARITY_FIXTURE_FILENAME,
64
+ handler: (input)=>{
65
+ const normalized = isRecord(input) ? input : {};
66
+ return {
67
+ data: normalized.data ?? null,
68
+ query: normalized.query ?? {},
69
+ cookie: normalized.cookies ?? null
70
+ };
71
+ }
72
+ },
73
+ {
74
+ name: 'getItem',
75
+ httpMethod: HttpMethod.Get,
76
+ routeName: '/items/:id',
77
+ routePath: '/items/:id',
78
+ filename: PARITY_FIXTURE_FILENAME,
79
+ handler: (id, input)=>({
80
+ id,
81
+ query: isRecord(input) ? input.query ?? {} : {}
82
+ })
83
+ },
84
+ {
85
+ name: 'patchSchema',
86
+ httpMethod: HttpMethod.Patch,
87
+ routeName: '/schema',
88
+ routePath: '/schema',
89
+ filename: PARITY_FIXTURE_FILENAME,
90
+ handler: createSchemaFixtureHandler()
91
+ }
92
+ ];
93
+ const createParityBffConfig = ()=>({
94
+ requestId: PARITY_REQUEST_ID,
95
+ crossProjectPolicy: {
96
+ enabled: true,
97
+ allowedNamespaces: [
98
+ PARITY_REQUEST_ID
99
+ ]
100
+ }
101
+ });
102
+ const getParityContracts = ()=>buildOperationContractMap({
103
+ handlers: createParityApiHandlerInfos(),
104
+ requestId: PARITY_REQUEST_ID
105
+ });
106
+ const envelopeHeader = (requestId)=>JSON.stringify(void 0 === requestId ? {} : {
107
+ requestId
108
+ });
109
+ const detailHeader = (details)=>JSON.stringify(details);
110
+ const deniedScenario = (name, reason, headers)=>({
111
+ name,
112
+ policy: true,
113
+ request: {
114
+ method: 'get',
115
+ path: '/hello',
116
+ headers
117
+ },
118
+ expected: {
119
+ kind: 'denied',
120
+ status: 403,
121
+ reason
122
+ }
123
+ });
124
+ const createAdapterParityScenarios = ()=>{
125
+ const contracts = getParityContracts();
126
+ const helloContract = contracts['GET:/hello'];
127
+ const validEnvelope = envelopeHeader(PARITY_PRODUCER_REQUEST_ID);
128
+ const validOperationId = `${PARITY_PRODUCER_REQUEST_ID}:parity`;
129
+ return [
130
+ {
131
+ name: 'plain handler returns object payload',
132
+ policy: false,
133
+ request: {
134
+ method: 'get',
135
+ path: '/hello'
136
+ },
137
+ expected: {
138
+ kind: 'payload',
139
+ status: 200,
140
+ payload: {
141
+ message: 'hello'
142
+ }
143
+ }
144
+ },
145
+ {
146
+ name: 'plain handler returns scalar payload',
147
+ policy: false,
148
+ request: {
149
+ method: 'post',
150
+ path: '/hello'
151
+ },
152
+ expected: {
153
+ kind: 'payload',
154
+ status: 200,
155
+ payload: 'hello'
156
+ }
157
+ },
158
+ {
159
+ name: 'plain handler returning undefined (pinned adapter drift)',
160
+ policy: false,
161
+ request: {
162
+ method: 'get',
163
+ path: '/nothing'
164
+ },
165
+ expected: {
166
+ kind: 'perAdapter',
167
+ expectations: {
168
+ express: {
169
+ status: 200,
170
+ payload: void 0
171
+ },
172
+ koa: {
173
+ status: 404,
174
+ payload: 'Not Found'
175
+ },
176
+ hono: {
177
+ status: 404,
178
+ payload: '404 Not Found'
179
+ }
180
+ }
181
+ }
182
+ },
183
+ {
184
+ name: 'plain handler receives data, query and cookies',
185
+ policy: false,
186
+ request: {
187
+ method: 'post',
188
+ path: '/echo?q=z',
189
+ headers: {
190
+ 'content-type': 'application/json',
191
+ cookie: 'id=666'
192
+ },
193
+ body: {
194
+ a: 1
195
+ }
196
+ },
197
+ expected: {
198
+ kind: 'payload',
199
+ status: 200,
200
+ payload: {
201
+ data: {
202
+ a: 1
203
+ },
204
+ query: {
205
+ q: 'z'
206
+ },
207
+ cookie: 'id=666'
208
+ }
209
+ }
210
+ },
211
+ {
212
+ name: 'plain handler receives positional route params',
213
+ policy: false,
214
+ request: {
215
+ method: 'get',
216
+ path: '/items/123?q=x'
217
+ },
218
+ expected: {
219
+ kind: 'payload',
220
+ status: 200,
221
+ payload: {
222
+ id: '123',
223
+ query: {
224
+ q: 'x'
225
+ }
226
+ }
227
+ }
228
+ },
229
+ {
230
+ name: 'schema handler success (pinned adapter drift)',
231
+ policy: false,
232
+ request: {
233
+ method: 'patch',
234
+ path: '/schema',
235
+ headers: {
236
+ 'content-type': 'application/json'
237
+ },
238
+ body: {
239
+ id: 777
240
+ }
241
+ },
242
+ expected: {
243
+ kind: 'perAdapter',
244
+ expectations: {
245
+ express: {
246
+ status: 200,
247
+ payload: {
248
+ id: 777
249
+ }
250
+ },
251
+ koa: {
252
+ status: 200,
253
+ payload: {
254
+ id: 777
255
+ }
256
+ },
257
+ hono: {
258
+ status: 200,
259
+ payload: {
260
+ type: 'HandleSuccess',
261
+ value: {
262
+ id: 777
263
+ }
264
+ }
265
+ }
266
+ }
267
+ }
268
+ },
269
+ {
270
+ name: 'schema handler input validation error (pinned adapter drift)',
271
+ policy: false,
272
+ request: {
273
+ method: 'patch',
274
+ path: '/schema',
275
+ headers: {
276
+ 'content-type': 'application/json'
277
+ },
278
+ body: {
279
+ id: 'aaa'
280
+ }
281
+ },
282
+ expected: {
283
+ kind: 'perAdapter',
284
+ expectations: {
285
+ express: {
286
+ status: 400,
287
+ payload: 'invalid input'
288
+ },
289
+ koa: {
290
+ status: 400,
291
+ payload: 'invalid input'
292
+ },
293
+ hono: {
294
+ status: 200,
295
+ payload: {
296
+ type: 'InputValidationError',
297
+ message: 'invalid input'
298
+ }
299
+ }
300
+ }
301
+ }
302
+ },
303
+ {
304
+ name: 'schema handler output validation error (pinned adapter drift)',
305
+ policy: false,
306
+ request: {
307
+ method: 'patch',
308
+ path: '/schema',
309
+ headers: {
310
+ 'content-type': 'application/json'
311
+ },
312
+ body: {
313
+ id: 'boom'
314
+ }
315
+ },
316
+ expected: {
317
+ kind: 'perAdapter',
318
+ expectations: {
319
+ express: {
320
+ status: 500,
321
+ payload: 'invalid output'
322
+ },
323
+ koa: {
324
+ status: 500,
325
+ payload: 'invalid output'
326
+ },
327
+ hono: {
328
+ status: 200,
329
+ payload: {
330
+ type: 'OutputValidationError',
331
+ message: 'invalid output'
332
+ }
333
+ }
334
+ }
335
+ }
336
+ },
337
+ {
338
+ name: 'policy pass with full operation context',
339
+ policy: true,
340
+ request: {
341
+ method: 'get',
342
+ path: '/hello',
343
+ headers: {
344
+ 'x-modernjs-bff-envelope': validEnvelope,
345
+ 'x-operation-id': validOperationId,
346
+ 'x-modernjs-bff-operation-context': detailHeader({
347
+ requestId: PARITY_PRODUCER_REQUEST_ID,
348
+ method: 'GET',
349
+ routePath: '/hello',
350
+ schemaHash: helloContract.schemaHash,
351
+ operationVersion: helloContract.operationVersion
352
+ })
353
+ }
354
+ },
355
+ expected: {
356
+ kind: 'payload',
357
+ status: 200,
358
+ payload: {
359
+ message: 'hello'
360
+ }
361
+ }
362
+ },
363
+ deniedScenario('policy denies missing envelope', 'missing_envelope', {}),
364
+ deniedScenario('policy denies invalid envelope', 'invalid_envelope', {
365
+ 'x-modernjs-bff-envelope': 'not-json'
366
+ }),
367
+ deniedScenario('policy denies envelope that is valid JSON but not an object', 'invalid_envelope', {
368
+ 'x-modernjs-bff-envelope': '123'
369
+ }),
370
+ deniedScenario('policy denies missing requestId', 'missing_request_id', {
371
+ 'x-modernjs-bff-envelope': envelopeHeader(void 0)
372
+ }),
373
+ deniedScenario('policy denies namespace outside allowlist', 'namespace_not_allowed', {
374
+ 'x-modernjs-bff-envelope': envelopeHeader('billing.producer-z')
375
+ }),
376
+ deniedScenario('policy denies missing operation context', 'missing_operation_context', {
377
+ 'x-modernjs-bff-envelope': validEnvelope
378
+ }),
379
+ deniedScenario('policy denies operation context mismatch', 'operation_context_mismatch', {
380
+ 'x-modernjs-bff-envelope': validEnvelope,
381
+ 'x-operation-id': 'someone-else:parity'
382
+ }),
383
+ deniedScenario('policy denies missing operation context details', 'missing_operation_context_details', {
384
+ 'x-modernjs-bff-envelope': validEnvelope,
385
+ 'x-operation-id': validOperationId
386
+ }),
387
+ deniedScenario('policy denies JSON-array operation context details', 'invalid_operation_context_details', {
388
+ 'x-modernjs-bff-envelope': validEnvelope,
389
+ 'x-operation-id': validOperationId,
390
+ 'x-modernjs-bff-operation-context': '[]'
391
+ }),
392
+ deniedScenario('policy denies invalid operation context details', 'invalid_operation_context_details', {
393
+ 'x-modernjs-bff-envelope': validEnvelope,
394
+ 'x-operation-id': validOperationId,
395
+ 'x-modernjs-bff-operation-context': 'not-json'
396
+ }),
397
+ deniedScenario('policy denies detail requestId mismatch', 'operation_context_details_request_id_mismatch', {
398
+ 'x-modernjs-bff-envelope': validEnvelope,
399
+ 'x-operation-id': validOperationId,
400
+ 'x-modernjs-bff-operation-context': detailHeader({
401
+ requestId: 'crm.producer-b',
402
+ method: 'GET',
403
+ routePath: '/hello',
404
+ schemaHash: helloContract.schemaHash,
405
+ operationVersion: helloContract.operationVersion
406
+ })
407
+ }),
408
+ deniedScenario('policy denies missing operation schema hash', 'missing_operation_schema_hash', {
409
+ 'x-modernjs-bff-envelope': validEnvelope,
410
+ 'x-operation-id': validOperationId,
411
+ 'x-modernjs-bff-operation-context': detailHeader({
412
+ requestId: PARITY_PRODUCER_REQUEST_ID,
413
+ method: 'GET',
414
+ routePath: '/hello',
415
+ operationVersion: helloContract.operationVersion
416
+ })
417
+ }),
418
+ deniedScenario('policy denies missing operation version', 'missing_operation_version', {
419
+ 'x-modernjs-bff-envelope': validEnvelope,
420
+ 'x-operation-id': validOperationId,
421
+ 'x-modernjs-bff-operation-context': detailHeader({
422
+ requestId: PARITY_PRODUCER_REQUEST_ID,
423
+ method: 'GET',
424
+ routePath: '/hello',
425
+ schemaHash: helloContract.schemaHash
426
+ })
427
+ }),
428
+ deniedScenario('policy denies unknown operation contract', 'unknown_operation_contract', {
429
+ 'x-modernjs-bff-envelope': validEnvelope,
430
+ 'x-operation-id': validOperationId,
431
+ 'x-modernjs-bff-operation-context': detailHeader({
432
+ requestId: PARITY_PRODUCER_REQUEST_ID,
433
+ method: 'GET',
434
+ routePath: '/does-not-exist',
435
+ schemaHash: helloContract.schemaHash,
436
+ operationVersion: helloContract.operationVersion
437
+ })
438
+ }),
439
+ deniedScenario('policy denies operation schema hash mismatch', 'operation_schema_hash_mismatch', {
440
+ 'x-modernjs-bff-envelope': validEnvelope,
441
+ 'x-operation-id': validOperationId,
442
+ 'x-modernjs-bff-operation-context': detailHeader({
443
+ requestId: PARITY_PRODUCER_REQUEST_ID,
444
+ method: 'GET',
445
+ routePath: '/hello',
446
+ schemaHash: 'deadbeef',
447
+ operationVersion: helloContract.operationVersion
448
+ })
449
+ }),
450
+ deniedScenario('policy denies operation version mismatch', 'operation_version_mismatch', {
451
+ 'x-modernjs-bff-envelope': validEnvelope,
452
+ 'x-operation-id': validOperationId,
453
+ 'x-modernjs-bff-operation-context': detailHeader({
454
+ requestId: PARITY_PRODUCER_REQUEST_ID,
455
+ method: 'GET',
456
+ routePath: '/hello',
457
+ schemaHash: helloContract.schemaHash,
458
+ operationVersion: helloContract.operationVersion + 1
459
+ })
460
+ })
461
+ ];
462
+ };
463
+ const toParityResult = (res)=>({
464
+ status: res.status,
465
+ payload: res.type.includes('json') ? res.body : '' === res.text ? void 0 : res.text
466
+ });
467
+ const formatValue = (value)=>JSON.stringify(value);
468
+ const assertParityResult = (scenario, res, adapter)=>{
469
+ const result = toParityResult(res);
470
+ const failures = [];
471
+ let { expected } = scenario;
472
+ if ('perAdapter' === expected.kind) {
473
+ if (void 0 === adapter) throw new Error(`Adapter parity scenario "${scenario.name}" pins per-adapter drift; pass the adapter id to assertParityResult.`);
474
+ expected = {
475
+ kind: 'payload',
476
+ ...expected.expectations[adapter]
477
+ };
478
+ }
479
+ if (result.status !== expected.status) failures.push(`status: expected ${expected.status}, received ${result.status}`);
480
+ if ('payload' === expected.kind) {
481
+ if (formatValue(result.payload) !== formatValue(expected.payload)) failures.push(`payload: expected ${formatValue(expected.payload)}, received ${formatValue(result.payload)}`);
482
+ } else {
483
+ const payload = isRecord(result.payload) ? result.payload : {};
484
+ if ('BFF_CROSS_PROJECT_POLICY_DENIED' !== payload.code) failures.push(`denial code: expected "BFF_CROSS_PROJECT_POLICY_DENIED", received ${formatValue(payload.code)}`);
485
+ if (payload.reason !== expected.reason) failures.push(`denial reason: expected "${expected.reason}", received ${formatValue(payload.reason)}`);
486
+ if ('string' != typeof payload.message || 0 === payload.message.length) failures.push(`denial message: expected non-empty string, received ${formatValue(payload.message)}`);
487
+ }
488
+ if (failures.length > 0) throw new Error(`Adapter parity scenario "${scenario.name}" failed:\n- ${failures.join('\n- ')}`);
489
+ };
490
+ export { PARITY_PRODUCER_REQUEST_ID, PARITY_REQUEST_ID, assertParityResult, createAdapterParityScenarios, createParityApiHandlerInfos, createParityBffConfig, toParityResult };
@@ -1,7 +1,19 @@
1
- import "path";
2
1
  import { ApiRouter } from "../router/index.mjs";
3
- import { createOperationEntries, createOperationSchemaHash } from "../security/operationContracts.mjs";
2
+ import { buildOperationContractMap, createOperationSchemaHash, deriveOperationVersion } from "../security/operationContracts.mjs";
4
3
  import { Err, Ok } from "./result.mjs";
4
+ import * as __rspack_external_path from "path";
5
+ const getPackageInfo = (appDir)=>{
6
+ try {
7
+ const packageJsonPath = __rspack_external_path.resolve(appDir, './package.json');
8
+ const packageJson = require(packageJsonPath);
9
+ return {
10
+ name: packageJson.name,
11
+ version: packageJson.version
12
+ };
13
+ } catch (error) {
14
+ return {};
15
+ }
16
+ };
5
17
  const INNER_CLIENT_REQUEST_CREATOR = '@modern-js/plugin-bff/client';
6
18
  const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix, port, target, requestCreator, fetcher, requireResolve = require.resolve, httpMethodDecider, domain, requestId })=>{
7
19
  requestCreator = requestCreator || INNER_CLIENT_REQUEST_CREATOR;
@@ -14,25 +26,65 @@ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix,
14
26
  });
15
27
  const handlerInfos = await apiRouter.getSingleModuleHandlers(resourcePath);
16
28
  if (!handlerInfos) return Err(`generate client error: Cannot require module ${resourcePath}`);
17
- const operationEntries = createOperationEntries(handlerInfos);
18
- const operationVersion = 1;
19
- const schemaHash = createOperationSchemaHash(operationEntries, requestId || 'default');
29
+ const normalizedRequestId = requestId || 'default';
30
+ const operationVersion = deriveOperationVersion(getPackageInfo(appDir).version);
31
+ const operationContracts = buildOperationContractMap({
32
+ handlers: handlerInfos,
33
+ requestId: normalizedRequestId,
34
+ operationVersion
35
+ });
36
+ const operationEntries = handlerInfos.map((handlerInfo)=>{
37
+ const upperHttpMethod = handlerInfo.httpMethod.toUpperCase();
38
+ const contract = operationContracts[`${upperHttpMethod}:${handlerInfo.routePath}`];
39
+ return {
40
+ name: handlerInfo.name,
41
+ httpMethod: upperHttpMethod,
42
+ routePath: handlerInfo.routePath,
43
+ schemaHash: contract?.schemaHash ?? ''
44
+ };
45
+ }).sort((a, b)=>{
46
+ const keyA = `${a.routePath}:${a.httpMethod}:${a.name}`;
47
+ const keyB = `${b.routePath}:${b.httpMethod}:${b.name}`;
48
+ return keyA.localeCompare(keyB);
49
+ });
50
+ const schemaHash = createOperationSchemaHash(operationEntries, normalizedRequestId);
51
+ let hasUploadHandler = false;
20
52
  let handlersCode = '';
21
53
  for (const handlerInfo of handlerInfos){
22
54
  const { name, httpMethod, routePath, action } = handlerInfo;
23
55
  let exportStatement = `var ${name} =`;
24
56
  if ('default' === name.toLowerCase()) exportStatement = 'default';
25
57
  const upperHttpMethod = httpMethod.toUpperCase();
26
- const serializedRouteName = JSON.stringify(routePath);
27
- const serializedMethod = JSON.stringify(upperHttpMethod);
28
- const serializedMethodDecider = JSON.stringify(httpMethodDecider ? httpMethodDecider : 'functionName');
29
- const serializedOperationContext = JSON.stringify({
58
+ const operationSchemaHash = operationContracts[`${upperHttpMethod}:${routePath}`]?.schemaHash ?? '';
59
+ const operationContext = {
30
60
  operationId: name,
31
61
  routePath,
32
62
  method: upperHttpMethod,
33
- schemaHash,
63
+ schemaHash: operationSchemaHash,
34
64
  operationVersion
35
- });
65
+ };
66
+ if ('upload' === action) {
67
+ hasUploadHandler = true;
68
+ const uploadOptions = {
69
+ path: routePath,
70
+ ...domain ? {
71
+ domain
72
+ } : {},
73
+ ...requestId ? {
74
+ requestId
75
+ } : {},
76
+ ...requestId ? {
77
+ operationContext
78
+ } : {}
79
+ };
80
+ handlersCode += `export ${exportStatement} createUploader(${JSON.stringify(uploadOptions)});
81
+ `;
82
+ continue;
83
+ }
84
+ const serializedRouteName = JSON.stringify(routePath);
85
+ const serializedMethod = JSON.stringify(upperHttpMethod);
86
+ const serializedMethodDecider = JSON.stringify(httpMethodDecider ? httpMethodDecider : 'functionName');
87
+ const serializedOperationContext = JSON.stringify(operationContext);
36
88
  const tailArgs = `, ${fetcher ? 'fetch' : 'undefined'}, ${requestId ? JSON.stringify(requestId) : 'undefined'}, ${serializedOperationContext}`;
37
89
  if ('server' === target) handlersCode += `export ${exportStatement} createRequest(${serializedRouteName}, ${serializedMethod}, process.env.PORT || ${String(port)}, ${serializedMethodDecider}${tailArgs});
38
90
  `;
@@ -41,9 +93,10 @@ const generateClient = async ({ appDir, resourcePath, apiDir, lambdaDir, prefix,
41
93
  }
42
94
  const serializedRequestCreator = JSON.stringify(requestCreator);
43
95
  const serializedFetcher = fetcher ? JSON.stringify(fetcher) : void 0;
96
+ const namedRequestImports = `createRequest${hasUploadHandler ? ', createUploader' : ''}`;
44
97
  const importCode = requestId ? `import * as requestRuntime from ${serializedRequestCreator};
45
- const { createRequest } = requestRuntime;
46
- ${serializedFetcher ? `import { fetch } from ${serializedFetcher};\n` : ''}` : `import { createRequest } from ${serializedRequestCreator};
98
+ const { ${namedRequestImports} } = requestRuntime;
99
+ ${serializedFetcher ? `import { fetch } from ${serializedFetcher};\n` : ''}` : `import { ${namedRequestImports} } from ${serializedRequestCreator};
47
100
  ${serializedFetcher ? `import { fetch } from ${serializedFetcher};\n` : ''}`;
48
101
  const bootstrapCode = requestId ? `export const initProducerClient = (options = {}) => {
49
102
  const configure = requestRuntime.configure;
@@ -1,9 +1,11 @@
1
+ export * from "./adapter-kit/index.mjs";
1
2
  export * from "./client/index.mjs";
2
3
  export * from "./contracts/eventContracts.mjs";
3
4
  export * from "./operators/http.mjs";
4
5
  export * from "./router/index.mjs";
5
6
  export * from "./security/crossProjectPolicy.mjs";
6
7
  export * from "./security/operationContracts.mjs";
8
+ export * from "./security/resolveCrossProjectPolicy.mjs";
7
9
  export * from "./types.mjs";
8
10
  export { Api } from "./api.mjs";
9
11
  export { HttpError, ValidationError } from "./errors/http.mjs";