@nvwa-app/sdk-core 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +726 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +217 -0
- package/dist/index.d.mts +217 -0
- package/dist/index.d.ts +217 -0
- package/dist/index.mjs +705 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +49 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
import { createAuthClient } from 'better-auth/client';
|
|
2
|
+
import { usernameClient } from 'better-auth/client/plugins';
|
|
3
|
+
import { PostgrestClient, FetchResponse as FetchResponse$1 } from '@nvwa-app/postgrest-js';
|
|
4
|
+
|
|
5
|
+
var __defProp$7 = Object.defineProperty;
|
|
6
|
+
var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __publicField$7 = (obj, key, value) => __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
+
const AUTH_BASE_PATH = "/auth";
|
|
9
|
+
const LOGIN_TOKEN_KEY = "nvwa_login_token";
|
|
10
|
+
const LOGIN_USER_PROFILE_KEY = "nvwa_user_profile";
|
|
11
|
+
class NvwaAuthClient {
|
|
12
|
+
constructor(baseUrl, fetch, storage) {
|
|
13
|
+
__publicField$7(this, "storage");
|
|
14
|
+
__publicField$7(this, "authClient");
|
|
15
|
+
this.storage = storage;
|
|
16
|
+
this.authClient = createAuthClient({
|
|
17
|
+
baseUrl,
|
|
18
|
+
fetchOptions: {
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
customFetchImpl: fetch,
|
|
21
|
+
auth: {
|
|
22
|
+
type: "Bearer",
|
|
23
|
+
token: () => storage.get(LOGIN_TOKEN_KEY)
|
|
24
|
+
},
|
|
25
|
+
onSuccess: (ctx) => {
|
|
26
|
+
const authToken = ctx.response.headers.get(
|
|
27
|
+
"set-auth-token"
|
|
28
|
+
);
|
|
29
|
+
if (authToken) {
|
|
30
|
+
storage.set(LOGIN_TOKEN_KEY, authToken);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
plugins: [
|
|
35
|
+
usernameClient()
|
|
36
|
+
]
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async signInWithUsername(username, password) {
|
|
40
|
+
const result = await this.authClient.signIn.username({
|
|
41
|
+
username,
|
|
42
|
+
password
|
|
43
|
+
});
|
|
44
|
+
this.saveSession(result);
|
|
45
|
+
}
|
|
46
|
+
saveSession(result) {
|
|
47
|
+
var _a, _b;
|
|
48
|
+
if (result.error) {
|
|
49
|
+
throw new Error(result.error.message);
|
|
50
|
+
}
|
|
51
|
+
this.storage.set(LOGIN_TOKEN_KEY, (_a = result.data) == null ? void 0 : _a.token);
|
|
52
|
+
this.storage.set(LOGIN_USER_PROFILE_KEY, (_b = result.data) == null ? void 0 : _b.user);
|
|
53
|
+
}
|
|
54
|
+
async signOut() {
|
|
55
|
+
await this.storage.remove(LOGIN_TOKEN_KEY);
|
|
56
|
+
await this.storage.remove(LOGIN_USER_PROFILE_KEY);
|
|
57
|
+
}
|
|
58
|
+
async updateUserPassword(oldPassword, newPassword, revokeOtherSessions = false) {
|
|
59
|
+
const result = await this.authClient.changePassword({
|
|
60
|
+
currentPassword: oldPassword,
|
|
61
|
+
newPassword,
|
|
62
|
+
revokeOtherSessions
|
|
63
|
+
});
|
|
64
|
+
this.saveSession(result);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
var __defProp$6 = Object.defineProperty;
|
|
69
|
+
var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
70
|
+
var __publicField$6 = (obj, key, value) => __defNormalProp$6(obj, key + "" , value);
|
|
71
|
+
class NvwaEdgeFunctions {
|
|
72
|
+
constructor(http) {
|
|
73
|
+
__publicField$6(this, "http");
|
|
74
|
+
this.http = http;
|
|
75
|
+
}
|
|
76
|
+
async invoke(name, options) {
|
|
77
|
+
const response = await this.http.request(
|
|
78
|
+
"/functions/" + name,
|
|
79
|
+
{
|
|
80
|
+
method: options.method || "POST",
|
|
81
|
+
data: options.body,
|
|
82
|
+
headers: options.headers
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
return response.body;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
var __defProp$5 = Object.defineProperty;
|
|
90
|
+
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
91
|
+
var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
92
|
+
class FetchResponse {
|
|
93
|
+
constructor(ok, status, statusText, headers, body) {
|
|
94
|
+
__publicField$5(this, "ok");
|
|
95
|
+
__publicField$5(this, "status");
|
|
96
|
+
__publicField$5(this, "statusText");
|
|
97
|
+
__publicField$5(this, "headers");
|
|
98
|
+
__publicField$5(this, "body");
|
|
99
|
+
this.ok = ok;
|
|
100
|
+
this.status = status;
|
|
101
|
+
this.statusText = statusText;
|
|
102
|
+
this.headers = headers;
|
|
103
|
+
this.body = body;
|
|
104
|
+
}
|
|
105
|
+
async text() {
|
|
106
|
+
if (typeof this.body === "string") {
|
|
107
|
+
return this.body;
|
|
108
|
+
}
|
|
109
|
+
return JSON.stringify(this.body);
|
|
110
|
+
}
|
|
111
|
+
async json() {
|
|
112
|
+
if (typeof this.body === "string") {
|
|
113
|
+
try {
|
|
114
|
+
return JSON.parse(this.body);
|
|
115
|
+
} catch {
|
|
116
|
+
return this.body;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return this.body;
|
|
120
|
+
}
|
|
121
|
+
async arrayBuffer() {
|
|
122
|
+
if (this.body instanceof ArrayBuffer) {
|
|
123
|
+
return this.body;
|
|
124
|
+
}
|
|
125
|
+
if (typeof this.body === "string") {
|
|
126
|
+
const encoder = new TextEncoder();
|
|
127
|
+
return encoder.encode(this.body).buffer;
|
|
128
|
+
}
|
|
129
|
+
throw new Error("Cannot convert body to ArrayBuffer");
|
|
130
|
+
}
|
|
131
|
+
async blob() {
|
|
132
|
+
if (this.body instanceof Blob) {
|
|
133
|
+
return this.body;
|
|
134
|
+
}
|
|
135
|
+
if (typeof this.body === "string") {
|
|
136
|
+
return new Blob([this.body], { type: this.headers["content-type"] || "text/plain" });
|
|
137
|
+
}
|
|
138
|
+
throw new Error("Cannot convert body to Blob");
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
class NvwaHttpClient {
|
|
142
|
+
// 便捷方法:发送 JSON 请求
|
|
143
|
+
async jsonRequest(url, request) {
|
|
144
|
+
const jsonRequest = {
|
|
145
|
+
...request,
|
|
146
|
+
headers: {
|
|
147
|
+
"Content-Type": "application/json",
|
|
148
|
+
...request.headers
|
|
149
|
+
},
|
|
150
|
+
data: typeof request.data === "object" ? JSON.stringify(request.data) : request.data
|
|
151
|
+
};
|
|
152
|
+
return this.request(url, jsonRequest);
|
|
153
|
+
}
|
|
154
|
+
// 便捷方法:发送带认证的 JSON 请求
|
|
155
|
+
async jsonRequestWithAuth(url, request, handleUnauthorized) {
|
|
156
|
+
const jsonRequest = {
|
|
157
|
+
...request,
|
|
158
|
+
headers: {
|
|
159
|
+
"Content-Type": "application/json",
|
|
160
|
+
...request.headers
|
|
161
|
+
},
|
|
162
|
+
data: typeof request.data === "object" ? JSON.stringify(request.data) : request.data
|
|
163
|
+
};
|
|
164
|
+
return this.requestWithAuth(url, jsonRequest, handleUnauthorized);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
var __defProp$4 = Object.defineProperty;
|
|
169
|
+
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
170
|
+
var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
171
|
+
const FILE_STORAGE_BASE_PATH = "/storage";
|
|
172
|
+
const GENERATE_UPLOAD_URL_PATH = FILE_STORAGE_BASE_PATH + "/generateUploadUrl";
|
|
173
|
+
class NvwaFileStorage {
|
|
174
|
+
constructor(baseUrl, http) {
|
|
175
|
+
__publicField$4(this, "baseUrl");
|
|
176
|
+
__publicField$4(this, "http");
|
|
177
|
+
this.baseUrl = baseUrl;
|
|
178
|
+
this.http = http;
|
|
179
|
+
}
|
|
180
|
+
async uploadFile(file) {
|
|
181
|
+
const uploadUrlResponse = await this.http.request(
|
|
182
|
+
this.baseUrl + GENERATE_UPLOAD_URL_PATH,
|
|
183
|
+
{
|
|
184
|
+
method: "POST",
|
|
185
|
+
data: {
|
|
186
|
+
fileName: file.name || file.fileName,
|
|
187
|
+
fileSize: file.size || file.fileSize,
|
|
188
|
+
fileType: file.type || file.fileType
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
const { url: uploadUrl } = uploadUrlResponse.body;
|
|
193
|
+
if (!uploadUrl) {
|
|
194
|
+
throw new Error("\u83B7\u53D6\u4E0A\u4F20URL\u5931\u8D25");
|
|
195
|
+
}
|
|
196
|
+
const uploadResult = await this.http.request(
|
|
197
|
+
uploadUrl,
|
|
198
|
+
{
|
|
199
|
+
method: "PUT",
|
|
200
|
+
data: file,
|
|
201
|
+
headers: {
|
|
202
|
+
"Content-Type": file.type || file.fileType || "application/octet-stream"
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
const fileUrl = uploadResult.body.url.split("?")[0];
|
|
207
|
+
return { url: fileUrl };
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const ENTITIES_BASE_PATH = "/entities";
|
|
212
|
+
const buildPostgrestFetch = (httpClient, handleUnauthorized) => {
|
|
213
|
+
const customFetch = async (input, init) => {
|
|
214
|
+
console.log("customFetch", input, init);
|
|
215
|
+
let url;
|
|
216
|
+
if (typeof input === "string") {
|
|
217
|
+
url = input;
|
|
218
|
+
} else if (input && typeof input === "object") {
|
|
219
|
+
if (typeof input.toString === "function" && !input.url) {
|
|
220
|
+
url = input.toString();
|
|
221
|
+
} else if (input.url) {
|
|
222
|
+
url = input.url;
|
|
223
|
+
if (!init) {
|
|
224
|
+
init = {};
|
|
225
|
+
}
|
|
226
|
+
init.method = input.method;
|
|
227
|
+
init.headers = input.headers;
|
|
228
|
+
init.body = input.body;
|
|
229
|
+
} else {
|
|
230
|
+
throw new Error("Unsupported input type for fetch");
|
|
231
|
+
}
|
|
232
|
+
} else {
|
|
233
|
+
throw new Error("Unsupported input type for fetch");
|
|
234
|
+
}
|
|
235
|
+
const method = ((init == null ? void 0 : init.method) || "GET").toUpperCase();
|
|
236
|
+
const headers = {};
|
|
237
|
+
if (init == null ? void 0 : init.headers) {
|
|
238
|
+
if (init.headers && typeof init.headers.forEach === "function") {
|
|
239
|
+
init.headers.forEach((v, k) => {
|
|
240
|
+
headers[k] = v;
|
|
241
|
+
});
|
|
242
|
+
} else if (Array.isArray(init.headers)) {
|
|
243
|
+
for (const [k, v] of init.headers) headers[k] = v;
|
|
244
|
+
} else {
|
|
245
|
+
Object.assign(headers, init.headers);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
let body = init == null ? void 0 : init.body;
|
|
249
|
+
let response;
|
|
250
|
+
try {
|
|
251
|
+
response = await httpClient.requestWithAuth(url, {
|
|
252
|
+
method,
|
|
253
|
+
data: body,
|
|
254
|
+
headers
|
|
255
|
+
}, handleUnauthorized);
|
|
256
|
+
} catch (e) {
|
|
257
|
+
return new FetchResponse$1((e == null ? void 0 : e.message) || "Internal Error", {
|
|
258
|
+
status: 500,
|
|
259
|
+
statusText: "Internal Error"
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
let responseBody = response.body;
|
|
263
|
+
let responseData = null;
|
|
264
|
+
const contentType = response.headers["content-type"] || response.headers["Content-Type"] || "";
|
|
265
|
+
if (contentType.startsWith("application/json") && typeof responseBody !== "string") {
|
|
266
|
+
responseData = JSON.stringify(responseBody);
|
|
267
|
+
} else if (typeof responseBody === "string") {
|
|
268
|
+
responseData = responseBody;
|
|
269
|
+
} else if (responseBody && (responseBody.constructor === Blob || responseBody.constructor === ArrayBuffer || typeof responseBody.getReader === "function")) {
|
|
270
|
+
responseData = responseBody;
|
|
271
|
+
} else {
|
|
272
|
+
responseData = JSON.stringify(responseBody);
|
|
273
|
+
}
|
|
274
|
+
return new FetchResponse$1(responseData, {
|
|
275
|
+
status: response.status,
|
|
276
|
+
headers: response.headers
|
|
277
|
+
});
|
|
278
|
+
};
|
|
279
|
+
return customFetch;
|
|
280
|
+
};
|
|
281
|
+
const createPostgrestClient = (baseUrl, httpClient, handleUnauthorized) => {
|
|
282
|
+
return new PostgrestClient(baseUrl + ENTITIES_BASE_PATH, {
|
|
283
|
+
fetch: buildPostgrestFetch(httpClient, handleUnauthorized)
|
|
284
|
+
});
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
var __defProp$3 = Object.defineProperty;
|
|
288
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
289
|
+
var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
290
|
+
class Headers {
|
|
291
|
+
constructor(init) {
|
|
292
|
+
__publicField$3(this, "headerMap", /* @__PURE__ */ new Map());
|
|
293
|
+
if (!init) return;
|
|
294
|
+
if (init instanceof Headers) {
|
|
295
|
+
init.forEach((v, k) => this.set(k, v));
|
|
296
|
+
} else if (Array.isArray(init)) {
|
|
297
|
+
for (const [k, v] of init) this.set(k, String(v));
|
|
298
|
+
} else if (typeof init === "object") {
|
|
299
|
+
for (const k of Object.keys(init)) this.set(k, String(init[k]));
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
append(name, value) {
|
|
303
|
+
const key = name.toLowerCase();
|
|
304
|
+
const existing = this.headerMap.get(key);
|
|
305
|
+
this.headerMap.set(key, existing ? `${existing}, ${value}` : value);
|
|
306
|
+
}
|
|
307
|
+
set(name, value) {
|
|
308
|
+
this.headerMap.set(name.toLowerCase(), String(value));
|
|
309
|
+
}
|
|
310
|
+
get(name) {
|
|
311
|
+
const v = this.headerMap.get(name.toLowerCase());
|
|
312
|
+
return v == null ? null : v;
|
|
313
|
+
}
|
|
314
|
+
has(name) {
|
|
315
|
+
return this.headerMap.has(name.toLowerCase());
|
|
316
|
+
}
|
|
317
|
+
delete(name) {
|
|
318
|
+
this.headerMap.delete(name.toLowerCase());
|
|
319
|
+
}
|
|
320
|
+
forEach(callback) {
|
|
321
|
+
for (const [k, v] of this.headerMap.entries()) callback(v, k, this);
|
|
322
|
+
}
|
|
323
|
+
entries() {
|
|
324
|
+
return this.headerMap.entries();
|
|
325
|
+
}
|
|
326
|
+
keys() {
|
|
327
|
+
return this.headerMap.keys();
|
|
328
|
+
}
|
|
329
|
+
values() {
|
|
330
|
+
return this.headerMap.values();
|
|
331
|
+
}
|
|
332
|
+
[Symbol.iterator]() {
|
|
333
|
+
return this.entries();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
class URLSearchParams {
|
|
337
|
+
constructor(init) {
|
|
338
|
+
__publicField$3(this, "params", /* @__PURE__ */ new Map());
|
|
339
|
+
if (init) {
|
|
340
|
+
if (typeof init === "string") {
|
|
341
|
+
this.parseString(init);
|
|
342
|
+
} else if (init instanceof URLSearchParams) {
|
|
343
|
+
this.params = new Map(init.params);
|
|
344
|
+
} else if (Array.isArray(init)) {
|
|
345
|
+
for (const [key, value] of init) {
|
|
346
|
+
this.append(key, value);
|
|
347
|
+
}
|
|
348
|
+
} else if (init && typeof init === "object") {
|
|
349
|
+
for (const [key, value] of Object.entries(init)) {
|
|
350
|
+
this.set(key, value);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
parseString(search) {
|
|
356
|
+
if (search.startsWith("?")) {
|
|
357
|
+
search = search.slice(1);
|
|
358
|
+
}
|
|
359
|
+
const pairs = search.split("&");
|
|
360
|
+
for (const pair of pairs) {
|
|
361
|
+
if (pair) {
|
|
362
|
+
const [key, value] = pair.split("=");
|
|
363
|
+
if (key) {
|
|
364
|
+
this.append(
|
|
365
|
+
decodeURIComponent(key),
|
|
366
|
+
value ? decodeURIComponent(value) : ""
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
append(name, value) {
|
|
373
|
+
const existing = this.params.get(name) || [];
|
|
374
|
+
existing.push(value);
|
|
375
|
+
this.params.set(name, existing);
|
|
376
|
+
}
|
|
377
|
+
delete(name) {
|
|
378
|
+
this.params.delete(name);
|
|
379
|
+
}
|
|
380
|
+
get(name) {
|
|
381
|
+
const values = this.params.get(name);
|
|
382
|
+
return values ? values[0] : null;
|
|
383
|
+
}
|
|
384
|
+
getAll(name) {
|
|
385
|
+
return this.params.get(name) || [];
|
|
386
|
+
}
|
|
387
|
+
has(name) {
|
|
388
|
+
return this.params.has(name);
|
|
389
|
+
}
|
|
390
|
+
set(name, value) {
|
|
391
|
+
this.params.set(name, [value]);
|
|
392
|
+
}
|
|
393
|
+
sort() {
|
|
394
|
+
const sortedEntries = Array.from(this.params.entries()).sort(([a], [b]) => a.localeCompare(b));
|
|
395
|
+
this.params = new Map(sortedEntries);
|
|
396
|
+
}
|
|
397
|
+
toString() {
|
|
398
|
+
const pairs = [];
|
|
399
|
+
for (const [name, values] of this.params.entries()) {
|
|
400
|
+
for (const value of values) {
|
|
401
|
+
pairs.push(
|
|
402
|
+
`${encodeURIComponent(name)}=${encodeURIComponent(value)}`
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return pairs.join("&");
|
|
407
|
+
}
|
|
408
|
+
forEach(callback) {
|
|
409
|
+
for (const [name, values] of this.params.entries()) {
|
|
410
|
+
for (const value of values) {
|
|
411
|
+
callback(value, name, this);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
keys() {
|
|
416
|
+
return this.params.keys();
|
|
417
|
+
}
|
|
418
|
+
values() {
|
|
419
|
+
const allValues = [];
|
|
420
|
+
for (const values of this.params.values()) {
|
|
421
|
+
allValues.push(...values);
|
|
422
|
+
}
|
|
423
|
+
return allValues[Symbol.iterator]();
|
|
424
|
+
}
|
|
425
|
+
entries() {
|
|
426
|
+
const allEntries = [];
|
|
427
|
+
for (const [name, values] of this.params.entries()) {
|
|
428
|
+
for (const value of values) {
|
|
429
|
+
allEntries.push([name, value]);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return allEntries[Symbol.iterator]();
|
|
433
|
+
}
|
|
434
|
+
[Symbol.iterator]() {
|
|
435
|
+
return this.entries();
|
|
436
|
+
}
|
|
437
|
+
get size() {
|
|
438
|
+
return this.params.size;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
class URL {
|
|
442
|
+
constructor(url, base) {
|
|
443
|
+
__publicField$3(this, "href");
|
|
444
|
+
__publicField$3(this, "origin");
|
|
445
|
+
__publicField$3(this, "protocol");
|
|
446
|
+
__publicField$3(this, "username");
|
|
447
|
+
__publicField$3(this, "password");
|
|
448
|
+
__publicField$3(this, "host");
|
|
449
|
+
__publicField$3(this, "hostname");
|
|
450
|
+
__publicField$3(this, "port");
|
|
451
|
+
__publicField$3(this, "pathname");
|
|
452
|
+
__publicField$3(this, "search");
|
|
453
|
+
__publicField$3(this, "searchParams");
|
|
454
|
+
__publicField$3(this, "hash");
|
|
455
|
+
let urlString;
|
|
456
|
+
if (url instanceof URL) {
|
|
457
|
+
urlString = url.href;
|
|
458
|
+
} else if (base) {
|
|
459
|
+
const baseUrl = base instanceof URL ? base.href : base;
|
|
460
|
+
urlString = this.resolve(baseUrl, url);
|
|
461
|
+
} else {
|
|
462
|
+
urlString = url;
|
|
463
|
+
}
|
|
464
|
+
const parsed = this.parseUrl(urlString);
|
|
465
|
+
this.href = urlString;
|
|
466
|
+
this.origin = `${parsed.protocol}//${parsed.host}`;
|
|
467
|
+
this.protocol = parsed.protocol;
|
|
468
|
+
this.username = parsed.username;
|
|
469
|
+
this.password = parsed.password;
|
|
470
|
+
this.host = parsed.host;
|
|
471
|
+
this.hostname = parsed.hostname;
|
|
472
|
+
this.port = parsed.port;
|
|
473
|
+
this.pathname = parsed.pathname;
|
|
474
|
+
this.search = parsed.search;
|
|
475
|
+
this.searchParams = new URLSearchParams(parsed.search);
|
|
476
|
+
this.hash = parsed.hash;
|
|
477
|
+
}
|
|
478
|
+
resolve(base, relative) {
|
|
479
|
+
if (relative.startsWith("http://") || relative.startsWith("https://")) {
|
|
480
|
+
return relative;
|
|
481
|
+
}
|
|
482
|
+
if (relative.startsWith("//")) {
|
|
483
|
+
const baseParsed2 = this.parseUrl(base);
|
|
484
|
+
return `${baseParsed2.protocol}${relative}`;
|
|
485
|
+
}
|
|
486
|
+
if (relative.startsWith("/")) {
|
|
487
|
+
const baseParsed2 = this.parseUrl(base);
|
|
488
|
+
return `${baseParsed2.protocol}//${baseParsed2.host}${relative}`;
|
|
489
|
+
}
|
|
490
|
+
const baseParsed = this.parseUrl(base);
|
|
491
|
+
const basePath = baseParsed.pathname.endsWith("/") ? baseParsed.pathname : baseParsed.pathname + "/";
|
|
492
|
+
return `${baseParsed.protocol}//${baseParsed.host}${basePath}${relative}`;
|
|
493
|
+
}
|
|
494
|
+
parseUrl(url) {
|
|
495
|
+
const urlMatch = url.match(
|
|
496
|
+
/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/
|
|
497
|
+
);
|
|
498
|
+
if (!urlMatch) {
|
|
499
|
+
throw new TypeError("Invalid URL");
|
|
500
|
+
}
|
|
501
|
+
const protocol = urlMatch[2] || "";
|
|
502
|
+
const authority = urlMatch[4] || "";
|
|
503
|
+
const pathname = urlMatch[5] || "/";
|
|
504
|
+
const search = urlMatch[7] ? `?${urlMatch[7]}` : "";
|
|
505
|
+
const hash = urlMatch[9] ? `#${urlMatch[9]}` : "";
|
|
506
|
+
if (!protocol && !authority && !pathname.startsWith("/")) {
|
|
507
|
+
if (!pathname.includes("/") && !pathname.includes(".")) {
|
|
508
|
+
throw new TypeError("Invalid URL");
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
const authMatch = authority.match(/^([^@]*)@(.+)$/);
|
|
512
|
+
let username = "";
|
|
513
|
+
let password = "";
|
|
514
|
+
let host = authority;
|
|
515
|
+
if (authMatch) {
|
|
516
|
+
const userInfo = authMatch[1];
|
|
517
|
+
host = authMatch[2];
|
|
518
|
+
const userMatch = userInfo.match(/^([^:]*):?(.*)$/);
|
|
519
|
+
if (userMatch) {
|
|
520
|
+
username = userMatch[1] || "";
|
|
521
|
+
password = userMatch[2] || "";
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
const hostMatch = host.match(/^([^:]+):?(\d*)$/);
|
|
525
|
+
const hostname = hostMatch ? hostMatch[1] : host;
|
|
526
|
+
const port = hostMatch && hostMatch[2] ? hostMatch[2] : "";
|
|
527
|
+
return {
|
|
528
|
+
protocol: protocol ? `${protocol}:` : "",
|
|
529
|
+
username,
|
|
530
|
+
password,
|
|
531
|
+
host,
|
|
532
|
+
hostname,
|
|
533
|
+
port,
|
|
534
|
+
pathname,
|
|
535
|
+
search,
|
|
536
|
+
hash
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
toString() {
|
|
540
|
+
const searchString = this.searchParams.toString();
|
|
541
|
+
const search = searchString ? `?${searchString}` : "";
|
|
542
|
+
return `${this.protocol}//${this.host}${this.pathname}${search}${this.hash}`;
|
|
543
|
+
}
|
|
544
|
+
toJSON() {
|
|
545
|
+
return this.toString();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
var __defProp$2 = Object.defineProperty;
|
|
550
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
551
|
+
var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
552
|
+
class Request {
|
|
553
|
+
constructor(input, init) {
|
|
554
|
+
__publicField$2(this, "url");
|
|
555
|
+
__publicField$2(this, "method");
|
|
556
|
+
__publicField$2(this, "headers");
|
|
557
|
+
__publicField$2(this, "body");
|
|
558
|
+
__publicField$2(this, "timeout");
|
|
559
|
+
__publicField$2(this, "signal");
|
|
560
|
+
if (typeof input === "string") {
|
|
561
|
+
this.url = input;
|
|
562
|
+
} else if (input == null ? void 0 : input.url) {
|
|
563
|
+
this.url = String(input.url);
|
|
564
|
+
} else if (typeof (input == null ? void 0 : input.toString) === "function") {
|
|
565
|
+
this.url = String(input.toString());
|
|
566
|
+
} else {
|
|
567
|
+
throw new Error("Invalid input for Request");
|
|
568
|
+
}
|
|
569
|
+
this.method = ((init == null ? void 0 : init.method) || "GET").toUpperCase();
|
|
570
|
+
this.headers = (init == null ? void 0 : init.headers) instanceof Headers ? init.headers : new Headers(init == null ? void 0 : init.headers);
|
|
571
|
+
this.body = init == null ? void 0 : init.body;
|
|
572
|
+
this.timeout = init == null ? void 0 : init.timeout;
|
|
573
|
+
this.signal = (init == null ? void 0 : init.signal) || void 0;
|
|
574
|
+
}
|
|
575
|
+
clone() {
|
|
576
|
+
return new Request(this.url, {
|
|
577
|
+
method: this.method,
|
|
578
|
+
headers: this.headers,
|
|
579
|
+
body: this.body,
|
|
580
|
+
timeout: this.timeout
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
toString() {
|
|
584
|
+
return this.url;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
var __defProp$1 = Object.defineProperty;
|
|
589
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
590
|
+
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
591
|
+
class Response {
|
|
592
|
+
constructor(body, init) {
|
|
593
|
+
__publicField$1(this, "bodyData");
|
|
594
|
+
__publicField$1(this, "status");
|
|
595
|
+
__publicField$1(this, "statusText");
|
|
596
|
+
__publicField$1(this, "headers");
|
|
597
|
+
__publicField$1(this, "ok");
|
|
598
|
+
var _a, _b;
|
|
599
|
+
this.bodyData = body;
|
|
600
|
+
this.status = (_a = init == null ? void 0 : init.status) != null ? _a : 200;
|
|
601
|
+
this.statusText = (_b = init == null ? void 0 : init.statusText) != null ? _b : "";
|
|
602
|
+
this.headers = normalizeHeaders(init == null ? void 0 : init.headers);
|
|
603
|
+
this.ok = this.status >= 200 && this.status < 300;
|
|
604
|
+
}
|
|
605
|
+
async text() {
|
|
606
|
+
if (typeof this.bodyData === "string") return this.bodyData;
|
|
607
|
+
if (this.bodyData == null) return "";
|
|
608
|
+
if (typeof this.bodyData === "object") return JSON.stringify(this.bodyData);
|
|
609
|
+
return String(this.bodyData);
|
|
610
|
+
}
|
|
611
|
+
async json() {
|
|
612
|
+
if (typeof this.bodyData === "string") {
|
|
613
|
+
try {
|
|
614
|
+
return JSON.parse(this.bodyData);
|
|
615
|
+
} catch {
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return this.bodyData;
|
|
619
|
+
}
|
|
620
|
+
async arrayBuffer() {
|
|
621
|
+
const text = await this.text();
|
|
622
|
+
const encoder = new TextEncoder();
|
|
623
|
+
return encoder.encode(text).buffer;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
function normalizeHeaders(h) {
|
|
627
|
+
if (!h) return new Headers();
|
|
628
|
+
return new Headers(h);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
var __defProp = Object.defineProperty;
|
|
632
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
633
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
634
|
+
class AbortSignal {
|
|
635
|
+
constructor() {
|
|
636
|
+
__publicField(this, "_aborted", false);
|
|
637
|
+
__publicField(this, "listeners", /* @__PURE__ */ new Set());
|
|
638
|
+
__publicField(this, "onabort", null);
|
|
639
|
+
}
|
|
640
|
+
get aborted() {
|
|
641
|
+
return this._aborted;
|
|
642
|
+
}
|
|
643
|
+
// 供控制器触发
|
|
644
|
+
_trigger() {
|
|
645
|
+
if (this._aborted) return;
|
|
646
|
+
this._aborted = true;
|
|
647
|
+
if (typeof this.onabort === "function") {
|
|
648
|
+
try {
|
|
649
|
+
this.onabort();
|
|
650
|
+
} catch {
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
for (const l of Array.from(this.listeners)) {
|
|
654
|
+
try {
|
|
655
|
+
l();
|
|
656
|
+
} catch {
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
this.listeners.clear();
|
|
660
|
+
}
|
|
661
|
+
addEventListener(_, listener) {
|
|
662
|
+
if (this._aborted) {
|
|
663
|
+
try {
|
|
664
|
+
listener();
|
|
665
|
+
} catch {
|
|
666
|
+
}
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
this.listeners.add(listener);
|
|
670
|
+
}
|
|
671
|
+
removeEventListener(_, listener) {
|
|
672
|
+
this.listeners.delete(listener);
|
|
673
|
+
}
|
|
674
|
+
toString() {
|
|
675
|
+
return "[object AbortSignal]";
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
class AbortController {
|
|
679
|
+
constructor() {
|
|
680
|
+
__publicField(this, "_signal");
|
|
681
|
+
this._signal = new AbortSignal();
|
|
682
|
+
}
|
|
683
|
+
get signal() {
|
|
684
|
+
return this._signal;
|
|
685
|
+
}
|
|
686
|
+
abort() {
|
|
687
|
+
this._signal._trigger();
|
|
688
|
+
}
|
|
689
|
+
toString() {
|
|
690
|
+
return "[object AbortController]";
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
function polyfill(_global) {
|
|
695
|
+
_global.URL = URL;
|
|
696
|
+
_global.URLSearchParams = URLSearchParams;
|
|
697
|
+
_global.Headers = Headers;
|
|
698
|
+
_global.Request = Request;
|
|
699
|
+
_global.Response = Response;
|
|
700
|
+
_global.AbortController = AbortController;
|
|
701
|
+
_global.AbortSignal = AbortSignal;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
export { AUTH_BASE_PATH, AbortController, AbortSignal, ENTITIES_BASE_PATH, FILE_STORAGE_BASE_PATH, FetchResponse, GENERATE_UPLOAD_URL_PATH, Headers, LOGIN_TOKEN_KEY, LOGIN_USER_PROFILE_KEY, NvwaAuthClient, NvwaEdgeFunctions, NvwaFileStorage, NvwaHttpClient, Request, Response, URL, URLSearchParams, createPostgrestClient, polyfill };
|
|
705
|
+
//# sourceMappingURL=index.mjs.map
|