ziphjs 0.0.0-stage → 0.0.2
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 +201 -0
- package/README.md +117 -2
- package/package.json +57 -5
- package/src/index.js +111 -0
- package/types/index.d.ts +42 -0
- package/types/models.d.ts +274 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Spikenard
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,118 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ziphjs
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lightweight JavaScript and TypeScript client for sending WhatsApp and
|
|
4
|
+
SMS messages with [Ziph](https://ziph.dev). No runtime dependencies.
|
|
5
|
+
|
|
6
|
+
The client covers sending messages; it does not list messages or look up message
|
|
7
|
+
details. Incoming webhook types support handling replies to your messages.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install ziphjs
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
Set `ZIPH_API_KEY` in your server environment. Keep it out of source control and
|
|
18
|
+
browser code.
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { ZiphClient } from "ziphjs";
|
|
22
|
+
|
|
23
|
+
const client = new ZiphClient({ apiKey: process.env.ZIPH_API_KEY });
|
|
24
|
+
|
|
25
|
+
const { data: message } = await client.sendMessage({
|
|
26
|
+
channel_type: "whatsapp",
|
|
27
|
+
to: "+23276123456",
|
|
28
|
+
text: "Your order is ready.",
|
|
29
|
+
idempotencyKey: "order-123-ready",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
console.log(message.id, message.status);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
WhatsApp messages use your default channel. To choose another, pass its
|
|
36
|
+
`channel_id`. For SMS, use `channel_type: "sms"`.
|
|
37
|
+
|
|
38
|
+
`sendMessage` requires `text` for text and list messages. For image messages, omit
|
|
39
|
+
`text` or leave it empty and use `image.caption` for a caption.
|
|
40
|
+
|
|
41
|
+
Use `sendMessage` for any supported message type, or the `sendText`, `sendImage`,
|
|
42
|
+
and `sendList` helpers when you want a type-specific method. Request and response
|
|
43
|
+
types are exported from `ziphjs`.
|
|
44
|
+
|
|
45
|
+
Every send needs a nonblank string `idempotencyKey`. The client does not impose
|
|
46
|
+
a key-length limit. Use a new key for each message. If you're unsure whether a
|
|
47
|
+
request succeeded, retry with the same key and unchanged request. The client
|
|
48
|
+
does not retry automatically.
|
|
49
|
+
|
|
50
|
+
## Typing incoming webhook payloads
|
|
51
|
+
|
|
52
|
+
`MessageReceivedEvent` is exported for incoming WhatsApp text and interactive
|
|
53
|
+
replies. In a JavaScript Hono handler, annotate the parsed body with JSDoc:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
/** @type {import("ziphjs").MessageReceivedEvent} */
|
|
57
|
+
const data = await c.req.json();
|
|
58
|
+
|
|
59
|
+
const reply = data.message.button_reply?.id ?? data.message.list_reply?.id;
|
|
60
|
+
const phone = data.message.sender_phone;
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
In TypeScript, import the type and supply it to Hono's JSON reader:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import type { MessageReceivedEvent } from "ziphjs";
|
|
67
|
+
|
|
68
|
+
const data = await c.req.json<MessageReceivedEvent>();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`sender_name`, `sender_phone`, and `sender_lid` are omitted when unavailable.
|
|
72
|
+
Check that `sender_phone` exists before using it as a send recipient. The
|
|
73
|
+
`sender` address and `sender_lid` may be opaque WhatsApp identifiers, not phone
|
|
74
|
+
numbers; `sender_name` is an unverified display name.
|
|
75
|
+
|
|
76
|
+
These annotations provide static typing, not runtime validation. Verify webhook
|
|
77
|
+
signatures against the raw body before parsing, validate the payload, and
|
|
78
|
+
deduplicate event IDs before performing business actions.
|
|
79
|
+
|
|
80
|
+
## Errors
|
|
81
|
+
|
|
82
|
+
Failed HTTP responses throw `ZiphError`, which includes `status`, `code`, `details`,
|
|
83
|
+
and `meta`. Network errors pass through unchanged. Invalid client options or
|
|
84
|
+
idempotency keys throw `TypeError`.
|
|
85
|
+
|
|
86
|
+
The constructor rejects missing, non-string, or whitespace-only API keys with
|
|
87
|
+
`TypeError: apiKey must be a non-empty string.`
|
|
88
|
+
|
|
89
|
+
WhatsApp sends can fail with `NUMBER_LIMIT_EXCEEDED` (HTTP 409) when your
|
|
90
|
+
workspace has more channels than its plan allows. This differs from
|
|
91
|
+
`MESSAGE_QUOTA_EXCEEDED` (HTTP 429), which means the remaining WhatsApp message
|
|
92
|
+
or SMS segment allowance is insufficient.
|
|
93
|
+
|
|
94
|
+
## Removed read-response types
|
|
95
|
+
|
|
96
|
+
`MessageDetail`, `MessageDetailEnvelope`, `MessageList`, `MessageListEnvelope`,
|
|
97
|
+
`MessageStatusEvent`, and `Message.events` have been removed. They described
|
|
98
|
+
lookup/list responses, not send responses. Consumers importing these types must
|
|
99
|
+
remove those imports. Send methods still return `MessageEnvelope`, whose `data`
|
|
100
|
+
is a `Message`; WhatsApp list-menu types and `sendList` are unchanged.
|
|
101
|
+
|
|
102
|
+
## Development and releases
|
|
103
|
+
|
|
104
|
+
The runtime is plain ESM JavaScript in `src/index.js`. TypeScript declarations
|
|
105
|
+
live in `types/index.d.ts` and `types/models.d.ts`; consumers can import types
|
|
106
|
+
directly from `ziphjs`. There is no compilation or bundling step.
|
|
107
|
+
|
|
108
|
+
Run `npm ci`, `npm run format:check`, and `npm run typecheck` before releasing.
|
|
109
|
+
Typechecking checks the JavaScript through JSDoc and the public declarations
|
|
110
|
+
under both NodeNext and bundler resolution.
|
|
111
|
+
|
|
112
|
+
Use `npm pack --dry-run` to inspect the package contents without publishing.
|
|
113
|
+
The package ships `src` and `types` directly. Keep public declarations and
|
|
114
|
+
implementation annotations in sync when changing the API.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
Apache-2.0. See [LICENSE](./LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "ziphjs",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Lightweight server-side JavaScript and TypeScript client for sending WhatsApp and SMS messages with Ziph.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20.0.0"
|
|
8
|
+
},
|
|
9
|
+
"types": "./types/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./types/index.d.ts",
|
|
13
|
+
"import": "./src/index.js",
|
|
14
|
+
"default": "./src/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"types"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"format": "biome check --write .",
|
|
24
|
+
"format:check": "biome check .",
|
|
25
|
+
"typecheck": "tsc && tsc --module ESNext --moduleResolution bundler",
|
|
26
|
+
"prepublishOnly": "npm run format:check && npm run typecheck"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/spikenardco/ziphjs.git"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"ziph",
|
|
34
|
+
"whatsapp",
|
|
35
|
+
"sms",
|
|
36
|
+
"messaging",
|
|
37
|
+
"sdk",
|
|
38
|
+
"nodejs",
|
|
39
|
+
"typescript"
|
|
40
|
+
],
|
|
41
|
+
"license": "Apache-2.0",
|
|
42
|
+
"contributors": [
|
|
43
|
+
{
|
|
44
|
+
"name": "Ben",
|
|
45
|
+
"email": "bnjox@users.noreply.github.com",
|
|
46
|
+
"url": "https://github.com/bnjox"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/spikenardco/ziphjs/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/spikenardco/ziphjs#readme",
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@biomejs/biome": "^2.5.12",
|
|
55
|
+
"@types/node": "^26.4.1",
|
|
56
|
+
"typescript": "^7.0.2"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/** @import {
|
|
2
|
+
ApiError,
|
|
3
|
+
ErrorEnvelope,
|
|
4
|
+
MessageEnvelope,
|
|
5
|
+
Meta,
|
|
6
|
+
SendImageInput,
|
|
7
|
+
SendListInput,
|
|
8
|
+
SendMessageInput,
|
|
9
|
+
SendTextInput,
|
|
10
|
+
ZiphOptions,
|
|
11
|
+
} from "../types/models.js" */
|
|
12
|
+
|
|
13
|
+
/** An unsuccessful HTTP response from Ziph. Network errors are not wrapped. */
|
|
14
|
+
export class ZiphError extends Error {
|
|
15
|
+
/**
|
|
16
|
+
* @param {number} status
|
|
17
|
+
* @param {ApiError | null} [error]
|
|
18
|
+
* @param {Meta | null} [meta]
|
|
19
|
+
*/
|
|
20
|
+
constructor(status, error = null, meta = null) {
|
|
21
|
+
super(error?.message ?? `Ziph request failed with HTTP ${status}.`);
|
|
22
|
+
this.name = "ZiphError";
|
|
23
|
+
this.status = status;
|
|
24
|
+
this.code = error?.code ?? null;
|
|
25
|
+
this.details = error?.details ?? [];
|
|
26
|
+
this.meta = meta;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Server-side client for sending WhatsApp and SMS messages. */
|
|
31
|
+
export class ZiphClient {
|
|
32
|
+
/** @type {string} */
|
|
33
|
+
#apiKey;
|
|
34
|
+
/** @type {URL} */
|
|
35
|
+
#messageUrl;
|
|
36
|
+
|
|
37
|
+
/** @param {ZiphOptions} options */
|
|
38
|
+
constructor({ apiKey, baseUrl = "https://api.ziph.dev" }) {
|
|
39
|
+
if (typeof apiKey !== "string" || !apiKey.trim()) {
|
|
40
|
+
throw new TypeError("apiKey must be a non-empty string.");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.#apiKey = apiKey;
|
|
44
|
+
this.#messageUrl = new URL(`${baseUrl.replace(/\/+$/, "")}/v1/messages`);
|
|
45
|
+
|
|
46
|
+
if (!["http:", "https:"].includes(this.#messageUrl.protocol)) {
|
|
47
|
+
throw new TypeError("baseUrl must use HTTP or HTTPS.");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sends a WhatsApp or SMS text message. text is required.
|
|
53
|
+
* @param {SendTextInput} input
|
|
54
|
+
* @returns {Promise<MessageEnvelope>}
|
|
55
|
+
*/
|
|
56
|
+
sendText(input) {
|
|
57
|
+
return this.sendMessage(input);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Sends a WhatsApp image. Use image.caption for accompanying text.
|
|
62
|
+
* @param {SendImageInput} input
|
|
63
|
+
* @returns {Promise<MessageEnvelope>}
|
|
64
|
+
*/
|
|
65
|
+
sendImage(input) {
|
|
66
|
+
return this.sendMessage({ ...input, channel_type: "whatsapp" });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Sends a WhatsApp list menu. Both text and list are required.
|
|
71
|
+
* @param {SendListInput} input
|
|
72
|
+
* @returns {Promise<MessageEnvelope>}
|
|
73
|
+
*/
|
|
74
|
+
sendList(input) {
|
|
75
|
+
return this.sendMessage({ ...input, channel_type: "whatsapp" });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Sends once, without automatic retries. Reuse the same key and unchanged
|
|
80
|
+
* request if retrying after a network error or an uncertain outcome.
|
|
81
|
+
* Acceptance does not confirm delivery. idempotencyKey is sent only as a header.
|
|
82
|
+
* @param {SendMessageInput} input
|
|
83
|
+
* @returns {Promise<MessageEnvelope>}
|
|
84
|
+
*/
|
|
85
|
+
async sendMessage({ idempotencyKey, ...request }) {
|
|
86
|
+
if (typeof idempotencyKey !== "string" || !idempotencyKey.trim()) {
|
|
87
|
+
throw new TypeError("idempotencyKey must be a non-empty string.");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const response = await fetch(this.#messageUrl, {
|
|
91
|
+
method: "POST",
|
|
92
|
+
redirect: "error",
|
|
93
|
+
headers: {
|
|
94
|
+
Authorization: `Bearer ${this.#apiKey}`,
|
|
95
|
+
"Content-Type": "application/json",
|
|
96
|
+
"Idempotency-Key": idempotencyKey,
|
|
97
|
+
},
|
|
98
|
+
body: JSON.stringify(request),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
// Proxies may return HTML or empty bodies instead of an API envelope.
|
|
103
|
+
const body = /** @type {ErrorEnvelope | null} */ (
|
|
104
|
+
await response.json().catch(() => null)
|
|
105
|
+
);
|
|
106
|
+
throw new ZiphError(response.status, body?.error, body?.meta);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return /** @type {Promise<MessageEnvelope>} */ (response.json());
|
|
110
|
+
}
|
|
111
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// biome-ignore-all lint/correctness/noUnusedPrivateClassMembers: The declaration preserves the runtime class's private-field identity.
|
|
2
|
+
import type {
|
|
3
|
+
ApiError,
|
|
4
|
+
ErrorCode,
|
|
5
|
+
FieldError,
|
|
6
|
+
MessageEnvelope,
|
|
7
|
+
Meta,
|
|
8
|
+
SendImageInput,
|
|
9
|
+
SendListInput,
|
|
10
|
+
SendMessageInput,
|
|
11
|
+
SendTextInput,
|
|
12
|
+
ZiphOptions,
|
|
13
|
+
} from "./models.js";
|
|
14
|
+
|
|
15
|
+
export type * from "./models.js";
|
|
16
|
+
|
|
17
|
+
/** An unsuccessful HTTP response from Ziph. Network errors are not wrapped. */
|
|
18
|
+
export declare class ZiphError extends Error {
|
|
19
|
+
status: number;
|
|
20
|
+
code: ErrorCode | null;
|
|
21
|
+
details: FieldError[];
|
|
22
|
+
meta: Meta | null;
|
|
23
|
+
constructor(status: number, error?: ApiError | null, meta?: Meta | null);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Server-side client for sending WhatsApp and SMS messages. */
|
|
27
|
+
export declare class ZiphClient {
|
|
28
|
+
#private;
|
|
29
|
+
constructor(options: ZiphOptions);
|
|
30
|
+
/** Sends a WhatsApp or SMS text message. text is required. */
|
|
31
|
+
sendText(input: SendTextInput): Promise<MessageEnvelope>;
|
|
32
|
+
/** Sends a WhatsApp image. Use image.caption for accompanying text. */
|
|
33
|
+
sendImage(input: SendImageInput): Promise<MessageEnvelope>;
|
|
34
|
+
/** Sends a WhatsApp list menu. Both text and list are required. */
|
|
35
|
+
sendList(input: SendListInput): Promise<MessageEnvelope>;
|
|
36
|
+
/**
|
|
37
|
+
* Sends once, without automatic retries. Reuse the same key and unchanged
|
|
38
|
+
* request if retrying after a network error or an uncertain outcome.
|
|
39
|
+
* Acceptance does not confirm delivery. idempotencyKey is sent only as a header.
|
|
40
|
+
*/
|
|
41
|
+
sendMessage(input: SendMessageInput): Promise<MessageEnvelope>;
|
|
42
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types for the Ziph API,
|
|
3
|
+
*
|
|
4
|
+
* Wire fields keep the API's snake_case names.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type ChannelType = "whatsapp" | "sms";
|
|
8
|
+
|
|
9
|
+
export type MessageStatus =
|
|
10
|
+
| "processing"
|
|
11
|
+
| "accepted"
|
|
12
|
+
| "delivered"
|
|
13
|
+
| "read"
|
|
14
|
+
| "failed"
|
|
15
|
+
| "unknown";
|
|
16
|
+
|
|
17
|
+
/** Buttons: 1–3 per message, WhatsApp only. */
|
|
18
|
+
export interface QuickReplyButton {
|
|
19
|
+
/** Defaults to quick_reply when omitted. */
|
|
20
|
+
type?: "" | "quick_reply";
|
|
21
|
+
/** Unique per message, 1–128 bytes. */
|
|
22
|
+
id: string;
|
|
23
|
+
/** 1–25 characters. */
|
|
24
|
+
title: string;
|
|
25
|
+
url?: never;
|
|
26
|
+
copy_code?: never;
|
|
27
|
+
phone_number?: never;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface UrlButton {
|
|
31
|
+
type: "url";
|
|
32
|
+
title: string;
|
|
33
|
+
/** Absolute http(s) URL. */
|
|
34
|
+
url: string;
|
|
35
|
+
id?: never;
|
|
36
|
+
copy_code?: never;
|
|
37
|
+
phone_number?: never;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface CopyButton {
|
|
41
|
+
type: "copy";
|
|
42
|
+
title: string;
|
|
43
|
+
copy_code: string;
|
|
44
|
+
id?: never;
|
|
45
|
+
url?: never;
|
|
46
|
+
phone_number?: never;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface CallButton {
|
|
50
|
+
type: "call";
|
|
51
|
+
title: string;
|
|
52
|
+
/** International format, e.g. +23276123456. */
|
|
53
|
+
phone_number: string;
|
|
54
|
+
id?: never;
|
|
55
|
+
url?: never;
|
|
56
|
+
copy_code?: never;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type MessageButton =
|
|
60
|
+
| QuickReplyButton
|
|
61
|
+
| UrlButton
|
|
62
|
+
| CopyButton
|
|
63
|
+
| CallButton;
|
|
64
|
+
|
|
65
|
+
export type MessageButtons =
|
|
66
|
+
| [MessageButton]
|
|
67
|
+
| [MessageButton, MessageButton]
|
|
68
|
+
| [MessageButton, MessageButton, MessageButton];
|
|
69
|
+
|
|
70
|
+
/** WhatsApp image: JPEG or PNG, up to 5 MiB, publicly accessible without redirects. */
|
|
71
|
+
export interface MessageImage {
|
|
72
|
+
url: string;
|
|
73
|
+
/** Up to 4096 bytes. Use this instead of text when sending an image. */
|
|
74
|
+
caption?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Single-select WhatsApp list menu. Cannot be combined with buttons or an image. */
|
|
78
|
+
export interface ListRow {
|
|
79
|
+
/** Unique per message, 1–128 bytes. */
|
|
80
|
+
id: string;
|
|
81
|
+
/** 1–24 characters. */
|
|
82
|
+
title: string;
|
|
83
|
+
/** Up to 72 characters. */
|
|
84
|
+
description?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface ListSection {
|
|
88
|
+
/** 1–24 characters. */
|
|
89
|
+
title: string;
|
|
90
|
+
rows: [ListRow, ...ListRow[]];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface MessageListMenu {
|
|
94
|
+
/** Caption of the menu button, 1–20 characters. */
|
|
95
|
+
button_text: string;
|
|
96
|
+
/** 1–10 sections, at most 10 rows in total. */
|
|
97
|
+
sections: [ListSection, ...ListSection[]];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** SMS is text only. */
|
|
101
|
+
export interface SmsMessage {
|
|
102
|
+
channel_type: "sms";
|
|
103
|
+
/** Recipient in international format, e.g. +23276123456. */
|
|
104
|
+
to: string;
|
|
105
|
+
channel_id?: never;
|
|
106
|
+
/** 1–4096 bytes. */
|
|
107
|
+
text: string;
|
|
108
|
+
buttons?: never;
|
|
109
|
+
image?: never;
|
|
110
|
+
list?: never;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface WhatsAppTextMessage {
|
|
114
|
+
channel_type: "whatsapp";
|
|
115
|
+
/** Recipient in international format, e.g. +23276123456. */
|
|
116
|
+
to: string;
|
|
117
|
+
/** Uses the default channel when omitted or null. */
|
|
118
|
+
channel_id?: string | null;
|
|
119
|
+
/** 1–4096 bytes. Must include non-whitespace text when adding buttons. */
|
|
120
|
+
text: string;
|
|
121
|
+
buttons?: MessageButtons;
|
|
122
|
+
image?: never;
|
|
123
|
+
list?: never;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface WhatsAppImageMessage {
|
|
127
|
+
channel_type: "whatsapp";
|
|
128
|
+
/** Recipient in international format, e.g. +23276123456. */
|
|
129
|
+
to: string;
|
|
130
|
+
/** Uses the default channel when omitted or null. */
|
|
131
|
+
channel_id?: string | null;
|
|
132
|
+
/** Omit, or pass an empty string, and use image.caption instead. */
|
|
133
|
+
text?: "";
|
|
134
|
+
buttons?: MessageButtons;
|
|
135
|
+
image: MessageImage;
|
|
136
|
+
list?: never;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface WhatsAppListMessage {
|
|
140
|
+
channel_type: "whatsapp";
|
|
141
|
+
/** Recipient in international format, e.g. +23276123456. */
|
|
142
|
+
to: string;
|
|
143
|
+
/** Uses the default channel when omitted or null. */
|
|
144
|
+
channel_id?: string | null;
|
|
145
|
+
/** 1–4096 bytes. */
|
|
146
|
+
text: string;
|
|
147
|
+
buttons?: never;
|
|
148
|
+
image?: never;
|
|
149
|
+
list: MessageListMenu;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export type SendMessageRequest =
|
|
153
|
+
| SmsMessage
|
|
154
|
+
| WhatsAppTextMessage
|
|
155
|
+
| WhatsAppImageMessage
|
|
156
|
+
| WhatsAppListMessage;
|
|
157
|
+
|
|
158
|
+
export type ZiphOptions = {
|
|
159
|
+
/** Server-side API key. Never expose it in browser code. */
|
|
160
|
+
apiKey: string;
|
|
161
|
+
/** API root without /v1. Defaults to https://api.ziph.dev. */
|
|
162
|
+
baseUrl?: string;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/** The key must be a nonblank string. Reuse it with the unchanged request when retrying. */
|
|
166
|
+
export type SendMessageInput = SendMessageRequest & { idempotencyKey: string };
|
|
167
|
+
|
|
168
|
+
export type SendTextInput = (
|
|
169
|
+
| Pick<SmsMessage, "channel_type" | "to" | "text">
|
|
170
|
+
| Pick<
|
|
171
|
+
WhatsAppTextMessage,
|
|
172
|
+
"channel_type" | "to" | "text" | "channel_id" | "buttons"
|
|
173
|
+
>
|
|
174
|
+
) & { idempotencyKey: string };
|
|
175
|
+
|
|
176
|
+
export type SendImageInput = Pick<
|
|
177
|
+
WhatsAppImageMessage,
|
|
178
|
+
"to" | "channel_id" | "image" | "buttons"
|
|
179
|
+
> & { idempotencyKey: string };
|
|
180
|
+
|
|
181
|
+
export type SendListInput = Pick<
|
|
182
|
+
WhatsAppListMessage,
|
|
183
|
+
"to" | "channel_id" | "text" | "list"
|
|
184
|
+
> & { idempotencyKey: string };
|
|
185
|
+
|
|
186
|
+
/** Selection returned when a recipient answers a quick-reply or list message. */
|
|
187
|
+
export interface InteractiveReply {
|
|
188
|
+
id: string;
|
|
189
|
+
title: string;
|
|
190
|
+
quoted_provider_message_id?: string;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export interface ReceivedMessage {
|
|
194
|
+
provider_message_id: string;
|
|
195
|
+
/** Original provider address. An @lid address is not a phone number. */
|
|
196
|
+
sender: string;
|
|
197
|
+
/** Unverified display name. Omitted when unavailable. */
|
|
198
|
+
sender_name?: string;
|
|
199
|
+
/** International +digits phone number. Omitted when unknown. */
|
|
200
|
+
sender_phone?: string;
|
|
201
|
+
/** Opaque @lid address without a device suffix. Omitted when unavailable. */
|
|
202
|
+
sender_lid?: string;
|
|
203
|
+
conversation: string;
|
|
204
|
+
text: string;
|
|
205
|
+
button_reply?: InteractiveReply;
|
|
206
|
+
list_reply?: InteractiveReply;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface MessageReceivedEvent {
|
|
210
|
+
version: 1;
|
|
211
|
+
id: string;
|
|
212
|
+
event: "message.received";
|
|
213
|
+
channel_id: string;
|
|
214
|
+
timestamp: string;
|
|
215
|
+
message: ReceivedMessage;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export interface Message {
|
|
219
|
+
id: string;
|
|
220
|
+
channel_id: string | null;
|
|
221
|
+
channel_type: ChannelType;
|
|
222
|
+
recipient_masked: string;
|
|
223
|
+
status: MessageStatus;
|
|
224
|
+
provider_message_id: string | null;
|
|
225
|
+
failure_code: string | null;
|
|
226
|
+
created_at: string;
|
|
227
|
+
updated_at: string;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export interface Meta {
|
|
231
|
+
request_id?: string;
|
|
232
|
+
timestamp: string;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface ResponseEnvelope<T> {
|
|
236
|
+
data: T;
|
|
237
|
+
meta: Meta;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export type MessageEnvelope = ResponseEnvelope<Message>;
|
|
241
|
+
|
|
242
|
+
export type ErrorCode =
|
|
243
|
+
| "AUTH_REQUIRED"
|
|
244
|
+
| "MESSAGE_QUOTA_EXCEEDED"
|
|
245
|
+
| "PLAN_EXPIRED"
|
|
246
|
+
| "NUMBER_LIMIT_EXCEEDED"
|
|
247
|
+
| "VALIDATION_ERROR"
|
|
248
|
+
| "NOT_FOUND"
|
|
249
|
+
| "CHANNEL_DISCONNECTED"
|
|
250
|
+
| "PROVIDER_UNAVAILABLE"
|
|
251
|
+
| "PROVIDER_TIMEOUT"
|
|
252
|
+
| "PROVIDER_REJECTED"
|
|
253
|
+
| "IDEMPOTENCY_CONFLICT"
|
|
254
|
+
| "CHANNEL_TYPE_UNAVAILABLE"
|
|
255
|
+
| "CHANNEL_TYPE_MISMATCH"
|
|
256
|
+
| "MESSAGE_TYPE_UNAVAILABLE"
|
|
257
|
+
| "INTERNAL_SERVER_ERROR";
|
|
258
|
+
|
|
259
|
+
export interface FieldError {
|
|
260
|
+
field: string;
|
|
261
|
+
message: string;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface ApiError {
|
|
265
|
+
code: ErrorCode;
|
|
266
|
+
message: string;
|
|
267
|
+
details: FieldError[];
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export interface ErrorEnvelope {
|
|
271
|
+
data: null;
|
|
272
|
+
error: ApiError;
|
|
273
|
+
meta: Meta;
|
|
274
|
+
}
|