Skip to content

Commit f211a02

Browse files
authored
fix(apisix): typecheck (#387)
1 parent 87f9730 commit f211a02

13 files changed

Lines changed: 128 additions & 167 deletions

File tree

libs/backend-apisix-standalone/src/typing.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,30 +139,28 @@ const UpstreamSchema = z.strictObject({
139139

140140
checks: z
141141
.strictObject({
142-
active: z
143-
.strictObject({
144-
type: upstreamHealthCheckType.optional(),
145-
timeout: z.coerce.number().default(1).optional(),
146-
concurrency: z.coerce.number().default(10).optional(),
147-
host: z.string().min(1).optional(),
148-
port: z.coerce.number().int().min(1).max(65535).optional(),
149-
http_path: z.string().default('/').optional(),
150-
https_verify_cert: z.boolean().default(true).optional(),
151-
http_request_headers: z.array(z.string()).min(1).optional(),
152-
healthy: z
153-
.strictObject({
154-
...upstreamHealthCheckPassiveHealthy.shape,
155-
interval: z.coerce.number().int().min(1).default(1),
156-
})
157-
.optional(),
158-
unhealthy: z
159-
.strictObject({
160-
...upstreamHealthCheckPassiveUnhealthy.shape,
161-
interval: z.coerce.number().int().min(1).default(1),
162-
})
163-
.optional(),
164-
})
165-
.optional(),
142+
active: z.strictObject({
143+
type: upstreamHealthCheckType.optional(),
144+
timeout: z.coerce.number().default(1).optional(),
145+
concurrency: z.coerce.number().default(10).optional(),
146+
host: z.string().min(1).optional(),
147+
port: z.coerce.number().int().min(1).max(65535).optional(),
148+
http_path: z.string().default('/').optional(),
149+
https_verify_cert: z.boolean().default(true).optional(),
150+
http_request_headers: z.array(z.string()).min(1).optional(),
151+
healthy: z
152+
.strictObject({
153+
...upstreamHealthCheckPassiveHealthy.shape,
154+
interval: z.coerce.number().int().min(1).default(1),
155+
})
156+
.optional(),
157+
unhealthy: z
158+
.strictObject({
159+
...upstreamHealthCheckPassiveUnhealthy.shape,
160+
interval: z.coerce.number().int().min(1).default(1),
161+
})
162+
.optional(),
163+
}),
166164
passive: z
167165
.strictObject({
168166
type: upstreamHealthCheckType.optional(),

libs/backend-apisix/e2e/misc.e2e-spec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ADCSDK from '@api7/adc-sdk';
22

33
import { BackendAPISIX } from '../src';
4-
import { server, token } from './support/constants';
4+
import { defaultBackendOptions } from './support/constants';
55
import {
66
createEvent,
77
deleteEvent,
@@ -14,10 +14,7 @@ describe('Miscellaneous', () => {
1414
let backend: BackendAPISIX;
1515

1616
beforeAll(() => {
17-
backend = new BackendAPISIX({
18-
server,
19-
token,
20-
});
17+
backend = new BackendAPISIX(defaultBackendOptions);
2118
});
2219

2320
describe('Sync resources with custom id', () => {
@@ -60,8 +57,8 @@ describe('Miscellaneous', () => {
6057
it('Dump', async () => {
6158
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
6259
expect(result.services).toHaveLength(1);
63-
expect(result.services[0]).toMatchObject(service);
64-
expect(result.services[0].routes[0]).toMatchObject(route);
60+
expect(result.services![0]).toMatchObject(service);
61+
expect(result.services![0].routes![0]).toMatchObject(route);
6562
});
6663

6764
it('Delete service', async () =>

libs/backend-apisix/e2e/ping.e2e-spec.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { AxiosError } from 'axios';
12
import { readFileSync } from 'node:fs';
2-
import { globalAgent as httpAgent } from 'node:http';
3+
import { globalAgent as httpGlobalAgent } from 'node:http';
34
import {
45
Agent as httpsAgent,
56
globalAgent as httpsGlobalAgent,
@@ -15,7 +16,7 @@ describe('Ping', () => {
1516
server,
1617
token,
1718
cacheKey: 'default',
18-
httpAgent,
19+
httpAgent: httpGlobalAgent,
1920
httpsAgent: httpsGlobalAgent,
2021
});
2122
await backend.ping();
@@ -26,7 +27,7 @@ describe('Ping', () => {
2627
server: 'https://localhost:29180',
2728
token,
2829
cacheKey: 'default',
29-
httpAgent,
30+
httpAgent: httpGlobalAgent,
3031
httpsAgent: new httpsAgent({
3132
cert: readFileSync(
3233
join(__dirname, 'assets/apisix_conf/mtls/client.cer'),
@@ -48,7 +49,7 @@ describe('Ping', () => {
4849
server: 'http://0.0.0.0',
4950
token: '',
5051
cacheKey: 'default',
51-
httpAgent,
52+
httpAgent: httpGlobalAgent,
5253
httpsAgent: httpsGlobalAgent,
5354
});
5455
await expect(backend.ping()).rejects.toThrow(
@@ -61,7 +62,7 @@ describe('Ping', () => {
6162
server: 'https://localhost:29180',
6263
token,
6364
cacheKey: 'default',
64-
httpAgent,
65+
httpAgent: httpGlobalAgent,
6566
httpsAgent: httpsGlobalAgent,
6667
});
6768
await expect(backend.ping()).rejects.toThrow(
@@ -74,7 +75,7 @@ describe('Ping', () => {
7475
server: 'https://localhost:29180',
7576
token,
7677
cacheKey: 'default',
77-
httpAgent,
78+
httpAgent: httpGlobalAgent,
7879
httpsAgent: new httpsAgent({
7980
ca: readFileSync(join(__dirname, 'assets/apisix_conf/mtls/ca.cer')),
8081
}),
@@ -84,8 +85,10 @@ describe('Ping', () => {
8485
try {
8586
await backend.ping();
8687
} catch (err) {
87-
expect(err.toString()).toContain('Request failed with status code 400');
88-
expect(err.response.data).toContain(
88+
expect((err as AxiosError).toString()).toContain(
89+
'Request failed with status code 400',
90+
);
91+
expect((err as AxiosError).response!.data).toContain(
8992
'No required SSL certificate was sent',
9093
);
9194
}

libs/backend-apisix/e2e/resources/consumer.e2e-spec.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as ADCSDK from '@api7/adc-sdk';
22
import { gte } from 'semver';
33

44
import { BackendAPISIX } from '../../src';
5-
import { server, token } from '../support/constants';
5+
import { defaultBackendOptions } from '../support/constants';
66
import { conditionalDescribe, semverCondition } from '../support/utils';
77
import {
88
createEvent,
@@ -16,11 +16,7 @@ describe('Consumer E2E', () => {
1616
let backend: BackendAPISIX;
1717

1818
beforeAll(() => {
19-
backend = new BackendAPISIX({
20-
server,
21-
token,
22-
tlsSkipVerify: true,
23-
});
19+
backend = new BackendAPISIX(defaultBackendOptions);
2420
});
2521

2622
conditionalDescribe(semverCondition(gte, '3.11.0'))(
@@ -54,14 +50,14 @@ describe('Consumer E2E', () => {
5450
backend,
5551
)) as ADCSDK.Configuration;
5652
expect(result.consumers).toHaveLength(1);
57-
expect(result.consumers[0]).toMatchObject(consumer1);
58-
expect(result.consumers[0].credentials).toMatchObject(
59-
consumer1.credentials,
53+
expect(result.consumers![0]).toMatchObject(consumer1);
54+
expect(result.consumers![0].credentials).toMatchObject(
55+
consumer1.credentials!,
6056
);
6157
});
6258

6359
it('Update consumer credential', async () => {
64-
consumer1.credentials[0].config.key = 'new-key';
60+
consumer1.credentials![0].config.key = 'new-key';
6561
await syncEvents(backend, [
6662
updateEvent(
6763
ADCSDK.ResourceType.CONSUMER_CREDENTIAL,
@@ -76,8 +72,8 @@ describe('Consumer E2E', () => {
7672
const result = (await dumpConfiguration(
7773
backend,
7874
)) as ADCSDK.Configuration;
79-
expect(result.consumers[0]).toMatchObject(consumer1);
80-
expect(result.consumers[0].credentials[0].config.key).toEqual(
75+
expect(result.consumers![0]).toMatchObject(consumer1);
76+
expect(result.consumers![0].credentials![0].config.key).toEqual(
8177
'new-key',
8278
);
8379
});
@@ -96,7 +92,7 @@ describe('Consumer E2E', () => {
9692
backend,
9793
)) as ADCSDK.Configuration;
9894
expect(result.consumers).toHaveLength(1);
99-
expect(result.consumers[0].credentials).toBeUndefined();
95+
expect(result.consumers![0].credentials).toBeUndefined();
10096
});
10197

10298
it('Delete consumer', async () =>

libs/backend-apisix/e2e/resources/service-upstream.e2e-spec.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Differ } from '@api7/adc-differ';
22
import * as ADCSDK from '@api7/adc-sdk';
33

44
import { BackendAPISIX } from '../../src';
5-
import { server, token } from '../support/constants';
5+
import { defaultBackendOptions } from '../support/constants';
66
import {
77
createEvent,
88
deleteEvent,
@@ -16,11 +16,7 @@ describe('Service-Upstreams E2E', () => {
1616
let backend: BackendAPISIX;
1717

1818
beforeAll(() => {
19-
backend = new BackendAPISIX({
20-
server,
21-
token,
22-
tlsSkipVerify: true,
23-
});
19+
backend = new BackendAPISIX(defaultBackendOptions);
2420
});
2521

2622
describe('Service inline upstream', () => {
@@ -173,10 +169,10 @@ describe('Service-Upstreams E2E', () => {
173169
it('Dump', async () => {
174170
const result = await dumpConfiguration(backend);
175171
expect(result.services).toHaveLength(1);
176-
expect(result.services[0]).toMatchObject(service);
177-
expect(result.services[0].upstreams).toHaveLength(2);
172+
expect(result.services![0]).toMatchObject(service);
173+
expect(result.services![0].upstreams).toHaveLength(2);
178174

179-
const upstreams = sortResult(result.services[0].upstreams, 'name');
175+
const upstreams = sortResult(result.services![0].upstreams!, 'name');
180176
expect(upstreams[0]).toMatchObject(upstreamND1);
181177
expect(upstreams[1]).toMatchObject(upstreamND2);
182178
});
@@ -199,7 +195,7 @@ describe('Service-Upstreams E2E', () => {
199195
const result = await dumpConfiguration(backend);
200196
expect(result.services).toHaveLength(1);
201197

202-
const upstreams = sortResult(result.services[0].upstreams, 'name');
198+
const upstreams = sortResult(result.services![0].upstreams!, 'name');
203199
expect(upstreams[0]).toMatchObject(newUpstreamND1);
204200
});
205201

@@ -211,8 +207,8 @@ describe('Service-Upstreams E2E', () => {
211207
it('Dump (non-default upstream 2 should not exist)', async () => {
212208
const result = await dumpConfiguration(backend);
213209
expect(result.services).toHaveLength(1);
214-
expect(result.services[0].upstreams).toHaveLength(1);
215-
expect(result.services[0].upstreams[0]).toMatchObject(newUpstreamND1);
210+
expect(result.services![0].upstreams).toHaveLength(1);
211+
expect(result.services![0].upstreams![0]).toMatchObject(newUpstreamND1);
216212
});
217213

218214
it('Delete', async () =>

libs/backend-apisix/e2e/resources/service.e2e-spec.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
import { Differ } from '@api7/adc-differ';
22
import * as ADCSDK from '@api7/adc-sdk';
3-
import { globalAgent as httpGlobalAgent } from 'node:http';
4-
import { globalAgent as httpsGlobalAgent } from 'node:https';
53

64
import { BackendAPISIX } from '../../src';
75
import * as typing from '../../src/typing';
8-
import { server, token } from '../support/constants';
6+
import { defaultBackendOptions, server, token } from '../support/constants';
97
import { cleanup, syncEvents } from '../support/utils';
108

119
describe('Service E2E', () => {
1210
let backend: BackendAPISIX;
1311

1412
beforeAll(async () => {
15-
backend = new BackendAPISIX({
16-
server,
17-
token,
18-
tlsSkipVerify: true,
19-
cacheKey: 'default',
20-
httpAgent: httpGlobalAgent,
21-
httpsAgent: httpsGlobalAgent,
22-
});
13+
backend = new BackendAPISIX(defaultBackendOptions);
2314

2415
await cleanup(backend);
2516
});

libs/backend-apisix/e2e/resources/upstream.e2e-spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ADCSDK from '@api7/adc-sdk';
22

33
import { BackendAPISIX } from '../../src';
4-
import { server, token } from '../support/constants';
4+
import { defaultBackendOptions } from '../support/constants';
55
import {
66
createEvent,
77
deleteEvent,
@@ -13,11 +13,7 @@ describe('Upstream E2E', () => {
1313
let backend: BackendAPISIX;
1414

1515
beforeAll(() => {
16-
backend = new BackendAPISIX({
17-
server,
18-
token,
19-
tlsSkipVerify: true,
20-
});
16+
backend = new BackendAPISIX(defaultBackendOptions);
2117
});
2218

2319
describe('Sync and dump upstream (nodes = null)', () => {
@@ -40,7 +36,7 @@ describe('Upstream E2E', () => {
4036
it('Dump', async () => {
4137
const result = (await dumpConfiguration(backend)) as ADCSDK.Configuration;
4238
expect(result.services).toHaveLength(1);
43-
expect(result.services[0]).toMatchObject(service);
39+
expect(result.services![0]).toMatchObject(service);
4440
});
4541

4642
it('Delete service', async () =>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1+
import * as ADCSDK from '@api7/adc-sdk';
2+
import { globalAgent as httpGlobalAgent } from 'node:http';
3+
import { globalAgent as httpsGlobalAgent } from 'node:https';
4+
15
export const server = 'http://localhost:19180';
26
export const token = 'edd1c9f034335f136f87ad84b625c8f1';
7+
8+
export const defaultBackendOptions: ADCSDK.BackendOptions = {
9+
server,
10+
token,
11+
cacheKey: 'default',
12+
httpAgent: httpGlobalAgent,
13+
httpsAgent: httpsGlobalAgent,
14+
};

libs/backend-apisix/e2e/support/utils.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,22 @@ export const overrideEventResourceId = (
9898
return event;
9999
};
100100

101-
export const sortResult = <T>(result: Array<T>, field: string) =>
102-
structuredClone(result).sort((a, b) => a[field].localeCompare(b[field]));
101+
export const sortResult = <T extends Record<string, any>>(
102+
result: Array<T>,
103+
field: string,
104+
) => structuredClone(result).sort((a, b) => a[field].localeCompare(b[field]));
103105

104106
export const wait = (ms: number) =>
105107
new Promise((resolve) => setTimeout(resolve, ms));
106108

107109
type cond = boolean | (() => boolean);
108110

109-
export const conditionalDescribe = (cond: cond) =>
110-
cond ? describe : describe.skip;
111+
export const conditionalDescribe = (
112+
cond: cond,
113+
): typeof describe | typeof describe.skip => (cond ? describe : describe.skip);
111114

112-
export const conditionalIt = (cond: cond) => (cond ? it : it.skip);
115+
export const conditionalIt = (cond: cond): typeof it | typeof it.skip =>
116+
cond ? it : it.skip;
113117

114118
export const semverCondition = (
115119
op: (v1: string | semver.SemVer, v2: string | semver.SemVer) => boolean,

0 commit comments

Comments
 (0)