Skip to content

Commit 697b0f5

Browse files
committed
deno - test api client
1 parent c5be478 commit 697b0f5

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

lib/api.ts

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
//import { resolve, parse } from "url";
2+
//import { join } from "path";
3+
4+
// @ts-ignore
5+
import { unique } from './misc.ts';
6+
7+
export type ItemId = string;
8+
9+
export interface ApiResponse {
10+
success: boolean;
11+
}
12+
13+
export interface BaseItem extends ApiResponse {
14+
_id?: ItemId;
15+
16+
name?: string;
17+
description?: string;
18+
19+
owner?: ItemId;
20+
group?: ItemId;
21+
22+
time?: number;
23+
_at?: number;
24+
}
25+
26+
export class ApiError extends Error {
27+
jti: string = '';
28+
url: string = '';
29+
tags: string[] = [];
30+
code = 500;
31+
32+
constructor(message: string) {
33+
super(message);
34+
this.name = this.constructor.name;
35+
if (typeof Error['captureStackTrace'] === 'function') {
36+
Error['captureStackTrace'](this, this.constructor);
37+
} else {
38+
this.stack = new Error(message).stack;
39+
}
40+
this.trySetTokenId();
41+
}
42+
43+
trySetTokenId() {
44+
// const [, b64] = (getToken() || '').split('.');
45+
// if (b64) {
46+
// try {
47+
// // TODO: move to ./base64.ts decode
48+
// const payload = Buffer.from(b64, 'base64').toString();
49+
// this.jti = JSON.parse(payload).jti || '';
50+
// } catch (err) {}
51+
// }
52+
}
53+
54+
withTags(tags: string[] = []) {
55+
this.tags = unique([...this.tags, ...(tags || [])]);
56+
return this;
57+
}
58+
59+
withCode(code: number) {
60+
this.code = +code;
61+
return this;
62+
}
63+
64+
withUrl(url: string) {
65+
this.url = url;
66+
return this;
67+
}
68+
}
69+
70+
export class NginxError extends ApiError {
71+
title: any = '';
72+
73+
withTitle(title = '') {
74+
this.title = title;
75+
return this;
76+
}
77+
78+
static fromHtml(resp = '', statusCode = 500) {
79+
let [, title = 'Unknown nginx errror'] = /<title>(.*?)<\/title>/gi.exec(resp) || [];
80+
title = title.replace(statusCode.toString(), '').trim();
81+
82+
// return new NginxError(http.STATUS_CODES[statusCode])
83+
// .withCode(statusCode)
84+
// .withTitle(title);
85+
}
86+
}
87+
88+
// export function req<T, U = unknown>(url: string, opts: any = {}, data: U = null) {
89+
// const { protocol, hostname, port, path } = parse(url);
90+
// const proto = protocol === 'https:' ? https : http;
91+
92+
// opts = {
93+
// method: 'GET',
94+
// host: hostname,
95+
// port: +port,
96+
// path,
97+
// ...opts
98+
// };
99+
100+
// if (!port) {
101+
// opts.port = protocol === 'https:' ? 443 : 80;
102+
// }
103+
104+
// return new Promise<T>((resolve, reject) => {
105+
// const req = proto.request(opts, (res) => {
106+
// let resp = '';
107+
// res.on('data', (chunk) => (resp += chunk.toString()));
108+
// res.on('end', () => {
109+
// try {
110+
// /*
111+
// * most nginx upstream errors should be handled by ingress default-backend
112+
// * but who knows ...
113+
// */
114+
// if (resp.startsWith('<html>') && resp.includes('nginx')) {
115+
// return reject(NginxError.fromHtml(resp, res.statusCode).withUrl(url));
116+
// }
117+
118+
// const json = JSON.parse(resp);
119+
// if (res.statusCode >= 400) {
120+
// return reject(
121+
// new ApiError(json.message)
122+
// .withCode(res.statusCode)
123+
// .withTags(json.tags)
124+
// .withUrl(url)
125+
// );
126+
// }
127+
// resolve(json);
128+
// } catch (err) {
129+
// console.log(resp);
130+
// reject(err);
131+
// }
132+
// });
133+
// });
134+
// req.on('error', (err) => reject(err));
135+
136+
// if (data) {
137+
// let send = data as any;
138+
// if (typeof data === 'object') {
139+
// send = JSON.stringify(data);
140+
// }
141+
// req.write(send);
142+
// }
143+
// req.end();
144+
// });
145+
// }
146+
147+
export interface ClientOpts {
148+
url?: string;
149+
token?: string;
150+
}
151+
152+
export class Client {
153+
_opts: ClientOpts;
154+
url: string;
155+
token: string;
156+
157+
constructor(opts: ClientOpts) {
158+
this._opts = { ...(opts || {}) };
159+
160+
this.url = this._opts.url!;
161+
this.token = this._opts.token!;
162+
}
163+
164+
getDefaultHeaders() {
165+
const defaults = {
166+
accept: 'application/json',
167+
'content-type': 'application/json',
168+
'user-agent': `npm:@rightech/utils 1.1`
169+
};
170+
// if (this.token) {
171+
// defaults.authorization = `Bearer ${this.token}`;
172+
// }
173+
return defaults;
174+
}
175+
176+
// get(path, query = {}) {
177+
// const url = resolve(this.url, path);
178+
// const headers = this.getDefaultHeaders();
179+
// return req(url, { method: 'GET', headers });
180+
// }
181+
182+
// post(path, data = {}) {
183+
// const url = resolve(this.url, path);
184+
// const headers = this.getDefaultHeaders();
185+
// return req(url, { method: 'POST', headers }, data);
186+
// }
187+
188+
// patch(path, data = {}) {
189+
// const url = resolve(this.url, path);
190+
// const headers = this.getDefaultHeaders();
191+
// return req(url, { method: 'PATCH', headers }, data);
192+
// }
193+
194+
// delete(path) {
195+
// const url = resolve(this.url, path);
196+
// const headers = this.getDefaultHeaders();
197+
// return req(url, { method: 'DELETE', headers });
198+
// }
199+
200+
// with(opts = {}) {
201+
// return new ApiClient({ ...(this._opts || {}), ...opts });
202+
// }
203+
}

lib/misc.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function unique<T = unknown>(array: T[] = []) {
2+
return array.filter((item, pos, self) => {
3+
return self.indexOf(item) === pos;
4+
});
5+
}
6+
7+
export function only<T, K extends keyof T>(
8+
object: T,
9+
keys: K | K[] | string[] = []
10+
): Pick<T, K> {
11+
object = (object || {}) as T;
12+
keys = (Array.isArray(keys) ? keys : [keys]) as string[];
13+
14+
return keys.reduce((result, key) => {
15+
(result as any)[key] = (object as any)[key];
16+
return result;
17+
}, {} as T);
18+
}

0 commit comments

Comments
 (0)