Skip to content

Commit 931181b

Browse files
committed
deno-test-03
1 parent 697b0f5 commit 931181b

File tree

2 files changed

+70
-82
lines changed

2 files changed

+70
-82
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"deno.enable": true,
3+
"deno.lint": true,
4+
"deno.unstable": true
5+
}

lib/api.ts

Lines changed: 65 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@ export interface BaseItem extends ApiResponse {
2323
_at?: number;
2424
}
2525

26+
export type ApiErrorHelper = {
27+
message?: string;
28+
links?: string[];
29+
};
30+
2631
export class ApiError extends Error {
2732
jti: string = '';
2833
url: string = '';
2934
tags: string[] = [];
3035
code = 500;
36+
helper = {} as ApiErrorHelper;
3137

3238
constructor(message: string) {
3339
super(message);
@@ -56,6 +62,11 @@ export class ApiError extends Error {
5662
return this;
5763
}
5864

65+
withHelper(helper: ApiErrorHelper) {
66+
this.helper = helper;
67+
return this;
68+
}
69+
5970
withCode(code: number) {
6071
this.code = +code;
6172
return this;
@@ -85,64 +96,36 @@ export class NginxError extends ApiError {
8596
}
8697
}
8798

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-
// }
99+
type KVM = { [k: string]: string };
100+
101+
export interface RequestOptions {
102+
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
103+
headers?: KVM;
104+
}
105+
106+
export async function req<T, U = unknown>(
107+
url: string,
108+
opts: RequestOptions = {},
109+
data?: U
110+
) {
111+
const resp = await fetch(url, {
112+
...opts
113+
});
114+
115+
const json = await resp.json();
116+
117+
console.log('.xxx', resp.status);
118+
119+
if (resp.status >= 300) {
120+
throw new ApiError(json.message)
121+
.withHelper(json.helper)
122+
.withTags(json.tags)
123+
.withCode(resp.status)
124+
.withUrl(url);
125+
}
126+
127+
return json;
128+
}
146129

147130
export interface ClientOpts {
148131
url?: string;
@@ -162,40 +145,40 @@ export class Client {
162145
}
163146

164147
getDefaultHeaders() {
165-
const defaults = {
148+
const defaults: KVM = {
166149
accept: 'application/json',
167150
'content-type': 'application/json',
168151
'user-agent': `npm:@rightech/utils 1.1`
169152
};
170-
// if (this.token) {
171-
// defaults.authorization = `Bearer ${this.token}`;
172-
// }
153+
if (this.token) {
154+
defaults.authorization = `Bearer ${this.token}`;
155+
}
173156
return defaults;
174157
}
175158

176-
// get(path, query = {}) {
177-
// const url = resolve(this.url, path);
178-
// const headers = this.getDefaultHeaders();
179-
// return req(url, { method: 'GET', headers });
180-
// }
159+
get<T = unknown>(path: string, query = {}): Promise<T> {
160+
const url = new URL(path, this.url);
161+
const headers = this.getDefaultHeaders();
162+
return req(url.toString(), { method: 'GET', headers });
163+
}
181164

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-
// }
165+
post<T = unknown>(path: string, data: Partial<T> = {}): Promise<T> {
166+
const url = new URL(path, this.url);
167+
const headers = this.getDefaultHeaders();
168+
return req(url.toString(), { method: 'POST', headers }, data);
169+
}
187170

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-
// }
171+
patch<T = unknown>(path: string, data: Partial<T> = {}): Promise<T> {
172+
const url = new URL(path, this.url);
173+
const headers = this.getDefaultHeaders();
174+
return req(url.toString(), { method: 'PATCH', headers }, data);
175+
}
193176

194-
// delete(path) {
195-
// const url = resolve(this.url, path);
196-
// const headers = this.getDefaultHeaders();
197-
// return req(url, { method: 'DELETE', headers });
198-
// }
177+
delete<T = unknown>(path: string): Promise<T> {
178+
const url = new URL(path, this.url);
179+
const headers = this.getDefaultHeaders();
180+
return req(url.toString(), { method: 'DELETE', headers });
181+
}
199182

200183
// with(opts = {}) {
201184
// return new ApiClient({ ...(this._opts || {}), ...opts });

0 commit comments

Comments
 (0)