@replicer/sdk 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +148 -2
- package/dist/index.cjs +345 -0
- package/dist/index.d.cts +602 -0
- package/dist/index.d.ts +602 -0
- package/dist/index.js +315 -0
- package/dist/types-CmxwElVc.d.cts +932 -0
- package/dist/types-CmxwElVc.d.ts +932 -0
- package/dist/webhooks.cjs +117 -0
- package/dist/webhooks.d.cts +41 -0
- package/dist/webhooks.d.ts +41 -0
- package/dist/webhooks.js +88 -0
- package/package.json +72 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var ReplicerError = class extends Error {
|
|
3
|
+
/** HTTP status code (0 when the request never produced a response). */
|
|
4
|
+
status;
|
|
5
|
+
/** `body.code` when the API sent one, else `network_error`, `invalid_response` or `http_<status>`. */
|
|
6
|
+
code;
|
|
7
|
+
/** Parsed JSON error body, or the raw response text when it was not JSON. */
|
|
8
|
+
body;
|
|
9
|
+
/** Request path (without the base URL or query string). */
|
|
10
|
+
path;
|
|
11
|
+
/** `x-request-id` response header, when present. */
|
|
12
|
+
requestId;
|
|
13
|
+
/** Seconds to wait before retrying (from `Retry-After` on a 429), when provided. */
|
|
14
|
+
retryAfter;
|
|
15
|
+
constructor(init) {
|
|
16
|
+
super(init.message, init.cause === void 0 ? void 0 : { cause: init.cause });
|
|
17
|
+
this.name = "ReplicerError";
|
|
18
|
+
this.status = init.status;
|
|
19
|
+
this.code = init.code;
|
|
20
|
+
this.body = init.body;
|
|
21
|
+
this.path = init.path ?? "";
|
|
22
|
+
this.requestId = init.requestId;
|
|
23
|
+
this.retryAfter = init.retryAfter;
|
|
24
|
+
}
|
|
25
|
+
/** Best human-readable message: the API's `error` string, else the raw text body, else `message`. */
|
|
26
|
+
get errorMessage() {
|
|
27
|
+
if (this.body && typeof this.body === "object" && typeof this.body.error === "string") {
|
|
28
|
+
return this.body.error;
|
|
29
|
+
}
|
|
30
|
+
if (typeof this.body === "string" && this.body !== "") return this.body;
|
|
31
|
+
return this.message;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/routes.ts
|
|
36
|
+
var ROUTES = [
|
|
37
|
+
{ method: "POST", path: "/v1/create-phone-call", sdk: "call.createPhoneCall", request: "CreatePhoneCallInput", response: "EnqueueCallResponse", status: 202, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [422, "idempotency_key_reuse"], [429, "rate_limit_exceeded"], [429, "rate_limited"], [502, "bad_gateway"]] },
|
|
38
|
+
{ method: "POST", path: "/v1/cancel-call", sdk: "call.cancel", request: "CancelCallInput", response: "OkResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"], [502, "bad_gateway"]] },
|
|
39
|
+
{ method: "POST", path: "/v1/cancel-campaign-pending", sdk: "call.cancelCampaignPending", request: "CancelCampaignPendingInput", response: "CancelCampaignPendingResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
40
|
+
{ method: "GET", path: "/v1/list-calls", sdk: "call.list", request: "ListCallsParams", response: "Page<Call>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
41
|
+
{ method: "GET", path: "/v1/get-call/:id", sdk: "call.get", request: "none", response: "Call", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
42
|
+
{ method: "POST", path: "/v1/campaigns", sdk: "campaign.create", request: "CreateCampaignInput", response: "CreateCampaignResponse", status: 201, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "forbidden"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [422, "idempotency_key_reuse"], [422, "validation_error"], [429, "rate_limit_exceeded"]] },
|
|
43
|
+
{ method: "GET", path: "/v1/campaigns/:id", sdk: "campaign.get", request: "none", response: "GetCampaignResponse", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
44
|
+
{ method: "GET", path: "/v1/list-campaigns", sdk: "campaign.list", request: "ListCampaignsParams", response: "Page<Campaign>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
45
|
+
{ method: "POST", path: "/v1/start-campaign/:id", sdk: "campaign.start", request: "none", response: "StartCampaignResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [402, "insufficient_balance"], [403, "forbidden"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
46
|
+
{ method: "GET", path: "/v1/list-phone-numbers", sdk: "phoneNumber.list", request: "PageParams", response: "Page<PhoneNumber>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
47
|
+
{ method: "POST", path: "/v1/phone-numbers/inbound", sdk: "phoneNumber.provisionInbound", request: "ProvisionInboundInput", response: "ProvisionInboundResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [403, "test_mode_unsupported"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"], [502, "bad_gateway"]] },
|
|
48
|
+
{ method: "DELETE", path: "/v1/phone-numbers/inbound/:id", sdk: "phoneNumber.deprovisionInbound", request: "DeprovisionInboundParams", response: "OkResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [403, "test_mode_unsupported"], [404, "not_found"], [429, "rate_limit_exceeded"], [502, "bad_gateway"]] },
|
|
49
|
+
{ method: "POST", path: "/v1/create-knowledge-base", sdk: "knowledgeBase.create", request: "KnowledgeBaseCreateInput", response: "KnowledgeBaseSourcesResponse", status: 201, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [403, "quota_exceeded"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
50
|
+
{ method: "GET", path: "/v1/get-knowledge-base/:id", sdk: "knowledgeBase.retrieve", request: "none", response: "KnowledgeBaseResponse", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
51
|
+
{ method: "GET", path: "/v1/list-knowledge-bases", sdk: "knowledgeBase.list", request: "none", response: "KnowledgeBaseListResponse", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
52
|
+
{ method: "DELETE", path: "/v1/delete-knowledge-base/:id", sdk: "knowledgeBase.delete", request: "none", response: "none", status: 204, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
53
|
+
{ method: "POST", path: "/v1/add-knowledge-base-sources/:id", sdk: "knowledgeBase.addSources", request: "KnowledgeBaseAddSourcesInput", response: "KnowledgeBaseSourcesResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [403, "quota_exceeded"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
54
|
+
{ method: "DELETE", path: "/v1/delete-knowledge-base-source/:source_id", sdk: "knowledgeBase.deleteSource", request: "DeleteKnowledgeBaseSourceParams", response: "KnowledgeBaseResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
55
|
+
{ method: "PATCH", path: "/v1/update-knowledge-base/:id", sdk: "knowledgeBase.update", request: "KnowledgeBaseUpdateInput", response: "KnowledgeBaseResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
56
|
+
{ method: "POST", path: "/v1/presign-knowledge-base-file/:id", sdk: "knowledgeBase.presignFile", request: "PresignFileInput", response: "PresignFileResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [403, "quota_exceeded"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
57
|
+
{ method: "POST", path: "/v1/complete-knowledge-base-file/:id/:source_id", sdk: "knowledgeBase.completeFile", request: "none", response: "KnowledgeBaseResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
58
|
+
{ method: "POST", path: "/v1/fail-knowledge-base-file/:id/:source_id", sdk: "knowledgeBase.failFile", request: "FailFileInput", response: "KnowledgeBaseResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
59
|
+
{ method: "PUT", path: "/v1/reindex-knowledge-base/:id", sdk: "knowledgeBase.reindex", request: "none", response: "KnowledgeBaseResponse", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
60
|
+
{ method: "PATCH", path: "/v1/update-knowledge-base-source/:id/:source_id", sdk: "knowledgeBase.updateSource", request: "UpdateSourceInput", response: "KnowledgeBaseResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
61
|
+
{ method: "PATCH", path: "/v1/update-knowledge-base-chunk/:id/:chunk_id", sdk: "knowledgeBase.updateChunk", request: "UpdateChunkInput", response: "OkResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
62
|
+
{ method: "GET", path: "/v1/get-knowledge-base-source/:id/:source_id", sdk: "knowledgeBase.getSource", request: "none", response: "KnowledgeBaseSourceDetail", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
63
|
+
{ method: "GET", path: "/v1/presign-knowledge-base-file-download/:id/:source_id", sdk: "knowledgeBase.presignFileDownload", request: "none", response: "PresignFileDownloadResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"], [503, "internal_error"]] },
|
|
64
|
+
{ method: "GET", path: "/v1/list-contacts", sdk: "contact.list", request: "ListContactsParams", response: "Page<Contact>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
65
|
+
{ method: "POST", path: "/v1/create-contact", sdk: "contact.create", request: "CreateContactInput", response: "CreateContactResponse", status: 201, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
66
|
+
{ method: "GET", path: "/v1/list-agents", sdk: "agent.list", request: "PageParams", response: "Page<Agent>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
67
|
+
{ method: "GET", path: "/v1/list-calendars", sdk: "calendar.listCalendars", request: "PageParams", response: "Page<Calendar>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
68
|
+
{ method: "GET", path: "/v1/list-calendar-events", sdk: "calendar.listEvents", request: "ListCalendarEventsParams", response: "Page<CalendarEvent>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
69
|
+
{ method: "POST", path: "/v1/create-calendar-event", sdk: "calendar.createEvent", request: "CreateCalendarEventInput", response: "CreateCalendarEventResponse", status: 201, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [409, "conflict"], [429, "rate_limit_exceeded"]] },
|
|
70
|
+
{ method: "PATCH", path: "/v1/update-calendar-event/:id", sdk: "calendar.updateEvent", request: "UpdateCalendarEventInput", response: "CalendarEvent", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"]] },
|
|
71
|
+
{ method: "POST", path: "/v1/cancel-calendar-event/:id", sdk: "calendar.cancelEvent", request: "none", response: "CalendarEvent", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"]] },
|
|
72
|
+
{ method: "GET", path: "/v1/list-messages", sdk: "message.list", request: "ListMessagesParams", response: "Page<Message>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
73
|
+
{ method: "GET", path: "/v1/get-message/:id", sdk: "message.get", request: "none", response: "Message", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
74
|
+
{ method: "POST", path: "/v1/send-sms", sdk: "message.sendSms", request: "SendSmsInput", response: "SendMessageResponse", status: 202, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [409, "conflict"], [422, "channel_not_configured"], [422, "idempotency_key_reuse"], [422, "sender_not_supported"], [422, "validation_error"], [429, "rate_limit_exceeded"], [502, "bad_gateway"]] },
|
|
75
|
+
{ method: "POST", path: "/v1/send-whatsapp", sdk: "message.sendWhatsapp", request: "SendWhatsappInput", response: "SendMessageResponse", status: 202, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [409, "conflict"], [422, "channel_not_configured"], [422, "idempotency_key_reuse"], [422, "media_not_supported"], [422, "validation_error"], [429, "rate_limit_exceeded"], [502, "bad_gateway"]] },
|
|
76
|
+
{ method: "POST", path: "/v1/send-email", sdk: "message.sendEmail", request: "SendEmailInput", response: "SendMessageResponse", status: 202, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [409, "conflict"], [422, "channel_not_configured"], [422, "email_provider_not_supported"], [422, "idempotency_key_reuse"], [422, "validation_error"], [429, "rate_limit_exceeded"], [502, "bad_gateway"]] },
|
|
77
|
+
{ method: "POST", path: "/v1/create-tts", sdk: "tts.create", request: "CreateTtsInput", response: "CreateTtsResponse", status: 201, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [409, "conflict"], [422, "idempotency_key_reuse"], [422, "validation_error"], [429, "rate_limit_exceeded"], [502, "bad_gateway"], [503, "service_unavailable"]] },
|
|
78
|
+
{ method: "GET", path: "/v1/list-tts", sdk: "tts.list", request: "ListTtsParams", response: "Page<TtsGeneration>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
79
|
+
{ method: "GET", path: "/v1/get-tts/:id", sdk: "tts.get", request: "none", response: "TtsGeneration", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
80
|
+
{ method: "POST", path: "/v1/create-webhook-endpoint", sdk: "webhookEndpoint.create", request: "CreateWebhookEndpointInput", response: "WebhookEndpointWithSecret", status: 201, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [409, "conflict"], [409, "webhook_endpoint_limit"], [422, "idempotency_key_reuse"], [422, "validation_error"], [429, "rate_limit_exceeded"], [503, "service_unavailable"]] },
|
|
81
|
+
{ method: "GET", path: "/v1/list-webhook-endpoints", sdk: "webhookEndpoint.list", request: "PageParams", response: "Page<WebhookEndpoint>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
82
|
+
{ method: "GET", path: "/v1/get-webhook-endpoint/:id", sdk: "webhookEndpoint.get", request: "none", response: "WebhookEndpoint", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
83
|
+
{ method: "PATCH", path: "/v1/update-webhook-endpoint/:id", sdk: "webhookEndpoint.update", request: "UpdateWebhookEndpointInput", response: "WebhookEndpoint", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "webhook_endpoint_key_revoked"], [409, "webhook_endpoint_admin_disabled"], [429, "rate_limit_exceeded"]] },
|
|
84
|
+
{ method: "DELETE", path: "/v1/delete-webhook-endpoint/:id", sdk: "webhookEndpoint.delete", request: "none", response: "OkResponse", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
85
|
+
{ method: "POST", path: "/v1/rotate-webhook-endpoint-secret/:id", sdk: "webhookEndpoint.rotateSecret", request: "none", response: "WebhookEndpointWithSecret", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"], [503, "service_unavailable"]] },
|
|
86
|
+
{ method: "POST", path: "/v1/test-webhook-endpoint/:id", sdk: "webhookEndpoint.test", request: "TestWebhookEndpointInput", response: "TestWebhookEndpointResponse", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [429, "rate_limit_exceeded"], [429, "rate_limited"]] },
|
|
87
|
+
{ method: "GET", path: "/v1/list-webhook-deliveries", sdk: "webhookEndpoint.listDeliveries", request: "ListWebhookDeliveriesParams", response: "Page<WebhookDelivery>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
88
|
+
{ method: "GET", path: "/v1/list-webhook-delivery-attempts", sdk: "webhookEndpoint.listDeliveryAttempts", request: "ListWebhookDeliveryAttemptsParams", response: "Page<WebhookDeliveryAttempt>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] },
|
|
89
|
+
{ method: "POST", path: "/v1/redeliver-webhook-delivery/:id", sdk: "webhookEndpoint.redeliver", request: "none", response: "WebhookDelivery", status: 201, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [409, "conflict"], [409, "redelivery_limit"], [409, "webhook_endpoint_key_revoked"], [429, "rate_limit_exceeded"]] },
|
|
90
|
+
{ method: "GET", path: "/v1/list-events", sdk: "event.list", request: "ListEventsParams", response: "Page<WebhookEventEnvelope>", status: 200, errors: [[400, "validation_error"], [401, "unauthorized"], [403, "insufficient_scope"], [429, "rate_limit_exceeded"]] },
|
|
91
|
+
{ method: "GET", path: "/v1/get-event/:id", sdk: "event.get", request: "none", response: "WebhookEventEnvelope", status: 200, errors: [[401, "unauthorized"], [403, "insufficient_scope"], [404, "not_found"], [429, "rate_limit_exceeded"]] }
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
// src/client.ts
|
|
95
|
+
var DEFAULT_BASE_URL = "https://api.replicer.ai";
|
|
96
|
+
var ROUTE_BY_SDK = new Map(ROUTES.map((r) => [r.sdk, r]));
|
|
97
|
+
var Replicer = class {
|
|
98
|
+
apiKey;
|
|
99
|
+
baseURL;
|
|
100
|
+
fetchImpl;
|
|
101
|
+
constructor(options) {
|
|
102
|
+
if (!options || typeof options.apiKey !== "string" || options.apiKey.trim() === "") {
|
|
103
|
+
throw new TypeError("Replicer: `apiKey` is required");
|
|
104
|
+
}
|
|
105
|
+
const impl = options.fetch ?? globalThis.fetch;
|
|
106
|
+
if (typeof impl !== "function") {
|
|
107
|
+
throw new TypeError("Replicer: no global `fetch` found; pass one via the `fetch` option");
|
|
108
|
+
}
|
|
109
|
+
this.apiKey = options.apiKey;
|
|
110
|
+
this.baseURL = (options.baseURL ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
111
|
+
this.fetchImpl = impl.bind(globalThis);
|
|
112
|
+
}
|
|
113
|
+
call = {
|
|
114
|
+
createPhoneCall: (input) => this.request("call.createPhoneCall", { body: input }),
|
|
115
|
+
cancel: (call_id) => this.request("call.cancel", { body: { call_id } }),
|
|
116
|
+
cancelCampaignPending: (campaign_id) => this.request("call.cancelCampaignPending", { body: { campaign_id } }),
|
|
117
|
+
list: (params) => this.request("call.list", { query: params }),
|
|
118
|
+
get: (id) => this.request("call.get", { params: { id } })
|
|
119
|
+
};
|
|
120
|
+
campaign = {
|
|
121
|
+
create: (input) => this.request("campaign.create", { body: input }),
|
|
122
|
+
/** Includes up to 1,000 contacts (`contacts_truncated` tells whether there are more). */
|
|
123
|
+
get: (id) => this.request("campaign.get", { params: { id } }),
|
|
124
|
+
list: (params) => this.request("campaign.list", { query: params }),
|
|
125
|
+
/** Starts a campaign, or resumes one paused by `call.cancelCampaignPending`. */
|
|
126
|
+
start: (id) => this.request("campaign.start", { params: { id } })
|
|
127
|
+
};
|
|
128
|
+
phoneNumber = {
|
|
129
|
+
/** Your organization's phone numbers; `id` is the `from_number_id` for `call.createPhoneCall`. */
|
|
130
|
+
list: (params) => this.request("phoneNumber.list", { query: params }),
|
|
131
|
+
provisionInbound: (input) => this.request("phoneNumber.provisionInbound", { body: input }),
|
|
132
|
+
deprovisionInbound: (id, trunk_id, rule_id) => this.request("phoneNumber.deprovisionInbound", { params: { id }, query: { trunk_id, rule_id } })
|
|
133
|
+
};
|
|
134
|
+
knowledgeBase = {
|
|
135
|
+
create: (input) => this.request("knowledgeBase.create", { body: input }),
|
|
136
|
+
retrieve: (knowledgeBaseId) => this.request("knowledgeBase.retrieve", { params: { id: knowledgeBaseId } }),
|
|
137
|
+
list: () => this.request("knowledgeBase.list"),
|
|
138
|
+
delete: (knowledgeBaseId) => this.request("knowledgeBase.delete", { params: { id: knowledgeBaseId } }),
|
|
139
|
+
addSources: (knowledgeBaseId, input) => this.request("knowledgeBase.addSources", { params: { id: knowledgeBaseId }, body: input }),
|
|
140
|
+
deleteSource: (sourceId, knowledgeBaseId) => this.request("knowledgeBase.deleteSource", {
|
|
141
|
+
params: { source_id: sourceId },
|
|
142
|
+
query: { knowledge_base_id: knowledgeBaseId }
|
|
143
|
+
}),
|
|
144
|
+
update: (knowledgeBaseId, input) => this.request("knowledgeBase.update", { params: { id: knowledgeBaseId }, body: input }),
|
|
145
|
+
presignFile: (knowledgeBaseId, input) => this.request("knowledgeBase.presignFile", { params: { id: knowledgeBaseId }, body: input }),
|
|
146
|
+
completeFile: (knowledgeBaseId, sourceId) => this.request("knowledgeBase.completeFile", {
|
|
147
|
+
params: { id: knowledgeBaseId, source_id: sourceId },
|
|
148
|
+
body: {}
|
|
149
|
+
}),
|
|
150
|
+
failFile: (knowledgeBaseId, sourceId, input) => this.request("knowledgeBase.failFile", {
|
|
151
|
+
params: { id: knowledgeBaseId, source_id: sourceId },
|
|
152
|
+
body: input ?? {}
|
|
153
|
+
}),
|
|
154
|
+
reindex: (knowledgeBaseId) => this.request("knowledgeBase.reindex", { params: { id: knowledgeBaseId }, body: {} }),
|
|
155
|
+
updateSource: (knowledgeBaseId, sourceId, input) => this.request("knowledgeBase.updateSource", {
|
|
156
|
+
params: { id: knowledgeBaseId, source_id: sourceId },
|
|
157
|
+
body: input
|
|
158
|
+
}),
|
|
159
|
+
updateChunk: (knowledgeBaseId, chunkId, input) => this.request("knowledgeBase.updateChunk", {
|
|
160
|
+
params: { id: knowledgeBaseId, chunk_id: chunkId },
|
|
161
|
+
body: input
|
|
162
|
+
}),
|
|
163
|
+
getSource: (knowledgeBaseId, sourceId) => this.request("knowledgeBase.getSource", {
|
|
164
|
+
params: { id: knowledgeBaseId, source_id: sourceId }
|
|
165
|
+
}),
|
|
166
|
+
presignFileDownload: (knowledgeBaseId, sourceId) => this.request("knowledgeBase.presignFileDownload", {
|
|
167
|
+
params: { id: knowledgeBaseId, source_id: sourceId }
|
|
168
|
+
})
|
|
169
|
+
};
|
|
170
|
+
contact = {
|
|
171
|
+
list: (params) => this.request("contact.list", { query: params }),
|
|
172
|
+
create: (input) => this.request("contact.create", { body: input })
|
|
173
|
+
};
|
|
174
|
+
agent = {
|
|
175
|
+
list: (params) => this.request("agent.list", { query: params })
|
|
176
|
+
};
|
|
177
|
+
calendar = {
|
|
178
|
+
/** Active calendars with their active event types: the source of `calendar_id` and `event_type_id`. */
|
|
179
|
+
listCalendars: (params) => this.request("calendar.listCalendars", { query: params }),
|
|
180
|
+
listEvents: (params) => this.request("calendar.listEvents", { query: params }),
|
|
181
|
+
createEvent: (input) => this.request("calendar.createEvent", { body: input }),
|
|
182
|
+
/** Reschedules a confirmed event (`start_time`) and/or edits customer fields, notes and metadata. */
|
|
183
|
+
updateEvent: (id, input) => this.request("calendar.updateEvent", { params: { id }, body: input }),
|
|
184
|
+
/** Soft cancel: the event stays listed with status `cancelled`. Cancelling twice is a no-op. */
|
|
185
|
+
cancelEvent: (id) => this.request("calendar.cancelEvent", { params: { id }, body: {} })
|
|
186
|
+
};
|
|
187
|
+
message = {
|
|
188
|
+
list: (params) => this.request("message.list", { query: params }),
|
|
189
|
+
/** One message by id (the `id` of a send response), with its current delivery `status`. */
|
|
190
|
+
get: (id) => this.request("message.get", { params: { id } }),
|
|
191
|
+
sendSms: (input) => this.request("message.sendSms", { body: input }),
|
|
192
|
+
sendWhatsapp: (input) => this.request("message.sendWhatsapp", { body: input }),
|
|
193
|
+
sendEmail: (input) => this.request("message.sendEmail", { body: input })
|
|
194
|
+
};
|
|
195
|
+
tts = {
|
|
196
|
+
create: (input) => this.request("tts.create", { body: input }),
|
|
197
|
+
list: (params) => this.request("tts.list", { query: params }),
|
|
198
|
+
get: (id) => this.request("tts.get", { params: { id } })
|
|
199
|
+
};
|
|
200
|
+
webhookEndpoint = {
|
|
201
|
+
/** Resolves with the signing `secret`, shown only here and in `rotateSecret`. */
|
|
202
|
+
create: (input) => this.request("webhookEndpoint.create", { body: input }),
|
|
203
|
+
list: (params) => this.request("webhookEndpoint.list", { query: params }),
|
|
204
|
+
get: (id) => this.request("webhookEndpoint.get", { params: { id } }),
|
|
205
|
+
update: (id, input) => this.request("webhookEndpoint.update", { params: { id }, body: input }),
|
|
206
|
+
delete: (id) => this.request("webhookEndpoint.delete", { params: { id } }),
|
|
207
|
+
/** New secret now; the previous one keeps signing deliveries for 24 hours. */
|
|
208
|
+
rotateSecret: (id) => this.request("webhookEndpoint.rotateSecret", { params: { id }, body: {} }),
|
|
209
|
+
/** Sends a test event (`livemode: false`) and waits up to 12 seconds for its first attempt. */
|
|
210
|
+
test: (id, input) => this.request("webhookEndpoint.test", { params: { id }, body: input ?? {} }),
|
|
211
|
+
listDeliveries: (params) => this.request("webhookEndpoint.listDeliveries", { query: params }),
|
|
212
|
+
/** HTTP attempts of one delivery, oldest first. */
|
|
213
|
+
listDeliveryAttempts: (params) => this.request("webhookEndpoint.listDeliveryAttempts", { query: params }),
|
|
214
|
+
/** Sends the event of a delivery again (same event id and body) as a new delivery. Events from the last 30 days only. */
|
|
215
|
+
redeliver: (deliveryId) => this.request("webhookEndpoint.redeliver", { params: { id: deliveryId }, body: {} })
|
|
216
|
+
};
|
|
217
|
+
event = {
|
|
218
|
+
/** Events of the last 30 days as delivered (each item is the parsed envelope). */
|
|
219
|
+
list: (params) => this.request("event.list", { query: params }),
|
|
220
|
+
get: (id) => this.request("event.get", { params: { id } })
|
|
221
|
+
};
|
|
222
|
+
async request(sdk, opts = {}) {
|
|
223
|
+
const route = ROUTE_BY_SDK.get(sdk);
|
|
224
|
+
if (!route) throw new Error(`Replicer: unknown route ${sdk}`);
|
|
225
|
+
const params = opts.params ?? {};
|
|
226
|
+
const path = route.path.replace(/:([A-Za-z_]+)/g, (_m, name) => {
|
|
227
|
+
const value = params[name];
|
|
228
|
+
if (typeof value !== "string" || value === "" || value === "." || value === "..") {
|
|
229
|
+
throw new ReplicerError({
|
|
230
|
+
status: 0,
|
|
231
|
+
code: "validation_error",
|
|
232
|
+
message: `Replicer: invalid \`${name}\` path parameter`,
|
|
233
|
+
path: route.path
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return encodeURIComponent(value);
|
|
237
|
+
});
|
|
238
|
+
const qs = buildQuery(opts.query);
|
|
239
|
+
const url = `${this.baseURL}${path}${qs ? `?${qs}` : ""}`;
|
|
240
|
+
const headers = {
|
|
241
|
+
authorization: `Bearer ${this.apiKey}`,
|
|
242
|
+
accept: "application/json"
|
|
243
|
+
};
|
|
244
|
+
let body;
|
|
245
|
+
if (opts.body !== void 0) {
|
|
246
|
+
headers["content-type"] = "application/json";
|
|
247
|
+
body = JSON.stringify(opts.body);
|
|
248
|
+
}
|
|
249
|
+
let res;
|
|
250
|
+
try {
|
|
251
|
+
res = await this.fetchImpl(url, { method: route.method, headers, body });
|
|
252
|
+
} catch (cause) {
|
|
253
|
+
throw new ReplicerError({
|
|
254
|
+
status: 0,
|
|
255
|
+
code: "network_error",
|
|
256
|
+
message: `Request to ${route.method} ${path} failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
257
|
+
path,
|
|
258
|
+
cause
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
262
|
+
const text = await res.text();
|
|
263
|
+
let parsed;
|
|
264
|
+
let isJson = false;
|
|
265
|
+
if (text) {
|
|
266
|
+
try {
|
|
267
|
+
parsed = JSON.parse(text);
|
|
268
|
+
isJson = true;
|
|
269
|
+
} catch {
|
|
270
|
+
isJson = false;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (!res.ok) {
|
|
274
|
+
const errBody = isJson && parsed && typeof parsed === "object" ? parsed : void 0;
|
|
275
|
+
const retryAfterRaw = res.headers.get("retry-after");
|
|
276
|
+
const retryAfter = retryAfterRaw !== null && retryAfterRaw.trim() !== "" && Number.isFinite(Number(retryAfterRaw)) ? Number(retryAfterRaw) : void 0;
|
|
277
|
+
throw new ReplicerError({
|
|
278
|
+
status: res.status,
|
|
279
|
+
code: typeof errBody?.code === "string" && errBody.code ? errBody.code : `http_${res.status}`,
|
|
280
|
+
message: typeof errBody?.error === "string" && errBody.error ? errBody.error : text || res.statusText || `HTTP ${res.status}`,
|
|
281
|
+
body: isJson ? parsed : text,
|
|
282
|
+
path,
|
|
283
|
+
requestId,
|
|
284
|
+
retryAfter
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
if (res.status === 204) return void 0;
|
|
288
|
+
if (!isJson) {
|
|
289
|
+
throw new ReplicerError({
|
|
290
|
+
status: res.status,
|
|
291
|
+
code: "invalid_response",
|
|
292
|
+
message: `Expected a JSON response from ${route.method} ${path}`,
|
|
293
|
+
body: text,
|
|
294
|
+
path,
|
|
295
|
+
requestId
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return parsed;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
function buildQuery(query) {
|
|
302
|
+
if (!query) return "";
|
|
303
|
+
const params = new URLSearchParams();
|
|
304
|
+
for (const [key, value] of Object.entries(query)) {
|
|
305
|
+
if (value === void 0 || value === null) continue;
|
|
306
|
+
params.set(key, String(value));
|
|
307
|
+
}
|
|
308
|
+
return params.toString();
|
|
309
|
+
}
|
|
310
|
+
export {
|
|
311
|
+
DEFAULT_BASE_URL,
|
|
312
|
+
ROUTES,
|
|
313
|
+
Replicer,
|
|
314
|
+
ReplicerError
|
|
315
|
+
};
|