Skip to content

Commit f9eac70

Browse files
authored
chore(sdk): move resources type derived from zod (#356)
1 parent 257c37f commit f9eac70

File tree

6 files changed

+772
-653
lines changed

6 files changed

+772
-653
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,18 @@ export const toADC = (input: typing.APISIXStandalone) => {
119119
id: ssl.id,
120120
labels: ssl.labels,
121121
type: ssl.type,
122-
snis: ssl.snis!,
122+
snis: ssl.snis,
123123
certificates: [
124124
{
125125
certificate: ssl.cert,
126126
key: ssl.key,
127127
},
128-
...(ssl.certs ?? []).map((cert, idx) => ({
129-
certificate: cert,
130-
key: ssl.keys![idx],
131-
})),
128+
...(ssl.certs && ssl.keys
129+
? ssl.certs.map((cert, idx) => ({
130+
certificate: cert,
131+
key: ssl.keys?.[idx],
132+
}))
133+
: []),
132134
] as Array<ADCSDK.SSLCertificate>,
133135
client: ssl.client,
134136
ssl_protocols: ssl.ssl_protocols,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const SSLSchema = z
212212
labels: Metadata.labels,
213213

214214
type: z.union([z.literal('server'), z.literal('client')]).optional(),
215-
snis: z.array(z.string()).min(1).optional(),
215+
snis: z.array(z.string()).min(1),
216216
cert: z.string(),
217217
key: z.string(),
218218
certs: z.array(z.string()).optional(),
@@ -224,7 +224,9 @@ const SSLSchema = z
224224
skip_mtls_uri_regex: z.array(z.string()).optional(),
225225
})
226226
.optional(),
227-
ssl_protocols: z.array(z.string()).optional(),
227+
ssl_protocols: z
228+
.array(z.enum(['TLSv1.1', 'TLSv1.2', 'TLSv1.3']))
229+
.optional(),
228230
status: Status.optional(),
229231
})
230232
.extend(ModifiedIndex);

libs/sdk/src/core/index.ts

Lines changed: 36 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -1,175 +1,44 @@
11
import { ResourceType } from './resource';
2+
import {
3+
Consumer,
4+
ConsumerCredential,
5+
GlobalRule,
6+
Labels,
7+
PluginMetadata,
8+
Plugins,
9+
Route,
10+
SSL,
11+
Service,
12+
StreamRoute,
13+
Upstream,
14+
} from './schema';
215

316
export * from './differ';
417
export * from './resource';
518

6-
export type Labels = Record<string, string | Array<string>>;
7-
export type Plugin = Record<string, unknown>;
8-
export type Plugins = Record<string, Plugin>;
9-
export type Expr = Array<unknown>;
10-
11-
export interface Route {
12-
id?: string;
13-
name: string;
14-
description?: string;
15-
labels?: Labels;
16-
17-
hosts?: Array<string>;
18-
uris: Array<string>;
19-
priority?: number;
20-
timeout?: UpstreamTimeout;
21-
vars?: Expr;
22-
methods?: Array<string>;
23-
enable_websocket?: boolean;
24-
remote_addrs?: Array<string>;
25-
plugins?: Plugins;
26-
filter_func?: string;
27-
service_id?: string;
28-
29-
metadata?: ResourceMetadata;
30-
}
31-
32-
export interface Service {
33-
id?: string;
34-
name: string;
35-
description?: string;
36-
labels?: Labels;
37-
38-
upstream?: Upstream;
39-
upstreams?: Array<Upstream>;
40-
plugins?: Plugins;
41-
path_prefix?: string;
42-
strip_path_prefix?: boolean;
43-
hosts?: Array<string>;
44-
45-
routes?: Array<Route>;
46-
stream_routes?: Array<StreamRoute>;
47-
48-
metadata?: ResourceMetadata;
49-
}
50-
51-
export type UpstreamBalancer = 'roundrobin' | 'chash' | 'least_conn' | 'ewma';
52-
export type UpstreamScheme =
53-
| 'grpc'
54-
| 'grpcs'
55-
| 'http'
56-
| 'https'
57-
| 'tcp'
58-
| 'tls'
59-
| 'udp'
60-
| 'kafka';
61-
export type UpstreamPassHost = 'pass' | 'node' | 'rewrite';
62-
export interface UpstreamNode {
63-
host: string;
64-
port: number;
65-
weight: number;
66-
priority?: number;
67-
metadata?: { [key: string]: unknown };
68-
}
69-
export interface UpstreamTimeout {
70-
connect: number;
71-
send: number;
72-
read: number;
73-
}
74-
export interface UpstreamClientTLS {
75-
client_cert?: string;
76-
client_key?: string;
77-
client_cert_id?: string;
78-
verify?: boolean;
79-
}
80-
export interface UpstreamKeepalivePool {
81-
size: number;
82-
idle_timeout: number;
83-
requests: number;
84-
}
85-
export interface UpstreamHealthCheck {
86-
active?: UpstreamHealthCheckActive;
87-
passive?: UpstreamHealthCheckPassive;
88-
}
89-
export interface UpstreamHealthCheckActive {
90-
type?: 'http' | 'https' | 'tcp';
91-
timeout?: number;
92-
concurrency?: number;
93-
host?: string;
94-
port?: number;
95-
http_path?: string;
96-
https_verify_cert?: boolean;
97-
http_request_headers?: Array<string>;
98-
healthy?: UpstreamHealthCheckActiveHealthy;
99-
unhealthy?: UpstreamHealthCheckActiveUnhealthy;
100-
}
101-
export interface UpstreamHealthCheckPassive {
102-
type?: 'http' | 'https' | 'tcp';
103-
healthy?: UpstreamHealthCheckPassiveHealthy;
104-
unhealthy?: UpstreamHealthCheckPassiveUnhealthy;
105-
}
106-
export interface UpstreamHealthCheckPassiveHealthy {
107-
http_statuses?: Array<number>;
108-
successes?: number;
109-
}
110-
export interface UpstreamHealthCheckPassiveUnhealthy {
111-
http_statuses?: Array<number>;
112-
http_failures?: number;
113-
tcp_failures?: number;
114-
timeouts?: number;
115-
}
116-
117-
export type UpstreamHealthCheckActiveHealthy = {
118-
interval: number;
119-
} & UpstreamHealthCheckPassiveHealthy;
120-
export type UpstreamHealthCheckActiveUnhealthy = {
121-
interval: number;
122-
} & UpstreamHealthCheckPassiveUnhealthy;
123-
124-
export interface Upstream {
125-
id?: string;
126-
name?: string;
127-
description?: string;
128-
labels?: Labels;
129-
130-
type?: UpstreamBalancer;
131-
hash_on?: string;
132-
key?: string;
133-
checks?: UpstreamHealthCheck;
134-
nodes?: Array<UpstreamNode>;
135-
scheme?: UpstreamScheme;
136-
retries?: number;
137-
retry_timeout?: number;
138-
timeout?: UpstreamTimeout;
139-
tls?: UpstreamClientTLS;
140-
keepalive_pool?: UpstreamKeepalivePool;
141-
pass_host?: UpstreamPassHost;
142-
upstream_host?: string;
143-
144-
service_name?: string;
145-
discovery_type?: string;
146-
discovery_args?: Record<string, unknown>;
147-
148-
metadata?: ResourceMetadata;
149-
}
150-
151-
export type SSLType = 'server' | 'client';
152-
export interface SSLClientMTLS {
153-
ca: string;
154-
depth: number;
155-
skip_mtls_uri_regex?: Array<string>;
156-
}
157-
export interface SSLCertificate {
158-
certificate: string;
159-
key: string;
160-
}
161-
export interface SSL {
162-
id?: string;
163-
labels?: Labels;
164-
165-
type?: SSLType;
166-
snis: Array<string>;
167-
certificates: Array<SSLCertificate>;
168-
client?: SSLClientMTLS;
169-
ssl_protocols?: Array<string>;
170-
171-
metadata?: ResourceMetadata;
172-
}
19+
export type {
20+
Labels,
21+
Plugin,
22+
Plugins,
23+
Expr,
24+
Route,
25+
Service,
26+
UpstreamBalancer,
27+
UpstreamScheme,
28+
UpstreamPassHost,
29+
UpstreamNode,
30+
UpstreamTimeout,
31+
Upstream,
32+
SSLCertificate,
33+
SSL,
34+
GlobalRule,
35+
PluginMetadata,
36+
ConsumerCredential,
37+
Consumer,
38+
StreamRoute,
39+
Configuration,
40+
InternalConfiguration,
41+
} from './schema';
17342

17443
export interface PluginConfig {
17544
id?: string;
@@ -178,34 +47,6 @@ export interface PluginConfig {
17847
labels?: Labels;
17948

18049
plugins: Plugins;
181-
182-
metadata?: ResourceMetadata;
183-
}
184-
185-
export type GlobalRule = Record<string, unknown>;
186-
187-
export type PluginMetadata = Record<string, unknown>;
188-
189-
export interface ConsumerCredential {
190-
id?: string;
191-
name: string;
192-
description?: string;
193-
labels?: Labels;
194-
195-
type: 'key-auth' | 'basic-auth' | 'jwt-auth' | 'hmac-auth';
196-
197-
config: Plugin;
198-
199-
metadata?: ResourceMetadata;
200-
}
201-
202-
export interface Consumer {
203-
username: string;
204-
description?: string;
205-
labels?: Labels;
206-
207-
plugins?: Plugins;
208-
credentials?: Array<ConsumerCredential>;
20950
}
21051

21152
export interface ConsumerGroup {
@@ -217,45 +58,6 @@ export interface ConsumerGroup {
21758
plugins: Plugins;
21859

21960
consumers?: Array<Consumer>;
220-
221-
metadata?: ResourceMetadata;
222-
}
223-
224-
export interface StreamRoute {
225-
id?: string;
226-
name: string;
227-
description?: string;
228-
labels?: Labels;
229-
230-
plugins?: Plugins;
231-
232-
remote_addr?: string;
233-
server_addr?: string;
234-
server_port?: number;
235-
sni?: string;
236-
237-
metadata?: ResourceMetadata;
238-
}
239-
240-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
241-
export interface ResourceMetadata {}
242-
243-
export interface Configuration {
244-
services?: Array<Service>;
245-
ssls?: Array<SSL>;
246-
consumers?: Array<Consumer>;
247-
248-
// object format resources
249-
global_rules?: Record<string, GlobalRule>;
250-
plugin_metadata?: Record<string, PluginMetadata>;
251-
252-
// internal use only
253-
routes?: Array<Route>;
254-
stream_routes?: Array<StreamRoute>;
255-
consumer_credentials?: Array<ConsumerCredential>;
256-
upstreams?: Array<Upstream>;
257-
/* consumer_groups?: Array<ConsumerGroup>;
258-
plugin_configs?: Array<PluginConfig>; */
25961
}
26062

26163
export type ResourceFor<T extends ResourceType> = T extends ResourceType.SERVICE

0 commit comments

Comments
 (0)