@zooid/sdk 0.0.1 → 0.0.10
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 +65 -28
- package/dist/index.js +65 -28
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -56,11 +56,19 @@ var ZooidClient = class {
|
|
|
56
56
|
if (body !== void 0) {
|
|
57
57
|
headers["Content-Type"] = "application/json";
|
|
58
58
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
let res;
|
|
60
|
+
try {
|
|
61
|
+
res = await this._fetch(this.server + path, {
|
|
62
|
+
method,
|
|
63
|
+
headers,
|
|
64
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0
|
|
65
|
+
});
|
|
66
|
+
} catch (err) {
|
|
67
|
+
throw new ZooidError(
|
|
68
|
+
0,
|
|
69
|
+
`Cannot connect to ${this.server} \u2014 ${err instanceof Error ? err.message : "network error"}`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
64
72
|
if (!res.ok) {
|
|
65
73
|
let message = `HTTP ${res.status}`;
|
|
66
74
|
try {
|
|
@@ -89,12 +97,19 @@ var ZooidClient = class {
|
|
|
89
97
|
}
|
|
90
98
|
/** List all channels via `GET /api/v1/channels`. */
|
|
91
99
|
async listChannels() {
|
|
92
|
-
const res = await this.request(
|
|
100
|
+
const res = await this.request(
|
|
101
|
+
"GET",
|
|
102
|
+
"/api/v1/channels"
|
|
103
|
+
);
|
|
93
104
|
return res.channels;
|
|
94
105
|
}
|
|
95
106
|
/** Create a new channel via `POST /api/v1/channels`. Requires admin token. */
|
|
96
107
|
async createChannel(options) {
|
|
97
|
-
return this.request(
|
|
108
|
+
return this.request(
|
|
109
|
+
"POST",
|
|
110
|
+
"/api/v1/channels",
|
|
111
|
+
options
|
|
112
|
+
);
|
|
98
113
|
}
|
|
99
114
|
/** Generate a signed claim for the Zooid Directory. Requires admin token. */
|
|
100
115
|
async getClaim(channels, action) {
|
|
@@ -116,7 +131,11 @@ var ZooidClient = class {
|
|
|
116
131
|
if (options.type !== void 0) {
|
|
117
132
|
body.type = options.type;
|
|
118
133
|
}
|
|
119
|
-
return this.request(
|
|
134
|
+
return this.request(
|
|
135
|
+
"POST",
|
|
136
|
+
`/api/v1/channels/${channelId}/events`,
|
|
137
|
+
body
|
|
138
|
+
);
|
|
120
139
|
}
|
|
121
140
|
/** Publish multiple events in a single request. Requires a publish-scoped token. */
|
|
122
141
|
async publishBatch(channelId, events) {
|
|
@@ -145,19 +164,23 @@ var ZooidClient = class {
|
|
|
145
164
|
let waiting = null;
|
|
146
165
|
let done = false;
|
|
147
166
|
let unsub = null;
|
|
148
|
-
this.subscribe(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
waiting
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
167
|
+
this.subscribe(
|
|
168
|
+
channelId,
|
|
169
|
+
(event) => {
|
|
170
|
+
if (waiting) {
|
|
171
|
+
const resolve = waiting;
|
|
172
|
+
waiting = null;
|
|
173
|
+
resolve({ value: event, done: false });
|
|
174
|
+
} else {
|
|
175
|
+
buffer.push(event);
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
mode: options.mode,
|
|
180
|
+
interval: options.interval,
|
|
181
|
+
type: options.type
|
|
155
182
|
}
|
|
156
|
-
|
|
157
|
-
mode: options.mode,
|
|
158
|
-
interval: options.interval,
|
|
159
|
-
type: options.type
|
|
160
|
-
}).then((fn) => {
|
|
183
|
+
).then((fn) => {
|
|
161
184
|
unsub = fn;
|
|
162
185
|
if (done) fn();
|
|
163
186
|
});
|
|
@@ -177,7 +200,10 @@ var ZooidClient = class {
|
|
|
177
200
|
return Promise.resolve({ value: buffer.shift(), done: false });
|
|
178
201
|
}
|
|
179
202
|
if (done) {
|
|
180
|
-
return Promise.resolve({
|
|
203
|
+
return Promise.resolve({
|
|
204
|
+
value: void 0,
|
|
205
|
+
done: true
|
|
206
|
+
});
|
|
181
207
|
}
|
|
182
208
|
return new Promise((resolve) => {
|
|
183
209
|
waiting = resolve;
|
|
@@ -185,7 +211,10 @@ var ZooidClient = class {
|
|
|
185
211
|
},
|
|
186
212
|
return() {
|
|
187
213
|
stream.close();
|
|
188
|
-
return Promise.resolve({
|
|
214
|
+
return Promise.resolve({
|
|
215
|
+
value: void 0,
|
|
216
|
+
done: true
|
|
217
|
+
});
|
|
189
218
|
}
|
|
190
219
|
};
|
|
191
220
|
}
|
|
@@ -196,7 +225,8 @@ var ZooidClient = class {
|
|
|
196
225
|
async poll(channelId, options) {
|
|
197
226
|
const params = new URLSearchParams();
|
|
198
227
|
if (options?.cursor) params.set("cursor", options.cursor);
|
|
199
|
-
if (options?.limit !== void 0)
|
|
228
|
+
if (options?.limit !== void 0)
|
|
229
|
+
params.set("limit", String(options.limit));
|
|
200
230
|
if (options?.type) params.set("type", options.type);
|
|
201
231
|
if (options?.since) params.set("since", options.since);
|
|
202
232
|
const qs = params.toString();
|
|
@@ -205,10 +235,14 @@ var ZooidClient = class {
|
|
|
205
235
|
}
|
|
206
236
|
/** Register a webhook to receive events via POST. */
|
|
207
237
|
async registerWebhook(channelId, url, options) {
|
|
208
|
-
return this.request(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
238
|
+
return this.request(
|
|
239
|
+
"POST",
|
|
240
|
+
`/api/v1/channels/${channelId}/webhooks`,
|
|
241
|
+
{
|
|
242
|
+
url,
|
|
243
|
+
...options
|
|
244
|
+
}
|
|
245
|
+
);
|
|
212
246
|
}
|
|
213
247
|
/** Remove a webhook registration. Requires admin token. */
|
|
214
248
|
async removeWebhook(channelId, webhookId) {
|
|
@@ -262,7 +296,10 @@ var ZooidClient = class {
|
|
|
262
296
|
tryWs(false).then(resolve, reject);
|
|
263
297
|
}, 1e3);
|
|
264
298
|
} else if (mode === "auto") {
|
|
265
|
-
this.startPolling(channelId, callback, options).then(
|
|
299
|
+
this.startPolling(channelId, callback, options).then(
|
|
300
|
+
resolve,
|
|
301
|
+
reject
|
|
302
|
+
);
|
|
266
303
|
} else {
|
|
267
304
|
reject(new Error("WebSocket connection failed"));
|
|
268
305
|
}
|
package/dist/index.js
CHANGED
|
@@ -28,11 +28,19 @@ var ZooidClient = class {
|
|
|
28
28
|
if (body !== void 0) {
|
|
29
29
|
headers["Content-Type"] = "application/json";
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
let res;
|
|
32
|
+
try {
|
|
33
|
+
res = await this._fetch(this.server + path, {
|
|
34
|
+
method,
|
|
35
|
+
headers,
|
|
36
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0
|
|
37
|
+
});
|
|
38
|
+
} catch (err) {
|
|
39
|
+
throw new ZooidError(
|
|
40
|
+
0,
|
|
41
|
+
`Cannot connect to ${this.server} \u2014 ${err instanceof Error ? err.message : "network error"}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
36
44
|
if (!res.ok) {
|
|
37
45
|
let message = `HTTP ${res.status}`;
|
|
38
46
|
try {
|
|
@@ -61,12 +69,19 @@ var ZooidClient = class {
|
|
|
61
69
|
}
|
|
62
70
|
/** List all channels via `GET /api/v1/channels`. */
|
|
63
71
|
async listChannels() {
|
|
64
|
-
const res = await this.request(
|
|
72
|
+
const res = await this.request(
|
|
73
|
+
"GET",
|
|
74
|
+
"/api/v1/channels"
|
|
75
|
+
);
|
|
65
76
|
return res.channels;
|
|
66
77
|
}
|
|
67
78
|
/** Create a new channel via `POST /api/v1/channels`. Requires admin token. */
|
|
68
79
|
async createChannel(options) {
|
|
69
|
-
return this.request(
|
|
80
|
+
return this.request(
|
|
81
|
+
"POST",
|
|
82
|
+
"/api/v1/channels",
|
|
83
|
+
options
|
|
84
|
+
);
|
|
70
85
|
}
|
|
71
86
|
/** Generate a signed claim for the Zooid Directory. Requires admin token. */
|
|
72
87
|
async getClaim(channels, action) {
|
|
@@ -88,7 +103,11 @@ var ZooidClient = class {
|
|
|
88
103
|
if (options.type !== void 0) {
|
|
89
104
|
body.type = options.type;
|
|
90
105
|
}
|
|
91
|
-
return this.request(
|
|
106
|
+
return this.request(
|
|
107
|
+
"POST",
|
|
108
|
+
`/api/v1/channels/${channelId}/events`,
|
|
109
|
+
body
|
|
110
|
+
);
|
|
92
111
|
}
|
|
93
112
|
/** Publish multiple events in a single request. Requires a publish-scoped token. */
|
|
94
113
|
async publishBatch(channelId, events) {
|
|
@@ -117,19 +136,23 @@ var ZooidClient = class {
|
|
|
117
136
|
let waiting = null;
|
|
118
137
|
let done = false;
|
|
119
138
|
let unsub = null;
|
|
120
|
-
this.subscribe(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
waiting
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
139
|
+
this.subscribe(
|
|
140
|
+
channelId,
|
|
141
|
+
(event) => {
|
|
142
|
+
if (waiting) {
|
|
143
|
+
const resolve = waiting;
|
|
144
|
+
waiting = null;
|
|
145
|
+
resolve({ value: event, done: false });
|
|
146
|
+
} else {
|
|
147
|
+
buffer.push(event);
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
mode: options.mode,
|
|
152
|
+
interval: options.interval,
|
|
153
|
+
type: options.type
|
|
127
154
|
}
|
|
128
|
-
|
|
129
|
-
mode: options.mode,
|
|
130
|
-
interval: options.interval,
|
|
131
|
-
type: options.type
|
|
132
|
-
}).then((fn) => {
|
|
155
|
+
).then((fn) => {
|
|
133
156
|
unsub = fn;
|
|
134
157
|
if (done) fn();
|
|
135
158
|
});
|
|
@@ -149,7 +172,10 @@ var ZooidClient = class {
|
|
|
149
172
|
return Promise.resolve({ value: buffer.shift(), done: false });
|
|
150
173
|
}
|
|
151
174
|
if (done) {
|
|
152
|
-
return Promise.resolve({
|
|
175
|
+
return Promise.resolve({
|
|
176
|
+
value: void 0,
|
|
177
|
+
done: true
|
|
178
|
+
});
|
|
153
179
|
}
|
|
154
180
|
return new Promise((resolve) => {
|
|
155
181
|
waiting = resolve;
|
|
@@ -157,7 +183,10 @@ var ZooidClient = class {
|
|
|
157
183
|
},
|
|
158
184
|
return() {
|
|
159
185
|
stream.close();
|
|
160
|
-
return Promise.resolve({
|
|
186
|
+
return Promise.resolve({
|
|
187
|
+
value: void 0,
|
|
188
|
+
done: true
|
|
189
|
+
});
|
|
161
190
|
}
|
|
162
191
|
};
|
|
163
192
|
}
|
|
@@ -168,7 +197,8 @@ var ZooidClient = class {
|
|
|
168
197
|
async poll(channelId, options) {
|
|
169
198
|
const params = new URLSearchParams();
|
|
170
199
|
if (options?.cursor) params.set("cursor", options.cursor);
|
|
171
|
-
if (options?.limit !== void 0)
|
|
200
|
+
if (options?.limit !== void 0)
|
|
201
|
+
params.set("limit", String(options.limit));
|
|
172
202
|
if (options?.type) params.set("type", options.type);
|
|
173
203
|
if (options?.since) params.set("since", options.since);
|
|
174
204
|
const qs = params.toString();
|
|
@@ -177,10 +207,14 @@ var ZooidClient = class {
|
|
|
177
207
|
}
|
|
178
208
|
/** Register a webhook to receive events via POST. */
|
|
179
209
|
async registerWebhook(channelId, url, options) {
|
|
180
|
-
return this.request(
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
210
|
+
return this.request(
|
|
211
|
+
"POST",
|
|
212
|
+
`/api/v1/channels/${channelId}/webhooks`,
|
|
213
|
+
{
|
|
214
|
+
url,
|
|
215
|
+
...options
|
|
216
|
+
}
|
|
217
|
+
);
|
|
184
218
|
}
|
|
185
219
|
/** Remove a webhook registration. Requires admin token. */
|
|
186
220
|
async removeWebhook(channelId, webhookId) {
|
|
@@ -234,7 +268,10 @@ var ZooidClient = class {
|
|
|
234
268
|
tryWs(false).then(resolve, reject);
|
|
235
269
|
}, 1e3);
|
|
236
270
|
} else if (mode === "auto") {
|
|
237
|
-
this.startPolling(channelId, callback, options).then(
|
|
271
|
+
this.startPolling(channelId, callback, options).then(
|
|
272
|
+
resolve,
|
|
273
|
+
reject
|
|
274
|
+
);
|
|
238
275
|
} else {
|
|
239
276
|
reject(new Error("WebSocket connection failed"));
|
|
240
277
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zooid/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@zooid/types": "0.0.
|
|
16
|
+
"@zooid/types": "0.0.10"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@cloudflare/vitest-pool-workers": "^0.8.34",
|