|
| 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