@chatwise/node 0.0.0-stage → 0.1.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 (62) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/LICENSE +202 -0
  3. package/README.md +506 -2
  4. package/dist/cjs/client.d.ts +20 -0
  5. package/dist/cjs/client.js +32 -0
  6. package/dist/cjs/core/errors.d.ts +91 -0
  7. package/dist/cjs/core/errors.js +131 -0
  8. package/dist/cjs/core/http.d.ts +74 -0
  9. package/dist/cjs/core/http.js +336 -0
  10. package/dist/cjs/core/pagination.d.ts +35 -0
  11. package/dist/cjs/core/pagination.js +101 -0
  12. package/dist/cjs/generated/types.d.ts +969 -0
  13. package/dist/cjs/generated/types.js +3 -0
  14. package/dist/cjs/index.d.ts +15 -0
  15. package/dist/cjs/index.js +25 -0
  16. package/dist/cjs/package.json +1 -0
  17. package/dist/cjs/resources/conversations.d.ts +20 -0
  18. package/dist/cjs/resources/conversations.js +32 -0
  19. package/dist/cjs/resources/messages.d.ts +46 -0
  20. package/dist/cjs/resources/messages.js +60 -0
  21. package/dist/cjs/resources/webhook-deliveries.d.ts +27 -0
  22. package/dist/cjs/resources/webhook-deliveries.js +42 -0
  23. package/dist/cjs/resources/webhook-endpoints.d.ts +54 -0
  24. package/dist/cjs/resources/webhook-endpoints.js +82 -0
  25. package/dist/cjs/resources/webhook-event-types.d.ts +13 -0
  26. package/dist/cjs/resources/webhook-event-types.js +22 -0
  27. package/dist/cjs/resources/whatsapp-templates.d.ts +48 -0
  28. package/dist/cjs/resources/whatsapp-templates.js +69 -0
  29. package/dist/cjs/version.d.ts +2 -0
  30. package/dist/cjs/version.js +5 -0
  31. package/dist/cjs/webhooks.d.ts +74 -0
  32. package/dist/cjs/webhooks.js +123 -0
  33. package/dist/esm/client.d.ts +20 -0
  34. package/dist/esm/client.js +28 -0
  35. package/dist/esm/core/errors.d.ts +91 -0
  36. package/dist/esm/core/errors.js +113 -0
  37. package/dist/esm/core/http.d.ts +74 -0
  38. package/dist/esm/core/http.js +332 -0
  39. package/dist/esm/core/pagination.d.ts +35 -0
  40. package/dist/esm/core/pagination.js +95 -0
  41. package/dist/esm/generated/types.d.ts +969 -0
  42. package/dist/esm/generated/types.js +2 -0
  43. package/dist/esm/index.d.ts +15 -0
  44. package/dist/esm/index.js +4 -0
  45. package/dist/esm/package.json +1 -0
  46. package/dist/esm/resources/conversations.d.ts +20 -0
  47. package/dist/esm/resources/conversations.js +28 -0
  48. package/dist/esm/resources/messages.d.ts +46 -0
  49. package/dist/esm/resources/messages.js +56 -0
  50. package/dist/esm/resources/webhook-deliveries.d.ts +27 -0
  51. package/dist/esm/resources/webhook-deliveries.js +38 -0
  52. package/dist/esm/resources/webhook-endpoints.d.ts +54 -0
  53. package/dist/esm/resources/webhook-endpoints.js +78 -0
  54. package/dist/esm/resources/webhook-event-types.d.ts +13 -0
  55. package/dist/esm/resources/webhook-event-types.js +18 -0
  56. package/dist/esm/resources/whatsapp-templates.d.ts +48 -0
  57. package/dist/esm/resources/whatsapp-templates.js +65 -0
  58. package/dist/esm/version.d.ts +2 -0
  59. package/dist/esm/version.js +2 -0
  60. package/dist/esm/webhooks.d.ts +74 -0
  61. package/dist/esm/webhooks.js +118 -0
  62. package/package.json +79 -4
@@ -0,0 +1,2 @@
1
+ // Generated by `npm run generate` from spec/openapi.json. Don't edit.
2
+ export {};
@@ -0,0 +1,15 @@
1
+ export { Chatwise } from './client.js';
2
+ export type { AbortSignalLike, ChatwiseOptions, FetchLike, RequestOptions, ResponseLike } from './core/http.js';
3
+ export type { Page, PagePromise } from './core/pagination.js';
4
+ export { APIError, AuthenticationError, BadRequestError, ChatwiseError, ConflictError, ConnectionError, NotFoundError, PayloadTooLargeError, PaymentRequiredError, PermissionDeniedError, RateLimitError, RequestAbortedError, RequestTimeoutError, ServerError, } from './core/errors.js';
5
+ export type { APIErrorDetails, FieldError } from './core/errors.js';
6
+ export { verifyWebhook, WebhookVerificationError } from './webhooks.js';
7
+ export type { UnknownWebhookEvent, UnknownWebhookEventType, VerifyWebhookOptions, WebhookEvent, WebhookHeaders, WebhookVerificationErrorCode } from './webhooks.js';
8
+ export { VERSION } from './version.js';
9
+ export type { Conversations } from './resources/conversations.js';
10
+ export type { Messages, SendMessageParams } from './resources/messages.js';
11
+ export type { WebhookDeliveries } from './resources/webhook-deliveries.js';
12
+ export type { WebhookEndpoints } from './resources/webhook-endpoints.js';
13
+ export type { WebhookEventTypes } from './resources/webhook-event-types.js';
14
+ export type { CreateWhatsappTemplateOptions, WhatsappTemplates } from './resources/whatsapp-templates.js';
15
+ export type { Channel, CommentReceivedEvent, CommentReceivedEventData, Conversation, ConversationStatus, CreateWebhookEndpointParams, CreateWhatsappTemplateParams, DeleteWebhookEndpointResult, KnownWebhookEvent, KnownWebhookEventType, ListConversationMessagesParams, ListConversationsParams, ListMessagesParams, ListWebhookDeliveriesParams, ListWhatsappTemplatesParams, Message, MessageDeliveredEvent, MessageDirection, MessageEventData, MessageFailedEvent, MessageFailedEventData, MessageReadEvent, MessageReceivedEvent, MessageSentEvent, Open, RotateWebhookSecretResult, SendMessageResult, SendWhatsappTemplateParams, SendWhatsappTemplateResult, TestWebhookEndpointResult, UpdateWebhookEndpointParams, WebhookDelivery, WebhookDeliveryDetails, WebhookDeliveryStatus, WebhookEndpoint, WebhookEndpointStatus, WebhookEndpointWithSecret, WebhookEventType, WebhookEventTypeInfo, WebhookTestEvent, WebhookTestEventData, WhatsappTemplate, WhatsappTemplateCategory, WhatsappTemplateComponent, WhatsappTemplateParam, } from './generated/types.js';
@@ -0,0 +1,4 @@
1
+ export { Chatwise } from './client.js';
2
+ export { APIError, AuthenticationError, BadRequestError, ChatwiseError, ConflictError, ConnectionError, NotFoundError, PayloadTooLargeError, PaymentRequiredError, PermissionDeniedError, RateLimitError, RequestAbortedError, RequestTimeoutError, ServerError, } from './core/errors.js';
3
+ export { verifyWebhook, WebhookVerificationError } from './webhooks.js';
4
+ export { VERSION } from './version.js';
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1,20 @@
1
+ import { type RequestOptions } from '../core/http.js';
2
+ import { type PagePromise } from '../core/pagination.js';
3
+ import type { Conversation, ListConversationsParams } from '../generated/types.js';
4
+ /** The conversations in the workspace's inbox. */
5
+ export declare class Conversations {
6
+ #private;
7
+ /**
8
+ * Lists conversations, or finds them by customer. The latest activity comes first. One that gets a
9
+ * message while you read the pages moves to the top, so you can miss it.
10
+ *
11
+ * Scope: `conversations:read`.
12
+ */
13
+ list(params?: ListConversationsParams, options?: RequestOptions): PagePromise<Conversation>;
14
+ /**
15
+ * Reads one conversation: its channel, status and customer.
16
+ *
17
+ * Scope: `conversations:read`.
18
+ */
19
+ get(conversationId: string, options?: RequestOptions): Promise<Conversation>;
20
+ }
@@ -0,0 +1,28 @@
1
+ import { path } from '../core/http.js';
2
+ import { list } from '../core/pagination.js';
3
+ /** The conversations in the workspace's inbox. */
4
+ export class Conversations {
5
+ #request;
6
+ /** @internal */
7
+ constructor(request) {
8
+ this.#request = request;
9
+ }
10
+ /**
11
+ * Lists conversations, or finds them by customer. The latest activity comes first. One that gets a
12
+ * message while you read the pages moves to the top, so you can miss it.
13
+ *
14
+ * Scope: `conversations:read`.
15
+ */
16
+ list(params = {}, options) {
17
+ return list(this.#request, () => '/v1/conversations', params, options);
18
+ }
19
+ /**
20
+ * Reads one conversation: its channel, status and customer.
21
+ *
22
+ * Scope: `conversations:read`.
23
+ */
24
+ async get(conversationId, options) {
25
+ const { data } = await this.#request({ method: 'GET', path: path `/v1/conversations/${conversationId}` }, options);
26
+ return data;
27
+ }
28
+ }
@@ -0,0 +1,46 @@
1
+ import { type RequestOptions } from '../core/http.js';
2
+ import { type PagePromise } from '../core/pagination.js';
3
+ import type { ListConversationMessagesParams, ListMessagesParams, Message, SendMessageParams as SendMessageBody, SendMessageResult } from '../generated/types.js';
4
+ /** A template to send by id, and its values. */
5
+ export type SendMessageParams = Omit<SendMessageBody, 'channel' | 'templateId'> & {
6
+ /** The template's `id`. */
7
+ templateId: string;
8
+ /**
9
+ * The channel to send on. Only WhatsApp templates can be sent.
10
+ * @defaultValue 'WHATSAPP'
11
+ */
12
+ channel?: 'WHATSAPP' | undefined;
13
+ };
14
+ /** Messages on every channel. */
15
+ export declare class Messages {
16
+ #private;
17
+ /**
18
+ * Lists messages across the workspace, or searches their text. Deleted and hidden messages are left out.
19
+ *
20
+ * Scope: `messages:read`.
21
+ */
22
+ list(params?: ListMessagesParams, options?: RequestOptions): PagePromise<Message>;
23
+ /**
24
+ * Lists one conversation's messages. Deleted and hidden messages are left out.
25
+ *
26
+ * Scope: `messages:read`.
27
+ */
28
+ listInConversation(conversationId: string, params?: ListConversationMessagesParams, options?: RequestOptions): PagePromise<Message>;
29
+ /**
30
+ * Reads one message. A deleted or hidden message is not found.
31
+ *
32
+ * Scope: `messages:read`.
33
+ */
34
+ get(conversationId: string, messageId: string, options?: RequestOptions): Promise<Message>;
35
+ /**
36
+ * Sends an approved WhatsApp template by its id. The answer means WhatsApp accepted the message, not
37
+ * that it was delivered. To send one by its name, use `whatsappTemplates.send`.
38
+ *
39
+ * Sending twice sends two messages, so this is retried only after a 429. After anything but a 4xx
40
+ * (a timeout, a connection error, an abort, a 5xx) it may have been sent, even if the conversation
41
+ * doesn't show it, so check before you send again.
42
+ *
43
+ * Scope: `messages:send`.
44
+ */
45
+ send(params: SendMessageParams, options?: RequestOptions): Promise<SendMessageResult>;
46
+ }
@@ -0,0 +1,56 @@
1
+ import { ChatwiseError } from '../core/errors.js';
2
+ import { path } from '../core/http.js';
3
+ import { list } from '../core/pagination.js';
4
+ /** Messages on every channel. */
5
+ export class Messages {
6
+ #request;
7
+ /** @internal */
8
+ constructor(request) {
9
+ this.#request = request;
10
+ }
11
+ /**
12
+ * Lists messages across the workspace, or searches their text. Deleted and hidden messages are left out.
13
+ *
14
+ * Scope: `messages:read`.
15
+ */
16
+ list(params = {}, options) {
17
+ return list(this.#request, () => '/v1/messages', params, options);
18
+ }
19
+ /**
20
+ * Lists one conversation's messages. Deleted and hidden messages are left out.
21
+ *
22
+ * Scope: `messages:read`.
23
+ */
24
+ listInConversation(conversationId, params = {}, options) {
25
+ return list(this.#request, () => path `/v1/conversations/${conversationId}/messages`, params, options);
26
+ }
27
+ /**
28
+ * Reads one message. A deleted or hidden message is not found.
29
+ *
30
+ * Scope: `messages:read`.
31
+ */
32
+ async get(conversationId, messageId, options) {
33
+ const { data } = await this.#request({ method: 'GET', path: path `/v1/conversations/${conversationId}/messages/${messageId}` }, options);
34
+ return data;
35
+ }
36
+ /**
37
+ * Sends an approved WhatsApp template by its id. The answer means WhatsApp accepted the message, not
38
+ * that it was delivered. To send one by its name, use `whatsappTemplates.send`.
39
+ *
40
+ * Sending twice sends two messages, so this is retried only after a 429. After anything but a 4xx
41
+ * (a timeout, a connection error, an abort, a 5xx) it may have been sent, even if the conversation
42
+ * doesn't show it, so check before you send again.
43
+ *
44
+ * Scope: `messages:send`.
45
+ */
46
+ async send(params, options) {
47
+ const channel = params?.channel ?? 'WHATSAPP';
48
+ // The API answers 201 to these too, and sends nothing.
49
+ if (!params?.templateId)
50
+ throw new ChatwiseError('messages.send needs a templateId: without one nothing would be sent.');
51
+ if (channel !== 'WHATSAPP')
52
+ throw new ChatwiseError('messages.send can only send on WHATSAPP: on another channel nothing would be sent.');
53
+ const { data } = await this.#request({ method: 'POST', path: '/v1/messages', body: { ...params, channel } }, options);
54
+ return data;
55
+ }
56
+ }
@@ -0,0 +1,27 @@
1
+ import { type RequestOptions } from '../core/http.js';
2
+ import { type PagePromise } from '../core/pagination.js';
3
+ import type { ListWebhookDeliveriesParams, WebhookDelivery, WebhookDeliveryDetails } from '../generated/types.js';
4
+ /** What was sent to an endpoint. Deliveries are kept for 30 days. */
5
+ export declare class WebhookDeliveries {
6
+ #private;
7
+ /**
8
+ * Lists an endpoint's deliveries, newest first.
9
+ *
10
+ * Scope: `webhooks:read`.
11
+ */
12
+ list(endpointId: string, params?: ListWebhookDeliveriesParams, options?: RequestOptions): PagePromise<WebhookDelivery>;
13
+ /**
14
+ * Reads one delivery, with the payload it sends and its attempts.
15
+ *
16
+ * Scope: `webhooks:read`, and `messages:read` if it is for a `message.*` event.
17
+ */
18
+ get(deliveryId: string, options?: RequestOptions): Promise<WebhookDeliveryDetails>;
19
+ /**
20
+ * Sends a delivery again, with the same event id, starting from its first attempt. If Chatwise was
21
+ * waiting before it tries the endpoint again, the wait ends. A `BadRequestError` says that it can't be
22
+ * sent again: its message was unsent, deleted or hidden, or its comment hidden or no longer stored.
23
+ *
24
+ * Scope: `webhooks:write`, and `messages:read` if it is for a `message.*` event.
25
+ */
26
+ redeliver(deliveryId: string, options?: RequestOptions): Promise<WebhookDeliveryDetails>;
27
+ }
@@ -0,0 +1,38 @@
1
+ import { path } from '../core/http.js';
2
+ import { list } from '../core/pagination.js';
3
+ /** What was sent to an endpoint. Deliveries are kept for 30 days. */
4
+ export class WebhookDeliveries {
5
+ #request;
6
+ /** @internal */
7
+ constructor(request) {
8
+ this.#request = request;
9
+ }
10
+ /**
11
+ * Lists an endpoint's deliveries, newest first.
12
+ *
13
+ * Scope: `webhooks:read`.
14
+ */
15
+ list(endpointId, params = {}, options) {
16
+ return list(this.#request, () => path `/v1/webhook-endpoints/${endpointId}/deliveries`, params, options);
17
+ }
18
+ /**
19
+ * Reads one delivery, with the payload it sends and its attempts.
20
+ *
21
+ * Scope: `webhooks:read`, and `messages:read` if it is for a `message.*` event.
22
+ */
23
+ async get(deliveryId, options) {
24
+ const { data } = await this.#request({ method: 'GET', path: path `/v1/webhook-deliveries/${deliveryId}` }, options);
25
+ return data;
26
+ }
27
+ /**
28
+ * Sends a delivery again, with the same event id, starting from its first attempt. If Chatwise was
29
+ * waiting before it tries the endpoint again, the wait ends. A `BadRequestError` says that it can't be
30
+ * sent again: its message was unsent, deleted or hidden, or its comment hidden or no longer stored.
31
+ *
32
+ * Scope: `webhooks:write`, and `messages:read` if it is for a `message.*` event.
33
+ */
34
+ async redeliver(deliveryId, options) {
35
+ const { data } = await this.#request({ method: 'POST', path: path `/v1/webhook-deliveries/${deliveryId}/redeliver` }, options);
36
+ return data;
37
+ }
38
+ }
@@ -0,0 +1,54 @@
1
+ import { type RequestOptions } from '../core/http.js';
2
+ import type { CreateWebhookEndpointParams, DeleteWebhookEndpointResult, RotateWebhookSecretResult, TestWebhookEndpointResult, UpdateWebhookEndpointParams, WebhookEndpoint, WebhookEndpointWithSecret } from '../generated/types.js';
3
+ /** Where Chatwise sends events. */
4
+ export declare class WebhookEndpoints {
5
+ #private;
6
+ /**
7
+ * Lists the workspace's webhook endpoints, oldest first. There are at most 10, so there are no pages.
8
+ *
9
+ * Scope: `webhooks:read`.
10
+ */
11
+ list(options?: RequestOptions): Promise<WebhookEndpoint[]>;
12
+ /**
13
+ * Creates an endpoint. Store `signingSecret` right away: it is returned only here.
14
+ *
15
+ * Scope: `webhooks:write`, and `messages:read` to subscribe to `message.*` events.
16
+ */
17
+ create(params: CreateWebhookEndpointParams, options?: RequestOptions): Promise<WebhookEndpointWithSecret>;
18
+ /**
19
+ * Reads one endpoint and how its deliveries are going.
20
+ *
21
+ * Scope: `webhooks:read`.
22
+ */
23
+ get(endpointId: string, options?: RequestOptions): Promise<WebhookEndpoint>;
24
+ /**
25
+ * Changes an endpoint: only the fields you pass, at least one. `status: 'PAUSED'` stops new
26
+ * deliveries, and `status: 'ACTIVE'` resumes them, also after Chatwise disabled the endpoint. It clears
27
+ * the endpoint's failures and any wait (`health.backoffUntil`), and so does a new `url`, which also
28
+ * receives the deliveries that were already waiting.
29
+ *
30
+ * Scope: `webhooks:write`, and `messages:read` if the endpoint has `message.*` events before or after the change.
31
+ */
32
+ update(endpointId: string, params: UpdateWebhookEndpointParams, options?: RequestOptions): Promise<WebhookEndpoint>;
33
+ /**
34
+ * Deletes an endpoint and cancels its deliveries that weren't sent yet.
35
+ *
36
+ * Scope: `webhooks:write`.
37
+ */
38
+ delete(endpointId: string, options?: RequestOptions): Promise<DeleteWebhookEndpointResult>;
39
+ /**
40
+ * Sends a signed `webhook.test` event now, also to a paused endpoint, and returns what your endpoint
41
+ * answered. Check `delivered`: this doesn't throw when your endpoint failed. If Chatwise is waiting
42
+ * before it tries the endpoint again (`health.backoffUntil`), a test that succeeds ends the wait.
43
+ *
44
+ * Scope: `webhooks:write`.
45
+ */
46
+ test(endpointId: string, options?: RequestOptions): Promise<TestWebhookEndpointResult>;
47
+ /**
48
+ * Replaces the signing secret. Store it right away: it is returned only here. For 24 hours deliveries
49
+ * are signed with the previous secret too.
50
+ *
51
+ * Scope: `webhooks:write`, and `messages:read` if the endpoint has `message.*` events.
52
+ */
53
+ rotateSecret(endpointId: string, options?: RequestOptions): Promise<RotateWebhookSecretResult>;
54
+ }
@@ -0,0 +1,78 @@
1
+ import { path } from '../core/http.js';
2
+ /** Where Chatwise sends events. */
3
+ export class WebhookEndpoints {
4
+ #request;
5
+ /** @internal */
6
+ constructor(request) {
7
+ this.#request = request;
8
+ }
9
+ /**
10
+ * Lists the workspace's webhook endpoints, oldest first. There are at most 10, so there are no pages.
11
+ *
12
+ * Scope: `webhooks:read`.
13
+ */
14
+ async list(options) {
15
+ const { data } = await this.#request({ method: 'GET', path: '/v1/webhook-endpoints' }, options);
16
+ return data;
17
+ }
18
+ /**
19
+ * Creates an endpoint. Store `signingSecret` right away: it is returned only here.
20
+ *
21
+ * Scope: `webhooks:write`, and `messages:read` to subscribe to `message.*` events.
22
+ */
23
+ async create(params, options) {
24
+ const { data } = await this.#request({ method: 'POST', path: '/v1/webhook-endpoints', body: params }, options);
25
+ return data;
26
+ }
27
+ /**
28
+ * Reads one endpoint and how its deliveries are going.
29
+ *
30
+ * Scope: `webhooks:read`.
31
+ */
32
+ async get(endpointId, options) {
33
+ const { data } = await this.#request({ method: 'GET', path: path `/v1/webhook-endpoints/${endpointId}` }, options);
34
+ return data;
35
+ }
36
+ /**
37
+ * Changes an endpoint: only the fields you pass, at least one. `status: 'PAUSED'` stops new
38
+ * deliveries, and `status: 'ACTIVE'` resumes them, also after Chatwise disabled the endpoint. It clears
39
+ * the endpoint's failures and any wait (`health.backoffUntil`), and so does a new `url`, which also
40
+ * receives the deliveries that were already waiting.
41
+ *
42
+ * Scope: `webhooks:write`, and `messages:read` if the endpoint has `message.*` events before or after the change.
43
+ */
44
+ async update(endpointId, params, options) {
45
+ const { data } = await this.#request({ method: 'PATCH', path: path `/v1/webhook-endpoints/${endpointId}`, body: params }, options);
46
+ return data;
47
+ }
48
+ /**
49
+ * Deletes an endpoint and cancels its deliveries that weren't sent yet.
50
+ *
51
+ * Scope: `webhooks:write`.
52
+ */
53
+ async delete(endpointId, options) {
54
+ const { data } = await this.#request({ method: 'DELETE', path: path `/v1/webhook-endpoints/${endpointId}` }, options);
55
+ return data;
56
+ }
57
+ /**
58
+ * Sends a signed `webhook.test` event now, also to a paused endpoint, and returns what your endpoint
59
+ * answered. Check `delivered`: this doesn't throw when your endpoint failed. If Chatwise is waiting
60
+ * before it tries the endpoint again (`health.backoffUntil`), a test that succeeds ends the wait.
61
+ *
62
+ * Scope: `webhooks:write`.
63
+ */
64
+ async test(endpointId, options) {
65
+ const { data } = await this.#request({ method: 'POST', path: path `/v1/webhook-endpoints/${endpointId}/test` }, options);
66
+ return data;
67
+ }
68
+ /**
69
+ * Replaces the signing secret. Store it right away: it is returned only here. For 24 hours deliveries
70
+ * are signed with the previous secret too.
71
+ *
72
+ * Scope: `webhooks:write`, and `messages:read` if the endpoint has `message.*` events.
73
+ */
74
+ async rotateSecret(endpointId, options) {
75
+ const { data } = await this.#request({ method: 'POST', path: path `/v1/webhook-endpoints/${endpointId}/rotate-secret` }, options);
76
+ return data;
77
+ }
78
+ }
@@ -0,0 +1,13 @@
1
+ import type { RequestOptions } from '../core/http.js';
2
+ import type { WebhookEventTypeInfo } from '../generated/types.js';
3
+ /** The events a webhook endpoint can subscribe to. */
4
+ export declare class WebhookEventTypes {
5
+ #private;
6
+ /**
7
+ * Lists the events an endpoint can subscribe to and the channels each covers. Some are listed before
8
+ * they are sent: today Chatwise sends only `message.received`.
9
+ *
10
+ * Scope: `webhooks:read`.
11
+ */
12
+ list(options?: RequestOptions): Promise<WebhookEventTypeInfo[]>;
13
+ }
@@ -0,0 +1,18 @@
1
+ /** The events a webhook endpoint can subscribe to. */
2
+ export class WebhookEventTypes {
3
+ #request;
4
+ /** @internal */
5
+ constructor(request) {
6
+ this.#request = request;
7
+ }
8
+ /**
9
+ * Lists the events an endpoint can subscribe to and the channels each covers. Some are listed before
10
+ * they are sent: today Chatwise sends only `message.received`.
11
+ *
12
+ * Scope: `webhooks:read`.
13
+ */
14
+ async list(options) {
15
+ const { data } = await this.#request({ method: 'GET', path: '/v1/webhook-event-types' }, options);
16
+ return data;
17
+ }
18
+ }
@@ -0,0 +1,48 @@
1
+ import { type RequestOptions } from '../core/http.js';
2
+ import { type PagePromise } from '../core/pagination.js';
3
+ import type { CreateWhatsappTemplateParams, ListWhatsappTemplatesParams, SendWhatsappTemplateParams, SendWhatsappTemplateResult, WhatsappTemplate } from '../generated/types.js';
4
+ export interface CreateWhatsappTemplateOptions extends RequestOptions {
5
+ /**
6
+ * A key you choose for this template, 1 to 128 letters, digits, dots, underscores, hyphens or colons.
7
+ * Use the same one, with the same template, when you repeat the call: you get the same template back.
8
+ * Without it, two identical submissions count as a repeat too.
9
+ */
10
+ idempotencyKey?: string | undefined;
11
+ }
12
+ /** WhatsApp message templates. */
13
+ export declare class WhatsappTemplates {
14
+ #private;
15
+ /**
16
+ * Lists the workspace's WhatsApp templates, newest first, with the variables and buttons each takes.
17
+ *
18
+ * Scope: `templates:read`.
19
+ */
20
+ list(params?: ListWhatsappTemplatesParams, options?: RequestOptions): PagePromise<WhatsappTemplate>;
21
+ /**
22
+ * Reads one WhatsApp template. After creating one, poll it until `status` is no longer `PENDING`.
23
+ * `sendable` then says whether it can be sent.
24
+ *
25
+ * Scope: `templates:read`.
26
+ */
27
+ get(templateId: string, options?: RequestOptions): Promise<WhatsappTemplate>;
28
+ /**
29
+ * Creates a WhatsApp template and submits it to Meta for review. The route recognizes a repeat, by
30
+ * `idempotencyKey` or by the template itself, so this is retried like a read. Only an answer with a
31
+ * `code` isn't. `TEMPLATE_SUBMISSION_IN_PROGRESS`, which a retry can end with, and
32
+ * `TEMPLATE_SUBMISSION_UNCONFIRMED` mean to repeat the call later, with the same key and template.
33
+ *
34
+ * Scope: `templates:write`.
35
+ */
36
+ create(params: CreateWhatsappTemplateParams, options?: CreateWhatsappTemplateOptions): Promise<WhatsappTemplate>;
37
+ /**
38
+ * Sends an approved WhatsApp template by its name. The answer means WhatsApp accepted the message,
39
+ * not that it was delivered. To send one by its id, use `messages.send`.
40
+ *
41
+ * Sending twice sends two messages, so this is retried only after a 429. After anything but a 4xx
42
+ * (a timeout, a connection error, an abort, a 5xx) it may have been sent, even if the conversation
43
+ * doesn't show it, so check before you send again.
44
+ *
45
+ * Scope: `messages:send`.
46
+ */
47
+ send(params: SendWhatsappTemplateParams, options?: RequestOptions): Promise<SendWhatsappTemplateResult>;
48
+ }
@@ -0,0 +1,65 @@
1
+ import { ChatwiseError } from '../core/errors.js';
2
+ import { path } from '../core/http.js';
3
+ import { list } from '../core/pagination.js';
4
+ /** WhatsApp message templates. */
5
+ export class WhatsappTemplates {
6
+ #request;
7
+ /** @internal */
8
+ constructor(request) {
9
+ this.#request = request;
10
+ }
11
+ /**
12
+ * Lists the workspace's WhatsApp templates, newest first, with the variables and buttons each takes.
13
+ *
14
+ * Scope: `templates:read`.
15
+ */
16
+ list(params = {}, options) {
17
+ return list(this.#request, () => '/v1/whatsapp/templates', params, options);
18
+ }
19
+ /**
20
+ * Reads one WhatsApp template. After creating one, poll it until `status` is no longer `PENDING`.
21
+ * `sendable` then says whether it can be sent.
22
+ *
23
+ * Scope: `templates:read`.
24
+ */
25
+ async get(templateId, options) {
26
+ const { data } = await this.#request({ method: 'GET', path: path `/v1/whatsapp/templates/${templateId}` }, options);
27
+ return data;
28
+ }
29
+ /**
30
+ * Creates a WhatsApp template and submits it to Meta for review. The route recognizes a repeat, by
31
+ * `idempotencyKey` or by the template itself, so this is retried like a read. Only an answer with a
32
+ * `code` isn't. `TEMPLATE_SUBMISSION_IN_PROGRESS`, which a retry can end with, and
33
+ * `TEMPLATE_SUBMISSION_UNCONFIRMED` mean to repeat the call later, with the same key and template.
34
+ *
35
+ * Scope: `templates:write`.
36
+ */
37
+ async create(params, options) {
38
+ const { idempotencyKey, ...rest } = options ?? {};
39
+ if (idempotencyKey !== undefined && !/^[\x21-\x7e]+$/.test(idempotencyKey)) {
40
+ throw new ChatwiseError('idempotencyKey must be 1 to 128 letters, digits, dots, underscores, hyphens or colons.');
41
+ }
42
+ const { data } = await this.#request({
43
+ method: 'POST',
44
+ path: '/v1/whatsapp/templates',
45
+ body: params,
46
+ headers: idempotencyKey === undefined ? undefined : { 'Idempotency-Key': idempotencyKey },
47
+ idempotent: true,
48
+ }, rest);
49
+ return data;
50
+ }
51
+ /**
52
+ * Sends an approved WhatsApp template by its name. The answer means WhatsApp accepted the message,
53
+ * not that it was delivered. To send one by its id, use `messages.send`.
54
+ *
55
+ * Sending twice sends two messages, so this is retried only after a 429. After anything but a 4xx
56
+ * (a timeout, a connection error, an abort, a 5xx) it may have been sent, even if the conversation
57
+ * doesn't show it, so check before you send again.
58
+ *
59
+ * Scope: `messages:send`.
60
+ */
61
+ async send(params, options) {
62
+ const { data } = await this.#request({ method: 'POST', path: '/v1/template/whatsapp', body: params }, options);
63
+ return data;
64
+ }
65
+ }
@@ -0,0 +1,2 @@
1
+ /** The version of this package. A test keeps it equal to package.json's. */
2
+ export declare const VERSION = "0.1.0";
@@ -0,0 +1,2 @@
1
+ /** The version of this package. A test keeps it equal to package.json's. */
2
+ export const VERSION = '0.1.0';
@@ -0,0 +1,74 @@
1
+ import { ChatwiseError } from './core/errors.js';
2
+ import type { KnownWebhookEvent } from './generated/types.js';
3
+ /**
4
+ * Not a value that is ever sent: it stands for every type this version doesn't know. It is exported so that
5
+ * TypeScript can name it in the declarations of code that uses an event's `type`.
6
+ */
7
+ export declare enum UnknownWebhookEventType {
8
+ Unknown = "a type this version does not know"
9
+ }
10
+ /**
11
+ * An event this version doesn't know, as it was sent. Its `type` is the one that was sent. TypeScript
12
+ * can't compare it with one, so write `(event.type as string) === 'the.type'` until you update.
13
+ */
14
+ export interface UnknownWebhookEvent {
15
+ id: string;
16
+ type: UnknownWebhookEventType;
17
+ timestamp: string;
18
+ apiVersion: string;
19
+ workspaceId: string;
20
+ data: unknown;
21
+ }
22
+ /**
23
+ * An event, told apart by its `type`. Handle the types you want and ignore the rest: Chatwise adds
24
+ * events, and they reach you as `UnknownWebhookEvent` until you update.
25
+ */
26
+ export type WebhookEvent = KnownWebhookEvent | UnknownWebhookEvent;
27
+ /** Request headers: a `Headers` object, or Node's `request.headers`. */
28
+ export type WebhookHeaders = {
29
+ get(name: string): string | null;
30
+ } | Readonly<Record<string, string | readonly string[] | undefined>>;
31
+ export interface VerifyWebhookOptions {
32
+ /** The request body exactly as received, not parsed: its text or its bytes. */
33
+ payload: string | Uint8Array;
34
+ /** The request's headers. */
35
+ headers: WebhookHeaders;
36
+ /** The endpoint's signing secret (`whsec_…`), or several to accept any of them. */
37
+ secret: string | readonly string[] | undefined;
38
+ /**
39
+ * How far the time of the request may be from this machine's, in seconds, so that a captured request
40
+ * can't be sent again later.
41
+ * @defaultValue 300
42
+ */
43
+ toleranceSeconds?: number | undefined;
44
+ /** The time to compare with instead of now, to test with a recorded request. */
45
+ now?: Date | undefined;
46
+ }
47
+ /** Why a request is refused. */
48
+ export type WebhookVerificationErrorCode =
49
+ /** `webhook-id`, `webhook-timestamp` or `webhook-signature` is missing. */
50
+ 'missing_header'
51
+ /** `webhook-timestamp` or `webhook-signature` isn't written the way Standard Webhooks writes it. */
52
+ | 'malformed_header'
53
+ /** The request is older than the tolerance, or this machine's clock is fast. */
54
+ | 'timestamp_too_old'
55
+ /** The request is from the future, or this machine's clock is slow. */
56
+ | 'timestamp_too_new'
57
+ /** No signature matches: another secret, a body that was changed or parsed, or not from Chatwise. */
58
+ | 'signature_mismatch'
59
+ /** The signature matches, but the body isn't an event. */
60
+ | 'invalid_payload';
61
+ /** The request isn't a valid webhook from Chatwise. Answer 400. */
62
+ export declare class WebhookVerificationError extends ChatwiseError {
63
+ name: string;
64
+ readonly code: WebhookVerificationErrorCode;
65
+ constructor(code: WebhookVerificationErrorCode, message: string);
66
+ }
67
+ /**
68
+ * Checks that a request really comes from Chatwise and returns its event. Throws a
69
+ * `WebhookVerificationError` if it doesn't: answer 400 then. It checks the signature in constant time
70
+ * and that the request isn't old, as Standard Webhooks says to.
71
+ *
72
+ * Pass the body exactly as received. The same event can arrive more than once, with the same `id`.
73
+ */
74
+ export declare function verifyWebhook(options: VerifyWebhookOptions): WebhookEvent;