@chatwise/node 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +202 -0
- package/README.md +592 -2
- package/dist/cjs/client.d.ts +20 -0
- package/dist/cjs/client.js +32 -0
- package/dist/cjs/core/errors.d.ts +101 -0
- package/dist/cjs/core/errors.js +143 -0
- package/dist/cjs/core/http.d.ts +87 -0
- package/dist/cjs/core/http.js +360 -0
- package/dist/cjs/core/pagination.d.ts +35 -0
- package/dist/cjs/core/pagination.js +101 -0
- package/dist/cjs/generated/types.d.ts +1017 -0
- package/dist/cjs/generated/types.js +3 -0
- package/dist/cjs/index.d.ts +15 -0
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/resources/conversations.d.ts +20 -0
- package/dist/cjs/resources/conversations.js +32 -0
- package/dist/cjs/resources/messages.d.ts +104 -0
- package/dist/cjs/resources/messages.js +112 -0
- package/dist/cjs/resources/webhook-deliveries.d.ts +27 -0
- package/dist/cjs/resources/webhook-deliveries.js +42 -0
- package/dist/cjs/resources/webhook-endpoints.d.ts +54 -0
- package/dist/cjs/resources/webhook-endpoints.js +82 -0
- package/dist/cjs/resources/webhook-event-types.d.ts +13 -0
- package/dist/cjs/resources/webhook-event-types.js +22 -0
- package/dist/cjs/resources/whatsapp-templates.d.ts +48 -0
- package/dist/cjs/resources/whatsapp-templates.js +71 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +5 -0
- package/dist/cjs/webhooks.d.ts +74 -0
- package/dist/cjs/webhooks.js +123 -0
- package/dist/esm/client.d.ts +20 -0
- package/dist/esm/client.js +28 -0
- package/dist/esm/core/errors.d.ts +101 -0
- package/dist/esm/core/errors.js +124 -0
- package/dist/esm/core/http.d.ts +87 -0
- package/dist/esm/core/http.js +356 -0
- package/dist/esm/core/pagination.d.ts +35 -0
- package/dist/esm/core/pagination.js +95 -0
- package/dist/esm/generated/types.d.ts +1017 -0
- package/dist/esm/generated/types.js +2 -0
- package/dist/esm/index.d.ts +15 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/resources/conversations.d.ts +20 -0
- package/dist/esm/resources/conversations.js +28 -0
- package/dist/esm/resources/messages.d.ts +104 -0
- package/dist/esm/resources/messages.js +108 -0
- package/dist/esm/resources/webhook-deliveries.d.ts +27 -0
- package/dist/esm/resources/webhook-deliveries.js +38 -0
- package/dist/esm/resources/webhook-endpoints.d.ts +54 -0
- package/dist/esm/resources/webhook-endpoints.js +78 -0
- package/dist/esm/resources/webhook-event-types.d.ts +13 -0
- package/dist/esm/resources/webhook-event-types.js +18 -0
- package/dist/esm/resources/whatsapp-templates.d.ts +48 -0
- package/dist/esm/resources/whatsapp-templates.js +67 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/webhooks.d.ts +74 -0
- package/dist/esm/webhooks.js +118 -0
- package/package.json +79 -4
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/** The base of every error this package throws. */
|
|
2
|
+
export declare class ChatwiseError extends Error {
|
|
3
|
+
name: string;
|
|
4
|
+
}
|
|
5
|
+
/** What was wrong with one field of a request. */
|
|
6
|
+
export interface FieldError {
|
|
7
|
+
/** The field, such as `body.name` or `query.limit`, or the name of the value that was refused, such as `url`. */
|
|
8
|
+
path: string;
|
|
9
|
+
message: string;
|
|
10
|
+
}
|
|
11
|
+
/** What an `APIError` is made of. */
|
|
12
|
+
export interface APIErrorDetails {
|
|
13
|
+
status: number;
|
|
14
|
+
method: string;
|
|
15
|
+
path: string;
|
|
16
|
+
/** The parsed body, or its text when it isn't JSON. */
|
|
17
|
+
body: unknown;
|
|
18
|
+
retryAfterMs?: number | undefined;
|
|
19
|
+
}
|
|
20
|
+
/** Chatwise answered with an error. Each status has a subclass. */
|
|
21
|
+
export declare class APIError extends ChatwiseError {
|
|
22
|
+
name: string;
|
|
23
|
+
/** The HTTP status. */
|
|
24
|
+
readonly status: number;
|
|
25
|
+
/** A code to check in your program, such as `TEMPLATE_EXISTS`, when the error has one. */
|
|
26
|
+
readonly code: string | undefined;
|
|
27
|
+
/** What was wrong with which field, when the API says so. */
|
|
28
|
+
readonly fieldErrors: readonly FieldError[];
|
|
29
|
+
/** Quote it when you contact support. Only some 500s have one. */
|
|
30
|
+
readonly errorId: string | undefined;
|
|
31
|
+
/** How long the API asks to wait before another request, in milliseconds. From `Retry-After`. */
|
|
32
|
+
readonly retryAfterMs: number | undefined;
|
|
33
|
+
readonly method: string;
|
|
34
|
+
/** The path of the request, without its query. */
|
|
35
|
+
readonly path: string;
|
|
36
|
+
/** The parsed body of the response, or its text when it wasn't JSON. */
|
|
37
|
+
readonly body: unknown;
|
|
38
|
+
constructor({ status, method, path, body, retryAfterMs }: APIErrorDetails);
|
|
39
|
+
}
|
|
40
|
+
/** 400: a parameter, field, cursor or id is invalid. `fieldErrors` says which. */
|
|
41
|
+
export declare class BadRequestError extends APIError {
|
|
42
|
+
name: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 401: the API key is missing, invalid, expired or revoked. With the message `Invalid API key`, Chatwise
|
|
46
|
+
* couldn't check the key: try again.
|
|
47
|
+
*/
|
|
48
|
+
export declare class AuthenticationError extends APIError {
|
|
49
|
+
name: string;
|
|
50
|
+
}
|
|
51
|
+
/** 402: sending is paused for the workspace, or its subscription needs attention. */
|
|
52
|
+
export declare class PaymentRequiredError extends APIError {
|
|
53
|
+
name: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 403: the key lacks a scope, WhatsApp has to be reconnected (`code` is `WHATSAPP_REAUTH_REQUIRED`), or the
|
|
57
|
+
* person who created the key may not send replies any more (`SEND_NOT_PERMITTED`).
|
|
58
|
+
*/
|
|
59
|
+
export declare class PermissionDeniedError extends APIError {
|
|
60
|
+
name: string;
|
|
61
|
+
}
|
|
62
|
+
/** 404: it doesn't exist in the key's workspace. */
|
|
63
|
+
export declare class NotFoundError extends APIError {
|
|
64
|
+
name: string;
|
|
65
|
+
}
|
|
66
|
+
/** 409: it conflicts with an earlier request, or with how things are now. `code` says how, such as `REPLY_WINDOW_CLOSED`. */
|
|
67
|
+
export declare class ConflictError extends APIError {
|
|
68
|
+
name: string;
|
|
69
|
+
}
|
|
70
|
+
/** 413: the request body is larger than 100 KB. */
|
|
71
|
+
export declare class PayloadTooLargeError extends APIError {
|
|
72
|
+
name: string;
|
|
73
|
+
}
|
|
74
|
+
/** 422: the channel refused a reply, or its file can't be downloaded or sent. `code` says which. This request sent nothing. */
|
|
75
|
+
export declare class UnprocessableEntityError extends APIError {
|
|
76
|
+
name: string;
|
|
77
|
+
}
|
|
78
|
+
/** 429: too many requests with this key, or too many replies or files. Wait `retryAfterMs`. */
|
|
79
|
+
export declare class RateLimitError extends APIError {
|
|
80
|
+
name: string;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 5xx: something failed or is busy in Chatwise or at the channel, or WhatsApp refused a template. The request
|
|
84
|
+
* may have been carried out. `SEND_OUTCOME_UNKNOWN` is one, and it must not be sent again with a new key by itself.
|
|
85
|
+
*/
|
|
86
|
+
export declare class ServerError extends APIError {
|
|
87
|
+
name: string;
|
|
88
|
+
}
|
|
89
|
+
/** There was no complete answer: the network failed, or the answer was cut off. The request may have been carried out. */
|
|
90
|
+
export declare class ConnectionError extends ChatwiseError {
|
|
91
|
+
name: string;
|
|
92
|
+
}
|
|
93
|
+
/** An attempt took longer than `timeoutMs`. The request may have been carried out. */
|
|
94
|
+
export declare class RequestTimeoutError extends ConnectionError {
|
|
95
|
+
name: string;
|
|
96
|
+
}
|
|
97
|
+
/** Your `signal` aborted the request. The request may have been carried out. `cause` is the signal's reason. */
|
|
98
|
+
export declare class RequestAbortedError extends ChatwiseError {
|
|
99
|
+
name: string;
|
|
100
|
+
}
|
|
101
|
+
export declare function apiError(details: APIErrorDetails): APIError;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestAbortedError = exports.RequestTimeoutError = exports.ConnectionError = exports.ServerError = exports.RateLimitError = exports.UnprocessableEntityError = exports.PayloadTooLargeError = exports.ConflictError = exports.NotFoundError = exports.PermissionDeniedError = exports.PaymentRequiredError = exports.AuthenticationError = exports.BadRequestError = exports.APIError = exports.ChatwiseError = void 0;
|
|
4
|
+
exports.apiError = apiError;
|
|
5
|
+
/** The base of every error this package throws. */
|
|
6
|
+
class ChatwiseError extends Error {
|
|
7
|
+
// Written out in each class, because minifiers rename classes.
|
|
8
|
+
name = 'ChatwiseError';
|
|
9
|
+
}
|
|
10
|
+
exports.ChatwiseError = ChatwiseError;
|
|
11
|
+
const text = (value) => (typeof value === 'string' && value ? value : undefined);
|
|
12
|
+
function readFieldErrors(body) {
|
|
13
|
+
if (Array.isArray(body.errors)) {
|
|
14
|
+
return body.errors.flatMap((entry) => typeof entry?.path === 'string' && typeof entry.message === 'string' ? [{ path: entry.path, message: entry.message }] : []);
|
|
15
|
+
}
|
|
16
|
+
const field = text(body.field);
|
|
17
|
+
return field ? [{ path: field, message: text(body.message) ?? '' }] : [];
|
|
18
|
+
}
|
|
19
|
+
/** Chatwise answered with an error. Each status has a subclass. */
|
|
20
|
+
class APIError extends ChatwiseError {
|
|
21
|
+
name = 'APIError';
|
|
22
|
+
/** The HTTP status. */
|
|
23
|
+
status;
|
|
24
|
+
/** A code to check in your program, such as `TEMPLATE_EXISTS`, when the error has one. */
|
|
25
|
+
code;
|
|
26
|
+
/** What was wrong with which field, when the API says so. */
|
|
27
|
+
fieldErrors;
|
|
28
|
+
/** Quote it when you contact support. Only some 500s have one. */
|
|
29
|
+
errorId;
|
|
30
|
+
/** How long the API asks to wait before another request, in milliseconds. From `Retry-After`. */
|
|
31
|
+
retryAfterMs;
|
|
32
|
+
method;
|
|
33
|
+
/** The path of the request, without its query. */
|
|
34
|
+
path;
|
|
35
|
+
/** The parsed body of the response, or its text when it wasn't JSON. */
|
|
36
|
+
body;
|
|
37
|
+
constructor({ status, method, path, body, retryAfterMs }) {
|
|
38
|
+
const fields = typeof body === 'object' && body !== null ? body : {};
|
|
39
|
+
const fieldErrors = readFieldErrors(fields);
|
|
40
|
+
let message = text(fields.message) ?? `Chatwise answered ${status} to ${method} ${path}`;
|
|
41
|
+
if (Array.isArray(fields.errors) && fieldErrors.length) {
|
|
42
|
+
message += ` (${fieldErrors.map((error) => `${error.path}: ${error.message}`).join('; ')})`;
|
|
43
|
+
}
|
|
44
|
+
super(message);
|
|
45
|
+
this.status = status;
|
|
46
|
+
this.code = text(fields.code);
|
|
47
|
+
this.fieldErrors = fieldErrors;
|
|
48
|
+
this.errorId = text(fields.errorId);
|
|
49
|
+
this.retryAfterMs = retryAfterMs;
|
|
50
|
+
this.method = method;
|
|
51
|
+
this.path = path;
|
|
52
|
+
this.body = body;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.APIError = APIError;
|
|
56
|
+
/** 400: a parameter, field, cursor or id is invalid. `fieldErrors` says which. */
|
|
57
|
+
class BadRequestError extends APIError {
|
|
58
|
+
name = 'BadRequestError';
|
|
59
|
+
}
|
|
60
|
+
exports.BadRequestError = BadRequestError;
|
|
61
|
+
/**
|
|
62
|
+
* 401: the API key is missing, invalid, expired or revoked. With the message `Invalid API key`, Chatwise
|
|
63
|
+
* couldn't check the key: try again.
|
|
64
|
+
*/
|
|
65
|
+
class AuthenticationError extends APIError {
|
|
66
|
+
name = 'AuthenticationError';
|
|
67
|
+
}
|
|
68
|
+
exports.AuthenticationError = AuthenticationError;
|
|
69
|
+
/** 402: sending is paused for the workspace, or its subscription needs attention. */
|
|
70
|
+
class PaymentRequiredError extends APIError {
|
|
71
|
+
name = 'PaymentRequiredError';
|
|
72
|
+
}
|
|
73
|
+
exports.PaymentRequiredError = PaymentRequiredError;
|
|
74
|
+
/**
|
|
75
|
+
* 403: the key lacks a scope, WhatsApp has to be reconnected (`code` is `WHATSAPP_REAUTH_REQUIRED`), or the
|
|
76
|
+
* person who created the key may not send replies any more (`SEND_NOT_PERMITTED`).
|
|
77
|
+
*/
|
|
78
|
+
class PermissionDeniedError extends APIError {
|
|
79
|
+
name = 'PermissionDeniedError';
|
|
80
|
+
}
|
|
81
|
+
exports.PermissionDeniedError = PermissionDeniedError;
|
|
82
|
+
/** 404: it doesn't exist in the key's workspace. */
|
|
83
|
+
class NotFoundError extends APIError {
|
|
84
|
+
name = 'NotFoundError';
|
|
85
|
+
}
|
|
86
|
+
exports.NotFoundError = NotFoundError;
|
|
87
|
+
/** 409: it conflicts with an earlier request, or with how things are now. `code` says how, such as `REPLY_WINDOW_CLOSED`. */
|
|
88
|
+
class ConflictError extends APIError {
|
|
89
|
+
name = 'ConflictError';
|
|
90
|
+
}
|
|
91
|
+
exports.ConflictError = ConflictError;
|
|
92
|
+
/** 413: the request body is larger than 100 KB. */
|
|
93
|
+
class PayloadTooLargeError extends APIError {
|
|
94
|
+
name = 'PayloadTooLargeError';
|
|
95
|
+
}
|
|
96
|
+
exports.PayloadTooLargeError = PayloadTooLargeError;
|
|
97
|
+
/** 422: the channel refused a reply, or its file can't be downloaded or sent. `code` says which. This request sent nothing. */
|
|
98
|
+
class UnprocessableEntityError extends APIError {
|
|
99
|
+
name = 'UnprocessableEntityError';
|
|
100
|
+
}
|
|
101
|
+
exports.UnprocessableEntityError = UnprocessableEntityError;
|
|
102
|
+
/** 429: too many requests with this key, or too many replies or files. Wait `retryAfterMs`. */
|
|
103
|
+
class RateLimitError extends APIError {
|
|
104
|
+
name = 'RateLimitError';
|
|
105
|
+
}
|
|
106
|
+
exports.RateLimitError = RateLimitError;
|
|
107
|
+
/**
|
|
108
|
+
* 5xx: something failed or is busy in Chatwise or at the channel, or WhatsApp refused a template. The request
|
|
109
|
+
* may have been carried out. `SEND_OUTCOME_UNKNOWN` is one, and it must not be sent again with a new key by itself.
|
|
110
|
+
*/
|
|
111
|
+
class ServerError extends APIError {
|
|
112
|
+
name = 'ServerError';
|
|
113
|
+
}
|
|
114
|
+
exports.ServerError = ServerError;
|
|
115
|
+
/** There was no complete answer: the network failed, or the answer was cut off. The request may have been carried out. */
|
|
116
|
+
class ConnectionError extends ChatwiseError {
|
|
117
|
+
name = 'ConnectionError';
|
|
118
|
+
}
|
|
119
|
+
exports.ConnectionError = ConnectionError;
|
|
120
|
+
/** An attempt took longer than `timeoutMs`. The request may have been carried out. */
|
|
121
|
+
class RequestTimeoutError extends ConnectionError {
|
|
122
|
+
name = 'RequestTimeoutError';
|
|
123
|
+
}
|
|
124
|
+
exports.RequestTimeoutError = RequestTimeoutError;
|
|
125
|
+
/** Your `signal` aborted the request. The request may have been carried out. `cause` is the signal's reason. */
|
|
126
|
+
class RequestAbortedError extends ChatwiseError {
|
|
127
|
+
name = 'RequestAbortedError';
|
|
128
|
+
}
|
|
129
|
+
exports.RequestAbortedError = RequestAbortedError;
|
|
130
|
+
function apiError(details) {
|
|
131
|
+
switch (details.status) {
|
|
132
|
+
case 400: return new BadRequestError(details);
|
|
133
|
+
case 401: return new AuthenticationError(details);
|
|
134
|
+
case 402: return new PaymentRequiredError(details);
|
|
135
|
+
case 403: return new PermissionDeniedError(details);
|
|
136
|
+
case 404: return new NotFoundError(details);
|
|
137
|
+
case 409: return new ConflictError(details);
|
|
138
|
+
case 413: return new PayloadTooLargeError(details);
|
|
139
|
+
case 422: return new UnprocessableEntityError(details);
|
|
140
|
+
case 429: return new RateLimitError(details);
|
|
141
|
+
default: return details.status >= 500 ? new ServerError(details) : new APIError(details);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { APIError } from './errors.js';
|
|
2
|
+
/** An `AbortSignal`. */
|
|
3
|
+
export interface AbortSignalLike {
|
|
4
|
+
readonly aborted: boolean;
|
|
5
|
+
readonly reason?: unknown;
|
|
6
|
+
addEventListener(type: 'abort', listener: () => void): void;
|
|
7
|
+
removeEventListener(type: 'abort', listener: () => void): void;
|
|
8
|
+
}
|
|
9
|
+
/** A `fetch` response. */
|
|
10
|
+
export interface ResponseLike {
|
|
11
|
+
readonly status: number;
|
|
12
|
+
readonly headers: {
|
|
13
|
+
get(name: string): string | null;
|
|
14
|
+
};
|
|
15
|
+
/** A `ReadableStream`. An answer without one is read with `text()`, however long it is. */
|
|
16
|
+
readonly body?: unknown;
|
|
17
|
+
text(): Promise<string>;
|
|
18
|
+
}
|
|
19
|
+
/** A `fetch` function, such as the global one. It receives the API key, in the `Authorization` header. */
|
|
20
|
+
export type FetchLike = (url: string, init: {
|
|
21
|
+
method: string;
|
|
22
|
+
headers: Record<string, string>;
|
|
23
|
+
body?: string;
|
|
24
|
+
redirect: 'manual';
|
|
25
|
+
signal: any;
|
|
26
|
+
}) => Promise<ResponseLike>;
|
|
27
|
+
export interface ChatwiseOptions {
|
|
28
|
+
/** The API key. Without it, the `CHATWISE_API_KEY` environment variable. */
|
|
29
|
+
apiKey?: string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Where the API is. It must be https, except on this machine.
|
|
32
|
+
* @defaultValue 'https://api.chatwise.so'
|
|
33
|
+
*/
|
|
34
|
+
baseUrl?: string | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* How long one attempt may take, in milliseconds. Waits between attempts don't count.
|
|
37
|
+
* @defaultValue 60000
|
|
38
|
+
*/
|
|
39
|
+
timeoutMs?: number | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* How many times to retry what is safe to retry: reads, replies and creating a template after a 429, a
|
|
42
|
+
* 5xx or a network error; anything else only after a 429, because such a request wasn't carried out.
|
|
43
|
+
* @defaultValue 2
|
|
44
|
+
*/
|
|
45
|
+
maxRetries?: number | undefined;
|
|
46
|
+
/** A `fetch` to use instead of the global one. */
|
|
47
|
+
fetch?: FetchLike | undefined;
|
|
48
|
+
}
|
|
49
|
+
/** Options of a single call. */
|
|
50
|
+
export interface RequestOptions {
|
|
51
|
+
/** Aborts the call, including its waits between attempts. */
|
|
52
|
+
signal?: AbortSignalLike | undefined;
|
|
53
|
+
/** How long one attempt may take, in milliseconds. Overrides the client's. */
|
|
54
|
+
timeoutMs?: number | undefined;
|
|
55
|
+
/** How many times to retry what is safe to retry. Overrides the client's. */
|
|
56
|
+
maxRetries?: number | undefined;
|
|
57
|
+
}
|
|
58
|
+
export interface Operation {
|
|
59
|
+
method: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
60
|
+
path: string;
|
|
61
|
+
query?: object | undefined;
|
|
62
|
+
body?: object | undefined;
|
|
63
|
+
headers?: Record<string, string> | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* The route recognizes a repeat, so a request that may have been carried out can be repeated. Says which
|
|
66
|
+
* answers it is repeated after, besides a 429.
|
|
67
|
+
*/
|
|
68
|
+
repeatAfter?: (answer: APIError) => boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Codes of answers that say to repeat the request until something is ready, and for how long to. These
|
|
71
|
+
* don't count as retries, and the count starts again after each. Without a time it is a default, or none if
|
|
72
|
+
* the call has no retries.
|
|
73
|
+
*/
|
|
74
|
+
pending?: {
|
|
75
|
+
codes: readonly string[];
|
|
76
|
+
timeoutMs: number | undefined;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/** Makes a request and returns the body of its answer. */
|
|
80
|
+
export type Requester = (operation: Operation, options?: RequestOptions) => Promise<Record<string, unknown>>;
|
|
81
|
+
/** A path with its ids. An id that would change the path, such as `..`, is refused. */
|
|
82
|
+
export declare function path(strings: TemplateStringsArray, ...ids: string[]): string;
|
|
83
|
+
/**
|
|
84
|
+
* The function every resource makes its requests with. The key stays in this closure: it is on no
|
|
85
|
+
* object, so printing a client can't show it.
|
|
86
|
+
*/
|
|
87
|
+
export declare function createRequester(options: ChatwiseOptions): Requester;
|