Skip to content

Commit 4709d69

Browse files
authored
Merge pull request #1 from cosmology-tech/feat/open-api
Feat/open api
2 parents e397e9c + 0d78ec2 commit 4709d69

File tree

92 files changed

+153261
-8988
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+153261
-8988
lines changed

.eslintrc.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@
1616
"ecmaVersion": "latest",
1717
"sourceType": "module"
1818
},
19-
"plugins": ["@typescript-eslint", "simple-import-sort", "unused-imports"],
19+
"plugins": [
20+
"@typescript-eslint",
21+
"simple-import-sort",
22+
"unused-imports"
23+
],
2024
"rules": {
25+
"indent": [
26+
"error",
27+
2
28+
],
29+
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
30+
"quote-props": ["error", "as-needed"],
31+
"semi": ["error", "always"],
2132
"simple-import-sort/imports": 1,
2233
"simple-import-sort/exports": 1,
2334
"unused-imports/no-unused-imports": 1,
@@ -36,4 +47,4 @@
3647
"no-implicit-globals": 0,
3748
"@typescript-eslint/no-unsafe-declaration-merging": 0
3849
}
39-
}
50+
}

.github/workflows/run-tests.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ jobs:
2525
yarn
2626
yarn build
2727
28-
- name: Test
28+
- name: Test Schema TypeScript
2929
run: |
30-
yarn test
30+
cd ./packages/schema-typescript && yarn test
31+
32+
- name: Test OpenAPI Spec
33+
run: |
34+
cd ./packages/schema-sdk && yarn test

.prettierrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"trailingComma": "es5",
33
"tabWidth": 2,
44
"semi": true,
5+
"useTabs": false,
56
"singleQuote": false
67
}

__fixtures__/openapi/paths-only.json

Lines changed: 65036 additions & 0 deletions
Large diffs are not rendered by default.

__fixtures__/output/api-client.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import * as http from 'http';
2+
import * as querystring from 'querystring';
3+
import { URLSearchParams } from 'url';
4+
5+
interface RequestOptions<Params> {
6+
hostname: string;
7+
path: string;
8+
headers?: { [key: string]: string };
9+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
10+
params?: Params;
11+
timeout?: number;
12+
}
13+
14+
export interface APIClientOptions {
15+
restEndpoint: string;
16+
}
17+
18+
export class APIClient {
19+
private hostname: string;
20+
private port: number = 8001; // Default Kubernetes proxy port
21+
private defaultTimeout: number = 10000; // 10 seconds
22+
23+
constructor(options: APIClientOptions) {
24+
const url = new URL(options.restEndpoint);
25+
this.hostname = url.hostname;
26+
this.port = url.port ? parseInt(url.port) : this.port;
27+
}
28+
29+
get<Resp = unknown>(endpoint: string, query?: { [key: string]: any }, opts: http.RequestOptions & { timeout?: number } = {}): Promise<Resp> {
30+
const queryString = query ? `?${querystring.stringify(query)}` : '';
31+
const fullPath = endpoint + queryString;
32+
return this.request<Resp, void>({
33+
path: fullPath,
34+
method: 'GET',
35+
// @ts-ignore
36+
headers: opts.headers,
37+
timeout: opts.timeout || this.defaultTimeout,
38+
});
39+
}
40+
41+
post<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: http.RequestOptions & { timeout?: number, isFormData?: boolean } = {}): Promise<Resp> {
42+
const headers = opts.isFormData ? {
43+
'Content-Type': 'application/x-www-form-urlencoded',
44+
...opts.headers
45+
} : {
46+
'Content-Type': 'application/json',
47+
...opts.headers
48+
};
49+
const body = opts.isFormData ? new URLSearchParams(params as any).toString() : JSON.stringify(params);
50+
headers['Content-Length'] = Buffer.byteLength(body).toString();
51+
52+
return this.request<Resp, Params>({
53+
path: endpoint,
54+
method: 'POST',
55+
// @ts-ignore
56+
headers,
57+
// @ts-ignore
58+
params: body,
59+
timeout: opts.timeout || this.defaultTimeout,
60+
});
61+
}
62+
63+
patch<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: http.RequestOptions & { timeout?: number, isFormData?: boolean } = {}): Promise<Resp> {
64+
const headers = opts.isFormData ? {
65+
'Content-Type': 'application/x-www-form-urlencoded',
66+
...opts.headers
67+
} : {
68+
'Content-Type': 'application/json',
69+
...opts.headers
70+
};
71+
const body = opts.isFormData ? new URLSearchParams(params as any).toString() : JSON.stringify(params);
72+
headers['Content-Length'] = Buffer.byteLength(body).toString();
73+
74+
return this.request<Resp, Params>({
75+
path: endpoint,
76+
// @ts-ignore
77+
method: 'PATCH',
78+
// @ts-ignore
79+
headers,
80+
// @ts-ignore
81+
params: body,
82+
timeout: opts.timeout || this.defaultTimeout,
83+
});
84+
}
85+
86+
put<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: http.RequestOptions & { timeout?: number } = {}): Promise<Resp> {
87+
const stringParams = JSON.stringify(params);
88+
const defaultHeaders = {
89+
'Content-Type': 'application/json',
90+
'Content-Length': Buffer.byteLength(stringParams).toString(),
91+
...opts.headers
92+
};
93+
return this.request<Resp, Params>({
94+
path: endpoint,
95+
method: 'PUT',
96+
// @ts-ignore
97+
headers: defaultHeaders,
98+
// @ts-ignore
99+
params: stringParams,
100+
timeout: opts.timeout || this.defaultTimeout,
101+
});
102+
}
103+
104+
delete<Resp = unknown>(endpoint: string, opts: http.RequestOptions & { timeout?: number } = {}): Promise<Resp> {
105+
return this.request<Resp, void>({
106+
path: endpoint,
107+
method: 'DELETE',
108+
// @ts-ignore
109+
headers: opts.headers,
110+
timeout: opts.timeout || this.defaultTimeout,
111+
});
112+
}
113+
114+
private request<Resp, Params>(options: RequestOptions<Params>): Promise<Resp> {
115+
return new Promise((resolve, reject) => {
116+
const requestOptions: http.RequestOptions = {
117+
hostname: this.hostname,
118+
port: this.port,
119+
path: options.path,
120+
method: options.method,
121+
headers: options.headers,
122+
};
123+
124+
const req = http.request(requestOptions, res => {
125+
let data = '';
126+
res.on('data', chunk => {
127+
data += chunk;
128+
});
129+
res.on('end', () => {
130+
try {
131+
const parsedData: Resp = JSON.parse(data);
132+
resolve(parsedData);
133+
} catch (error) {
134+
reject(error);
135+
}
136+
});
137+
});
138+
139+
req.on('error', error => {
140+
reject(error);
141+
});
142+
143+
// @ts-ignore
144+
req.setTimeout(options.timeout, () => {
145+
req.abort();
146+
reject(new Error('Request timeout'));
147+
});
148+
149+
if (options.params && (options.method === 'POST' || options.method === 'PUT')) {
150+
req.write(options.params);
151+
}
152+
153+
req.end();
154+
});
155+
}
156+
}

0 commit comments

Comments
 (0)