@geek-fun/serverlessinsight 0.7.9 → 0.8.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geek-fun/serverlessinsight",
3
- "version": "0.7.9",
3
+ "version": "0.8.0",
4
4
  "description": "Full life cycle cross providers serverless application management for your fast-growing business.",
5
5
  "homepage": "https://serverlessinsight.geekfun.club",
6
6
  "main": "dist/src/index.js",
@@ -626,6 +626,25 @@ const createApigwOperations = (apigwClient, dnsClient, _context) => {
626
626
  });
627
627
  await apigwClient.modifyApi(request);
628
628
  },
629
+ /**
630
+ * List all APIs in a group
631
+ */
632
+ listApisByGroup: async (groupId) => {
633
+ const request = new cloudapi.DescribeApisRequest({
634
+ groupId,
635
+ pageSize: 100,
636
+ pageNumber: 1,
637
+ });
638
+ const response = await apigwClient.describeApis(request);
639
+ const apiSummaries = response.body?.apiSummarys?.apiSummary ?? [];
640
+ const results = [];
641
+ for (const api of apiSummaries) {
642
+ if (api.apiId && api.apiName) {
643
+ results.push({ apiId: api.apiId, apiName: api.apiName });
644
+ }
645
+ }
646
+ return results;
647
+ },
629
648
  /**
630
649
  * Delete API
631
650
  */
@@ -444,15 +444,93 @@ const updateApigwResource = async (context, event, serviceName, roleArn, state)
444
444
  const logicalId = `events.${event.key}`;
445
445
  const existingState = (0, stateManager_1.getResource)(state, logicalId);
446
446
  const client = (0, aliyunClient_1.createAliyunClient)(context);
447
- if (!existingState) {
447
+ let existingInstances;
448
+ let groupId;
449
+ // Precompute expected API names from triggers to safely filter cloud APIs
450
+ const expectedApiNames = new Set(event.triggers.map((trigger) => {
451
+ const apiConfig = (0, apigwTypes_1.triggerToApigwApiConfig)(event, trigger, 'placeholder', serviceName, context.region, context.stage, roleArn);
452
+ return apiConfig.apiName;
453
+ }));
454
+ const buildCloudInstance = (cloudGroup, cloudApis) => {
455
+ const gId = cloudGroup.groupId;
456
+ return {
457
+ groupId: gId,
458
+ instances: [
459
+ buildApigwGroupInstanceFromProvider(cloudGroup, context.stage),
460
+ // Only include cloud APIs whose name matches our expected set.
461
+ // This prevents accidentally managing or deleting APIs created
462
+ // by other processes within the same group.
463
+ ...cloudApis
464
+ .filter((api) => expectedApiNames.has(api.apiName))
465
+ .map((api) => ({
466
+ type: 'ALIYUN_APIGW_API',
467
+ sid: (0, common_1.buildSid)('aliyun', 'apigateway', context.stage, `${gId}/${api.apiId}`),
468
+ id: api.apiId,
469
+ apiId: api.apiId,
470
+ apiName: api.apiName,
471
+ groupId: gId,
472
+ })),
473
+ ],
474
+ };
475
+ };
476
+ // Shared helper: try to find the group from cloud. On any error (or not found),
477
+ // fall back to full creation since there's nothing to update.
478
+ const findCloudGroupOrCreate = async () => {
448
479
  return (0, exports.createApigwResource)(context, event, serviceName, roleArn, state);
480
+ };
481
+ if (existingState) {
482
+ existingInstances = existingState.instances;
483
+ const groupInstance = existingInstances.find((i) => i.type === 'ALIYUN_APIGW_GROUP');
484
+ if (groupInstance) {
485
+ groupId = groupInstance.id;
486
+ }
487
+ else {
488
+ const groupConfig = (0, apigwTypes_1.eventToApigwGroupConfig)(event, serviceName, context.stage);
489
+ try {
490
+ const cloudGroup = await client.apigw.findApiGroupByName(groupConfig.groupName);
491
+ if (!cloudGroup?.groupId) {
492
+ return findCloudGroupOrCreate();
493
+ }
494
+ const cloudApis = await client.apigw.listApisByGroup(cloudGroup.groupId);
495
+ const built = buildCloudInstance(cloudGroup, cloudApis);
496
+ groupId = built.groupId;
497
+ existingInstances = built.instances;
498
+ state = (0, stateManager_1.setResource)(state, logicalId, {
499
+ mode: 'managed',
500
+ region: context.region,
501
+ definition: {},
502
+ instances: existingInstances,
503
+ lastUpdated: new Date().toISOString(),
504
+ });
505
+ }
506
+ catch {
507
+ return findCloudGroupOrCreate();
508
+ }
509
+ }
449
510
  }
450
- const existingInstances = existingState.instances;
451
- const groupInstance = existingInstances.find((i) => i.type === 'ALIYUN_APIGW_GROUP');
452
- if (!groupInstance) {
453
- return (0, exports.createApigwResource)(context, event, serviceName, roleArn, state);
511
+ else {
512
+ const groupConfig = (0, apigwTypes_1.eventToApigwGroupConfig)(event, serviceName, context.stage);
513
+ try {
514
+ const cloudGroup = await client.apigw.findApiGroupByName(groupConfig.groupName);
515
+ if (!cloudGroup?.groupId) {
516
+ return findCloudGroupOrCreate();
517
+ }
518
+ const cloudApis = await client.apigw.listApisByGroup(cloudGroup.groupId);
519
+ const built = buildCloudInstance(cloudGroup, cloudApis);
520
+ groupId = built.groupId;
521
+ existingInstances = built.instances;
522
+ state = (0, stateManager_1.setResource)(state, logicalId, {
523
+ mode: 'managed',
524
+ region: context.region,
525
+ definition: {},
526
+ instances: existingInstances,
527
+ lastUpdated: new Date().toISOString(),
528
+ });
529
+ }
530
+ catch {
531
+ return findCloudGroupOrCreate();
532
+ }
454
533
  }
455
- const groupId = groupInstance.id;
456
534
  const groupConfig = (0, apigwTypes_1.eventToApigwGroupConfig)(event, serviceName, context.stage);
457
535
  await client.apigw.updateApiGroup(groupId, groupConfig);
458
536
  const groupInfo = await client.apigw.getApiGroup(groupId);
@@ -515,7 +593,7 @@ const updateApigwResource = async (context, event, serviceName, roleArn, state)
515
593
  const wwwBindApex = event.domain.www_bind_apex === true;
516
594
  const wwwDomain = wwwBindApex ? (0, domainUtils_1.deriveWwwDomain)(primaryDomain) : null;
517
595
  const isCdnEnabled = getIsCdnEnabled(event);
518
- const existingDomain = existingState.definition?.domain;
596
+ const existingDomain = existingState?.definition?.domain;
519
597
  const previousWwwBindApex = existingDomain?.wwwBindApex === true;
520
598
  const previousDomainName = existingDomain?.domainName;
521
599
  const previousCdnEnabled = existingDomain?.cdnEnabled === true;
@@ -585,7 +663,7 @@ const updateApigwResource = async (context, event, serviceName, roleArn, state)
585
663
  }
586
664
  }
587
665
  else {
588
- const existingDomain = existingState.definition?.domain;
666
+ const existingDomain = existingState?.definition?.domain;
589
667
  if (existingDomain?.domainName) {
590
668
  const previousDomain = existingDomain.domainName;
591
669
  const previousCdnEnabled = existingDomain.cdnEnabled === true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geek-fun/serverlessinsight",
3
- "version": "0.7.9",
3
+ "version": "0.8.0",
4
4
  "description": "Full life cycle cross providers serverless application management for your fast-growing business.",
5
5
  "homepage": "https://serverlessinsight.geekfun.club",
6
6
  "main": "dist/src/index.js",