Skip to content

Commit 8769226

Browse files
committed
fix(faas): get alias bug
1 parent 155ec7f commit 8769226

File tree

6 files changed

+288
-141
lines changed

6 files changed

+288
-141
lines changed

packages/capi/src/factory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function ApiFactory<ACTIONS_T extends readonly string[]>({
132132
return customHandler(action, res);
133133
}
134134
const { Response } = res;
135-
if (Response && Response.Error && Response.Error.Code) {
135+
if (Response?.Error?.Code) {
136136
if (errorHandler) {
137137
return errorHandler(action, Response);
138138
}

packages/faas/__tests__/index.test.ts

+167-129
Original file line numberDiff line numberDiff line change
@@ -20,168 +20,206 @@ describe('FaaS', () => {
2020
region,
2121
});
2222

23-
test('getRegion', async () => {
24-
const region = faas.getRegion();
23+
describe('base methods', () => {
24+
test('getRegion', async () => {
25+
const region = faas.getRegion();
2526

26-
expect(region).toBe(region);
27-
});
28-
29-
test('setRegion', async () => {
30-
faas.setRegion('ap-shanghai');
31-
32-
expect(faas.getRegion()).toBe('ap-shanghai');
27+
expect(region).toBe(region);
28+
});
3329

34-
// 还原为 ap-guangzhou
35-
faas.setRegion(region);
36-
});
30+
test('setRegion', async () => {
31+
faas.setRegion('ap-shanghai');
3732

38-
test('getNamespaces', async () => {
39-
const res = await faas.getNamespaces();
40-
expect(Array.isArray(res)).toBe(true);
41-
});
33+
expect(faas.getRegion()).toBe('ap-shanghai');
4234

43-
test('getVersions', async () => {
44-
const res = await faas.getVersions({
45-
...faasConfig,
35+
// 还原为 ap-guangzhou
36+
faas.setRegion(region);
4637
});
4738

48-
expect(Array.isArray(res)).toBe(true);
49-
});
50-
51-
test('invoke', async () => {
52-
const res = (await faas.invoke({
53-
...faasConfig,
54-
})) as InvokeResult;
55-
56-
expect(res).toEqual({
57-
billDuration: expect.any(Number),
58-
duration: expect.any(Number),
59-
errMsg: expect.any(String),
60-
memUsage: expect.any(Number),
61-
functionRequestId: expect.any(String),
62-
invokeResult: expect.any(Number),
63-
log: expect.any(String),
64-
retMsg: expect.any(String),
39+
test('getNamespaces', async () => {
40+
const res = await faas.getNamespaces();
41+
expect(Array.isArray(res)).toBe(true);
6542
});
6643

67-
reqId = res.functionRequestId;
68-
});
69-
test('invoke with wrong region', async () => {
70-
try {
71-
faas.setRegion('ap-test');
72-
await faas.invoke({
44+
test('getVersions', async () => {
45+
const res = await faas.getVersions({
7346
...faasConfig,
7447
});
75-
} catch (e) {
76-
expect(e.code).toBe('1001');
77-
}
78-
faas.setRegion(region);
48+
49+
expect(Array.isArray(res)).toBe(true);
50+
});
7951
});
80-
test('invoke with wrong namespace', async () => {
81-
try {
82-
await faas.invoke({
52+
53+
describe('invoke', () => {
54+
test('invoke', async () => {
55+
const res = (await faas.invoke({
8356
...faasConfig,
84-
namespace: 'not_exist_namespace',
57+
})) as InvokeResult;
58+
59+
expect(res).toEqual({
60+
billDuration: expect.any(Number),
61+
duration: expect.any(Number),
62+
errMsg: expect.any(String),
63+
memUsage: expect.any(Number),
64+
functionRequestId: expect.any(String),
65+
invokeResult: expect.any(Number),
66+
log: expect.any(String),
67+
retMsg: expect.any(String),
8568
});
86-
} catch (e) {
87-
expect(e.code).toBe('1005');
88-
}
89-
});
90-
test('invoke with wrong qualifier', async () => {
91-
try {
92-
await faas.invoke({
69+
70+
reqId = res.functionRequestId;
71+
});
72+
73+
test('invoke with qualifier $DEFAULT', async () => {
74+
const res = (await faas.invoke({
9375
...faasConfig,
94-
qualifier: 'not_exist_qualifier',
76+
qualifier: '$DEFAULT',
77+
})) as InvokeResult;
78+
79+
expect(res).toEqual({
80+
billDuration: expect.any(Number),
81+
duration: expect.any(Number),
82+
errMsg: expect.any(String),
83+
memUsage: expect.any(Number),
84+
functionRequestId: expect.any(String),
85+
invokeResult: expect.any(Number),
86+
log: expect.any(String),
87+
retMsg: expect.any(String),
9588
});
96-
} catch (e) {
97-
expect(e.code).toBe('1006');
98-
}
99-
});
89+
});
10090

101-
test('getClsConfig', async () => {
102-
const res = await faas.getClsConfig({
103-
...faasConfig,
91+
test('invoke with wrong qualifier', async () => {
92+
try {
93+
await faas.invoke({
94+
...faasConfig,
95+
qualifier: 'wrong_qualifier',
96+
});
97+
} catch (e) {
98+
expect(e.code).toBe('1006');
99+
}
104100
});
105101

106-
expect(res).toEqual(clsConfig);
102+
test('invoke with wrong region', async () => {
103+
try {
104+
faas.setRegion('ap-test');
105+
await faas.invoke({
106+
...faasConfig,
107+
});
108+
} catch (e) {
109+
expect(e.code).toBe('1001');
110+
}
111+
faas.setRegion(region);
112+
});
113+
test('invoke with wrong namespace', async () => {
114+
try {
115+
await faas.invoke({
116+
...faasConfig,
117+
namespace: 'not_exist_namespace',
118+
});
119+
} catch (e) {
120+
expect(e.code).toBe('1005');
121+
}
122+
});
123+
test('invoke with wrong qualifier', async () => {
124+
try {
125+
await faas.invoke({
126+
...faasConfig,
127+
qualifier: 'not_exist_qualifier',
128+
});
129+
} catch (e) {
130+
expect(e.code).toBe('1006');
131+
}
132+
});
107133
});
108134

109-
test('getLogList', async () => {
110-
const res = await faas.getLogList({
111-
...faasConfig,
135+
describe('log', () => {
136+
test('getClsConfig', async () => {
137+
const res = await faas.getClsConfig({
138+
...faasConfig,
139+
});
140+
141+
expect(res).toEqual(clsConfig);
112142
});
113143

114-
if (res[0]) {
115-
reqId = res[0]!.requestId;
116-
}
117-
expect(res).toBeInstanceOf(Array);
118-
});
144+
test('getLogList', async () => {
145+
const res = await faas.getLogList({
146+
...faasConfig,
147+
});
119148

120-
test('getLogDetail', async () => {
121-
const res = await faas.getLogDetail({
122-
...faasConfig,
123-
...clsConfig,
124-
reqId,
149+
if (res[0]) {
150+
reqId = res[0]!.requestId;
151+
}
152+
expect(res).toBeInstanceOf(Array);
125153
});
126-
expect(res).toBeInstanceOf(Array);
127-
});
128-
test('getLogByReqId', async () => {
129-
const res = await faas.getLogByReqId({
130-
...faasConfig,
131-
reqId,
154+
155+
test('getLogDetail', async () => {
156+
const res = await faas.getLogDetail({
157+
...faasConfig,
158+
...clsConfig,
159+
reqId,
160+
});
161+
expect(res).toBeInstanceOf(Array);
132162
});
163+
test('getLogByReqId', async () => {
164+
const res = await faas.getLogByReqId({
165+
...faasConfig,
166+
reqId,
167+
});
133168

134-
expect(res).toEqual({
135-
requestId: reqId,
136-
retryNum: 0,
137-
startTime: expect.any(String),
138-
memoryUsage: expect.any(String),
139-
duration: expect.any(String),
140-
message: expect.any(String),
141-
isCompleted: expect.any(Boolean),
169+
expect(res).toEqual({
170+
requestId: reqId,
171+
retryNum: 0,
172+
startTime: expect.any(String),
173+
memoryUsage: expect.any(String),
174+
duration: expect.any(String),
175+
message: expect.any(String),
176+
isCompleted: expect.any(Boolean),
177+
});
142178
});
143179
});
144180

145-
test('getMetric', async () => {
146-
const res = await faas.getMetric({
147-
...faasConfig,
148-
metric: 'Invocation',
149-
isRaw: false,
181+
describe('metric', () => {
182+
test('getMetric', async () => {
183+
const res = await faas.getMetric({
184+
...faasConfig,
185+
metric: 'Invocation',
186+
isRaw: false,
187+
});
188+
expect(res).toBeInstanceOf(Array);
189+
if (res.length > 0) {
190+
expect(res).toEqual(
191+
expect.arrayContaining([
192+
{
193+
time: expect.any(String),
194+
value: expect.any(Number),
195+
timestamp: expect.any(Number),
196+
},
197+
]),
198+
);
199+
}
150200
});
151-
expect(res).toBeInstanceOf(Array);
152-
if (res.length > 0) {
153-
expect(res).toEqual(
154-
expect.arrayContaining([
201+
202+
test('[isRaw = true] getMetric', async () => {
203+
const res = await faas.getMetric({
204+
...faasConfig,
205+
metric: 'Invocation',
206+
isRaw: true,
207+
});
208+
209+
expect(res).toEqual({
210+
StartTime: expect.any(String),
211+
EndTime: expect.any(String),
212+
MetricName: expect.any(String),
213+
Period: expect.any(Number),
214+
DataPoints: expect.arrayContaining([
155215
{
156-
time: expect.any(String),
157-
value: expect.any(Number),
158-
timestamp: expect.any(Number),
216+
Dimensions: expect.any(Array),
217+
Timestamps: expect.any(Array),
218+
Values: expect.any(Array),
159219
},
160220
]),
161-
);
162-
}
163-
});
164-
165-
test('[isRaw = true] getMetric', async () => {
166-
const res = await faas.getMetric({
167-
...faasConfig,
168-
metric: 'Invocation',
169-
isRaw: true,
170-
});
171-
172-
expect(res).toEqual({
173-
StartTime: expect.any(String),
174-
EndTime: expect.any(String),
175-
MetricName: expect.any(String),
176-
Period: expect.any(Number),
177-
DataPoints: expect.arrayContaining([
178-
{
179-
Dimensions: expect.any(Array),
180-
Timestamps: expect.any(Array),
181-
Values: expect.any(Array),
182-
},
183-
]),
184-
RequestId: expect.stringMatching(/.{36}/g),
221+
RequestId: expect.stringMatching(/.{36}/g),
222+
});
185223
});
186224
});
187225
});

packages/faas/src/apis.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const ACTIONS = [
1818
'ListTriggers',
1919
'ListNamespaces',
2020
'ListVersionByFunction',
21+
'ListAliases',
2122
] as const;
2223

2324
export type ActionType = typeof ACTIONS[number];
@@ -37,6 +38,9 @@ function initializeApis({
3738
serviceType: ServiceType.faas,
3839
version: '2018-04-16',
3940
actions: ACTIONS,
41+
errorHandler: (e) => {
42+
console.log(e);
43+
},
4044
});
4145
}
4246

packages/faas/src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const ERRORS = {
3232
QUALIFIER_NOT_EXIST_ERROR: {
3333
type: 'API_FAAS_qualifier',
3434
code: `1006`,
35-
message: '[FAAS] 未找到指定的 qualifier',
35+
message: '[FAAS] 未找到指定的 qualifier (版本或者别名)',
3636
},
3737
};
3838

0 commit comments

Comments
 (0)