memorysync-sdk 1.0.2 → 1.1.1
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 +50 -0
- package/dist/index.d.mts +414 -28
- package/dist/index.d.ts +414 -28
- package/dist/index.js +452 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +450 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,45 +1,473 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/errors.ts
|
|
2
2
|
var MemorySyncError = class extends Error {
|
|
3
|
-
constructor(message,
|
|
3
|
+
constructor(message, options = {}) {
|
|
4
4
|
super(message);
|
|
5
5
|
this.name = "MemorySyncError";
|
|
6
|
-
this.statusCode =
|
|
7
|
-
this.response =
|
|
8
|
-
this.requestId =
|
|
6
|
+
this.statusCode = options.statusCode;
|
|
7
|
+
this.response = options.response;
|
|
8
|
+
this.requestId = options.requestId;
|
|
9
9
|
}
|
|
10
10
|
};
|
|
11
11
|
var AuthError = class extends MemorySyncError {
|
|
12
|
-
constructor(message,
|
|
13
|
-
super(message,
|
|
12
|
+
constructor(message, options) {
|
|
13
|
+
super(message, options);
|
|
14
14
|
this.name = "AuthError";
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
17
|
var ValidationError = class extends MemorySyncError {
|
|
18
|
-
constructor(message,
|
|
19
|
-
super(message,
|
|
18
|
+
constructor(message, options) {
|
|
19
|
+
super(message, options);
|
|
20
20
|
this.name = "ValidationError";
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
23
|
var NotFoundError = class extends MemorySyncError {
|
|
24
|
-
constructor(message,
|
|
25
|
-
super(message,
|
|
24
|
+
constructor(message, options) {
|
|
25
|
+
super(message, options);
|
|
26
26
|
this.name = "NotFoundError";
|
|
27
27
|
}
|
|
28
28
|
};
|
|
29
29
|
var RateLimitError = class extends MemorySyncError {
|
|
30
|
-
constructor(message, retryAfterSeconds,
|
|
31
|
-
super(message,
|
|
30
|
+
constructor(message, retryAfterSeconds, options) {
|
|
31
|
+
super(message, options);
|
|
32
32
|
this.name = "RateLimitError";
|
|
33
33
|
this.retryAfterSeconds = retryAfterSeconds;
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
36
|
var ServerError = class extends MemorySyncError {
|
|
37
|
-
constructor(message,
|
|
38
|
-
super(message,
|
|
37
|
+
constructor(message, options) {
|
|
38
|
+
super(message, options);
|
|
39
39
|
this.name = "ServerError";
|
|
40
40
|
}
|
|
41
41
|
};
|
|
42
|
-
|
|
42
|
+
|
|
43
|
+
// src/control-plane.ts
|
|
44
|
+
var SDK_VERSION = "1.1.1";
|
|
45
|
+
function safeJson(text) {
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(text);
|
|
48
|
+
} catch {
|
|
49
|
+
return text;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function extractDetail(body) {
|
|
53
|
+
if (!body || typeof body !== "object") return void 0;
|
|
54
|
+
const value = body;
|
|
55
|
+
if (typeof value.detail === "string") return value.detail;
|
|
56
|
+
if (value.error && typeof value.error === "object") {
|
|
57
|
+
const error = value.error;
|
|
58
|
+
if (typeof error.message === "string") return error.message;
|
|
59
|
+
}
|
|
60
|
+
if (Array.isArray(value.detail) && value.detail.length > 0) {
|
|
61
|
+
const first = value.detail[0];
|
|
62
|
+
if (first && typeof first === "object" && typeof first.msg === "string") {
|
|
63
|
+
return first.msg;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
function extractBodyRetryAfter(body) {
|
|
69
|
+
if (!body || typeof body !== "object") return 0;
|
|
70
|
+
const value = body;
|
|
71
|
+
if (typeof value.retry_after === "number") return value.retry_after;
|
|
72
|
+
if (value.error && typeof value.error === "object") {
|
|
73
|
+
const retryAfter = value.error.retry_after;
|
|
74
|
+
if (typeof retryAfter === "number") return retryAfter;
|
|
75
|
+
}
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
function parseRetryAfter(value) {
|
|
79
|
+
if (!value) return void 0;
|
|
80
|
+
const seconds = Number(value);
|
|
81
|
+
if (Number.isFinite(seconds) && seconds >= 0) return seconds;
|
|
82
|
+
const date = Date.parse(value);
|
|
83
|
+
if (Number.isNaN(date)) return void 0;
|
|
84
|
+
return Math.max(0, Math.ceil((date - Date.now()) / 1e3));
|
|
85
|
+
}
|
|
86
|
+
function snakeToCamel(key) {
|
|
87
|
+
return key.replace(/_([a-z0-9])/g, (_, character) => character.toUpperCase());
|
|
88
|
+
}
|
|
89
|
+
var OPAQUE_RESPONSE_KEYS = /* @__PURE__ */ new Set(["payload", "metadata", "geo", "configuration"]);
|
|
90
|
+
function normalizeResponse(value) {
|
|
91
|
+
if (Array.isArray(value)) return value.map(normalizeResponse);
|
|
92
|
+
if (!value || typeof value !== "object") return value;
|
|
93
|
+
const normalized = {};
|
|
94
|
+
for (const [key, item] of Object.entries(value)) {
|
|
95
|
+
const camelKey = snakeToCamel(key);
|
|
96
|
+
normalized[camelKey] = OPAQUE_RESPONSE_KEYS.has(camelKey) ? item : normalizeResponse(item);
|
|
97
|
+
}
|
|
98
|
+
return normalized;
|
|
99
|
+
}
|
|
100
|
+
function queryString(values) {
|
|
101
|
+
const params = new URLSearchParams();
|
|
102
|
+
for (const [key, value] of Object.entries(values)) {
|
|
103
|
+
if (value !== void 0 && value !== null) params.set(key, String(value));
|
|
104
|
+
}
|
|
105
|
+
const encoded = params.toString();
|
|
106
|
+
return encoded ? `?${encoded}` : "";
|
|
107
|
+
}
|
|
108
|
+
function positiveId(value, name) {
|
|
109
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
110
|
+
throw new ValidationError(`${name} must be a positive integer`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function nonEmpty(value, name) {
|
|
114
|
+
if (typeof value !== "string" || !value.trim()) throw new ValidationError(`${name} must not be empty`);
|
|
115
|
+
}
|
|
116
|
+
function boundedInteger(value, name, minimum, maximum) {
|
|
117
|
+
if (!Number.isInteger(value) || value < minimum || value > maximum) {
|
|
118
|
+
throw new ValidationError(`${name} must be an integer between ${minimum} and ${maximum}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function nonNegativeInteger(value, name) {
|
|
122
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
123
|
+
throw new ValidationError(`${name} must be a non-negative integer`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function nonEmptyStrings(values, name) {
|
|
127
|
+
if (!Array.isArray(values) || values.length === 0 || values.some((value) => typeof value !== "string" || !value.trim())) {
|
|
128
|
+
throw new ValidationError(`${name} must contain at least one non-empty string`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function webhookRetryConfig(config) {
|
|
132
|
+
const wire = {};
|
|
133
|
+
if (config.enabled !== void 0) wire.enabled = config.enabled;
|
|
134
|
+
if (config.maxRetries !== void 0) wire.max_retries = config.maxRetries;
|
|
135
|
+
if (config.initialDelaySeconds !== void 0) wire.initial_delay_seconds = config.initialDelaySeconds;
|
|
136
|
+
if (config.maxDelaySeconds !== void 0) wire.max_delay_seconds = config.maxDelaySeconds;
|
|
137
|
+
if (config.backoffMultiplier !== void 0) wire.backoff_multiplier = config.backoffMultiplier;
|
|
138
|
+
if (config.retryStatusCodes !== void 0) wire.retry_status_codes = config.retryStatusCodes;
|
|
139
|
+
return wire;
|
|
140
|
+
}
|
|
141
|
+
function webhookSignatureConfig(config) {
|
|
142
|
+
const wire = {};
|
|
143
|
+
if (config.algorithm !== void 0) wire.algorithm = config.algorithm;
|
|
144
|
+
if (config.headerName !== void 0) wire.header_name = config.headerName;
|
|
145
|
+
if (config.timestampHeader !== void 0) wire.timestamp_header = config.timestampHeader;
|
|
146
|
+
if (config.toleranceSeconds !== void 0) wire.tolerance_seconds = config.toleranceSeconds;
|
|
147
|
+
return wire;
|
|
148
|
+
}
|
|
149
|
+
function validateWebhook(name, url, events) {
|
|
150
|
+
nonEmpty(name, "name");
|
|
151
|
+
if (name.length > 128) throw new ValidationError("name may contain at most 128 characters");
|
|
152
|
+
nonEmptyStrings(events, "events");
|
|
153
|
+
validateWebhookUrl(url);
|
|
154
|
+
}
|
|
155
|
+
function validateWebhookUrl(url) {
|
|
156
|
+
nonEmpty(url, "url");
|
|
157
|
+
let parsed;
|
|
158
|
+
try {
|
|
159
|
+
parsed = new URL(url);
|
|
160
|
+
} catch {
|
|
161
|
+
throw new ValidationError("url must be a valid HTTP or HTTPS URL");
|
|
162
|
+
}
|
|
163
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
164
|
+
throw new ValidationError("url must be a valid HTTP or HTTPS URL");
|
|
165
|
+
}
|
|
166
|
+
if (parsed.username || parsed.password) throw new ValidationError("url must not contain credentials");
|
|
167
|
+
}
|
|
168
|
+
var ControlPlaneClient = class {
|
|
169
|
+
constructor(config) {
|
|
170
|
+
if (!config.baseUrl?.trim()) throw new ValidationError("baseUrl is required");
|
|
171
|
+
let parsed;
|
|
172
|
+
try {
|
|
173
|
+
parsed = new URL(config.baseUrl);
|
|
174
|
+
} catch {
|
|
175
|
+
throw new ValidationError("baseUrl must be an absolute HTTP or HTTPS URL");
|
|
176
|
+
}
|
|
177
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
178
|
+
throw new ValidationError("baseUrl must be an absolute HTTP or HTTPS URL");
|
|
179
|
+
}
|
|
180
|
+
if (config.accessToken !== void 0 && !config.accessToken.trim()) {
|
|
181
|
+
throw new ValidationError("accessToken must not be empty when provided");
|
|
182
|
+
}
|
|
183
|
+
if (config.projectId !== void 0 && !config.projectId.trim()) {
|
|
184
|
+
throw new ValidationError("projectId must not be empty when provided");
|
|
185
|
+
}
|
|
186
|
+
if (config.timeoutMs !== void 0 && (!Number.isFinite(config.timeoutMs) || config.timeoutMs <= 0)) {
|
|
187
|
+
throw new ValidationError("timeoutMs must be greater than zero");
|
|
188
|
+
}
|
|
189
|
+
this.baseUrl = config.baseUrl.replace(/\/+$/, "");
|
|
190
|
+
this.accessToken = config.accessToken?.trim();
|
|
191
|
+
this.projectId = config.projectId?.trim();
|
|
192
|
+
this.timeoutMs = config.timeoutMs ?? 3e4;
|
|
193
|
+
const implementation = config.fetch ?? (typeof fetch !== "undefined" ? fetch : void 0);
|
|
194
|
+
if (!implementation) {
|
|
195
|
+
throw new Error("No fetch implementation available. Pass `fetch` in config or use Node 18+.");
|
|
196
|
+
}
|
|
197
|
+
this.fetchImpl = implementation;
|
|
198
|
+
}
|
|
199
|
+
async request(method, path, options = {}) {
|
|
200
|
+
const requiresAuth = options.auth !== false;
|
|
201
|
+
if (requiresAuth && !this.accessToken) {
|
|
202
|
+
throw new AuthError("accessToken is required for this operation");
|
|
203
|
+
}
|
|
204
|
+
if (options.projectId !== void 0 && !options.projectId.trim()) {
|
|
205
|
+
throw new ValidationError("projectId override must not be empty");
|
|
206
|
+
}
|
|
207
|
+
const headers = {
|
|
208
|
+
Accept: "application/json",
|
|
209
|
+
"User-Agent": `memorysync-sdk-js/${SDK_VERSION}`
|
|
210
|
+
};
|
|
211
|
+
if (requiresAuth) headers.Authorization = `Bearer ${this.accessToken}`;
|
|
212
|
+
const selectedProject = options.projectId?.trim() ?? this.projectId;
|
|
213
|
+
if (selectedProject) headers["X-Project-ID"] = selectedProject;
|
|
214
|
+
if (options.body !== void 0) headers["Content-Type"] = "application/json";
|
|
215
|
+
const controller = new AbortController();
|
|
216
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
217
|
+
try {
|
|
218
|
+
const response = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
219
|
+
method,
|
|
220
|
+
headers,
|
|
221
|
+
body: options.body === void 0 ? void 0 : JSON.stringify(options.body),
|
|
222
|
+
signal: controller.signal
|
|
223
|
+
});
|
|
224
|
+
const requestId = response.headers.get("X-Request-ID") ?? void 0;
|
|
225
|
+
if (response.status === 204) return void 0;
|
|
226
|
+
const text = await response.text();
|
|
227
|
+
const parsedBody = text ? safeJson(text) : null;
|
|
228
|
+
if (!response.ok) {
|
|
229
|
+
this.throwForStatus(response.status, parsedBody, requestId, response.headers.get("Retry-After"));
|
|
230
|
+
}
|
|
231
|
+
return normalizeResponse(parsedBody);
|
|
232
|
+
} catch (error) {
|
|
233
|
+
if (error instanceof MemorySyncError) throw error;
|
|
234
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
235
|
+
throw new MemorySyncError(`Request timed out after ${this.timeoutMs}ms`);
|
|
236
|
+
}
|
|
237
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
238
|
+
throw new MemorySyncError(`Network error: ${message}`);
|
|
239
|
+
} finally {
|
|
240
|
+
clearTimeout(timer);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
throwForStatus(status, body, requestId, retryAfterHeader) {
|
|
244
|
+
const detail = extractDetail(body);
|
|
245
|
+
const options = { statusCode: status, response: body, requestId };
|
|
246
|
+
if (status === 401) throw new AuthError(detail || "Unauthenticated", options);
|
|
247
|
+
if (status === 403) throw new AuthError(detail || "Forbidden", options);
|
|
248
|
+
if (status === 404) throw new NotFoundError(detail || "Not found", options);
|
|
249
|
+
if (status === 400 || status === 409 || status === 422) {
|
|
250
|
+
throw new ValidationError(detail || "Validation error", options);
|
|
251
|
+
}
|
|
252
|
+
if (status === 429) {
|
|
253
|
+
const retryAfter = parseRetryAfter(retryAfterHeader) ?? extractBodyRetryAfter(body);
|
|
254
|
+
throw new RateLimitError(detail || "Rate limited", retryAfter, options);
|
|
255
|
+
}
|
|
256
|
+
if (status >= 500) throw new ServerError(detail || `Server error (${status})`, options);
|
|
257
|
+
throw new MemorySyncError(detail || `Unexpected status ${status}`, options);
|
|
258
|
+
}
|
|
259
|
+
async bulkRevokeApiKeys(request, options = {}) {
|
|
260
|
+
if (!Array.isArray(request.keyIds) || request.keyIds.length < 1 || request.keyIds.length > 100) {
|
|
261
|
+
throw new ValidationError("keyIds must contain between 1 and 100 entries");
|
|
262
|
+
}
|
|
263
|
+
request.keyIds.forEach((id) => positiveId(id, "keyIds entry"));
|
|
264
|
+
return this.request("POST", "/org/api-keys/bulk-revoke", {
|
|
265
|
+
...options,
|
|
266
|
+
body: { key_ids: request.keyIds }
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
async testApiKey(keyId, options = {}) {
|
|
270
|
+
positiveId(keyId, "keyId");
|
|
271
|
+
return this.request("POST", `/org/api-keys/${keyId}/test`, options);
|
|
272
|
+
}
|
|
273
|
+
async login(request, options = {}) {
|
|
274
|
+
nonEmpty(request.email, "email");
|
|
275
|
+
nonEmpty(request.password, "password");
|
|
276
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(request.email)) {
|
|
277
|
+
throw new ValidationError("email must be a valid email address");
|
|
278
|
+
}
|
|
279
|
+
return this.request("POST", "/auth/login", {
|
|
280
|
+
...options,
|
|
281
|
+
auth: false,
|
|
282
|
+
body: { email: request.email, password: request.password }
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
async getCurrentPlan(options = {}) {
|
|
286
|
+
return this.request("GET", "/org/billing/current-plan", options);
|
|
287
|
+
}
|
|
288
|
+
async listTeamMembers(options = {}) {
|
|
289
|
+
return this.request("GET", "/admin/team/members", options);
|
|
290
|
+
}
|
|
291
|
+
async suspendTeamMember(memberId, options = {}) {
|
|
292
|
+
positiveId(memberId, "memberId");
|
|
293
|
+
return this.request("PATCH", `/admin/team/members/${memberId}`, {
|
|
294
|
+
...options,
|
|
295
|
+
body: { status: "suspended" }
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
async removeTeamMember(memberId, options = {}) {
|
|
299
|
+
positiveId(memberId, "memberId");
|
|
300
|
+
return this.request("DELETE", `/admin/team/members/${memberId}`, options);
|
|
301
|
+
}
|
|
302
|
+
async listSessions(options = {}) {
|
|
303
|
+
return this.request("GET", "/auth/sessions", options);
|
|
304
|
+
}
|
|
305
|
+
async revokeSession(sessionId, options = {}) {
|
|
306
|
+
positiveId(sessionId, "sessionId");
|
|
307
|
+
return this.request("POST", `/auth/sessions/${sessionId}/revoke`, options);
|
|
308
|
+
}
|
|
309
|
+
async listAuditEvents(query = {}, options = {}) {
|
|
310
|
+
if (query.limit !== void 0) boundedInteger(query.limit, "limit", 1, 200);
|
|
311
|
+
if (query.cursor !== void 0) nonNegativeInteger(query.cursor, "cursor");
|
|
312
|
+
if (query.skip !== void 0) nonNegativeInteger(query.skip, "skip");
|
|
313
|
+
if (query.sortDirection !== void 0 && query.sortDirection !== "asc" && query.sortDirection !== "desc") {
|
|
314
|
+
throw new ValidationError("sortDirection must be 'asc' or 'desc'");
|
|
315
|
+
}
|
|
316
|
+
const path = "/admin/audit-logs" + queryString({
|
|
317
|
+
limit: query.limit,
|
|
318
|
+
cursor: query.cursor,
|
|
319
|
+
skip: query.skip,
|
|
320
|
+
sort: query.sortDirection,
|
|
321
|
+
tenant_id: query.tenantId,
|
|
322
|
+
actor: query.actor,
|
|
323
|
+
actor_email: query.actorEmail,
|
|
324
|
+
ip: query.ip,
|
|
325
|
+
action: query.action,
|
|
326
|
+
resource_type: query.resourceType,
|
|
327
|
+
resource_id: query.resourceId,
|
|
328
|
+
severity: query.severity,
|
|
329
|
+
category: query.category,
|
|
330
|
+
start: query.start,
|
|
331
|
+
end: query.end,
|
|
332
|
+
success: query.success,
|
|
333
|
+
source: query.source,
|
|
334
|
+
ingest_method: query.ingestMethod,
|
|
335
|
+
search: query.search,
|
|
336
|
+
include_stats: query.includeStats
|
|
337
|
+
});
|
|
338
|
+
const raw = await this.request("GET", path, options);
|
|
339
|
+
return {
|
|
340
|
+
events: raw.logs ?? [],
|
|
341
|
+
nextCursor: raw.nextCursor ?? null,
|
|
342
|
+
stats: raw.stats ?? null,
|
|
343
|
+
sort: raw.sort ?? query.sortDirection ?? "desc"
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
async listIntegrations(query = {}, options = {}) {
|
|
347
|
+
if (query.category !== void 0) nonEmpty(query.category, "category");
|
|
348
|
+
const path = "/api/v1/integrations/catalog" + queryString({ category: query.category });
|
|
349
|
+
return this.request("GET", path, options);
|
|
350
|
+
}
|
|
351
|
+
async createOrganization(request, options = {}) {
|
|
352
|
+
nonEmpty(request.name, "name");
|
|
353
|
+
if (request.domain !== void 0) nonEmpty(request.domain, "domain");
|
|
354
|
+
const body = { name: request.name };
|
|
355
|
+
if (request.domain !== void 0) body.domain = request.domain;
|
|
356
|
+
return this.request("POST", "/organizations", { ...options, body });
|
|
357
|
+
}
|
|
358
|
+
async listOrganizations(options = {}) {
|
|
359
|
+
return this.request("GET", "/organizations", options);
|
|
360
|
+
}
|
|
361
|
+
async listOrganizationMembers(options = {}) {
|
|
362
|
+
return this.listTeamMembers(options);
|
|
363
|
+
}
|
|
364
|
+
async getOrganizationSettings(query = {}, options = {}) {
|
|
365
|
+
if (query.tenantId !== void 0) nonEmpty(query.tenantId, "tenantId");
|
|
366
|
+
const path = "/admin/tenant-settings" + queryString({ tenant_id: query.tenantId });
|
|
367
|
+
return this.request("GET", path, options);
|
|
368
|
+
}
|
|
369
|
+
async listProjects(options = {}) {
|
|
370
|
+
return this.request("GET", "/org/projects", options);
|
|
371
|
+
}
|
|
372
|
+
async createWebhook(request, options = {}) {
|
|
373
|
+
validateWebhook(request.name, request.url, request.events);
|
|
374
|
+
if (request.description !== void 0 && request.description.length > 500) {
|
|
375
|
+
throw new ValidationError("description may contain at most 500 characters");
|
|
376
|
+
}
|
|
377
|
+
if (request.projectId !== void 0) nonEmpty(request.projectId, "projectId");
|
|
378
|
+
if (options.projectId !== void 0) nonEmpty(options.projectId, "projectId override");
|
|
379
|
+
if (request.projectId && options.projectId && request.projectId.trim() !== options.projectId.trim()) {
|
|
380
|
+
throw new ValidationError("request projectId and options projectId must match");
|
|
381
|
+
}
|
|
382
|
+
const body = { name: request.name, url: request.url, events: request.events };
|
|
383
|
+
if (request.description !== void 0) body.description = request.description;
|
|
384
|
+
if (request.retryConfig !== void 0) body.retry_config = webhookRetryConfig(request.retryConfig);
|
|
385
|
+
if (request.signatureConfig !== void 0) body.signature_config = webhookSignatureConfig(request.signatureConfig);
|
|
386
|
+
if (request.projectId !== void 0) body.project_id = request.projectId;
|
|
387
|
+
return this.request("POST", "/org/webhooks", {
|
|
388
|
+
...options,
|
|
389
|
+
projectId: options.projectId ?? request.projectId,
|
|
390
|
+
body
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
async listWebhooks(options = {}) {
|
|
394
|
+
return this.request("GET", "/org/webhooks", options);
|
|
395
|
+
}
|
|
396
|
+
async updateWebhook(endpointId, request, options = {}) {
|
|
397
|
+
positiveId(endpointId, "endpointId");
|
|
398
|
+
const body = {};
|
|
399
|
+
if (request.name !== void 0) {
|
|
400
|
+
nonEmpty(request.name, "name");
|
|
401
|
+
if (request.name.length > 128) throw new ValidationError("name may contain at most 128 characters");
|
|
402
|
+
body.name = request.name;
|
|
403
|
+
}
|
|
404
|
+
if (request.url !== void 0) {
|
|
405
|
+
validateWebhookUrl(request.url);
|
|
406
|
+
body.url = request.url;
|
|
407
|
+
}
|
|
408
|
+
if (request.description !== void 0) {
|
|
409
|
+
if (request.description.length > 500) {
|
|
410
|
+
throw new ValidationError("description may contain at most 500 characters");
|
|
411
|
+
}
|
|
412
|
+
body.description = request.description;
|
|
413
|
+
}
|
|
414
|
+
if (request.events !== void 0) {
|
|
415
|
+
nonEmptyStrings(request.events, "events");
|
|
416
|
+
body.events = request.events;
|
|
417
|
+
}
|
|
418
|
+
if (request.retryConfig !== void 0) body.retry_config = webhookRetryConfig(request.retryConfig);
|
|
419
|
+
if (request.signatureConfig !== void 0) body.signature_config = webhookSignatureConfig(request.signatureConfig);
|
|
420
|
+
if (Object.keys(body).length === 0) {
|
|
421
|
+
throw new ValidationError("updateWebhook requires at least one editable field");
|
|
422
|
+
}
|
|
423
|
+
return this.request("PATCH", `/org/webhooks/${endpointId}`, { ...options, body });
|
|
424
|
+
}
|
|
425
|
+
async deleteWebhook(endpointId, options = {}) {
|
|
426
|
+
positiveId(endpointId, "endpointId");
|
|
427
|
+
return this.request("DELETE", `/org/webhooks/${endpointId}`, options);
|
|
428
|
+
}
|
|
429
|
+
async testWebhook(endpointId, request = {}, options = {}) {
|
|
430
|
+
positiveId(endpointId, "endpointId");
|
|
431
|
+
const body = {};
|
|
432
|
+
if (request.eventType !== void 0) {
|
|
433
|
+
nonEmpty(request.eventType, "eventType");
|
|
434
|
+
body.event_type = request.eventType;
|
|
435
|
+
}
|
|
436
|
+
return this.request("POST", `/org/webhooks/${endpointId}/test`, { ...options, body });
|
|
437
|
+
}
|
|
438
|
+
async replayWebhookDeliveries(endpointId, request = {}, options = {}) {
|
|
439
|
+
positiveId(endpointId, "endpointId");
|
|
440
|
+
if (request.sinceMinutes !== void 0 && (!Number.isInteger(request.sinceMinutes) || request.sinceMinutes < 1 || request.sinceMinutes > 10080)) {
|
|
441
|
+
throw new ValidationError("sinceMinutes must be an integer between 1 and 10080");
|
|
442
|
+
}
|
|
443
|
+
if (request.limit !== void 0 && (!Number.isInteger(request.limit) || request.limit < 1 || request.limit > 1e3)) {
|
|
444
|
+
throw new ValidationError("limit must be an integer between 1 and 1000");
|
|
445
|
+
}
|
|
446
|
+
if (request.statuses !== void 0) nonEmptyStrings(request.statuses, "statuses");
|
|
447
|
+
const body = {};
|
|
448
|
+
if (request.sinceMinutes !== void 0) body.since_minutes = request.sinceMinutes;
|
|
449
|
+
if (request.statuses !== void 0) body.statuses = request.statuses;
|
|
450
|
+
if (request.limit !== void 0) body.limit = request.limit;
|
|
451
|
+
return this.request("POST", `/org/webhooks/${endpointId}/replay`, { ...options, body });
|
|
452
|
+
}
|
|
453
|
+
async listWebhookDeliveries(endpointId, query = {}, options = {}) {
|
|
454
|
+
positiveId(endpointId, "endpointId");
|
|
455
|
+
if (query.page !== void 0) positiveId(query.page, "page");
|
|
456
|
+
if (query.pageSize !== void 0 && (!Number.isInteger(query.pageSize) || query.pageSize < 1 || query.pageSize > 100)) {
|
|
457
|
+
throw new ValidationError("pageSize must be an integer between 1 and 100");
|
|
458
|
+
}
|
|
459
|
+
if (query.status !== void 0) nonEmpty(query.status, "status");
|
|
460
|
+
const path = `/org/webhooks/${endpointId}/deliveries` + queryString({
|
|
461
|
+
page: query.page,
|
|
462
|
+
page_size: query.pageSize,
|
|
463
|
+
status_filter: query.status
|
|
464
|
+
});
|
|
465
|
+
return this.request("GET", path, options);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
// src/index.ts
|
|
470
|
+
var SDK_VERSION2 = "1.1.0";
|
|
43
471
|
function camelToSnakeKey(key) {
|
|
44
472
|
return key.replace(/([A-Z])/g, "_$1").toLowerCase();
|
|
45
473
|
}
|
|
@@ -68,14 +496,14 @@ function snakeToCamelMemory(m) {
|
|
|
68
496
|
score: m.score ?? null
|
|
69
497
|
};
|
|
70
498
|
}
|
|
71
|
-
function
|
|
499
|
+
function safeJson2(text) {
|
|
72
500
|
try {
|
|
73
501
|
return JSON.parse(text);
|
|
74
502
|
} catch {
|
|
75
503
|
return text;
|
|
76
504
|
}
|
|
77
505
|
}
|
|
78
|
-
function
|
|
506
|
+
function extractDetail2(body) {
|
|
79
507
|
if (!body || typeof body !== "object") return void 0;
|
|
80
508
|
const b = body;
|
|
81
509
|
if (typeof b.detail === "string") return b.detail;
|
|
@@ -119,7 +547,7 @@ var MemorySyncClient = class {
|
|
|
119
547
|
"X-API-Key": this.apiKey,
|
|
120
548
|
"Content-Type": "application/json",
|
|
121
549
|
"Accept": "application/json",
|
|
122
|
-
"User-Agent": `memorysync-sdk-js/${
|
|
550
|
+
"User-Agent": `memorysync-sdk-js/${SDK_VERSION2}`,
|
|
123
551
|
...extra
|
|
124
552
|
};
|
|
125
553
|
if (this.projectId) h["X-Project-ID"] = this.projectId;
|
|
@@ -143,7 +571,7 @@ var MemorySyncClient = class {
|
|
|
143
571
|
const requestId = res.headers.get("X-Request-ID") ?? void 0;
|
|
144
572
|
if (res.status === 204) return void 0;
|
|
145
573
|
const text = await res.text();
|
|
146
|
-
const parsed = text ?
|
|
574
|
+
const parsed = text ? safeJson2(text) : null;
|
|
147
575
|
if (!res.ok) this.throwForStatus(res.status, parsed, requestId);
|
|
148
576
|
return parsed;
|
|
149
577
|
} catch (e) {
|
|
@@ -157,7 +585,7 @@ var MemorySyncClient = class {
|
|
|
157
585
|
}
|
|
158
586
|
}
|
|
159
587
|
throwForStatus(status, body, requestId) {
|
|
160
|
-
const detail =
|
|
588
|
+
const detail = extractDetail2(body);
|
|
161
589
|
const opts = { statusCode: status, response: body, requestId };
|
|
162
590
|
if (status === 401) throw new AuthError(detail || "Unauthenticated", opts);
|
|
163
591
|
if (status === 403) throw new AuthError(detail || "Forbidden", opts);
|
|
@@ -332,6 +760,7 @@ var MemorySyncClient = class {
|
|
|
332
760
|
};
|
|
333
761
|
export {
|
|
334
762
|
AuthError,
|
|
763
|
+
ControlPlaneClient,
|
|
335
764
|
MemorySyncClient,
|
|
336
765
|
MemorySyncError,
|
|
337
766
|
NotFoundError,
|