@homecloud-platform/sdk 0.5.0 → 0.5.3
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/README.md +2 -2
- package/package.json +1 -1
- package/src/management.js +11 -2
- package/src/mq.js +88 -5
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
Part of the **homecloud-sdk** monorepo — Python at repo root; Node in `js/`.
|
|
4
4
|
|
|
5
|
-
**Expectation:** Node supports the **same product surface** as Python (`homecloud-sdk`), released under the **same** `v*` version. See [PARITY.md](./PARITY.md)
|
|
5
|
+
**Expectation:** Node supports the **same product surface** as Python (`homecloud-sdk`), released under the **same** `v*` version. See [PARITY.md](./PARITY.md).
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Shipped as of **0.5.0**: SO / MQ / Secrets / Mail / Functions / Apps / Accounts / Queues, typed errors, `fromEnv` / `fromProfile` / `fromCredentials` / `fromSts` / `fromFunctionContext`, console login helpers.
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
package/package.json
CHANGED
package/src/management.js
CHANGED
|
@@ -39,14 +39,23 @@ class QueuesAPI {
|
|
|
39
39
|
this._c = client;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
async list() {
|
|
42
|
+
async list({ live = false } = {}) {
|
|
43
43
|
this._c.requireConsole();
|
|
44
44
|
const data = await this._c.consoleRequest(
|
|
45
45
|
"GET",
|
|
46
|
-
`accounts/${this._c.accountId}/queues
|
|
46
|
+
`accounts/${this._c.accountId}/queues`,
|
|
47
|
+
{ params: live ? { live: "true" } : undefined }
|
|
47
48
|
);
|
|
48
49
|
return data.items || [];
|
|
49
50
|
}
|
|
51
|
+
|
|
52
|
+
async get(queueName) {
|
|
53
|
+
this._c.requireConsole();
|
|
54
|
+
return this._c.consoleRequest(
|
|
55
|
+
"GET",
|
|
56
|
+
`accounts/${this._c.accountId}/queues/${encodeURIComponent(queueName)}`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
50
59
|
}
|
|
51
60
|
|
|
52
61
|
class FunctionsAPI {
|
package/src/mq.js
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const MQ_BATCH_MAX = 10;
|
|
4
|
+
|
|
5
|
+
function entryBodyStr(value) {
|
|
6
|
+
if (typeof value === "string") {
|
|
7
|
+
if (!value) throw new Error("mq.send batch entry body must be non-empty");
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
return JSON.stringify(value);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function buildMqBatchEntries(items) {
|
|
14
|
+
if (!Array.isArray(items) || items.length < 1 || items.length > MQ_BATCH_MAX) {
|
|
15
|
+
throw new Error(`mq.send batch requires 1–${MQ_BATCH_MAX} messages`);
|
|
16
|
+
}
|
|
17
|
+
const entries = [];
|
|
18
|
+
const seenIds = new Set();
|
|
19
|
+
for (let index = 0; index < items.length; index += 1) {
|
|
20
|
+
const item = items[index];
|
|
21
|
+
let entry;
|
|
22
|
+
if (item && typeof item === "object" && typeof item.body === "string") {
|
|
23
|
+
const entryId = item.id != null ? String(item.id) : String(index);
|
|
24
|
+
if (!item.body) throw new Error("mq.send batch entry body must be non-empty");
|
|
25
|
+
entry = { id: entryId, body: item.body };
|
|
26
|
+
if (item.headers) entry.headers = item.headers;
|
|
27
|
+
} else {
|
|
28
|
+
entry = { id: String(index), body: entryBodyStr(item) };
|
|
29
|
+
}
|
|
30
|
+
if (seenIds.has(entry.id)) {
|
|
31
|
+
throw new Error("mq.send batch entry ids must be unique");
|
|
32
|
+
}
|
|
33
|
+
seenIds.add(entry.id);
|
|
34
|
+
entries.push(entry);
|
|
35
|
+
}
|
|
36
|
+
return entries;
|
|
37
|
+
}
|
|
38
|
+
|
|
3
39
|
class MqAPI {
|
|
4
40
|
constructor(client) {
|
|
5
41
|
this._c = client;
|
|
@@ -8,6 +44,15 @@ class MqAPI {
|
|
|
8
44
|
async send(queueName, body, { headers } = {}) {
|
|
9
45
|
this._c.requireAccessKey();
|
|
10
46
|
const accountId = this._c.accountId;
|
|
47
|
+
if (Array.isArray(body)) {
|
|
48
|
+
if (headers != null) {
|
|
49
|
+
throw new Error("headers is only supported for single mq.send, not batch");
|
|
50
|
+
}
|
|
51
|
+
const reqPath = `/${accountId}/${queueName}/messages/batch`;
|
|
52
|
+
return this._c.dataPlaneRequest("mq", "POST", reqPath, {
|
|
53
|
+
json: { entries: buildMqBatchEntries(body) },
|
|
54
|
+
});
|
|
55
|
+
}
|
|
11
56
|
const reqPath = `/${accountId}/${queueName}/messages`;
|
|
12
57
|
const bodyStr = typeof body === "string" ? body : JSON.stringify(body);
|
|
13
58
|
const payload = { body: bodyStr };
|
|
@@ -15,15 +60,53 @@ class MqAPI {
|
|
|
15
60
|
return this._c.dataPlaneRequest("mq", "POST", reqPath, { json: payload });
|
|
16
61
|
}
|
|
17
62
|
|
|
18
|
-
async receive(queueName, { maxMessages = 1, waitSeconds = 20 } = {}) {
|
|
63
|
+
async receive(queueName, { maxMessages = 1, waitSeconds = 20, delete: shouldDelete = false } = {}) {
|
|
19
64
|
this._c.requireAccessKey();
|
|
20
65
|
const accountId = this._c.accountId;
|
|
21
66
|
const reqPath = `/${accountId}/${queueName}/messages`;
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
});
|
|
67
|
+
const params = { max_messages: maxMessages, wait_seconds: waitSeconds };
|
|
68
|
+
if (shouldDelete) params.delete = "true";
|
|
69
|
+
const data = await this._c.dataPlaneRequest("mq", "GET", reqPath, { params });
|
|
25
70
|
return data.items || [];
|
|
26
71
|
}
|
|
72
|
+
|
|
73
|
+
async delete(queueName, sequence) {
|
|
74
|
+
this._c.requireAccessKey();
|
|
75
|
+
const accountId = this._c.accountId;
|
|
76
|
+
const reqPath = `/${accountId}/${queueName}/messages/${sequence}`;
|
|
77
|
+
await this._c.dataPlaneRequest("mq", "DELETE", reqPath);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async purge(queueName) {
|
|
81
|
+
this._c.requireAccessKey();
|
|
82
|
+
const accountId = this._c.accountId;
|
|
83
|
+
const reqPath = `/${accountId}/${queueName}/purge`;
|
|
84
|
+
await this._c.dataPlaneRequest("mq", "POST", reqPath);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async receiveDlq(queueName, { maxMessages = 1, waitSeconds = 20, delete: shouldDelete = false } = {}) {
|
|
88
|
+
this._c.requireAccessKey();
|
|
89
|
+
const accountId = this._c.accountId;
|
|
90
|
+
const reqPath = `/${accountId}/${queueName}/dlq/messages`;
|
|
91
|
+
const params = { max_messages: maxMessages, wait_seconds: waitSeconds };
|
|
92
|
+
if (shouldDelete) params.delete = "true";
|
|
93
|
+
const data = await this._c.dataPlaneRequest("mq", "GET", reqPath, { params });
|
|
94
|
+
return data.items || [];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async deleteDlq(queueName, sequence) {
|
|
98
|
+
this._c.requireAccessKey();
|
|
99
|
+
const accountId = this._c.accountId;
|
|
100
|
+
const reqPath = `/${accountId}/${queueName}/dlq/messages/${sequence}`;
|
|
101
|
+
await this._c.dataPlaneRequest("mq", "DELETE", reqPath);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async purgeDlq(queueName) {
|
|
105
|
+
this._c.requireAccessKey();
|
|
106
|
+
const accountId = this._c.accountId;
|
|
107
|
+
const reqPath = `/${accountId}/${queueName}/dlq/purge`;
|
|
108
|
+
await this._c.dataPlaneRequest("mq", "POST", reqPath);
|
|
109
|
+
}
|
|
27
110
|
}
|
|
28
111
|
|
|
29
|
-
module.exports = { MqAPI };
|
|
112
|
+
module.exports = { MqAPI, buildMqBatchEntries, MQ_BATCH_MAX };
|