Skip to content

Commit

Permalink
Merge pull request #1 from cosmology-tech/feat/open-api
Browse files Browse the repository at this point in the history
Feat/open api
  • Loading branch information
pyramation authored May 25, 2024
2 parents e397e9c + 0d78ec2 commit 4709d69
Show file tree
Hide file tree
Showing 92 changed files with 153,261 additions and 8,988 deletions.
15 changes: 13 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["@typescript-eslint", "simple-import-sort", "unused-imports"],
"plugins": [
"@typescript-eslint",
"simple-import-sort",
"unused-imports"
],
"rules": {
"indent": [
"error",
2
],
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
"quote-props": ["error", "as-needed"],
"semi": ["error", "always"],
"simple-import-sort/imports": 1,
"simple-import-sort/exports": 1,
"unused-imports/no-unused-imports": 1,
Expand All @@ -36,4 +47,4 @@
"no-implicit-globals": 0,
"@typescript-eslint/no-unsafe-declaration-merging": 0
}
}
}
8 changes: 6 additions & 2 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
yarn
yarn build
- name: Test
- name: Test Schema TypeScript
run: |
yarn test
cd ./packages/schema-typescript && yarn test
- name: Test OpenAPI Spec
run: |
cd ./packages/schema-sdk && yarn test
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"useTabs": false,
"singleQuote": false
}
65,036 changes: 65,036 additions & 0 deletions __fixtures__/openapi/paths-only.json

Large diffs are not rendered by default.

156 changes: 156 additions & 0 deletions __fixtures__/output/api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import * as http from 'http';
import * as querystring from 'querystring';
import { URLSearchParams } from 'url';

interface RequestOptions<Params> {
hostname: string;
path: string;
headers?: { [key: string]: string };
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
params?: Params;
timeout?: number;
}

export interface APIClientOptions {
restEndpoint: string;
}

export class APIClient {
private hostname: string;
private port: number = 8001; // Default Kubernetes proxy port
private defaultTimeout: number = 10000; // 10 seconds

constructor(options: APIClientOptions) {
const url = new URL(options.restEndpoint);
this.hostname = url.hostname;
this.port = url.port ? parseInt(url.port) : this.port;
}

get<Resp = unknown>(endpoint: string, query?: { [key: string]: any }, opts: http.RequestOptions & { timeout?: number } = {}): Promise<Resp> {
const queryString = query ? `?${querystring.stringify(query)}` : '';
const fullPath = endpoint + queryString;
return this.request<Resp, void>({
path: fullPath,
method: 'GET',
// @ts-ignore
headers: opts.headers,
timeout: opts.timeout || this.defaultTimeout,
});
}

post<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: http.RequestOptions & { timeout?: number, isFormData?: boolean } = {}): Promise<Resp> {
const headers = opts.isFormData ? {
'Content-Type': 'application/x-www-form-urlencoded',
...opts.headers
} : {
'Content-Type': 'application/json',
...opts.headers
};
const body = opts.isFormData ? new URLSearchParams(params as any).toString() : JSON.stringify(params);
headers['Content-Length'] = Buffer.byteLength(body).toString();

return this.request<Resp, Params>({
path: endpoint,
method: 'POST',
// @ts-ignore
headers,
// @ts-ignore
params: body,
timeout: opts.timeout || this.defaultTimeout,
});
}

patch<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: http.RequestOptions & { timeout?: number, isFormData?: boolean } = {}): Promise<Resp> {
const headers = opts.isFormData ? {
'Content-Type': 'application/x-www-form-urlencoded',
...opts.headers
} : {
'Content-Type': 'application/json',
...opts.headers
};
const body = opts.isFormData ? new URLSearchParams(params as any).toString() : JSON.stringify(params);
headers['Content-Length'] = Buffer.byteLength(body).toString();

return this.request<Resp, Params>({
path: endpoint,
// @ts-ignore
method: 'PATCH',
// @ts-ignore
headers,
// @ts-ignore
params: body,
timeout: opts.timeout || this.defaultTimeout,
});
}

put<Resp = unknown, Params = any>(endpoint: string, params: Params, opts: http.RequestOptions & { timeout?: number } = {}): Promise<Resp> {
const stringParams = JSON.stringify(params);
const defaultHeaders = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(stringParams).toString(),
...opts.headers
};
return this.request<Resp, Params>({
path: endpoint,
method: 'PUT',
// @ts-ignore
headers: defaultHeaders,
// @ts-ignore
params: stringParams,
timeout: opts.timeout || this.defaultTimeout,
});
}

delete<Resp = unknown>(endpoint: string, opts: http.RequestOptions & { timeout?: number } = {}): Promise<Resp> {
return this.request<Resp, void>({
path: endpoint,
method: 'DELETE',
// @ts-ignore
headers: opts.headers,
timeout: opts.timeout || this.defaultTimeout,
});
}

private request<Resp, Params>(options: RequestOptions<Params>): Promise<Resp> {
return new Promise((resolve, reject) => {
const requestOptions: http.RequestOptions = {
hostname: this.hostname,
port: this.port,
path: options.path,
method: options.method,
headers: options.headers,
};

const req = http.request(requestOptions, res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
try {
const parsedData: Resp = JSON.parse(data);
resolve(parsedData);
} catch (error) {
reject(error);
}
});
});

req.on('error', error => {
reject(error);
});

// @ts-ignore
req.setTimeout(options.timeout, () => {
req.abort();
reject(new Error('Request timeout'));
});

if (options.params && (options.method === 'POST' || options.method === 'PUT')) {
req.write(options.params);
}

req.end();
});
}
}
Loading

0 comments on commit 4709d69

Please sign in to comment.