Skip to content

Commit d0c4d48

Browse files
committed
fix: fix apigw types
1 parent b0a2ab1 commit d0c4d48

File tree

3 files changed

+121
-60
lines changed

3 files changed

+121
-60
lines changed

__tests__/apigw.test.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import { ApigwDeployInputs, ApigwDeployOutputs } from './../src/modules/apigw/interface';
12
import { Apigw } from '../src';
23

3-
const deepClone = (obj) => {
4+
function deepClone<T>(obj: T): T {
45
return JSON.parse(JSON.stringify(obj));
5-
};
6+
}
67

78
describe('apigw', () => {
89
const credentials = {
910
SecretId: process.env.TENCENT_SECRET_ID,
1011
SecretKey: process.env.TENCENT_SECRET_KEY,
1112
};
12-
const inputs = {
13+
const inputs: ApigwDeployInputs = {
1314
protocols: ['http', 'https'],
1415
serviceName: 'serverless_test',
1516
environment: 'release',
@@ -145,7 +146,7 @@ describe('apigw', () => {
145146
],
146147
};
147148
const apigw = new Apigw(credentials, process.env.REGION);
148-
let outputs;
149+
let outputs: ApigwDeployOutputs;
149150

150151
test('[Environment UsagePlan] should deploy a apigw success', async () => {
151152
const apigwInputs = deepClone(inputs);
@@ -377,4 +378,42 @@ describe('apigw', () => {
377378

378379
expect(detail).toBeNull();
379380
});
381+
382+
test.only('[Apigw] Bind CustomDomain success', async () => {
383+
const apigwInputs = deepClone(inputs);
384+
apigwInputs.customDomains = [
385+
{
386+
domain: 'test-1.sls.plus',
387+
// certificateId: 'cWOJJjax',
388+
isDefaultMapping: false,
389+
pathMappingSet: [
390+
{
391+
path: '/',
392+
environment: 'release',
393+
},
394+
],
395+
protocols: ['http'],
396+
isForcedHttps: true,
397+
},
398+
{
399+
domain: 'test-2.sls.plus',
400+
// certificateId: 'cWOJJjax',
401+
isDefaultMapping: false,
402+
pathMappingSet: [
403+
{
404+
path: '/',
405+
environment: 'release',
406+
},
407+
],
408+
protocols: ['http'],
409+
isForcedHttps: true,
410+
},
411+
];
412+
const deployOutputs = await apigw.deploy(inputs);
413+
414+
const deployOutputsAgain = await apigw.deploy(inputs);
415+
416+
console.log({ deployOutputs, deployOutputsAgain });
417+
await apigw.remove(deployOutputs);
418+
});
380419
});

src/modules/apigw/index.ts

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ export default class Apigw {
485485

486486
const unboundSecretIds = await this.getUnboundSecretIds({
487487
usagePlanId: usagePlan.usagePlanId,
488-
secretIds: secrets.secretIds,
488+
secretIds: secrets.secretIds!,
489489
});
490490

491491
if (unboundSecretIds.length > 0) {
@@ -654,7 +654,7 @@ export default class Apigw {
654654
path,
655655
method,
656656
}: {
657-
serviceId: string;
657+
serviceId?: string;
658658
path: string;
659659
method: string;
660660
}) {
@@ -704,42 +704,47 @@ export default class Apigw {
704704

705705
async createOrUpdateApi({ serviceId, endpoint, environment, created }: CreateOrUpdateApiInputs) {
706706
// compatibility for secret auth config depends on auth & usagePlan
707-
const authType = endpoint.auth ? 'SECRET' : endpoint.authType || 'NONE';
708-
const businessType = endpoint.businessType || 'NORMAL';
707+
const authType = endpoint?.auth ? 'SECRET' : endpoint?.authType ?? 'NONE';
708+
const businessType = endpoint?.businessType ?? 'NORMAL';
709709
const output: ApiDeployerOutputs = {
710-
path: endpoint.path,
711-
method: endpoint.method,
712-
apiName: endpoint.apiName || 'index',
710+
path: endpoint?.path,
711+
method: endpoint?.method,
712+
apiName: endpoint?.apiName || 'index',
713713
created: true,
714714
authType: authType,
715715
businessType: businessType,
716-
isBase64Encoded: endpoint.isBase64Encoded === true,
716+
isBase64Encoded: endpoint?.isBase64Encoded === true,
717717
};
718-
if (endpoint.authRelationApiId) {
718+
if (endpoint?.authRelationApiId) {
719719
output.authRelationApiId = endpoint.authRelationApiId;
720720
}
721721

722722
const apiInputs = {
723-
protocol: endpoint.protocol || 'HTTP',
723+
protocol: endpoint?.protocol ?? 'HTTP',
724724
serviceId: serviceId,
725-
apiName: endpoint.apiName || 'index',
726-
apiDesc: endpoint.description,
725+
apiName: endpoint?.apiName ?? 'index',
726+
apiDesc: endpoint?.description,
727727
apiType: 'NORMAL',
728728
authType: authType,
729-
apiBusinessType: endpoint.businessType || 'NORMAL',
730-
serviceType: endpoint.serviceType || 'SCF',
729+
apiBusinessType: endpoint?.businessType ?? 'NORMAL',
730+
serviceType: endpoint?.serviceType ?? 'SCF',
731731
requestConfig: {
732-
path: endpoint.path,
733-
method: endpoint.method,
732+
path: endpoint?.path,
733+
method: endpoint?.method,
734734
},
735-
serviceTimeout: endpoint.serviceTimeout || 15,
736-
responseType: endpoint.responseType || 'HTML',
737-
enableCORS: endpoint.enableCORS === true,
738-
isBase64Encoded: endpoint.isBase64Encoded === true,
735+
serviceTimeout: endpoint?.serviceTimeout ?? 15,
736+
responseType: endpoint?.responseType ?? 'HTML',
737+
enableCORS: endpoint?.enableCORS === true,
738+
isBase64Encoded: endpoint?.isBase64Encoded === true,
739739
isBase64Trigger: undefined as undefined | boolean,
740-
base64EncodedTriggerRules: undefined as undefined | string[],
741-
oauthConfig: endpoint.oauthConfig,
742-
authRelationApiId: endpoint.authRelationApiId,
740+
base64EncodedTriggerRules: undefined as
741+
| undefined
742+
| {
743+
name: string;
744+
value: string[];
745+
}[],
746+
oauthConfig: endpoint?.oauthConfig,
747+
authRelationApiId: endpoint?.authRelationApiId,
743748
};
744749

745750
this.marshalApiInput(endpoint, apiInputs);
@@ -749,20 +754,20 @@ export default class Apigw {
749754
InternalDomain?: string;
750755
};
751756

752-
if (endpoint.apiId) {
753-
apiDetail = await this.getApiById({ serviceId, apiId: endpoint.apiId });
757+
if (endpoint?.apiId) {
758+
apiDetail = await this.getApiById({ serviceId: serviceId!, apiId: endpoint.apiId });
754759
}
755760

756761
if (!apiDetail!) {
757762
apiDetail = await this.getApiByPathAndMethod({
758-
serviceId,
759-
path: endpoint.path,
760-
method: endpoint.method,
763+
serviceId: serviceId!,
764+
path: endpoint?.path!,
765+
method: endpoint?.method!,
761766
});
762767
}
763768

764-
if (apiDetail) {
765-
console.log(`Api method ${endpoint.method}, path ${endpoint.path} already exist`);
769+
if (apiDetail && endpoint) {
770+
console.log(`Api method ${endpoint?.method}, path ${endpoint?.path} already exist`);
766771
endpoint.apiId = apiDetail.ApiId;
767772

768773
if (endpoint.isBase64Encoded && endpoint.isBase64Trigger) {
@@ -796,7 +801,7 @@ export default class Apigw {
796801
});
797802
output.internalDomain = apiDetail.InternalDomain || '';
798803

799-
if (endpoint.isBase64Encoded && endpoint.isBase64Trigger) {
804+
if (endpoint?.isBase64Encoded && endpoint.isBase64Trigger) {
800805
apiInputs.isBase64Trigger = endpoint.isBase64Trigger;
801806
apiInputs.base64EncodedTriggerRules = endpoint.base64EncodedTriggerRules;
802807
}
@@ -810,7 +815,7 @@ export default class Apigw {
810815

811816
output.apiName = apiInputs.apiName;
812817

813-
if (endpoint.usagePlan) {
818+
if (endpoint?.usagePlan) {
814819
const usagePlan = await this.bindUsagePlan({
815820
apiId: output.apiId,
816821
serviceId,
@@ -837,7 +842,7 @@ export default class Apigw {
837842
// if exist in state list, set created to be true
838843
const [exist] = oldList.filter(
839844
(item) =>
840-
item.method.toLowerCase() === apiConfig.method.toLowerCase() &&
845+
item?.method?.toLowerCase() === apiConfig?.method?.toLowerCase() &&
841846
item.path === apiConfig.path,
842847
);
843848

@@ -855,7 +860,7 @@ export default class Apigw {
855860
if (authRelationApi) {
856861
const [relativeApi] = apiList.filter(
857862
(item) =>
858-
item.method.toLowerCase() === authRelationApi.method.toLowerCase() &&
863+
item.method?.toLowerCase() === authRelationApi.method.toLowerCase() &&
859864
item.path === authRelationApi.path,
860865
);
861866
if (relativeApi) {

src/modules/apigw/interface.ts

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ export interface ApigwSetupUsagePlanInputs {
1515

1616
created?: boolean;
1717

18-
secrets?: { secretIds: string[]; created: boolean };
18+
secrets?: { secretIds?: string[]; created: boolean };
1919
}
2020

2121
export interface ApigwSetupUsagePlanOutputs extends ApigwSetupUsagePlanInputs {}
2222

2323
export interface ApigwSetupUsagePlanSecretInputs {
2424
/** 要使用的密钥 id 列表 */
25-
secretIds: string[];
25+
secretIds?: string[];
2626
/** 用户自定义的密钥名 */
27-
secretName: string;
27+
secretName?: string;
2828
created?: boolean;
2929
}
3030
export interface ApigwBindUsagePlanInputs {
@@ -38,42 +38,59 @@ export interface ApigwBindUsagePlanInputs {
3838
export interface ApigwBindUsagePlanOutputs extends ApigwBindUsagePlanInputs {}
3939

4040
export interface ApiEndpoint {
41-
created: boolean;
41+
created?: boolean;
4242
apiId?: string;
4343
usagePlan?: ApigwSetupUsagePlanInputs;
4444
auth?: ApigwSetupUsagePlanSecretInputs;
4545
authType?: 'NONE' | string;
4646
businessType?: 'NORMAL' | string;
47-
path: string;
48-
method: string;
47+
path?: string;
48+
method?: string;
4949
apiName?: string;
50-
protocol?: 'HTTP' | 'HTTPS';
50+
protocol?: 'HTTP' | 'HTTPS' | 'WEBSOCKET';
5151
description?: string;
5252
serviceType?: 'SCF' | string;
5353
serviceTimeout?: 15;
5454
responseType?: 'HTML' | string;
5555
enableCORS?: boolean;
56-
oauthConfig?: string;
5756
authRelationApiId?: string;
5857
authRelationApi?: {
5958
method: string;
6059
path: string;
6160
};
61+
function?: {
62+
functionName?: string;
63+
functionNamespace?: string;
64+
functionQualifier?: string;
65+
transportFunctionName?: string;
66+
registerFunctionName?: string;
67+
};
6268
internalDomain?: string;
6369
isBase64Encoded?: boolean;
6470
isBase64Trigger?: boolean;
65-
base64EncodedTriggerRules?: string[];
71+
base64EncodedTriggerRules?: { name: string; value: string[] }[];
72+
serviceMockReturnMessage?: string;
73+
serviceConfig?: {
74+
url?: string;
75+
path?: string;
76+
method?: string;
77+
};
78+
oauthConfig?: {
79+
loginRedirectUrl: string;
80+
publicKey: string;
81+
tokenLocation: string;
82+
};
6683
}
6784

6885
export interface ApigwBindCustomDomainInputs {
6986
customDomains?: {
7087
domain: string;
71-
protocols: ('http' | 'https')[];
88+
protocols?: ('http' | 'https')[];
7289

73-
certificateId: string;
90+
certificateId?: string;
7491
isDefaultMapping?: boolean;
75-
pathMappingSet: [];
76-
netType: string;
92+
pathMappingSet: { path: string; environment: string }[];
93+
netType?: string;
7794

7895
isForcedHttps: boolean;
7996

@@ -90,7 +107,7 @@ export interface ApigwCreateOrUpdateServiceInputs {
90107
netTypes?: string[];
91108
serviceName?: string;
92109
serviceDesc?: string;
93-
serviceId: string;
110+
serviceId?: string;
94111

95112
usagePlan?: ApigwSetupUsagePlanInputs;
96113
auth?: ApigwSetupUsagePlanSecretInputs;
@@ -99,10 +116,10 @@ export interface ApigwCreateOrUpdateServiceInputs {
99116
export type ApiDeployerOutputs = ApiEndpoint;
100117

101118
export interface CreateOrUpdateApiInputs {
102-
serviceId: string;
103-
endpoint: ApiEndpoint;
104-
environment: EnviromentType;
105-
created: boolean;
119+
serviceId?: string;
120+
endpoint?: ApiEndpoint;
121+
environment?: EnviromentType;
122+
created?: boolean;
106123
}
107124

108125
export interface ApiDeployerInputs {
@@ -117,11 +134,11 @@ export interface ApiDeployerInputs {
117134
export interface ApigwDeployInputs
118135
extends ApigwCreateOrUpdateServiceInputs,
119136
ApigwBindCustomDomainInputs {
120-
region: RegionType;
121-
oldState: any;
137+
region?: RegionType;
138+
oldState?: any;
122139
environment?: EnviromentType;
123140

124-
endpoints: ApiEndpoint[];
141+
endpoints?: ApiEndpoint[];
125142
}
126143

127144
export interface ApigwBindCustomDomainOutputs {
@@ -164,7 +181,7 @@ export interface ApigwApiRemoverInputs {
164181
}
165182

166183
export interface ApigwRemoveInputs {
167-
created: boolean;
184+
created?: boolean;
168185
environment: EnviromentType;
169186
serviceId: string;
170187
apiList: ApiEndpoint[];

0 commit comments

Comments
 (0)