teleleo 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.
Files changed (140) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/LICENSE +21 -0
  3. package/README.md +236 -2
  4. package/api.md +187 -0
  5. package/dist/client.d.ts +28 -0
  6. package/dist/client.d.ts.map +1 -0
  7. package/dist/client.js +27 -0
  8. package/dist/client.js.map +1 -0
  9. package/dist/core.d.ts +66 -0
  10. package/dist/core.d.ts.map +1 -0
  11. package/dist/core.js +195 -0
  12. package/dist/core.js.map +1 -0
  13. package/dist/errors.d.ts +92 -0
  14. package/dist/errors.d.ts.map +1 -0
  15. package/dist/errors.js +163 -0
  16. package/dist/errors.js.map +1 -0
  17. package/dist/index.d.ts +23 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +23 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/pagination.d.ts +41 -0
  22. package/dist/pagination.d.ts.map +1 -0
  23. package/dist/pagination.js +98 -0
  24. package/dist/pagination.js.map +1 -0
  25. package/dist/resource.d.ts +7 -0
  26. package/dist/resource.d.ts.map +1 -0
  27. package/dist/resource.js +8 -0
  28. package/dist/resource.js.map +1 -0
  29. package/dist/resources/call-scenarios.d.ts +77 -0
  30. package/dist/resources/call-scenarios.d.ts.map +1 -0
  31. package/dist/resources/call-scenarios.js +137 -0
  32. package/dist/resources/call-scenarios.js.map +1 -0
  33. package/dist/resources/calls.d.ts +69 -0
  34. package/dist/resources/calls.d.ts.map +1 -0
  35. package/dist/resources/calls.js +109 -0
  36. package/dist/resources/calls.js.map +1 -0
  37. package/dist/resources/contact-groups.d.ts +65 -0
  38. package/dist/resources/contact-groups.d.ts.map +1 -0
  39. package/dist/resources/contact-groups.js +99 -0
  40. package/dist/resources/contact-groups.js.map +1 -0
  41. package/dist/resources/contacts.d.ts +75 -0
  42. package/dist/resources/contacts.d.ts.map +1 -0
  43. package/dist/resources/contacts.js +132 -0
  44. package/dist/resources/contacts.js.map +1 -0
  45. package/dist/resources/dongles.d.ts +201 -0
  46. package/dist/resources/dongles.d.ts.map +1 -0
  47. package/dist/resources/dongles.js +296 -0
  48. package/dist/resources/dongles.js.map +1 -0
  49. package/dist/resources/files.d.ts +52 -0
  50. package/dist/resources/files.d.ts.map +1 -0
  51. package/dist/resources/files.js +73 -0
  52. package/dist/resources/files.js.map +1 -0
  53. package/dist/resources/follow-up-sms-settings.d.ts +65 -0
  54. package/dist/resources/follow-up-sms-settings.d.ts.map +1 -0
  55. package/dist/resources/follow-up-sms-settings.js +111 -0
  56. package/dist/resources/follow-up-sms-settings.js.map +1 -0
  57. package/dist/resources/index.d.ts +50 -0
  58. package/dist/resources/index.d.ts.map +1 -0
  59. package/dist/resources/index.js +36 -0
  60. package/dist/resources/index.js.map +1 -0
  61. package/dist/resources/mailings.d.ts +85 -0
  62. package/dist/resources/mailings.d.ts.map +1 -0
  63. package/dist/resources/mailings.js +191 -0
  64. package/dist/resources/mailings.js.map +1 -0
  65. package/dist/resources/public-vpn.d.ts +51 -0
  66. package/dist/resources/public-vpn.d.ts.map +1 -0
  67. package/dist/resources/public-vpn.js +67 -0
  68. package/dist/resources/public-vpn.js.map +1 -0
  69. package/dist/resources/redirections.d.ts +65 -0
  70. package/dist/resources/redirections.d.ts.map +1 -0
  71. package/dist/resources/redirections.js +113 -0
  72. package/dist/resources/redirections.js.map +1 -0
  73. package/dist/resources/sip-users.d.ts +30 -0
  74. package/dist/resources/sip-users.d.ts.map +1 -0
  75. package/dist/resources/sip-users.js +40 -0
  76. package/dist/resources/sip-users.js.map +1 -0
  77. package/dist/resources/sms.d.ts +116 -0
  78. package/dist/resources/sms.d.ts.map +1 -0
  79. package/dist/resources/sms.js +203 -0
  80. package/dist/resources/sms.js.map +1 -0
  81. package/dist/resources/socks-proxies.d.ts +65 -0
  82. package/dist/resources/socks-proxies.d.ts.map +1 -0
  83. package/dist/resources/socks-proxies.js +91 -0
  84. package/dist/resources/socks-proxies.js.map +1 -0
  85. package/dist/resources/ussd.d.ts +71 -0
  86. package/dist/resources/ussd.d.ts.map +1 -0
  87. package/dist/resources/ussd.js +89 -0
  88. package/dist/resources/ussd.js.map +1 -0
  89. package/dist/types/enums.d.ts +280 -0
  90. package/dist/types/enums.d.ts.map +1 -0
  91. package/dist/types/enums.js +233 -0
  92. package/dist/types/enums.js.map +1 -0
  93. package/dist/types/index.d.ts +4 -0
  94. package/dist/types/index.d.ts.map +1 -0
  95. package/dist/types/index.js +5 -0
  96. package/dist/types/index.js.map +1 -0
  97. package/dist/types/models.d.ts +686 -0
  98. package/dist/types/models.d.ts.map +1 -0
  99. package/dist/types/models.js +3 -0
  100. package/dist/types/models.js.map +1 -0
  101. package/dist/types/params.d.ts +711 -0
  102. package/dist/types/params.d.ts.map +1 -0
  103. package/dist/types/params.js +3 -0
  104. package/dist/types/params.js.map +1 -0
  105. package/dist/utils.d.ts +32 -0
  106. package/dist/utils.d.ts.map +1 -0
  107. package/dist/utils.js +118 -0
  108. package/dist/utils.js.map +1 -0
  109. package/dist/version.d.ts +2 -0
  110. package/dist/version.d.ts.map +1 -0
  111. package/dist/version.js +2 -0
  112. package/dist/version.js.map +1 -0
  113. package/package.json +63 -4
  114. package/src/client.ts +42 -0
  115. package/src/core.ts +278 -0
  116. package/src/errors.ts +170 -0
  117. package/src/index.ts +46 -0
  118. package/src/pagination.ts +110 -0
  119. package/src/resource.ts +10 -0
  120. package/src/resources/call-scenarios.ts +154 -0
  121. package/src/resources/calls.ts +117 -0
  122. package/src/resources/contact-groups.ts +115 -0
  123. package/src/resources/contacts.ts +149 -0
  124. package/src/resources/dongles.ts +346 -0
  125. package/src/resources/files.ts +79 -0
  126. package/src/resources/follow-up-sms-settings.ts +130 -0
  127. package/src/resources/index.ts +86 -0
  128. package/src/resources/mailings.ts +207 -0
  129. package/src/resources/public-vpn.ts +72 -0
  130. package/src/resources/redirections.ts +129 -0
  131. package/src/resources/sip-users.ts +44 -0
  132. package/src/resources/sms.ts +232 -0
  133. package/src/resources/socks-proxies.ts +107 -0
  134. package/src/resources/ussd.ts +97 -0
  135. package/src/types/enums.ts +300 -0
  136. package/src/types/index.ts +5 -0
  137. package/src/types/models.ts +747 -0
  138. package/src/types/params.ts +767 -0
  139. package/src/utils.ts +123 -0
  140. package/src/version.ts +1 -0
package/dist/core.js ADDED
@@ -0,0 +1,195 @@
1
+ var _a;
2
+ import { APIConnectionError, APIResponseValidationError, APITimeoutError, APIUserAbortError, TeleleoError, makeStatusError, } from './errors.js';
3
+ import { Page, PagePromise } from './pagination.js';
4
+ import { buildBody, buildQuery, readEnv, sleep } from './utils.js';
5
+ import { VERSION } from './version.js';
6
+ export const DEFAULT_BASE_URL = 'https://app.teleleo.com/react_api';
7
+ export const DEFAULT_TIMEOUT = 60_000;
8
+ export const DEFAULT_MAX_RETRIES = 2;
9
+ const INITIAL_RETRY_DELAY = 500;
10
+ const MAX_RETRY_DELAY = 8_000;
11
+ const MAX_RETRY_AFTER = 60_000;
12
+ const IDEMPOTENT_METHODS = new Set(['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE']);
13
+ // The server refused the request before acting on it: safe to repeat for any method.
14
+ const RETRY_ANY_METHOD = new Set([429]);
15
+ // The outcome is unknown: repeat only requests that are safe to repeat.
16
+ const RETRY_IDEMPOTENT = new Set([408, 502, 503, 504]);
17
+ // Nothing reached the server.
18
+ const NOT_SENT_CODES = new Set([
19
+ 'ECONNREFUSED',
20
+ 'ENOTFOUND',
21
+ 'EAI_AGAIN',
22
+ 'ENETUNREACH',
23
+ 'EHOSTUNREACH',
24
+ 'UND_ERR_CONNECT_TIMEOUT',
25
+ ]);
26
+ function errorCode(error) {
27
+ let current = error;
28
+ for (let depth = 0; depth < 4 && current && typeof current === 'object'; depth++) {
29
+ const code = current.code;
30
+ if (typeof code === 'string')
31
+ return code;
32
+ current = current.cause;
33
+ }
34
+ return undefined;
35
+ }
36
+ function cleanQuery(query) {
37
+ const out = {};
38
+ for (const [key, value] of Object.entries(query ?? {})) {
39
+ if (value === undefined || value === null)
40
+ continue;
41
+ out[key] =
42
+ typeof value === 'object' && !(value instanceof Date) && Symbol.iterator in value
43
+ ? Array.from(value)
44
+ : value;
45
+ }
46
+ return out;
47
+ }
48
+ export class BaseClient {
49
+ apiKey;
50
+ baseURL;
51
+ timeout;
52
+ maxRetries;
53
+ _options;
54
+ #fetch;
55
+ constructor(options = {}) {
56
+ const apiKey = options.apiKey ?? readEnv('TELELEO_API_KEY');
57
+ if (!apiKey) {
58
+ throw new TeleleoError('The API key is not set: pass apiKey to the client or set the TELELEO_API_KEY environment variable. ' +
59
+ 'Create an organization API key in the Teleleo cabinet, in the team settings of the organization.');
60
+ }
61
+ const fetchImpl = options.fetch ?? globalThis.fetch;
62
+ if (!fetchImpl)
63
+ throw new TeleleoError('fetch is not available; pass your own fetch in the client options');
64
+ this.apiKey = apiKey;
65
+ this.baseURL = (options.baseURL ?? readEnv('TELELEO_BASE_URL') ?? DEFAULT_BASE_URL).replace(/\/+$/, '');
66
+ this.timeout = options.timeout ?? DEFAULT_TIMEOUT;
67
+ this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
68
+ this._options = options;
69
+ this.#fetch = fetchImpl;
70
+ }
71
+ defaultHeaders() {
72
+ return {
73
+ Accept: 'application/json',
74
+ 'User-Agent': `teleleo-node/${VERSION}`,
75
+ 'auth-key': this.apiKey,
76
+ ...this._options.defaultHeaders,
77
+ };
78
+ }
79
+ static #shouldRetryStatus(method, status) {
80
+ return RETRY_ANY_METHOD.has(status) || (RETRY_IDEMPOTENT.has(status) && IDEMPOTENT_METHODS.has(method));
81
+ }
82
+ static #retryDelay(attempt, headers) {
83
+ const header = headers?.get('retry-after');
84
+ if (header) {
85
+ let ms = Number(header) * 1000;
86
+ if (Number.isNaN(ms))
87
+ ms = Date.parse(header) - Date.now();
88
+ if (ms >= 0 && ms <= MAX_RETRY_AFTER)
89
+ return ms;
90
+ }
91
+ const delay = Math.min(INITIAL_RETRY_DELAY * 2 ** attempt, MAX_RETRY_DELAY);
92
+ return delay * (1 - 0.25 * Math.random());
93
+ }
94
+ async #send(method, path, { query, body, options, accept }) {
95
+ const qs = buildQuery(query, options?.query);
96
+ const url = `${this.baseURL}${path}${qs ? `?${qs}` : ''}`;
97
+ const payload = body !== undefined || options?.body !== undefined ? buildBody(body, options?.body) : undefined;
98
+ const headers = { ...this.defaultHeaders() };
99
+ if (accept)
100
+ headers['Accept'] = accept;
101
+ if (payload !== undefined)
102
+ headers['Content-Type'] = 'application/json';
103
+ Object.assign(headers, options?.headers);
104
+ const maxRetries = options?.maxRetries ?? this.maxRetries;
105
+ const timeout = options?.timeout ?? this.timeout;
106
+ const userSignal = options?.signal;
107
+ const idempotent = IDEMPOTENT_METHODS.has(method);
108
+ for (let attempt = 0;; attempt++) {
109
+ if (userSignal?.aborted)
110
+ throw new APIUserAbortError();
111
+ const controller = new AbortController();
112
+ let timedOut = false;
113
+ const timer = setTimeout(() => {
114
+ timedOut = true;
115
+ controller.abort();
116
+ }, timeout);
117
+ const onAbort = () => controller.abort();
118
+ userSignal?.addEventListener('abort', onAbort, { once: true });
119
+ let answer;
120
+ try {
121
+ const init = { method, headers, signal: controller.signal };
122
+ if (payload !== undefined)
123
+ init.body = payload;
124
+ const response = await this.#fetch(url, init);
125
+ const bytes = new Uint8Array(await response.arrayBuffer());
126
+ answer = { status: response.status, headers: response.headers, bytes };
127
+ }
128
+ catch (error) {
129
+ if (userSignal?.aborted)
130
+ throw new APIUserAbortError();
131
+ const notSent = NOT_SENT_CODES.has(errorCode(error) ?? '');
132
+ if (attempt < maxRetries && (notSent || idempotent)) {
133
+ await sleep(_a.#retryDelay(attempt));
134
+ continue;
135
+ }
136
+ if (timedOut)
137
+ throw new APITimeoutError({ cause: error });
138
+ throw new APIConnectionError({ cause: error });
139
+ }
140
+ finally {
141
+ clearTimeout(timer);
142
+ userSignal?.removeEventListener('abort', onAbort);
143
+ }
144
+ if (answer.status >= 200 && answer.status < 300)
145
+ return answer;
146
+ if (attempt < maxRetries && _a.#shouldRetryStatus(method, answer.status)) {
147
+ await sleep(_a.#retryDelay(attempt, answer.headers));
148
+ continue;
149
+ }
150
+ throw makeStatusError(answer.status, answer.headers, new TextDecoder().decode(answer.bytes));
151
+ }
152
+ }
153
+ /** @internal Send a request and return the `data` of the answer. */
154
+ async request(method, path, args = {}) {
155
+ const answer = await this.#send(method, path, args);
156
+ const text = new TextDecoder().decode(answer.bytes);
157
+ let payload;
158
+ try {
159
+ payload = JSON.parse(text);
160
+ }
161
+ catch {
162
+ const type = answer.headers.get('content-type') ?? 'no content type';
163
+ throw new APIResponseValidationError(answer.status, text, `Expected a JSON answer from ${path}, got ${type}`);
164
+ }
165
+ if (payload && typeof payload === 'object' && !Array.isArray(payload) && 'data' in payload) {
166
+ return payload.data;
167
+ }
168
+ return payload;
169
+ }
170
+ /** @internal Send a request and return the raw bytes of the answer. */
171
+ async requestBytes(method, path, args = {}) {
172
+ const answer = await this.#send(method, path, { ...args, accept: '*/*' });
173
+ return answer.bytes;
174
+ }
175
+ /** @internal Request one page of a list. */
176
+ getPage(path, args) {
177
+ const load = async (query) => {
178
+ const data = await this.request('GET', path, { query, options: args.options });
179
+ let items = [];
180
+ let total = null;
181
+ if (Array.isArray(data)) {
182
+ items = data;
183
+ }
184
+ else if (data && typeof data === 'object') {
185
+ items = data[args.itemKey] ?? [];
186
+ total = data['total_count'] ?? null;
187
+ }
188
+ return new Page(Array.isArray(items) ? items : [], typeof total === 'number' ? total : null, query, load);
189
+ };
190
+ const query = cleanQuery(args.query);
191
+ return new PagePromise(() => load(query));
192
+ }
193
+ }
194
+ _a = BaseClient;
195
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":";AAAA,OAAO,EACL,kBAAkB,EAClB,0BAA0B,EAC1B,eAAe,EACf,iBAAiB,EACjB,YAAY,EACZ,eAAe,GAChB,M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,CAAC;YAC1B,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;gBACxB,KAAK,GAAG,IAAI,CAAC;YACf,CAAC;iBAAM,IAAI,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;gBAC5C,KAAK,GAAI,IAAgC,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;gBAC9D,KAAK,GAAI,IAAgC,CAAC,aAAa,CAAC,IAAI,IAAI,CAAC;YACnE,CAAC;YACD,OAAO,IAAI,IAAI,CACb,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAE,KAAa,CAAC,CAAC,CAAC,EAAE,EAC1C,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,EACxC,KAAK,EACL,IAAI,CACL,CAAC;QACJ,CAAC,CAAC;QACF,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACrC,OAAO,IAAI,WAAW,CAAI,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAC/C,CAAC;CACF"}
@@ -0,0 +1,92 @@
1
+ /** Base class for every error thrown by this library. */
2
+ export declare class TeleleoError extends Error {
3
+ constructor(message: string, options?: {
4
+ cause?: unknown;
5
+ });
6
+ }
7
+ /** An error that happened while talking to the API. */
8
+ export declare class APIError extends TeleleoError {
9
+ }
10
+ /** The API could not be reached. */
11
+ export declare class APIConnectionError extends APIError {
12
+ constructor({ message, cause }?: {
13
+ message?: string;
14
+ cause?: unknown;
15
+ });
16
+ }
17
+ /** The request timed out. */
18
+ export declare class APITimeoutError extends APIConnectionError {
19
+ constructor({ cause }?: {
20
+ cause?: unknown;
21
+ });
22
+ }
23
+ /** The request was aborted through the `signal` you passed. */
24
+ export declare class APIUserAbortError extends APIError {
25
+ constructor();
26
+ }
27
+ /** The API answered with a success status, but the body is not the JSON the library expects. */
28
+ export declare class APIResponseValidationError extends APIError {
29
+ readonly status: number;
30
+ readonly body: string;
31
+ constructor(status: number, body: string, message: string);
32
+ }
33
+ /** The API answered with a 4xx or 5xx status. */
34
+ export declare class APIStatusError extends APIError {
35
+ /** HTTP status of the answer. */
36
+ readonly status: number;
37
+ /** Machine-readable error code, for example `DONGLE_ERROR_DONGLE_NOT_FOUND`. Act on this. */
38
+ readonly errorMessage: string | null;
39
+ /** Human-readable details; may be empty. */
40
+ readonly errorDetails: string[];
41
+ /** Quote it to Teleleo support. */
42
+ readonly requestId: string | null;
43
+ /** Headers of the answer. */
44
+ readonly headers: Headers;
45
+ /** The decoded body of the answer. */
46
+ readonly body: unknown;
47
+ constructor(message: string, init: {
48
+ status: number;
49
+ errorMessage: string | null;
50
+ errorDetails: string[];
51
+ requestId: string | null;
52
+ headers: Headers;
53
+ body: unknown;
54
+ });
55
+ }
56
+ /** 400: the request is invalid. */
57
+ export declare class BadRequestError extends APIStatusError {
58
+ }
59
+ /** 401: the API key is missing, unknown, revoked or expired, or the account is not active. */
60
+ export declare class AuthenticationError extends APIStatusError {
61
+ }
62
+ /**
63
+ * 403: the request is outside the public API (`PUBLIC_API_ROUTE_FORBIDDEN`), a field is not allowed, or the
64
+ * role of the organization API key does not have the capability the method needs
65
+ * (`ORGANIZATION_CAPABILITY_FORBIDDEN`).
66
+ */
67
+ export declare class PermissionDeniedError extends APIStatusError {
68
+ }
69
+ /**
70
+ * The object or path does not exist.
71
+ *
72
+ * API v1 answers 400 with an `errorMessage` that contains `NOT_FOUND` for a missing object, and 404 for an
73
+ * unknown path. Both throw this error; `status` keeps the real status.
74
+ */
75
+ export declare class NotFoundError extends APIStatusError {
76
+ }
77
+ /** 409: the request conflicts with the current state. */
78
+ export declare class ConflictError extends APIStatusError {
79
+ }
80
+ /**
81
+ * 429: too many requests. An organization API key over its budget for the minute answers
82
+ * `ORGANIZATION_API_KEY_RATE_LIMIT_EXCEEDED` with a `Retry-After` header; the client waits that long and
83
+ * repeats the request within `maxRetries` before it throws this error.
84
+ */
85
+ export declare class RateLimitError extends APIStatusError {
86
+ }
87
+ /** 5xx: a failure on the Teleleo side. Retry later and quote `requestId` if it persists. */
88
+ export declare class InternalServerError extends APIStatusError {
89
+ }
90
+ /** @internal */
91
+ export declare function makeStatusError(status: number, headers: Headers, text: string): APIStatusError;
92
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,qBAAa,YAAa,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAI3D;AAED,uDAAuD;AACvD,qBAAa,QAAS,SAAQ,YAAY;CAAG;AAE7C,oCAAoC;AACpC,qBAAa,kBAAmB,SAAQ,QAAQ;gBAClC,EAAE,OAAO,EAAE,KAAK,EAAE,GAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAO;CAG3E;AAED,6BAA6B;AAC7B,qBAAa,eAAgB,SAAQ,kBAAkB;gBACzC,EAAE,KAAK,EAAE,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAO;CAGhD;AAED,+DAA+D;AAC/D,qBAAa,iBAAkB,SAAQ,QAAQ;;CAI9C;AAED,gGAAgG;AAChG,qBAAa,0BAA2B,SAAQ,QAAQ;IACtD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK1D;AAED,iDAAiD;AACjD,qBAAa,cAAe,SAAQ,QAAQ;IAC1C,iCAAiC;IACjC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,6FAA6F;IAC7F,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC,4CAA4C;IAC5C,QAAQ,CAAC,YAAY,EAAE,MAAM,EAAE,CAAC;IAChC,mCAAmC;IACnC,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,6BAA6B;IAC7B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,sCAAsC;IACtC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;gBAGrB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE;QACJ,MAAM,EAAE,MAAM,CAAC;QACf,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;QAC5B,YAAY,EAAE,MAAM,EAAE,CAAC;QACvB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;QACzB,OAAO,EAAE,OAAO,CAAC;QACjB,IAAI,EAAE,OAAO,CAAC;KACf;CAUJ;AAED,mCAAmC;AACnC,qBAAa,eAAgB,SAAQ,cAAc;CAAG;AAEtD,8FAA8F;AAC9F,qBAAa,mBAAoB,SAAQ,cAAc;CAAG;AAE1D;;;;GAIG;AACH,qBAAa,qBAAsB,SAAQ,cAAc;CAAG;AAE5D;;;;;GAKG;AACH,qBAAa,aAAc,SAAQ,cAAc;CAAG;AAEpD,yDAAyD;AACzD,qBAAa,aAAc,SAAQ,cAAc;CAAG;AAEpD;;;;GAIG;AACH,qBAAa,cAAe,SAAQ,cAAc;CAAG;AAErD,4FAA4F;AAC5F,qBAAa,mBAAoB,SAAQ,cAAc;CAAG;AAW1D,gBAAgB;AAChB,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,cAAc,CA6C9F"}
package/dist/errors.js ADDED
@@ -0,0 +1,163 @@
1
+ /** Base class for every error thrown by this library. */
2
+ export class TeleleoError extends Error {
3
+ constructor(message, options) {
4
+ super(message, options);
5
+ this.name = new.target.name;
6
+ }
7
+ }
8
+ /** An error that happened while talking to the API. */
9
+ export class APIError extends TeleleoError {
10
+ }
11
+ /** The API could not be reached. */
12
+ export class APIConnectionError extends APIError {
13
+ constructor({ message, cause } = {}) {
14
+ super(message ?? 'Connection error.', { cause });
15
+ }
16
+ }
17
+ /** The request timed out. */
18
+ export class APITimeoutError extends APIConnectionError {
19
+ constructor({ cause } = {}) {
20
+ super({ message: 'Request timed out.', cause });
21
+ }
22
+ }
23
+ /** The request was aborted through the `signal` you passed. */
24
+ export class APIUserAbortError extends APIError {
25
+ constructor() {
26
+ super('Request was aborted.');
27
+ }
28
+ }
29
+ /** The API answered with a success status, but the body is not the JSON the library expects. */
30
+ export class APIResponseValidationError extends APIError {
31
+ status;
32
+ body;
33
+ constructor(status, body, message) {
34
+ super(message);
35
+ this.status = status;
36
+ this.body = body;
37
+ }
38
+ }
39
+ /** The API answered with a 4xx or 5xx status. */
40
+ export class APIStatusError extends APIError {
41
+ /** HTTP status of the answer. */
42
+ status;
43
+ /** Machine-readable error code, for example `DONGLE_ERROR_DONGLE_NOT_FOUND`. Act on this. */
44
+ errorMessage;
45
+ /** Human-readable details; may be empty. */
46
+ errorDetails;
47
+ /** Quote it to Teleleo support. */
48
+ requestId;
49
+ /** Headers of the answer. */
50
+ headers;
51
+ /** The decoded body of the answer. */
52
+ body;
53
+ constructor(message, init) {
54
+ super(message);
55
+ this.status = init.status;
56
+ this.errorMessage = init.errorMessage;
57
+ this.errorDetails = init.errorDetails;
58
+ this.requestId = init.requestId;
59
+ this.headers = init.headers;
60
+ this.body = init.body;
61
+ }
62
+ }
63
+ /** 400: the request is invalid. */
64
+ export class BadRequestError extends APIStatusError {
65
+ }
66
+ /** 401: the API key is missing, unknown, revoked or expired, or the account is not active. */
67
+ export class AuthenticationError extends APIStatusError {
68
+ }
69
+ /**
70
+ * 403: the request is outside the public API (`PUBLIC_API_ROUTE_FORBIDDEN`), a field is not allowed, or the
71
+ * role of the organization API key does not have the capability the method needs
72
+ * (`ORGANIZATION_CAPABILITY_FORBIDDEN`).
73
+ */
74
+ export class PermissionDeniedError extends APIStatusError {
75
+ }
76
+ /**
77
+ * The object or path does not exist.
78
+ *
79
+ * API v1 answers 400 with an `errorMessage` that contains `NOT_FOUND` for a missing object, and 404 for an
80
+ * unknown path. Both throw this error; `status` keeps the real status.
81
+ */
82
+ export class NotFoundError extends APIStatusError {
83
+ }
84
+ /** 409: the request conflicts with the current state. */
85
+ export class ConflictError extends APIStatusError {
86
+ }
87
+ /**
88
+ * 429: too many requests. An organization API key over its budget for the minute answers
89
+ * `ORGANIZATION_API_KEY_RATE_LIMIT_EXCEEDED` with a `Retry-After` header; the client waits that long and
90
+ * repeats the request within `maxRetries` before it throws this error.
91
+ */
92
+ export class RateLimitError extends APIStatusError {
93
+ }
94
+ /** 5xx: a failure on the Teleleo side. Retry later and quote `requestId` if it persists. */
95
+ export class InternalServerError extends APIStatusError {
96
+ }
97
+ function parseErrorBody(text) {
98
+ if (!text)
99
+ return null;
100
+ try {
101
+ return JSON.parse(text);
102
+ }
103
+ catch {
104
+ return text;
105
+ }
106
+ }
107
+ /** @internal */
108
+ export function makeStatusError(status, headers, text) {
109
+ const body = parseErrorBody(text);
110
+ let errorMessage = null;
111
+ let errorDetails = [];
112
+ let requestId = null;
113
+ if (body && typeof body === 'object' && !Array.isArray(body)) {
114
+ const record = body;
115
+ if (typeof record['error_message'] === 'string' && record['error_message']) {
116
+ errorMessage = record['error_message'];
117
+ }
118
+ else if (typeof record['code'] === 'string') {
119
+ errorMessage = record['code'];
120
+ }
121
+ for (const key of ['error_details', 'error_message_detail']) {
122
+ const raw = record[key];
123
+ if (Array.isArray(raw) && raw.length) {
124
+ errorDetails = raw.map(String);
125
+ break;
126
+ }
127
+ if (typeof raw === 'string' && raw) {
128
+ errorDetails = [raw];
129
+ break;
130
+ }
131
+ }
132
+ if (record['request_id'])
133
+ requestId = String(record['request_id']);
134
+ }
135
+ else if (typeof body === 'string' && body.trim() && !body.trimStart().startsWith('<')) {
136
+ errorDetails = [body.trim().slice(0, 500)];
137
+ }
138
+ requestId ??= headers.get('x-request-id');
139
+ let message = `Error code: ${status}`;
140
+ if (errorMessage)
141
+ message += ` - ${errorMessage}`;
142
+ if (errorDetails.length)
143
+ message += `: ${errorDetails.join('; ')}`;
144
+ if (requestId)
145
+ message += ` (request_id: ${requestId})`;
146
+ const init = { status, errorMessage, errorDetails, requestId, headers, body };
147
+ if (status === 404 || (status === 400 && errorMessage?.includes('NOT_FOUND')))
148
+ return new NotFoundError(message, init);
149
+ if (status === 400)
150
+ return new BadRequestError(message, init);
151
+ if (status === 401)
152
+ return new AuthenticationError(message, init);
153
+ if (status === 403)
154
+ return new PermissionDeniedError(message, init);
155
+ if (status === 409)
156
+ return new ConflictError(message, init);
157
+ if (status === 429)
158
+ return new RateLimitError(message, init);
159
+ if (status >= 500)
160
+ return new InternalServerError(message, init);
161
+ return new APIStatusError(message, init);
162
+ }
163
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,yDAAyD;AACzD,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe,EAAE,OAA6B;QACxD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;IAC9B,CAAC;CACF;AAED,uDAAuD;AACvD,MAAM,OAAO,QAAS,SAAQ,YAAY;CAAG;AAE7C,oCAAoC;AACpC,MAAM,OAAO,kBAAmB,SAAQ,QAAQ;IAC9C,YAAY,EAAE,OAAO,EAAE,KAAK,KAA4C,EAAE;QACxE,KAAK,CAAC,OAAO,IAAI,mBAAmB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IACnD,CAAC;CACF;AAED,6BAA6B;AAC7B,MAAM,OAAO,eAAgB,SAAQ,kBAAkB;IACrD,YAAY,EAAE,KAAK,K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AAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC9E,IAAI,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,KAAK,GAAG,IAAI,YAAY,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;QAC3E,OAAO,IAAI,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC1C,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,eAAe,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC9D,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAClE,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,qBAAqB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACpE,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,aAAa,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC5D,IAAI,MAAM,KAAK,GAAG;QAAE,OAAO,IAAI,cAAc,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IAC7D,IAAI,MAAM,IAAI,GAAG;QAAE,OAAO,IAAI,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACjE,OAAO,IAAI,cAAc,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The official TypeScript and JavaScript library for the Teleleo API.
3
+ *
4
+ * ```ts
5
+ * import Teleleo from 'teleleo';
6
+ *
7
+ * const client = new Teleleo(); // reads TELELEO_API_KEY
8
+ * const sms = await client.sms.send(1234, { number: '+447700900123', message: 'Hello from Teleleo' });
9
+ * ```
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ import { Teleleo } from './client.js';
14
+ export { Teleleo };
15
+ export default Teleleo;
16
+ export { type ClientOptions, type RequestOptions, DEFAULT_BASE_URL, DEFAULT_MAX_RETRIES, DEFAULT_TIMEOUT, } from './core.js';
17
+ export { TeleleoError, APIError, APIConnectionError, APITimeoutError, APIUserAbortError, APIResponseValidationError, APIStatusError, BadRequestError, AuthenticationError, PermissionDeniedError, NotFoundError, ConflictError, RateLimitError, InternalServerError, } from './errors.js';
18
+ export { Page, PagePromise } from './pagination.js';
19
+ export { formatDate, toDate } from './utils.js';
20
+ export { VERSION } from './version.js';
21
+ export * from './types/index.js';
22
+ export * from './resources/index.js';
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,CAAC;AACnB,eAAe,OAAO,CAAC;AAEvB,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,GAChB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,0BAA0B,EAC1B,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,qBAAqB,EACrB,aAAa,EACb,aAAa,EACb,cAAc,EACd,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The official TypeScript and JavaScript library for the Teleleo API.
3
+ *
4
+ * ```ts
5
+ * import Teleleo from 'teleleo';
6
+ *
7
+ * const client = new Teleleo(); // reads TELELEO_API_KEY
8
+ * const sms = await client.sms.send(1234, { number: '+447700900123', message: 'Hello from Teleleo' });
9
+ * ```
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ import { Teleleo } from './client.js';
14
+ export { Teleleo };
15
+ export default Teleleo;
16
+ export { DEFAULT_BASE_URL, DEFAULT_MAX_RETRIES, DEFAULT_TIMEOUT, } from './core.js';
17
+ export { TeleleoError, APIError, APIConnectionError, APITimeoutError, APIUserAbortError, APIResponseValidationError, APIStatusError, BadRequestError, AuthenticationError, PermissionDeniedError, NotFoundError, ConflictError, RateLimitError, InternalServerError, } from './errors.js';
18
+ export { Page, PagePromise } from './pagination.js';
19
+ export { formatDate, toDate } from './utils.js';
20
+ export { VERSION } from './version.js';
21
+ export * from './types/index.js';
22
+ export * from './resources/index.js';
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,CAAC;AACnB,eAAe,OAAO,CAAC;AAEvB,OAAO,EAGL,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,GAChB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,0BAA0B,EAC1B,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,qBAAqB,EACrB,aAAa,EACb,aAAa,EACb,cAAc,EACd,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1,41 @@
1
+ /** @internal */
2
+ export type FetchPage<T> = (query: Record<string, unknown>) => Promise<Page<T>>;
3
+ /**
4
+ * One page of a list.
5
+ *
6
+ * `page.items` holds this page. `for await` over the page goes through every item of every page and requests the
7
+ * next pages as needed.
8
+ */
9
+ export declare class Page<T> implements AsyncIterable<T> {
10
+ #private;
11
+ /** The items of this page. */
12
+ readonly items: T[];
13
+ /** How many items match the request across all pages, when the API reports it. */
14
+ readonly totalCount: number | null;
15
+ /** @internal */
16
+ constructor(items: T[], totalCount: number | null, query: Record<string, unknown>, fetch: FetchPage<T>);
17
+ hasNextPage(): boolean;
18
+ getNextPage(): Promise<Page<T>>;
19
+ /** Every page, starting with this one. */
20
+ iterPages(): AsyncGenerator<Page<T>>;
21
+ [Symbol.asyncIterator](): AsyncGenerator<T>;
22
+ }
23
+ /**
24
+ * The result of a list method: await it for the first page, or iterate it with `for await` to go through every
25
+ * item of every page.
26
+ *
27
+ * ```ts
28
+ * const page = await client.sms.list({ limit: 100 });
29
+ * for await (const sms of client.sms.list({ limit: 100 })) console.log(sms.message_decoded);
30
+ * ```
31
+ */
32
+ export declare class PagePromise<T> implements PromiseLike<Page<T>>, AsyncIterable<T> {
33
+ #private;
34
+ /** @internal */
35
+ constructor(load: () => Promise<Page<T>>);
36
+ then<R1 = Page<T>, R2 = never>(onfulfilled?: ((value: Page<T>) => R1 | PromiseLike<R1>) | null, onrejected?: ((reason: unknown) => R2 | PromiseLike<R2>) | null): Promise<R1 | R2>;
37
+ catch<R = never>(onrejected?: ((reason: unknown) => R | PromiseLike<R>) | null): Promise<Page<T> | R>;
38
+ finally(onfinally?: (() => void) | null): Promise<Page<T>>;
39
+ [Symbol.asyncIterator](): AsyncGenerator<T>;
40
+ }
41
+ //# sourceMappingURL=pagination.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../src/pagination.ts"],"names":[],"mappings":"AAGA,gBAAgB;AAChB,MAAM,MAAM,SAAS,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