@homecloud-platform/sdk 0.5.10 → 0.5.11
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/package.json +1 -1
- package/src/client.js +48 -0
- package/src/management.js +14 -9
- package/src/so.js +23 -7
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -153,6 +153,10 @@ class HomeCloud {
|
|
|
153
153
|
if (!this.accessToken) throw new NotLoggedInError();
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
+
get hasAccessKey() {
|
|
157
|
+
return Boolean(this.accessKeyId && this.secretAccessKey);
|
|
158
|
+
}
|
|
159
|
+
|
|
156
160
|
baseUrl(service) {
|
|
157
161
|
if (this.dataPlaneBases[service]) return this.dataPlaneBases[service].replace(/\/$/, "");
|
|
158
162
|
if (service === "so") return soUrl(this.apex);
|
|
@@ -320,6 +324,50 @@ class HomeCloud {
|
|
|
320
324
|
return data;
|
|
321
325
|
}
|
|
322
326
|
|
|
327
|
+
async consoleSignedRequest(method, pathSeg, { json, params } = {}) {
|
|
328
|
+
this.requireAccessKey();
|
|
329
|
+
await this.ensureAccountId();
|
|
330
|
+
const base = (this.consoleBaseUrl || consoleUrl(this.apex)).replace(/\/$/, "");
|
|
331
|
+
const rel = String(pathSeg).replace(/^\/+/, "");
|
|
332
|
+
const url = new URL(`${base}/${rel}`);
|
|
333
|
+
if (params) {
|
|
334
|
+
for (const [k, v] of Object.entries(params)) {
|
|
335
|
+
if (v !== undefined && v !== null) url.searchParams.set(k, String(v));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const headers = signRequestHeaders({
|
|
339
|
+
accessKeyId: this.accessKeyId,
|
|
340
|
+
secret: this.secretAccessKey,
|
|
341
|
+
method,
|
|
342
|
+
path: `/api/v1/${rel}`,
|
|
343
|
+
accountId: this.accountId,
|
|
344
|
+
sessionToken: this.sessionToken,
|
|
345
|
+
});
|
|
346
|
+
const init = { method, headers, signal: AbortSignal.timeout(this.timeoutMs) };
|
|
347
|
+
if (json !== undefined) {
|
|
348
|
+
headers["Content-Type"] = "application/json";
|
|
349
|
+
init.body = JSON.stringify(json);
|
|
350
|
+
}
|
|
351
|
+
const res = await fetch(url, init);
|
|
352
|
+
if (method === "DELETE" && res.status === 204) return null;
|
|
353
|
+
const text = await res.text();
|
|
354
|
+
let data = null;
|
|
355
|
+
if (text) {
|
|
356
|
+
try {
|
|
357
|
+
data = JSON.parse(text);
|
|
358
|
+
} catch (_) {
|
|
359
|
+
data = { raw: text };
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (!res.ok) {
|
|
363
|
+
throw errorFromStatus(res.status, {
|
|
364
|
+
detail: data && (data.detail !== undefined ? data.detail : data),
|
|
365
|
+
url: String(url),
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
return data;
|
|
369
|
+
}
|
|
370
|
+
|
|
323
371
|
async consoleRequestBytes(method, pathSeg) {
|
|
324
372
|
this.requireConsole();
|
|
325
373
|
const base = (this.consoleBaseUrl || consoleUrl(this.apex)).replace(/\/$/, "");
|
package/src/management.js
CHANGED
|
@@ -40,21 +40,26 @@ class QueuesAPI {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
async list({ live = false } = {}) {
|
|
43
|
+
await this._c.ensureAccountId();
|
|
44
|
+
const path = `accounts/${this._c.accountId}/queues`;
|
|
45
|
+
const params = live ? { live: "true" } : undefined;
|
|
46
|
+
if (this._c.hasAccessKey) {
|
|
47
|
+
const data = await this._c.consoleSignedRequest("GET", path, { params });
|
|
48
|
+
return data.items || [];
|
|
49
|
+
}
|
|
43
50
|
this._c.requireConsole();
|
|
44
|
-
const data = await this._c.consoleRequest(
|
|
45
|
-
"GET",
|
|
46
|
-
`accounts/${this._c.accountId}/queues`,
|
|
47
|
-
{ params: live ? { live: "true" } : undefined }
|
|
48
|
-
);
|
|
51
|
+
const data = await this._c.consoleRequest("GET", path, { params });
|
|
49
52
|
return data.items || [];
|
|
50
53
|
}
|
|
51
54
|
|
|
52
55
|
async get(queueName) {
|
|
56
|
+
await this._c.ensureAccountId();
|
|
57
|
+
const path = `accounts/${this._c.accountId}/queues/${encodeURIComponent(queueName)}`;
|
|
58
|
+
if (this._c.hasAccessKey) {
|
|
59
|
+
return this._c.consoleSignedRequest("GET", path);
|
|
60
|
+
}
|
|
53
61
|
this._c.requireConsole();
|
|
54
|
-
return this._c.consoleRequest(
|
|
55
|
-
"GET",
|
|
56
|
-
`accounts/${this._c.accountId}/queues/${encodeURIComponent(queueName)}`
|
|
57
|
-
);
|
|
62
|
+
return this._c.consoleRequest("GET", path);
|
|
58
63
|
}
|
|
59
64
|
}
|
|
60
65
|
|
package/src/so.js
CHANGED
|
@@ -40,6 +40,15 @@ class SoAPI {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
async listBuckets() {
|
|
43
|
+
await this._c.ensureAccountId();
|
|
44
|
+
if (this._c.hasAccessKey) {
|
|
45
|
+
const data = await this._c.dataPlaneRequest(
|
|
46
|
+
"so",
|
|
47
|
+
"GET",
|
|
48
|
+
`/${this._c.accountId}/buckets`
|
|
49
|
+
);
|
|
50
|
+
return data.items || [];
|
|
51
|
+
}
|
|
43
52
|
this._c.requireConsole();
|
|
44
53
|
const data = await this._c.consoleRequest(
|
|
45
54
|
"GET",
|
|
@@ -49,18 +58,25 @@ class SoAPI {
|
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
async createBucket(name) {
|
|
61
|
+
await this._c.ensureAccountId();
|
|
62
|
+
const path = `accounts/${this._c.accountId}/storage/buckets`;
|
|
63
|
+
const body = { name: String(name).trim().toLowerCase() };
|
|
64
|
+
if (this._c.hasAccessKey) {
|
|
65
|
+
return this._c.consoleSignedRequest("POST", path, { json: body });
|
|
66
|
+
}
|
|
52
67
|
this._c.requireConsole();
|
|
53
|
-
return this._c.consoleRequest("POST",
|
|
54
|
-
json: { name: String(name).trim().toLowerCase() },
|
|
55
|
-
});
|
|
68
|
+
return this._c.consoleRequest("POST", path, { json: body });
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
async deleteBucket(name) {
|
|
72
|
+
await this._c.ensureAccountId();
|
|
73
|
+
const path = `accounts/${this._c.accountId}/storage/buckets/${String(name).trim().toLowerCase()}`;
|
|
74
|
+
if (this._c.hasAccessKey) {
|
|
75
|
+
await this._c.consoleSignedRequest("DELETE", path);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
59
78
|
this._c.requireConsole();
|
|
60
|
-
await this._c.consoleRequest(
|
|
61
|
-
"DELETE",
|
|
62
|
-
`accounts/${this._c.accountId}/storage/buckets/${String(name).trim().toLowerCase()}`
|
|
63
|
-
);
|
|
79
|
+
await this._c.consoleRequest("DELETE", path);
|
|
64
80
|
}
|
|
65
81
|
|
|
66
82
|
async listObjects(bucketName, { prefix = "", recursive = false, page = 1, pageSize = 100 } = {}) {
|