-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
136 lines (127 loc) · 3.78 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
declare const fetch: typeof import("undici").fetch;
import type { PipelineOptions, Transform } from "node:stream";
import type { RequestInit, Response } from "undici";
import type { AbortSignal } from "abort-controller";
import { PassThrough, Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
type StreamOptions = RequestInit & {
signal?: AbortSignal;
highWaterMark?: number;
};
const nyreFetch = {
post(url: string, body: any, options?: RequestInit) {
return fetch(url, {
...options,
method: "POST",
body: JSON.stringify(body),
});
},
get(url: string, options?: RequestInit) {
return fetch(url, { ...options, method: "GET" });
},
put(url: string, body: any, options?: RequestInit) {
return fetch(url, {
...options,
method: "PUT",
body: JSON.stringify(body),
});
},
delete(url: string, options?: RequestInit) {
return fetch(url, { ...options, method: "DELETE" });
},
head(url: string, options?: RequestInit) {
return fetch(url, { ...options, method: "HEAD" });
},
async stream(source: string, options?: StreamOptions) {
const response = await fetch(source, {
...options,
signal: options?.signal,
keepalive: true,
});
if (!response.ok) {
throw new Error(response.statusText);
}
if (!response.body) {
throw new Error(`No readable stream at source: ${source}`);
}
return new ExtendReadableStream(Readable.from(response.body));
},
};
export class Client {
constructor(private baseUrl: string) {}
private getURL(baseUrl: string, path: string) {
baseUrl = baseUrl.replace(/\/$/, "");
path = path.replace(/^\//, "");
return `${baseUrl}/${path}`;
}
fetch(path: string, options?: RequestInit) {
return fetch(this.getURL(this.baseUrl, path), options);
}
stream(path: string, options?: StreamOptions) {
return nyreFetch.stream(this.getURL(this.baseUrl, path), options);
}
post(path: string, body: any, options?: RequestInit) {
return nyreFetch.post(this.getURL(this.baseUrl, path), body, options);
}
get(path: string, options?: RequestInit) {
return nyreFetch.get(this.getURL(this.baseUrl, path), options);
}
put(path: string, body: any, options?: RequestInit) {
return nyreFetch.put(this.getURL(this.baseUrl, path), body, options);
}
delete(path: string, options?: RequestInit) {
return nyreFetch.delete(this.getURL(this.baseUrl, path), options);
}
head(path: string, options?: RequestInit) {
return nyreFetch.head(this.getURL(this.baseUrl, path), options);
}
}
export class ExtendReadableStream extends Readable {
constructor(protected readableStream: NodeJS.ReadableStream) {
super(readableStream);
}
async pipeTo(
writableStream: NodeJS.WritableStream,
options?: PipelineOptions
): Promise<void> {
if (!options) {
return pipeline(this.readableStream, new PassThrough(), writableStream);
}
return pipeline(
this.readableStream,
new PassThrough(),
writableStream,
options
);
}
async pipeThrough(
transformStream: Transform,
options?: PipelineOptions
): Promise<void> {
if (!options) {
return pipeline(this.readableStream, new PassThrough(), transformStream);
}
return pipeline(
this.readableStream,
new PassThrough(),
transformStream,
options
);
}
}
export function createClient(baseUrl: string) {
return new Client(baseUrl);
}
export function responseOk(r: Response) {
class RequestError extends Error {
constructor() {
super(r.statusText);
this.stack = `Request failed with status: ${r.status} - ${r.statusText}`;
this.name = "RequestError";
}
}
if (r.ok) return r;
throw new RequestError();
}
export type { Response };
export default nyreFetch;