silon-sdk 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 +560 -0
- package/dist/client.d.ts +141 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +249 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +117 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +199 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/utils.d.ts +24 -0
- package/dist/internal/utils.d.ts.map +1 -0
- package/dist/internal/utils.js +58 -0
- package/dist/internal/utils.js.map +1 -0
- package/dist/pagination.d.ts +52 -0
- package/dist/pagination.d.ts.map +1 -0
- package/dist/pagination.js +80 -0
- package/dist/pagination.js.map +1 -0
- package/dist/resources/auth.d.ts +20 -0
- package/dist/resources/auth.d.ts.map +1 -0
- package/dist/resources/auth.js +41 -0
- package/dist/resources/auth.js.map +1 -0
- package/dist/resources/broadcasts.d.ts +63 -0
- package/dist/resources/broadcasts.d.ts.map +1 -0
- package/dist/resources/broadcasts.js +103 -0
- package/dist/resources/broadcasts.js.map +1 -0
- package/dist/resources/bulk.d.ts +60 -0
- package/dist/resources/bulk.d.ts.map +1 -0
- package/dist/resources/bulk.js +137 -0
- package/dist/resources/bulk.js.map +1 -0
- package/dist/resources/client-groups.d.ts +50 -0
- package/dist/resources/client-groups.d.ts.map +1 -0
- package/dist/resources/client-groups.js +97 -0
- package/dist/resources/client-groups.js.map +1 -0
- package/dist/resources/clients.d.ts +51 -0
- package/dist/resources/clients.d.ts.map +1 -0
- package/dist/resources/clients.js +99 -0
- package/dist/resources/clients.js.map +1 -0
- package/dist/resources/events.d.ts +22 -0
- package/dist/resources/events.d.ts.map +1 -0
- package/dist/resources/events.js +38 -0
- package/dist/resources/events.js.map +1 -0
- package/dist/resources/messages.d.ts +99 -0
- package/dist/resources/messages.d.ts.map +1 -0
- package/dist/resources/messages.js +167 -0
- package/dist/resources/messages.js.map +1 -0
- package/dist/resources/otp.d.ts +26 -0
- package/dist/resources/otp.d.ts.map +1 -0
- package/dist/resources/otp.js +40 -0
- package/dist/resources/otp.js.map +1 -0
- package/dist/resources/profile.d.ts +18 -0
- package/dist/resources/profile.d.ts.map +1 -0
- package/dist/resources/profile.js +40 -0
- package/dist/resources/profile.js.map +1 -0
- package/dist/resources/push.d.ts +45 -0
- package/dist/resources/push.d.ts.map +1 -0
- package/dist/resources/push.js +103 -0
- package/dist/resources/push.js.map +1 -0
- package/dist/resources/reports.d.ts +44 -0
- package/dist/resources/reports.d.ts.map +1 -0
- package/dist/resources/reports.js +127 -0
- package/dist/resources/reports.js.map +1 -0
- package/dist/resources/suppressions.d.ts +54 -0
- package/dist/resources/suppressions.d.ts.map +1 -0
- package/dist/resources/suppressions.js +79 -0
- package/dist/resources/suppressions.js.map +1 -0
- package/dist/resources/templates.d.ts +58 -0
- package/dist/resources/templates.d.ts.map +1 -0
- package/dist/resources/templates.js +96 -0
- package/dist/resources/templates.js.map +1 -0
- package/dist/resources/webhook-endpoints.d.ts +54 -0
- package/dist/resources/webhook-endpoints.d.ts.map +1 -0
- package/dist/resources/webhook-endpoints.js +96 -0
- package/dist/resources/webhook-endpoints.js.map +1 -0
- package/dist/resources/whatsapp-templates.d.ts +20 -0
- package/dist/resources/whatsapp-templates.d.ts.map +1 -0
- package/dist/resources/whatsapp-templates.js +33 -0
- package/dist/resources/whatsapp-templates.js.map +1 -0
- package/dist/types/auth.d.ts +34 -0
- package/dist/types/auth.d.ts.map +1 -0
- package/dist/types/auth.js +8 -0
- package/dist/types/auth.js.map +1 -0
- package/dist/types/broadcasts.d.ts +174 -0
- package/dist/types/broadcasts.d.ts.map +1 -0
- package/dist/types/broadcasts.js +8 -0
- package/dist/types/broadcasts.js.map +1 -0
- package/dist/types/bulk.d.ts +161 -0
- package/dist/types/bulk.d.ts.map +1 -0
- package/dist/types/bulk.js +8 -0
- package/dist/types/bulk.js.map +1 -0
- package/dist/types/client-groups.d.ts +52 -0
- package/dist/types/client-groups.d.ts.map +1 -0
- package/dist/types/client-groups.js +8 -0
- package/dist/types/client-groups.js.map +1 -0
- package/dist/types/clients.d.ts +64 -0
- package/dist/types/clients.d.ts.map +1 -0
- package/dist/types/clients.js +8 -0
- package/dist/types/clients.js.map +1 -0
- package/dist/types/events.d.ts +64 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +9 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/messages.d.ts +334 -0
- package/dist/types/messages.d.ts.map +1 -0
- package/dist/types/messages.js +8 -0
- package/dist/types/messages.js.map +1 -0
- package/dist/types/otp.d.ts +61 -0
- package/dist/types/otp.d.ts.map +1 -0
- package/dist/types/otp.js +8 -0
- package/dist/types/otp.js.map +1 -0
- package/dist/types/profile.d.ts +37 -0
- package/dist/types/profile.d.ts.map +1 -0
- package/dist/types/profile.js +8 -0
- package/dist/types/profile.js.map +1 -0
- package/dist/types/push.d.ts +90 -0
- package/dist/types/push.d.ts.map +1 -0
- package/dist/types/push.js +8 -0
- package/dist/types/push.js.map +1 -0
- package/dist/types/reports.d.ts +109 -0
- package/dist/types/reports.d.ts.map +1 -0
- package/dist/types/reports.js +8 -0
- package/dist/types/reports.js.map +1 -0
- package/dist/types/suppressions.d.ts +72 -0
- package/dist/types/suppressions.d.ts.map +1 -0
- package/dist/types/suppressions.js +8 -0
- package/dist/types/suppressions.js.map +1 -0
- package/dist/types/templates.d.ts +96 -0
- package/dist/types/templates.d.ts.map +1 -0
- package/dist/types/templates.js +9 -0
- package/dist/types/templates.js.map +1 -0
- package/dist/types/webhook-endpoints.d.ts +123 -0
- package/dist/types/webhook-endpoints.d.ts.map +1 -0
- package/dist/types/webhook-endpoints.js +8 -0
- package/dist/types/webhook-endpoints.js.map +1 -0
- package/dist/types/whatsapp-templates.d.ts +59 -0
- package/dist/types/whatsapp-templates.d.ts.map +1 -0
- package/dist/types/whatsapp-templates.js +8 -0
- package/dist/types/whatsapp-templates.js.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/dist/webhooks.d.ts +48 -0
- package/dist/webhooks.d.ts.map +1 -0
- package/dist/webhooks.js +97 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +60 -0
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/** The Silon API client. */
|
|
2
|
+
import { Auth } from "./resources/auth.js";
|
|
3
|
+
import { Broadcasts } from "./resources/broadcasts.js";
|
|
4
|
+
import { Bulk } from "./resources/bulk.js";
|
|
5
|
+
import { ClientGroups } from "./resources/client-groups.js";
|
|
6
|
+
import { Clients } from "./resources/clients.js";
|
|
7
|
+
import { Events } from "./resources/events.js";
|
|
8
|
+
import { Messages } from "./resources/messages.js";
|
|
9
|
+
import { Otp } from "./resources/otp.js";
|
|
10
|
+
import { Profile } from "./resources/profile.js";
|
|
11
|
+
import { Push } from "./resources/push.js";
|
|
12
|
+
import { Reports } from "./resources/reports.js";
|
|
13
|
+
import { Suppressions } from "./resources/suppressions.js";
|
|
14
|
+
import { Templates } from "./resources/templates.js";
|
|
15
|
+
import { WebhookEndpoints } from "./resources/webhook-endpoints.js";
|
|
16
|
+
import { WhatsAppTemplates } from "./resources/whatsapp-templates.js";
|
|
17
|
+
/** Default per-attempt timeout, in milliseconds. */
|
|
18
|
+
export declare const DEFAULT_TIMEOUT_MS = 30000;
|
|
19
|
+
/** Default number of automatic retries after the initial attempt. */
|
|
20
|
+
export declare const DEFAULT_MAX_RETRIES = 2;
|
|
21
|
+
/** A `fetch`-compatible function — the test seam and the custom-TLS seam. */
|
|
22
|
+
export type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
|
|
23
|
+
/** HTTP methods used by the SDK. */
|
|
24
|
+
export type HttpMethod = "GET" | "POST" | "PATCH" | "PUT" | "DELETE" | "HEAD" | "OPTIONS";
|
|
25
|
+
/**
|
|
26
|
+
* Query-string parameters. `null`/`undefined` values are omitted; arrays are
|
|
27
|
+
* serialized as repeated keys.
|
|
28
|
+
*/
|
|
29
|
+
export type QueryParams = Record<string, string | number | boolean | null | undefined | ReadonlyArray<string | number | boolean>>;
|
|
30
|
+
/** Options for {@link Silon.request} — the low-level escape hatch. */
|
|
31
|
+
export interface RequestOptions {
|
|
32
|
+
/** Query-string parameters (`null`/`undefined` entries are skipped). */
|
|
33
|
+
query?: QueryParams;
|
|
34
|
+
/** JSON request body (serialized with `JSON.stringify`). */
|
|
35
|
+
body?: unknown;
|
|
36
|
+
/** Multipart body — mutually exclusive with `body`. */
|
|
37
|
+
formData?: FormData;
|
|
38
|
+
/** Extra request headers, merged over the client defaults. */
|
|
39
|
+
headers?: Record<string, string>;
|
|
40
|
+
}
|
|
41
|
+
/** Constructor options for {@link Silon}. */
|
|
42
|
+
export interface SilonOptions {
|
|
43
|
+
/** API key (`sk_live_...`). Falls back to the `SILON_API_KEY` env var. Required. */
|
|
44
|
+
apiKey?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Workspace slug, expanded to `https://<workspace>.silon.tech`. Falls back
|
|
47
|
+
* to the `SILON_WORKSPACE` env var. Ignored when a base URL is given.
|
|
48
|
+
*/
|
|
49
|
+
workspace?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Full API origin, e.g. `https://acme.silon.tech` — takes precedence over
|
|
52
|
+
* `workspace`. Falls back to the `SILON_BASE_URL` env var (which also takes
|
|
53
|
+
* precedence over `workspace`). Trailing slashes are stripped.
|
|
54
|
+
*/
|
|
55
|
+
baseUrl?: string;
|
|
56
|
+
/** Per-attempt timeout in milliseconds. Default `30000`. */
|
|
57
|
+
timeoutMs?: number;
|
|
58
|
+
/** Automatic retries after the initial attempt. Default `2`. */
|
|
59
|
+
maxRetries?: number;
|
|
60
|
+
/** Headers added to every request (override the computed defaults). */
|
|
61
|
+
defaultHeaders?: Record<string, string>;
|
|
62
|
+
/**
|
|
63
|
+
* Injectable `fetch` implementation. Use it for testing, or to control TLS
|
|
64
|
+
* on-prem — e.g. an undici-`Agent`-bound fetch trusting a private CA.
|
|
65
|
+
* Defaults to the global `fetch`.
|
|
66
|
+
*/
|
|
67
|
+
fetch?: Fetch;
|
|
68
|
+
/** @internal Test seam — overrides the retry backoff sleep (receives milliseconds). */
|
|
69
|
+
_sleep?: (ms: number) => Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Client for the Silon API.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* import Silon from "silon-sdk";
|
|
76
|
+
*
|
|
77
|
+
* const client = new Silon({ apiKey: "sk_live_...", workspace: "acme" });
|
|
78
|
+
* const sent = await client.messages.send({
|
|
79
|
+
* channel: "whatsapp",
|
|
80
|
+
* to: { client_id: "cust_001" },
|
|
81
|
+
* content: { body: "Your order has shipped" },
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* Configuration falls back to the `SILON_API_KEY`, `SILON_WORKSPACE` and
|
|
86
|
+
* `SILON_BASE_URL` environment variables; missing required config throws a
|
|
87
|
+
* {@link SilonError} from the constructor.
|
|
88
|
+
*/
|
|
89
|
+
export declare class Silon {
|
|
90
|
+
#private;
|
|
91
|
+
/** The resolved API key. */
|
|
92
|
+
readonly apiKey: string;
|
|
93
|
+
/** The resolved API origin, without a trailing slash. */
|
|
94
|
+
readonly baseUrl: string;
|
|
95
|
+
/** Per-attempt timeout in milliseconds. */
|
|
96
|
+
readonly timeoutMs: number;
|
|
97
|
+
/** Automatic retries after the initial attempt. */
|
|
98
|
+
readonly maxRetries: number;
|
|
99
|
+
/** Headers added to every request. */
|
|
100
|
+
readonly defaultHeaders: Record<string, string>;
|
|
101
|
+
/** Send messages on any channel and look up their status. */
|
|
102
|
+
readonly messages: Messages;
|
|
103
|
+
/** Track audience sends: aggregate counts + per-recipient deliveries. */
|
|
104
|
+
readonly broadcasts: Broadcasts;
|
|
105
|
+
/** Send and verify one-time passwords. */
|
|
106
|
+
readonly otp: Otp;
|
|
107
|
+
/** Manage CRM contact profiles. */
|
|
108
|
+
readonly clients: Clients;
|
|
109
|
+
/** Group contacts into broadcast audiences. */
|
|
110
|
+
readonly clientGroups: ClientGroups;
|
|
111
|
+
/** Bulk (CSV) sends: batches, saved files, per-recipient rows. */
|
|
112
|
+
readonly bulk: Bulk;
|
|
113
|
+
/** Read the event stream your webhooks are fed from. */
|
|
114
|
+
readonly events: Events;
|
|
115
|
+
/** Manage outbound webhook subscriptions. */
|
|
116
|
+
readonly webhookEndpoints: WebhookEndpoints;
|
|
117
|
+
/** The do-not-contact list, enforced on every send path. */
|
|
118
|
+
readonly suppressions: Suppressions;
|
|
119
|
+
/** Slug-keyed message templates with an immutable version spine. */
|
|
120
|
+
readonly templates: Templates;
|
|
121
|
+
/** Activity reports and provider balances. */
|
|
122
|
+
readonly reports: Reports;
|
|
123
|
+
/** Approved WhatsApp templates synced from Meta. */
|
|
124
|
+
readonly whatsappTemplates: WhatsAppTemplates;
|
|
125
|
+
/** Mobile / web push device registration and notification feeds. */
|
|
126
|
+
readonly push: Push;
|
|
127
|
+
/** The authenticated user's own profile. */
|
|
128
|
+
readonly profile: Profile;
|
|
129
|
+
/** Account signup and the legacy token login. */
|
|
130
|
+
readonly auth: Auth;
|
|
131
|
+
constructor(options?: SilonOptions);
|
|
132
|
+
/**
|
|
133
|
+
* Perform a raw API request — the escape hatch for endpoints without a
|
|
134
|
+
* typed resource method yet. Applies auth headers, the per-attempt
|
|
135
|
+
* timeout, error normalization and the retry policy.
|
|
136
|
+
*
|
|
137
|
+
* Resolves with the parsed JSON body, or `null` for 204 / empty bodies.
|
|
138
|
+
*/
|
|
139
|
+
request(method: HttpMethod, path: string, options?: RequestOptions): Promise<unknown>;
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=client.d.ts.map
|
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C,OAAO,CAAC;CAiHhG"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/** The Silon API client. */
|
|
2
|
+
import { APIConnectionError, APITimeoutError, SilonError, makeStatusError, } from "./errors.js";
|
|
3
|
+
import { parseRetryAfter } from "./internal/utils.js";
|
|
4
|
+
import { Auth } from "./resources/auth.js";
|
|
5
|
+
import { Broadcasts } from "./resources/broadcasts.js";
|
|
6
|
+
import { Bulk } from "./resources/bulk.js";
|
|
7
|
+
import { ClientGroups } from "./resources/client-groups.js";
|
|
8
|
+
import { Clients } from "./resources/clients.js";
|
|
9
|
+
import { Events } from "./resources/events.js";
|
|
10
|
+
import { Messages } from "./resources/messages.js";
|
|
11
|
+
import { Otp } from "./resources/otp.js";
|
|
12
|
+
import { Profile } from "./resources/profile.js";
|
|
13
|
+
import { Push } from "./resources/push.js";
|
|
14
|
+
import { Reports } from "./resources/reports.js";
|
|
15
|
+
import { Suppressions } from "./resources/suppressions.js";
|
|
16
|
+
import { Templates } from "./resources/templates.js";
|
|
17
|
+
import { WebhookEndpoints } from "./resources/webhook-endpoints.js";
|
|
18
|
+
import { WhatsAppTemplates } from "./resources/whatsapp-templates.js";
|
|
19
|
+
import { VERSION } from "./version.js";
|
|
20
|
+
/** Default per-attempt timeout, in milliseconds. */
|
|
21
|
+
export const DEFAULT_TIMEOUT_MS = 30_000;
|
|
22
|
+
/** Default number of automatic retries after the initial attempt. */
|
|
23
|
+
export const DEFAULT_MAX_RETRIES = 2;
|
|
24
|
+
const RETRYABLE_STATUSES = new Set([429, 500, 502, 503, 504]);
|
|
25
|
+
const IDEMPOTENT_METHODS = new Set(["GET", "HEAD", "OPTIONS", "PUT", "DELETE"]);
|
|
26
|
+
const MAX_RETRY_DELAY_S = 30;
|
|
27
|
+
/**
|
|
28
|
+
* Client for the Silon API.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import Silon from "silon-sdk";
|
|
32
|
+
*
|
|
33
|
+
* const client = new Silon({ apiKey: "sk_live_...", workspace: "acme" });
|
|
34
|
+
* const sent = await client.messages.send({
|
|
35
|
+
* channel: "whatsapp",
|
|
36
|
+
* to: { client_id: "cust_001" },
|
|
37
|
+
* content: { body: "Your order has shipped" },
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* Configuration falls back to the `SILON_API_KEY`, `SILON_WORKSPACE` and
|
|
42
|
+
* `SILON_BASE_URL` environment variables; missing required config throws a
|
|
43
|
+
* {@link SilonError} from the constructor.
|
|
44
|
+
*/
|
|
45
|
+
export class Silon {
|
|
46
|
+
/** The resolved API key. */
|
|
47
|
+
apiKey;
|
|
48
|
+
/** The resolved API origin, without a trailing slash. */
|
|
49
|
+
baseUrl;
|
|
50
|
+
/** Per-attempt timeout in milliseconds. */
|
|
51
|
+
timeoutMs;
|
|
52
|
+
/** Automatic retries after the initial attempt. */
|
|
53
|
+
maxRetries;
|
|
54
|
+
/** Headers added to every request. */
|
|
55
|
+
defaultHeaders;
|
|
56
|
+
/** Send messages on any channel and look up their status. */
|
|
57
|
+
messages;
|
|
58
|
+
/** Track audience sends: aggregate counts + per-recipient deliveries. */
|
|
59
|
+
broadcasts;
|
|
60
|
+
/** Send and verify one-time passwords. */
|
|
61
|
+
otp;
|
|
62
|
+
/** Manage CRM contact profiles. */
|
|
63
|
+
clients;
|
|
64
|
+
/** Group contacts into broadcast audiences. */
|
|
65
|
+
clientGroups;
|
|
66
|
+
/** Bulk (CSV) sends: batches, saved files, per-recipient rows. */
|
|
67
|
+
bulk;
|
|
68
|
+
/** Read the event stream your webhooks are fed from. */
|
|
69
|
+
events;
|
|
70
|
+
/** Manage outbound webhook subscriptions. */
|
|
71
|
+
webhookEndpoints;
|
|
72
|
+
/** The do-not-contact list, enforced on every send path. */
|
|
73
|
+
suppressions;
|
|
74
|
+
/** Slug-keyed message templates with an immutable version spine. */
|
|
75
|
+
templates;
|
|
76
|
+
/** Activity reports and provider balances. */
|
|
77
|
+
reports;
|
|
78
|
+
/** Approved WhatsApp templates synced from Meta. */
|
|
79
|
+
whatsappTemplates;
|
|
80
|
+
/** Mobile / web push device registration and notification feeds. */
|
|
81
|
+
push;
|
|
82
|
+
/** The authenticated user's own profile. */
|
|
83
|
+
profile;
|
|
84
|
+
/** Account signup and the legacy token login. */
|
|
85
|
+
auth;
|
|
86
|
+
#fetch;
|
|
87
|
+
#sleep;
|
|
88
|
+
constructor(options = {}) {
|
|
89
|
+
const apiKey = options.apiKey || process.env["SILON_API_KEY"];
|
|
90
|
+
if (!apiKey) {
|
|
91
|
+
throw new SilonError("No API key provided. Pass apiKey: '...' or set the SILON_API_KEY environment " +
|
|
92
|
+
"variable. Create a key in the dashboard under Settings > API keys.");
|
|
93
|
+
}
|
|
94
|
+
this.apiKey = apiKey;
|
|
95
|
+
let baseUrl = options.baseUrl || process.env["SILON_BASE_URL"];
|
|
96
|
+
const workspace = options.workspace || process.env["SILON_WORKSPACE"];
|
|
97
|
+
if (!baseUrl && workspace) {
|
|
98
|
+
baseUrl = `https://${workspace}.silon.tech`;
|
|
99
|
+
}
|
|
100
|
+
if (!baseUrl) {
|
|
101
|
+
throw new SilonError("No base URL. Pass workspace: '<your-workspace>' (=> https://<workspace>" +
|
|
102
|
+
".silon.tech), baseUrl: '...', or set SILON_WORKSPACE / SILON_BASE_URL.");
|
|
103
|
+
}
|
|
104
|
+
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
105
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
106
|
+
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
107
|
+
this.defaultHeaders = { ...options.defaultHeaders };
|
|
108
|
+
this.#fetch = options.fetch ?? globalThis.fetch;
|
|
109
|
+
this.#sleep =
|
|
110
|
+
options._sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
111
|
+
this.messages = new Messages(this);
|
|
112
|
+
this.broadcasts = new Broadcasts(this);
|
|
113
|
+
this.otp = new Otp(this);
|
|
114
|
+
this.clients = new Clients(this);
|
|
115
|
+
this.clientGroups = new ClientGroups(this);
|
|
116
|
+
this.bulk = new Bulk(this);
|
|
117
|
+
this.events = new Events(this);
|
|
118
|
+
this.webhookEndpoints = new WebhookEndpoints(this);
|
|
119
|
+
this.suppressions = new Suppressions(this);
|
|
120
|
+
this.templates = new Templates(this);
|
|
121
|
+
this.reports = new Reports(this);
|
|
122
|
+
this.whatsappTemplates = new WhatsAppTemplates(this);
|
|
123
|
+
this.push = new Push(this);
|
|
124
|
+
this.profile = new Profile(this);
|
|
125
|
+
this.auth = new Auth(this);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Perform a raw API request — the escape hatch for endpoints without a
|
|
129
|
+
* typed resource method yet. Applies auth headers, the per-attempt
|
|
130
|
+
* timeout, error normalization and the retry policy.
|
|
131
|
+
*
|
|
132
|
+
* Resolves with the parsed JSON body, or `null` for 204 / empty bodies.
|
|
133
|
+
*/
|
|
134
|
+
async request(method, path, options = {}) {
|
|
135
|
+
const headers = this.#buildHeaders(options);
|
|
136
|
+
const url = this.#buildUrl(path, options.query);
|
|
137
|
+
const body = options.formData ?? (options.body !== undefined ? JSON.stringify(options.body) : undefined);
|
|
138
|
+
let attempt = 0;
|
|
139
|
+
for (;;) {
|
|
140
|
+
let response;
|
|
141
|
+
try {
|
|
142
|
+
response = await this.#fetch(url, {
|
|
143
|
+
method,
|
|
144
|
+
headers,
|
|
145
|
+
body,
|
|
146
|
+
signal: AbortSignal.timeout(this.timeoutMs),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (this.#shouldRetry(method, headers, attempt)) {
|
|
151
|
+
await this.#sleep(this.#retryDelayMs(null, attempt));
|
|
152
|
+
attempt += 1;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const name = error?.name;
|
|
156
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
157
|
+
throw new APITimeoutError();
|
|
158
|
+
}
|
|
159
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
160
|
+
throw new APIConnectionError(`Connection error while requesting ${path}: ${message}`);
|
|
161
|
+
}
|
|
162
|
+
if (response.status >= 400) {
|
|
163
|
+
if (this.#shouldRetry(method, headers, attempt, response.status)) {
|
|
164
|
+
await this.#sleep(this.#retryDelayMs(response.headers, attempt));
|
|
165
|
+
attempt += 1;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const raw = await response.text();
|
|
169
|
+
throw makeStatusError(response.status, response.statusText, response.headers, raw);
|
|
170
|
+
}
|
|
171
|
+
return this.#parseBody(response);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
#buildHeaders(options) {
|
|
175
|
+
const headers = new Headers({
|
|
176
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
177
|
+
Accept: "application/json",
|
|
178
|
+
"User-Agent": `silon-node/${VERSION} node/${process.versions.node}`,
|
|
179
|
+
});
|
|
180
|
+
if (options.body !== undefined && !options.formData) {
|
|
181
|
+
headers.set("Content-Type", "application/json");
|
|
182
|
+
}
|
|
183
|
+
for (const [key, value] of Object.entries(this.defaultHeaders)) {
|
|
184
|
+
headers.set(key, value);
|
|
185
|
+
}
|
|
186
|
+
for (const [key, value] of Object.entries(options.headers ?? {})) {
|
|
187
|
+
headers.set(key, value);
|
|
188
|
+
}
|
|
189
|
+
return headers;
|
|
190
|
+
}
|
|
191
|
+
#buildUrl(path, query) {
|
|
192
|
+
const url = new URL(this.baseUrl + path);
|
|
193
|
+
if (query) {
|
|
194
|
+
for (const [key, value] of Object.entries(query)) {
|
|
195
|
+
if (value === null || value === undefined)
|
|
196
|
+
continue;
|
|
197
|
+
if (Array.isArray(value)) {
|
|
198
|
+
for (const item of value)
|
|
199
|
+
url.searchParams.append(key, String(item));
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
url.searchParams.append(key, String(value));
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return url.toString();
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* A request may be replayed only when the method is inherently idempotent
|
|
210
|
+
* or it carries an `Idempotency-Key` header — so a retried POST can never
|
|
211
|
+
* double-send.
|
|
212
|
+
*/
|
|
213
|
+
#shouldRetry(method, headers, attempt, status) {
|
|
214
|
+
if (attempt >= this.maxRetries)
|
|
215
|
+
return false;
|
|
216
|
+
if (!IDEMPOTENT_METHODS.has(method.toUpperCase()) && !headers.has("Idempotency-Key")) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
return status === undefined || RETRYABLE_STATUSES.has(status);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* `min(0.5 * 2^attempt, 8) + jitter(0..0.25)` seconds, raised to the
|
|
223
|
+
* server's Retry-After / RateLimit-Reset hint when larger, clamped to
|
|
224
|
+
* [0, 30] — returned in milliseconds.
|
|
225
|
+
*/
|
|
226
|
+
#retryDelayMs(headers, attempt) {
|
|
227
|
+
let delay = Math.min(0.5 * 2 ** attempt, 8) + Math.random() * 0.25;
|
|
228
|
+
if (headers) {
|
|
229
|
+
const advertised = parseRetryAfter(headers);
|
|
230
|
+
if (advertised !== null)
|
|
231
|
+
delay = Math.max(delay, advertised);
|
|
232
|
+
}
|
|
233
|
+
return Math.max(0, Math.min(delay, MAX_RETRY_DELAY_S)) * 1000;
|
|
234
|
+
}
|
|
235
|
+
async #parseBody(response) {
|
|
236
|
+
if (response.status === 204)
|
|
237
|
+
return null;
|
|
238
|
+
const text = await response.text();
|
|
239
|
+
if (text === "")
|
|
240
|
+
return null;
|
|
241
|
+
try {
|
|
242
|
+
return JSON.parse(text);
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
throw new SilonError(`Could not parse response body as JSON (HTTP ${response.status}).`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -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nE,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,UAAU,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;YAC5C,IAAI,UAAU,KAAK,IAAI;gBAAE,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,iBAAiB,CAAC,CAAC,GAAG,IAAI,CAAC;IAChE,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,QAAkB;QACjC,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG;YAAE,OAAO,IAAI,CAAC;QACzC,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACnC,IAAI,IAAI,KAAK,EAAE;YAAE,OAAO,IAAI,CAAC;QAC7B,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,UAAU,CAAC,+CAA+C,QAAQ,CAAC,MAAM,IAAI,CAAC,CAAC;QAC3F,CAAC;IACH,CAAC;CACF"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exception hierarchy for the Silon SDK.
|
|
3
|
+
*
|
|
4
|
+
* Every non-2xx API response rejects with a subclass of {@link APIStatusError}
|
|
5
|
+
* selected by HTTP status code. The Silon API returns two error body shapes,
|
|
6
|
+
* both normalized onto the error object:
|
|
7
|
+
*
|
|
8
|
+
* - **Standard errors** (`application/json`) — DRF-standardized:
|
|
9
|
+
* `{"type": "validation_error", "errors": [{"code", "detail", "attr"}]}`
|
|
10
|
+
* - **Inline problems** (`application/problem+json`) — RFC 9457-style:
|
|
11
|
+
* `{"type": "<url>", "title", "status", "detail", "field"}`
|
|
12
|
+
*
|
|
13
|
+
* `.errors` always holds a list of {@link ErrorDetail}, `.errorType` the
|
|
14
|
+
* `type` discriminator, and `.body` the parsed JSON body (useful for shapes
|
|
15
|
+
* like the OTP-verify failure, which carries `remaining_attempts`).
|
|
16
|
+
*/
|
|
17
|
+
/** Base class for every error thrown by this SDK. */
|
|
18
|
+
export declare class SilonError extends Error {
|
|
19
|
+
constructor(message: string);
|
|
20
|
+
}
|
|
21
|
+
/** The request never produced an HTTP response (DNS, TLS, socket...). */
|
|
22
|
+
export declare class APIConnectionError extends SilonError {
|
|
23
|
+
constructor(message?: string);
|
|
24
|
+
}
|
|
25
|
+
/** The per-attempt timeout (`timeoutMs`) elapsed before a response arrived. */
|
|
26
|
+
export declare class APITimeoutError extends APIConnectionError {
|
|
27
|
+
constructor();
|
|
28
|
+
}
|
|
29
|
+
/** A webhook payload failed `Silon-Signature` verification. */
|
|
30
|
+
export declare class WebhookSignatureVerificationError extends SilonError {
|
|
31
|
+
}
|
|
32
|
+
/** One normalized error entry from an API error response. */
|
|
33
|
+
export interface ErrorDetail {
|
|
34
|
+
/** Machine-readable code, e.g. `required` / `invalid` / a problem slug. */
|
|
35
|
+
code: string;
|
|
36
|
+
/** Human-readable description of the problem. */
|
|
37
|
+
detail: string;
|
|
38
|
+
/** The request field the error refers to, when applicable. */
|
|
39
|
+
attr: string | null;
|
|
40
|
+
}
|
|
41
|
+
/** Constructor options shared by every {@link APIStatusError} subclass. */
|
|
42
|
+
export interface APIStatusErrorOptions {
|
|
43
|
+
statusCode: number;
|
|
44
|
+
requestId?: string | null;
|
|
45
|
+
body?: unknown;
|
|
46
|
+
errorType?: string | null;
|
|
47
|
+
errors?: ErrorDetail[];
|
|
48
|
+
/** Server-advertised backoff in seconds (429 responses only). */
|
|
49
|
+
retryAfter?: number;
|
|
50
|
+
/**
|
|
51
|
+
* The error body's `retryable` bool, read verbatim, or `null` when a
|
|
52
|
+
* legacy/non-v1 body omits it.
|
|
53
|
+
*/
|
|
54
|
+
retryable?: boolean | null;
|
|
55
|
+
}
|
|
56
|
+
/** An API response with a 4xx/5xx status code. */
|
|
57
|
+
export declare class APIStatusError extends SilonError {
|
|
58
|
+
/** HTTP status code of the response. */
|
|
59
|
+
readonly statusCode: number;
|
|
60
|
+
/** Value of the `X-Request-Id` response header — quote it in support requests. */
|
|
61
|
+
readonly requestId: string | null;
|
|
62
|
+
/** The `type` discriminator from the error body, when present. */
|
|
63
|
+
readonly errorType: string | null;
|
|
64
|
+
/** Normalized error entries (both API error shapes map onto this). */
|
|
65
|
+
readonly errors: ErrorDetail[];
|
|
66
|
+
/** The parsed JSON error body, or `null` when the body was not JSON. */
|
|
67
|
+
readonly body: unknown;
|
|
68
|
+
/**
|
|
69
|
+
* The error body's `retryable` bool: `true` iff retrying the SAME request
|
|
70
|
+
* could ever succeed (429, 5xx, an in-flight idempotency twin), `false` for
|
|
71
|
+
* every other 4xx. Read verbatim from the v1 error body — NOT recomputed
|
|
72
|
+
* from the status code — and `null` when a legacy/non-v1 body omits it.
|
|
73
|
+
*/
|
|
74
|
+
readonly retryable: boolean | null;
|
|
75
|
+
constructor(message: string, options: APIStatusErrorOptions);
|
|
76
|
+
}
|
|
77
|
+
/** 400 Bad Request. */
|
|
78
|
+
export declare class BadRequestError extends APIStatusError {
|
|
79
|
+
}
|
|
80
|
+
/** 401 Unauthorized — check the API key. */
|
|
81
|
+
export declare class AuthenticationError extends APIStatusError {
|
|
82
|
+
}
|
|
83
|
+
/** 403 Forbidden — the key lacks the required scope or tenant access. */
|
|
84
|
+
export declare class PermissionDeniedError extends APIStatusError {
|
|
85
|
+
}
|
|
86
|
+
/** 404 Not Found. */
|
|
87
|
+
export declare class NotFoundError extends APIStatusError {
|
|
88
|
+
}
|
|
89
|
+
/** 409 Conflict — e.g. an Idempotency-Key replay with a different body. */
|
|
90
|
+
export declare class ConflictError extends APIStatusError {
|
|
91
|
+
}
|
|
92
|
+
/** 410 Gone — e.g. an expired OTP. */
|
|
93
|
+
export declare class GoneError extends APIStatusError {
|
|
94
|
+
}
|
|
95
|
+
/** 422 Unprocessable Entity — semantically invalid request. */
|
|
96
|
+
export declare class UnprocessableEntityError extends APIStatusError {
|
|
97
|
+
}
|
|
98
|
+
/** 5xx server-side failure. */
|
|
99
|
+
export declare class InternalServerError extends APIStatusError {
|
|
100
|
+
}
|
|
101
|
+
/** 429 Too Many Requests — back off until {@link retryAfter} seconds pass. */
|
|
102
|
+
export declare class RateLimitError extends APIStatusError {
|
|
103
|
+
/**
|
|
104
|
+
* Seconds to wait before retrying, parsed from the `Retry-After`
|
|
105
|
+
* (delta-seconds or HTTP-date) or `RateLimit-Reset` (Unix epoch) response
|
|
106
|
+
* headers. `undefined` when the server did not advertise a delay.
|
|
107
|
+
*/
|
|
108
|
+
readonly retryAfter?: number;
|
|
109
|
+
constructor(message: string, options: APIStatusErrorOptions);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Build the right {@link APIStatusError} subclass for a non-2xx response.
|
|
113
|
+
*
|
|
114
|
+
* @internal — exposed for the transport layer; not part of the public API.
|
|
115
|
+
*/
|
|
116
|
+
export declare function makeStatusError(status: number, statusText: string, headers: Headers, rawBody: string): APIStatusError;
|
|
117
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAIH,qDAAqD;AACrD,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM;CAI5B;AAED,yEAAyE;AACzE,qBAAa,kBAAmB,SAAQ,UAAU;gBACpC,OAAO,SAAsB;CAG1C;AAED,+EAA+E;AAC/E,qBAAa,eAAgB,SAAQ,kBAAkB;;CAItD;AAED,+DAA+D;AAC/D,qBAAa,iCAAkC,SAAQ,UAAU;CAAG;AAEpE,6DAA6D;AAC7D,MAAM,WAAW,WAAW;IAC1B,2EAA2E;IAC3E,IAAI,EAAE,MAAM,CAAC;IAC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