-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.ts
More file actions
141 lines (119 loc) · 3.15 KB
/
client.ts
File metadata and controls
141 lines (119 loc) · 3.15 KB
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
137
138
139
140
141
import { type Region, regionBaseURL } from "./region.ts";
import { cleanPath, getFileExtension } from "./utils.ts";
import { Key } from "./key.ts";
type CreateClientOptions = {
apiKey: string;
region: Region;
project: string;
};
export type Attachment = {
fileName: string;
fileUrl: string;
};
let defaultClient: Client | undefined;
/**
* Creates and returns the _default_ client instance.
*
* @throws {Error} if a default client already exists.
* @see {@link Client.default}
*/
export function createClient(
keyOrOptions: Key | CreateClientOptions,
): Client {
if (defaultClient) {
throw new Error("default client already exists");
}
const key = keyOrOptions instanceof Key ? keyOrOptions : new Key({
key: keyOrOptions.apiKey,
region: keyOrOptions.region,
project: keyOrOptions.project,
});
defaultClient = new Client(key);
return defaultClient;
}
export class Client {
/**
* Returns the _default_ client instance.
*
* @throws {Error} if there is no default client.
* @see {@link createClient}
*/
public static default(): Client {
if (!defaultClient) {
throw new Error("no default client");
}
return defaultClient;
}
public readonly key: Key;
private readonly baseURL: string;
public constructor(key: Key) {
this.key = key;
this.baseURL = regionBaseURL(this.key.region);
}
public get region(): Region {
return this.key.region;
}
public get project(): string {
return this.key.project;
}
public isEmbedKey(): boolean {
return this.key.isEmbed();
}
public async fetch<T>(
input: string,
init?: RequestInit,
): Promise<T> {
const url = this.url(input);
const reqInit = Object.assign({}, init, {
headers: {
...this.key.fetchHeaders(),
...init?.headers,
},
});
const response = await fetch(url, reqInit);
if (!response.ok) {
const body = await response.text();
console.error("client#fetch(): request failed.", url, body, reqInit);
throw new Error(response.statusText);
}
return response.json() as T;
}
public url(path: string): URL {
if (/^https?:\/\//.test(path)) {
return new URL(path);
}
return new URL(cleanPath(path), this.baseURL);
}
public async uploadTempFile(file: File): Promise<Attachment> {
const { upload_url: uploadUrl, download_url: downloadUrl } = await this
.fetch<{ upload_url: string; download_url: string }>(
"/services/get_temporary_file_upload_url",
{
method: "POST",
body: JSON.stringify({
filename: file.name,
extension: getFileExtension(file.name),
}),
},
);
let content = file.type;
if (file.type.startsWith("text/")) {
content += "; charset=utf-8"; // force utf-8 for text files
}
const response = await fetch(uploadUrl, {
method: "PUT",
body: file,
headers: {
"content-type": content,
"x-amz-tagging": "Expire=true",
},
});
if (!response.ok) {
throw new Error(response.statusText);
}
return {
fileName: file.name,
fileUrl: downloadUrl,
};
}
}