@effect/chat-whatsapp 0.0.0 → 4.1.5
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/AGENTS.md +434 -0
- package/CLAUDE.md +434 -0
- package/LICENSE +21 -0
- package/README.md +61 -0
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +7 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +163 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +111 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/40_sql/10_basics.ts +175 -0
- package/ai-docs/src/40_sql/index.md +6 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +118 -0
- package/ai-docs/src/51_http-server/20_testing.ts +100 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/52_graphql/10_basics.ts +117 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +118 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +162 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +28 -0
- package/dist/WhatsAppChat.d.ts +373 -0
- package/dist/WhatsAppChat.d.ts.map +1 -0
- package/dist/WhatsAppChat.js +388 -0
- package/dist/WhatsAppChat.js.map +1 -0
- package/dist/WhatsAppGraphApi.d.ts +90 -0
- package/dist/WhatsAppGraphApi.d.ts.map +1 -0
- package/dist/WhatsAppGraphApi.js +97 -0
- package/dist/WhatsAppGraphApi.js.map +1 -0
- package/dist/WhatsAppUpdate.d.ts +168 -0
- package/dist/WhatsAppUpdate.d.ts.map +1 -0
- package/dist/WhatsAppUpdate.js +103 -0
- package/dist/WhatsAppUpdate.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/actions.d.ts +2 -0
- package/dist/internal/actions.d.ts.map +1 -0
- package/dist/internal/actions.js +224 -0
- package/dist/internal/actions.js.map +1 -0
- package/dist/internal/adapter.d.ts +2 -0
- package/dist/internal/adapter.d.ts.map +1 -0
- package/dist/internal/adapter.js +330 -0
- package/dist/internal/adapter.js.map +1 -0
- package/dist/internal/address.d.ts +2 -0
- package/dist/internal/address.d.ts.map +1 -0
- package/dist/internal/address.js +36 -0
- package/dist/internal/address.js.map +1 -0
- package/dist/internal/errors.d.ts +2 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +93 -0
- package/dist/internal/errors.js.map +1 -0
- package/dist/internal/normalize.d.ts +2 -0
- package/dist/internal/normalize.d.ts.map +1 -0
- package/dist/internal/normalize.js +292 -0
- package/dist/internal/normalize.js.map +1 -0
- package/dist/internal/options.d.ts +2 -0
- package/dist/internal/options.d.ts.map +1 -0
- package/dist/internal/options.js +10 -0
- package/dist/internal/options.js.map +1 -0
- package/dist/internal/text.d.ts +11 -0
- package/dist/internal/text.d.ts.map +1 -0
- package/dist/internal/text.js +485 -0
- package/dist/internal/text.js.map +1 -0
- package/dist/internal/webhook.d.ts +2 -0
- package/dist/internal/webhook.d.ts.map +1 -0
- package/dist/internal/webhook.js +150 -0
- package/dist/internal/webhook.js.map +1 -0
- package/package.json +61 -1
- package/src/WhatsAppChat.ts +505 -0
- package/src/WhatsAppGraphApi.ts +147 -0
- package/src/WhatsAppUpdate.ts +142 -0
- package/src/index.ts +24 -0
- package/src/internal/actions.ts +220 -0
- package/src/internal/adapter.ts +284 -0
- package/src/internal/address.ts +41 -0
- package/src/internal/errors.ts +102 -0
- package/src/internal/normalize.ts +250 -0
- package/src/internal/options.ts +13 -0
- package/src/internal/text.ts +495 -0
- package/src/internal/webhook.ts +216 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The WhatsApp Cloud API adapter for `effect/chat`: the `Registry` layer
|
|
3
|
+
* for one business phone number, the webhook, the pure normaliser, delivery
|
|
4
|
+
* keys, templates, read receipts and the scoped post options.
|
|
5
|
+
*
|
|
6
|
+
* **Details**
|
|
7
|
+
*
|
|
8
|
+
* - Cloud API only. One adapter is one business phone number, and a
|
|
9
|
+
* `Registry` holds one adapter per platform.
|
|
10
|
+
* - An address is `{phoneNumberId}/u/{user}`, where `user` is a phone
|
|
11
|
+
* number in digits or a business-scoped user id (BSUID), such as
|
|
12
|
+
* `US.13491208655302741918`. `{phoneNumberId}/g/{groupId}` is
|
|
13
|
+
* reserved for groups, which are not supported yet: every op on it fails
|
|
14
|
+
* `InvalidRequest` with detail `group_unsupported`, and group webhooks
|
|
15
|
+
* are logged and dropped.
|
|
16
|
+
* - Inbound messages carry the BSUID form when the webhook has a BSUID, and
|
|
17
|
+
* the phone number only when it has none. The adapter never rewrites an
|
|
18
|
+
* address: a reply goes where the thread says. `WhatsAppUpdate.Alias`
|
|
19
|
+
* reports a phone number and a BSUID that name one user.
|
|
20
|
+
* - A business may only write freely within 24 hours of the user's last
|
|
21
|
+
* message. Outside it, a `post` fails `Forbidden` with detail
|
|
22
|
+
* `outsideWindow`, and `sendTemplate` is the way to write.
|
|
23
|
+
* - The access token needs `whatsapp_business_messaging`. Subscribe the app
|
|
24
|
+
* to the WhatsApp Business Account's `messages` field.
|
|
25
|
+
*
|
|
26
|
+
* @stability unstable
|
|
27
|
+
* @since 4.1.5
|
|
28
|
+
*/
|
|
29
|
+
import * as ByteSize from "effect/ByteSize";
|
|
30
|
+
import { Registry } from "effect/chat/ChatAdapter";
|
|
31
|
+
import { ChatHandler } from "effect/chat/ChatHandler";
|
|
32
|
+
import * as ChatInteraction from "effect/chat/ChatInteraction";
|
|
33
|
+
import * as Context from "effect/Context";
|
|
34
|
+
import * as Duration from "effect/Duration";
|
|
35
|
+
import * as Effect from "effect/Effect";
|
|
36
|
+
import { dual } from "effect/Function";
|
|
37
|
+
import * as HttpClient from "effect/http/HttpClient";
|
|
38
|
+
import * as HttpRouter from "effect/http/HttpRouter";
|
|
39
|
+
import * as Layer from "effect/Layer";
|
|
40
|
+
import * as Option from "effect/Option";
|
|
41
|
+
import * as Adapter from "./internal/adapter.js";
|
|
42
|
+
import * as Address from "./internal/address.js";
|
|
43
|
+
import * as Errors from "./internal/errors.js";
|
|
44
|
+
import * as Normalize from "./internal/normalize.js";
|
|
45
|
+
import { CurrentPostOptions } from "./internal/options.js";
|
|
46
|
+
import * as Webhook from "./internal/webhook.js";
|
|
47
|
+
import { WhatsAppGraphApi } from "./WhatsAppGraphApi.js";
|
|
48
|
+
import * as GraphApi from "./WhatsAppGraphApi.js";
|
|
49
|
+
/**
|
|
50
|
+
* The detail of a `post` refused outside the 24 hour customer service
|
|
51
|
+
* window, Graph error 131047. The reason is `Forbidden`. Write with
|
|
52
|
+
* `sendTemplate` instead.
|
|
53
|
+
*
|
|
54
|
+
* @stability unstable
|
|
55
|
+
* @category constants
|
|
56
|
+
* @since 4.1.5
|
|
57
|
+
*/
|
|
58
|
+
export const outsideWindow = Errors.outsideWindow;
|
|
59
|
+
/**
|
|
60
|
+
* The WhatsApp side of the adapter, for what the core has no op for.
|
|
61
|
+
*
|
|
62
|
+
* **Details**
|
|
63
|
+
*
|
|
64
|
+
* - `sendTemplate` sends an approved template, which is how v1 writes
|
|
65
|
+
* outside the 24 hour window. A template needs Meta's approval first; a
|
|
66
|
+
* Direct Send template, where Meta offers it, does not need a separate
|
|
67
|
+
* review. An authentication template with one-tap, zero-tap or copy-code
|
|
68
|
+
* buttons can't be sent to a BSUID: Meta refuses it with error 131062,
|
|
69
|
+
* which is `InvalidRequest`. Send those to a phone number address.
|
|
70
|
+
* - `markRead` marks a received message read, which shows blue ticks, and
|
|
71
|
+
* with `typing` shows the typing indicator until the next message or for
|
|
72
|
+
* up to 25 seconds.
|
|
73
|
+
* - `receive` runs one webhook delivery, already verified and parsed, as
|
|
74
|
+
* `layerWebhook` does, for a consumer's own transport. It needs a
|
|
75
|
+
* `ChatHandler`, and a `ChatInteraction.Handler` when there are
|
|
76
|
+
* clicks: without one a click is logged and dropped. `deadline` is a
|
|
77
|
+
* click handler's share, 3 seconds by default.
|
|
78
|
+
*
|
|
79
|
+
* @stability unstable
|
|
80
|
+
* @category services
|
|
81
|
+
* @since 4.1.5
|
|
82
|
+
*/
|
|
83
|
+
export class WhatsAppChat extends /*#__PURE__*/Context.Service()("@effect/chat-whatsapp/WhatsAppChat") {}
|
|
84
|
+
/**
|
|
85
|
+
* Registers the WhatsApp adapter in the `Registry`, for one business phone
|
|
86
|
+
* number.
|
|
87
|
+
*
|
|
88
|
+
* **Details**
|
|
89
|
+
*
|
|
90
|
+
* - Capabilities: `replyTo`, `files`, `download`, `getThread`,
|
|
91
|
+
* `opener`, `buttons` and `selects`. Every other op fails
|
|
92
|
+
* `Unsupported`. WhatsApp has no edit, so `ChatMarkdown` posts each
|
|
93
|
+
* part of a streamed reply once.
|
|
94
|
+
* - Every `post` is one send, and every check runs before any upload or
|
|
95
|
+
* send. `Posted` means Meta accepted the message, not that it was
|
|
96
|
+
* delivered: a later `failed` status goes to `onUpdate`. A send that
|
|
97
|
+
* failed after the request left is `uncertain`.
|
|
98
|
+
* - Markdown is rendered to WhatsApp's formatting: bold as `*b*`, italics
|
|
99
|
+
* as `_i_`, strikethrough as `~s~`, a link as `label (url)`, a heading
|
|
100
|
+
* as bold, a table without its delimiter row and a fence without its info
|
|
101
|
+
* string. Each replacement is used only when it is no longer than the
|
|
102
|
+
* Markdown it replaces, so the text never grows; anything else is sent as
|
|
103
|
+
* typed, including reference links and their definitions, and code fenced
|
|
104
|
+
* inside a quote or a list item.
|
|
105
|
+
* - `descriptor.measure` and the label limits count UTF-16 units. In live
|
|
106
|
+
* checks on Graph `v23.0`, Meta counted text bodies, interactive bodies,
|
|
107
|
+
* reply button titles, list row titles and row ids in code points, which a
|
|
108
|
+
* UTF-16 count never undercounts: it is stricter only for characters
|
|
109
|
+
* outside the Basic Multilingual Plane, such as emoji. Captions, the list
|
|
110
|
+
* button and section titles were not measured, and are counted the same
|
|
111
|
+
* way.
|
|
112
|
+
* - Link previews are off unless `withPostOptions({ linkPreview: true })`.
|
|
113
|
+
* - One file per post. It is uploaded, then sent as one media message with
|
|
114
|
+
* the text as its caption, or as the header of a message with reply
|
|
115
|
+
* buttons or a link button. Refused with `TooLarge`, subject `files` and
|
|
116
|
+
* detail `file_refused`, before anything is sent: more than one file, a
|
|
117
|
+
* MIME type outside Meta's table, `image/webp`, a file past its type's
|
|
118
|
+
* size limit, audio with text or actions, text over 1,024 beside a file,
|
|
119
|
+
* and a file beside a list. An upload Meta refuses as invalid or too large
|
|
120
|
+
* fails the same way, so `ChatMarkdown`'s `unattachedNote` applies. `text/markdown` is uploaded as
|
|
121
|
+
* `text/plain`, with its bytes and name unchanged.
|
|
122
|
+
* - Actions: one to three buttons with labels up to 20 go as reply buttons;
|
|
123
|
+
* a lone link button with a label up to 20 as a link button; otherwise
|
|
124
|
+
* buttons and select options become the rows of one list, at most ten,
|
|
125
|
+
* with labels up to 24. Link buttons beside anything else, or with a
|
|
126
|
+
* longer label, are appended to the text as `Label: url` lines. A body
|
|
127
|
+
* beside reply buttons or a link button is held to 1,024, which
|
|
128
|
+
* `descriptor.measure` counts. A post with actions and no text gets the
|
|
129
|
+
* labels, joined by ", ", as its text.
|
|
130
|
+
* - The list's button is the first select's placeholder, or
|
|
131
|
+
* `withPostOptions({ listButton })`, "Options" by default, up to 20.
|
|
132
|
+
* With buttons and a select, or two selects, each section has a title up
|
|
133
|
+
* to 24: the select's placeholder, or `buttonsSection`, "Actions" by
|
|
134
|
+
* default, for the buttons.
|
|
135
|
+
* - Refused before the call: more than ten rows (`TooLarge`,
|
|
136
|
+
* `list_rows`), a label past its limit (`TooLarge`,
|
|
137
|
+
* `label_too_long`), an id holding U+241F, two reply buttons with one
|
|
138
|
+
* label, and two elements with one id and value (`InvalidRequest`). A
|
|
139
|
+
* select's `selected` and a button's `style` are ignored.
|
|
140
|
+
* - A reply button's id on the wire is `b`, the id, U+241F and the value,
|
|
141
|
+
* and Meta counts it in UTF-8 bytes, up to 256. Over that, the post is
|
|
142
|
+
* refused before the call with `TooLarge`, `action_data_too_long`,
|
|
143
|
+
* even when the id and value are within `limits.actionDataLength`.
|
|
144
|
+
* - `getThread` is built from the address alone, as `direct`. `opener`
|
|
145
|
+
* is always `None`.
|
|
146
|
+
* - The pair rate limit is one message every 6 seconds to one user, which is
|
|
147
|
+
* `limits.minWriteInterval`. Meta accepts short bursts above that rate
|
|
148
|
+
* but refuses a sustained faster pace with error 131056, `RateLimited`.
|
|
149
|
+
* - `messagingAccountId` is sent as `messaging_account_id` on every
|
|
150
|
+
* message. Include it to say which Messaging account sends when the
|
|
151
|
+
* access token can send from more than one on the phone number. Meta
|
|
152
|
+
* documents it as optional for now, required for such a token on the
|
|
153
|
+
* newest Graph version from H1 2027, and on every version from H1 2028.
|
|
154
|
+
* Without it, Meta picks the account from the token. Not yet checked
|
|
155
|
+
* against a production number.
|
|
156
|
+
* - Built against Graph API `v24.0`, the default `baseUrl`. The
|
|
157
|
+
* adapter's live checks ran on `v23.0`.
|
|
158
|
+
*
|
|
159
|
+
* @stability unstable
|
|
160
|
+
* @category layers
|
|
161
|
+
* @since 4.1.5
|
|
162
|
+
*/
|
|
163
|
+
export const layer = options => Layer.effect(WhatsAppChat, Effect.gen(function* () {
|
|
164
|
+
const graph = yield* WhatsAppGraphApi;
|
|
165
|
+
const http = yield* HttpClient.HttpClient;
|
|
166
|
+
const registry = yield* Registry;
|
|
167
|
+
const {
|
|
168
|
+
adapter,
|
|
169
|
+
markRead,
|
|
170
|
+
sendTemplate
|
|
171
|
+
} = Adapter.make({
|
|
172
|
+
graph,
|
|
173
|
+
http,
|
|
174
|
+
accessToken: options.accessToken,
|
|
175
|
+
phoneNumberId: options.phoneNumberId,
|
|
176
|
+
messagingAccountId: options.messagingAccountId
|
|
177
|
+
});
|
|
178
|
+
yield* registry.register(adapter);
|
|
179
|
+
const context = {
|
|
180
|
+
phoneNumberId: options.phoneNumberId
|
|
181
|
+
};
|
|
182
|
+
return WhatsAppChat.of({
|
|
183
|
+
phoneNumberId: options.phoneNumberId,
|
|
184
|
+
sendTemplate,
|
|
185
|
+
markRead,
|
|
186
|
+
receive: (envelope, receiveOptions) => Effect.flatMap(ChatHandler, handler => Webhook.receive(Normalize.normalize(envelope, context), {
|
|
187
|
+
handle: handler.handle,
|
|
188
|
+
onUpdate: receiveOptions?.onUpdate,
|
|
189
|
+
clickDeadline: Effect.succeed(receiveOptions?.deadline ?? Duration.seconds(3))
|
|
190
|
+
}))
|
|
191
|
+
});
|
|
192
|
+
})).pipe(Layer.provideMerge(GraphApi.layer({
|
|
193
|
+
accessToken: options.accessToken,
|
|
194
|
+
baseUrl: options.baseUrl
|
|
195
|
+
})));
|
|
196
|
+
/**
|
|
197
|
+
* The address of a WhatsApp user: `{phoneNumberId}/u/{user}`, where
|
|
198
|
+
* `user` is a phone number in digits or a BSUID.
|
|
199
|
+
*
|
|
200
|
+
* @stability unstable
|
|
201
|
+
* @category constructors
|
|
202
|
+
* @since 4.1.5
|
|
203
|
+
*/
|
|
204
|
+
export const thread = parts => Address.thread({
|
|
205
|
+
phoneNumberId: parts.phoneNumberId,
|
|
206
|
+
kind: "user",
|
|
207
|
+
id: parts.user
|
|
208
|
+
});
|
|
209
|
+
/**
|
|
210
|
+
* The parts of a WhatsApp address, or `None` for another platform's or a
|
|
211
|
+
* malformed one. A group address, which is reserved, has `kind` `group`.
|
|
212
|
+
*
|
|
213
|
+
* @stability unstable
|
|
214
|
+
* @category destructors
|
|
215
|
+
* @since 4.1.5
|
|
216
|
+
*/
|
|
217
|
+
export const parts = Address.parts;
|
|
218
|
+
/**
|
|
219
|
+
* `GET {path}` and `POST {path}`: the webhook's subscription check and its
|
|
220
|
+
* deliveries.
|
|
221
|
+
*
|
|
222
|
+
* **Details**
|
|
223
|
+
*
|
|
224
|
+
* - `GET` answers Meta's check with `hub.challenge` when `hub.verify_token`
|
|
225
|
+
* is `verifyToken`, and 403 otherwise.
|
|
226
|
+
* - `POST` reads at most `maxBodyBytes` (3 MiB by default, 413; a body
|
|
227
|
+
* that can't be read is 400), checks `X-Hub-Signature-256` with the app
|
|
228
|
+
* secret (401), and drops a delivery for another object or another phone
|
|
229
|
+
* number with a 200. `maxBodyBytes` must be a non-negative safe integer;
|
|
230
|
+
* another value is a defect when the layer is built.
|
|
231
|
+
* - The updates run in order. Messages and reactions go to the
|
|
232
|
+
* `ChatHandler`, clicks to `ChatInteraction.handle`, and statuses and
|
|
233
|
+
* changed ids to `onUpdate`. A removed reaction is dropped.
|
|
234
|
+
* - A `NotAccepted` stops the delivery and answers 503, as does a request
|
|
235
|
+
* that takes longer than `deadline`, 10 seconds by default. Meta then
|
|
236
|
+
* delivers the whole batch again, for up to 7 days, so a batch that
|
|
237
|
+
* always runs past the deadline repeats until then. At the deadline the
|
|
238
|
+
* work is interrupted without waiting for its finalizers. In a live check
|
|
239
|
+
* Meta gave up on a response after about 22 seconds and redelivered.
|
|
240
|
+
* - A click never causes a 503. It gets what is left of the deadline, less
|
|
241
|
+
* a margin. `Ack` and `Late` count as handled, and a late handler keeps
|
|
242
|
+
* running. `OpenModal` goes to `Handler.refused` as
|
|
243
|
+
* `Unsupported("modals")`, which is awaited only within the click's share. Without a `ChatInteraction.Handler` in the
|
|
244
|
+
* layer's context, a click is logged and dropped.
|
|
245
|
+
* - Delivery is at least once. A message, a reaction or a click can arrive
|
|
246
|
+
* twice, a click even at the same time as its first delivery. Drop
|
|
247
|
+
* repeated events with `ChatEvent.dedupeKey`; a click handler for which
|
|
248
|
+
* a second run is unsafe must claim `deliveryKey` atomically, before any
|
|
249
|
+
* side effect. The adapter keeps no seen-store. Deliveries are not in
|
|
250
|
+
* order either: order by the updates' timestamps.
|
|
251
|
+
* - A user can tap more than one reply button of a message, or pick one
|
|
252
|
+
* list row twice. Each tap is a click of its own, with its own
|
|
253
|
+
* `deliveryKey`, so a choice that must be made once needs its own
|
|
254
|
+
* claim.
|
|
255
|
+
*
|
|
256
|
+
* Signature checks use the required `Mac` service. Provide
|
|
257
|
+
* `NodeCrypto.layerMac`, `BunCrypto.layerMac`, `DenoCrypto.layerMac`,
|
|
258
|
+
* `BrowserCrypto.layerMac`, or `Mac.layerWebCrypto` with
|
|
259
|
+
* `SecureRandom.layerWebCrypto` for other runtimes such as Workers.
|
|
260
|
+
* Key construction failures are configuration defects; verification errors
|
|
261
|
+
* fail the signature check (401).
|
|
262
|
+
*
|
|
263
|
+
* @stability unstable
|
|
264
|
+
* @category layers
|
|
265
|
+
* @since 4.1.5
|
|
266
|
+
*/
|
|
267
|
+
export const layerWebhook = options => HttpRouter.use(Effect.fnUntraced(function* (router) {
|
|
268
|
+
const whatsapp = yield* WhatsAppChat;
|
|
269
|
+
const handler = yield* ChatHandler;
|
|
270
|
+
const interactions = yield* Effect.serviceOption(ChatInteraction.Handler);
|
|
271
|
+
const verify = yield* Webhook.makeVerifier(options.appSecret);
|
|
272
|
+
const context = {
|
|
273
|
+
phoneNumberId: whatsapp.phoneNumberId
|
|
274
|
+
};
|
|
275
|
+
const webhookOptions = {
|
|
276
|
+
verify,
|
|
277
|
+
normalize: envelope => Normalize.normalize(envelope, context),
|
|
278
|
+
handle: handler.handle,
|
|
279
|
+
onUpdate: options.onUpdate,
|
|
280
|
+
deadline: options.deadline ?? Duration.seconds(10),
|
|
281
|
+
maxBodySize: ByteSize.bytes(options.maxBodyBytes ?? 3 * 1024 * 1024)
|
|
282
|
+
};
|
|
283
|
+
yield* router.add("GET", options.path, request => Effect.succeed(Webhook.handleVerification(request, options.verifyToken)));
|
|
284
|
+
yield* router.add("POST", options.path, request => {
|
|
285
|
+
const response = Webhook.handleRequest(request, webhookOptions);
|
|
286
|
+
return Option.isSome(interactions) ? Effect.provideService(response, ChatInteraction.Handler, interactions.value) : response;
|
|
287
|
+
});
|
|
288
|
+
}));
|
|
289
|
+
/**
|
|
290
|
+
* The normaliser, for a consumer's own transport.
|
|
291
|
+
*
|
|
292
|
+
* **Details**
|
|
293
|
+
*
|
|
294
|
+
* - Pure: takes a parsed webhook body, gives what it holds, in order. A
|
|
295
|
+
* change for another phone number, and a group message, give nothing.
|
|
296
|
+
* - A message's `thread` is the sender's BSUID address when the webhook
|
|
297
|
+
* has one, and its phone number address otherwise. `kind` is
|
|
298
|
+
* `direct`. Text is as the user typed it, a caption for media, and empty
|
|
299
|
+
* for a message with neither, such as a location. Media are attachments,
|
|
300
|
+
* with the media id as their `id`.
|
|
301
|
+
* - `raw` of an event or a click is the change's `metadata`, the
|
|
302
|
+
* sender's entry of `contacts`, with `wa_id` when Meta shares the
|
|
303
|
+
* number, and the one message. `message.raw` is the message.
|
|
304
|
+
* - A reaction with an emoji is `ReactionAdded`; a removed reaction gives
|
|
305
|
+
* nothing.
|
|
306
|
+
* - A reply button or a list row the adapter built is `ButtonClicked` or
|
|
307
|
+
* `SelectChanged`, whose `messageId` is the message that carried it.
|
|
308
|
+
* Any other interactive reply gives nothing.
|
|
309
|
+
* - Statuses give `Status`, and `Alias` too when they carry both ids. A
|
|
310
|
+
* `system` message about a changed number or id gives `UserIdChanged`.
|
|
311
|
+
*
|
|
312
|
+
* @stability unstable
|
|
313
|
+
* @category normalization
|
|
314
|
+
* @since 4.1.5
|
|
315
|
+
*/
|
|
316
|
+
export const normalize = Normalize.normalize;
|
|
317
|
+
const encodeKey = parts => parts.map(encodeURIComponent).join("/");
|
|
318
|
+
const phoneNumberOf = thread => thread.id.split("/")[0];
|
|
319
|
+
/**
|
|
320
|
+
* The key of one delivery of a click or a reaction:
|
|
321
|
+
* `{phoneNumberId}/{messageId}`, the id of the webhook message that carried
|
|
322
|
+
* it, each part percent-encoded.
|
|
323
|
+
*
|
|
324
|
+
* **When to use**
|
|
325
|
+
*
|
|
326
|
+
* Use to make a click handler safe to run twice. Meta redelivers a batch
|
|
327
|
+
* that was not answered in time, so a click can run again, even at the same
|
|
328
|
+
* time as the first run. Claim the key atomically, such as with an insert
|
|
329
|
+
* that fails on a duplicate, before any side effect, and skip the click when
|
|
330
|
+
* the claim fails.
|
|
331
|
+
*
|
|
332
|
+
* @stability unstable
|
|
333
|
+
* @category getters
|
|
334
|
+
* @since 4.1.5
|
|
335
|
+
*/
|
|
336
|
+
export const deliveryKey = event => {
|
|
337
|
+
const raw = event.raw;
|
|
338
|
+
const id = Array.isArray(raw?.messages) && typeof raw.messages[0]?.id === "string" ? raw.messages[0].id : "";
|
|
339
|
+
return encodeKey([phoneNumberOf(event.thread), id]);
|
|
340
|
+
};
|
|
341
|
+
/**
|
|
342
|
+
* The key of an update, for dropping a redelivered one, its parts
|
|
343
|
+
* percent-encoded and joined by `/`.
|
|
344
|
+
*
|
|
345
|
+
* **Details**
|
|
346
|
+
*
|
|
347
|
+
* - `Status`: `{phoneNumberId}/status/{messageId}/{status}`.
|
|
348
|
+
* - `UserIdChanged`: `{phoneNumberId}/user_changed/{id}`.
|
|
349
|
+
* - `Alias`: `{phoneNumberId}/alias/{messageId}/{status}/{phone}/{userId}`.
|
|
350
|
+
*
|
|
351
|
+
* @stability unstable
|
|
352
|
+
* @category getters
|
|
353
|
+
* @since 4.1.5
|
|
354
|
+
*/
|
|
355
|
+
export const updateKey = update => {
|
|
356
|
+
const phoneNumberId = phoneNumberOf(update.thread);
|
|
357
|
+
switch (update._tag) {
|
|
358
|
+
case "Status":
|
|
359
|
+
return encodeKey([phoneNumberId, "status", update.messageId, update.status]);
|
|
360
|
+
case "UserIdChanged":
|
|
361
|
+
return encodeKey([phoneNumberId, "user_changed", update.id]);
|
|
362
|
+
case "Alias":
|
|
363
|
+
return encodeKey([phoneNumberId, "alias", update.messageId, update.status, update.phone, update.userId]);
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
/**
|
|
367
|
+
* Scoped WhatsApp options for every write inside `effect`. Nested calls
|
|
368
|
+
* merge, the inner one winning field by field.
|
|
369
|
+
*
|
|
370
|
+
* @stability unstable
|
|
371
|
+
* @category combinators
|
|
372
|
+
* @since 4.1.5
|
|
373
|
+
*/
|
|
374
|
+
export const withPostOptions = /*#__PURE__*/dual(2, (effect, options) => Effect.updateService(effect, CurrentPostOptions, current => ({
|
|
375
|
+
...current,
|
|
376
|
+
...options
|
|
377
|
+
})));
|
|
378
|
+
/**
|
|
379
|
+
* WhatsApp has no mention tokens outside groups, so nothing is a mention:
|
|
380
|
+
* for `ChatTest.checkMessage`, `ChatTest.checkEvent` and
|
|
381
|
+
* `ChatTest.conformance`.
|
|
382
|
+
*
|
|
383
|
+
* @stability unstable
|
|
384
|
+
* @category testing
|
|
385
|
+
* @since 4.1.5
|
|
386
|
+
*/
|
|
387
|
+
export const parseMention = () => Option.none();
|
|
388
|
+
//# sourceMappingURL=WhatsAppChat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WhatsAppChat.js","names":[],"sources":["../src/WhatsAppChat.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,OAAO,KAAK,QAAQ,MAAM,iBAAiB;AAC3C,SAAsB,QAAQ,QAAQ,yBAAyB;AAG/D,SAAS,WAAW,QAA0B,yBAAyB;AACvE,OAAO,KAAK,eAAe,MAAM,6BAA6B;AAG9D,OAAO,KAAK,OAAO,MAAM,gBAAgB;AAEzC,OAAO,KAAK,QAAQ,MAAM,iBAAiB;AAC3C,OAAO,KAAK,MAAM,MAAM,eAAe;AACvC,SAAS,IAAI,QAAQ,iBAAiB;AACtC,OAAO,KAAK,UAAU,MAAM,wBAAwB;AACpD,OAAO,KAAK,UAAU,MAAM,wBAAwB;AACpD,OAAO,KAAK,KAAK,MAAM,cAAc;AACrC,OAAO,KAAK,MAAM,MAAM,eAAe;AAGvC,OAAO,KAAK,OAAO,MAAM,uBAAuB;AAChD,OAAO,KAAK,OAAO,MAAM,uBAAuB;AAChD,OAAO,KAAK,MAAM,MAAM,sBAAsB;AAC9C,OAAO,KAAK,SAAS,MAAM,yBAAyB;AACpD,SAAS,k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+1,90 @@
|
|
|
1
|
+
import type { ChatError } from "effect/chat/ChatError";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as HttpClient from "effect/http/HttpClient";
|
|
5
|
+
import * as Layer from "effect/Layer";
|
|
6
|
+
import * as Redacted from "effect/Redacted";
|
|
7
|
+
import type * as Schema from "effect/Schema";
|
|
8
|
+
/**
|
|
9
|
+
* Options for one Graph call.
|
|
10
|
+
*
|
|
11
|
+
* @stability unstable
|
|
12
|
+
* @category models
|
|
13
|
+
* @since 4.1.5
|
|
14
|
+
*/
|
|
15
|
+
export interface CallOptions {
|
|
16
|
+
/** Default `GET`, or `POST` when there is a `body` or a `form`. */
|
|
17
|
+
readonly method?: "GET" | "POST" | "DELETE" | undefined;
|
|
18
|
+
/** Sent as JSON. */
|
|
19
|
+
readonly body?: Readonly<Record<string, unknown>> | undefined;
|
|
20
|
+
/** Sent as `multipart/form-data`, as a media upload is. */
|
|
21
|
+
readonly form?: FormData | undefined;
|
|
22
|
+
/** Query parameters. */
|
|
23
|
+
readonly params?: Readonly<Record<string, string>> | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The call changes something. It is never repeated once the request left,
|
|
26
|
+
* and a failure that may have landed is `uncertain`.
|
|
27
|
+
*/
|
|
28
|
+
readonly write?: boolean | undefined;
|
|
29
|
+
/** Retry `retry` failures, as `ChatAdapter.retrySafe` does. Default true. */
|
|
30
|
+
readonly retry?: boolean | undefined;
|
|
31
|
+
}
|
|
32
|
+
declare const WhatsAppGraphApi_base: Context.ServiceClass<WhatsAppGraphApi, "@effect/chat-whatsapp/WhatsAppGraphApi", {
|
|
33
|
+
readonly call: (path: string, options?: CallOptions | undefined) => Effect.Effect<Schema.Json, ChatError>;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* The minimal Graph API client, with the access token.
|
|
37
|
+
*
|
|
38
|
+
* **Details**
|
|
39
|
+
*
|
|
40
|
+
* - `call` requests `{baseUrl}/{path}` with the token as a bearer token,
|
|
41
|
+
* and succeeds with the JSON of a 2xx answer.
|
|
42
|
+
* - Another answer fails with the reason for its Graph error code, whose
|
|
43
|
+
* `detail` is `graph_{code}` and whose `cause` is the Graph `error`
|
|
44
|
+
* object, or for its HTTP status when it has no Graph error.
|
|
45
|
+
* - A write never repeats once the request left: a transport failure, a
|
|
46
|
+
* server error or an unreadable answer is `uncertain`. A refusal that
|
|
47
|
+
* Meta answers before sending, such as a rate limit, is retried unless
|
|
48
|
+
* `retry: false`, as a read's `retry` failures are.
|
|
49
|
+
* - The error's `op` is the path.
|
|
50
|
+
*
|
|
51
|
+
* @stability unstable
|
|
52
|
+
* @category services
|
|
53
|
+
* @since 4.1.5
|
|
54
|
+
*/
|
|
55
|
+
export declare class WhatsAppGraphApi extends WhatsAppGraphApi_base {
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The Graph API version the adapter was written against.
|
|
59
|
+
*
|
|
60
|
+
* @stability unstable
|
|
61
|
+
* @category constants
|
|
62
|
+
* @since 4.1.5
|
|
63
|
+
*/
|
|
64
|
+
export declare const defaultBaseUrl = "https://graph.facebook.com/v24.0";
|
|
65
|
+
/**
|
|
66
|
+
* Builds the client from an `HttpClient` and a system user access token.
|
|
67
|
+
*
|
|
68
|
+
* @stability unstable
|
|
69
|
+
* @category constructors
|
|
70
|
+
* @since 4.1.5
|
|
71
|
+
*/
|
|
72
|
+
export declare const make: (options: {
|
|
73
|
+
readonly accessToken: Redacted.Redacted;
|
|
74
|
+
readonly baseUrl?: string | undefined;
|
|
75
|
+
}) => Effect.Effect<{
|
|
76
|
+
readonly call: (path: string, options?: CallOptions | undefined) => Effect.Effect<Schema.Json, ChatError>;
|
|
77
|
+
}, never, HttpClient.HttpClient>;
|
|
78
|
+
/**
|
|
79
|
+
* The client as a layer.
|
|
80
|
+
*
|
|
81
|
+
* @stability unstable
|
|
82
|
+
* @category layers
|
|
83
|
+
* @since 4.1.5
|
|
84
|
+
*/
|
|
85
|
+
export declare const layer: (options: {
|
|
86
|
+
readonly accessToken: Redacted.Redacted;
|
|
87
|
+
readonly baseUrl?: string | undefined;
|
|
88
|
+
}) => Layer.Layer<WhatsAppGraphApi, never, HttpClient.HttpClient>;
|
|
89
|
+
export {};
|
|
90
|
+
//# sourceMappingURL=WhatsAppGraphApi.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WhatsAppGraphApi.d.ts","sourceRoot":"","sources":["../src/WhatsAppGraphApi.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,SAAS,EAAmB,MAAM,uBAAuB,CAAA;AACvE,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAA;AAEpD,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAErC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAA;AAG5C;;;;;;GAMG;AACH,MAAM,WAAW,WAAW;IAC1B,mEAAmE;IACnE,QAAQ,CAAC,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAA;IACvD,oBAAoB;IACpB,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,CAAA;IAC7D,2DAA2D;IAC3D,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAA;IACpC,wBAAwB;IACxB,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAA;IAC9D;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IACpC,6EAA6E;IAC7E,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;CACrC;;mBAuBgB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,SAAS,KAAK,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC;;AArB3G;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,gBAAiB,SAAQ,qBAEQ;CAAG;AAEjD;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,qCAAqC,CAAA;AAEhE;;;;;;GAMG;AACH,eAAO,MAAM,IAAI;0BACO,QAAQ,CAAC,QAAQ;uBACpB,MAAM,GAAG,SAAS;;mBArBtB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,SAAS,KAAK,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC;gCAoEzG,CAAA;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,KAAK,YAAa;IAC7B,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAAC,QAAQ,CAAA;IACvC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACtC,KAAG,KAAK,CAAC,KAAK,CAAC,gBAAgB,EAAE,KAAK,EAAE,UAAU,CAAC,UAAU,CAAkD,CAAA"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The minimal Graph API client the WhatsApp adapter is built on. It is
|
|
3
|
+
* public, so a consumer can call an endpoint the adapter has no op for, with
|
|
4
|
+
* the same error mapping and the same rule for writes.
|
|
5
|
+
*
|
|
6
|
+
* @stability unstable
|
|
7
|
+
* @since 4.1.5
|
|
8
|
+
*/
|
|
9
|
+
import { retrySafe } from "effect/chat/ChatAdapter";
|
|
10
|
+
import * as Context from "effect/Context";
|
|
11
|
+
import * as Effect from "effect/Effect";
|
|
12
|
+
import * as HttpClient from "effect/http/HttpClient";
|
|
13
|
+
import * as HttpClientRequest from "effect/http/HttpClientRequest";
|
|
14
|
+
import * as Layer from "effect/Layer";
|
|
15
|
+
import * as Predicate from "effect/Predicate";
|
|
16
|
+
import * as Redacted from "effect/Redacted";
|
|
17
|
+
import * as Errors from "./internal/errors.js";
|
|
18
|
+
/**
|
|
19
|
+
* The minimal Graph API client, with the access token.
|
|
20
|
+
*
|
|
21
|
+
* **Details**
|
|
22
|
+
*
|
|
23
|
+
* - `call` requests `{baseUrl}/{path}` with the token as a bearer token,
|
|
24
|
+
* and succeeds with the JSON of a 2xx answer.
|
|
25
|
+
* - Another answer fails with the reason for its Graph error code, whose
|
|
26
|
+
* `detail` is `graph_{code}` and whose `cause` is the Graph `error`
|
|
27
|
+
* object, or for its HTTP status when it has no Graph error.
|
|
28
|
+
* - A write never repeats once the request left: a transport failure, a
|
|
29
|
+
* server error or an unreadable answer is `uncertain`. A refusal that
|
|
30
|
+
* Meta answers before sending, such as a rate limit, is retried unless
|
|
31
|
+
* `retry: false`, as a read's `retry` failures are.
|
|
32
|
+
* - The error's `op` is the path.
|
|
33
|
+
*
|
|
34
|
+
* @stability unstable
|
|
35
|
+
* @category services
|
|
36
|
+
* @since 4.1.5
|
|
37
|
+
*/
|
|
38
|
+
export class WhatsAppGraphApi extends /*#__PURE__*/Context.Service()("@effect/chat-whatsapp/WhatsAppGraphApi") {}
|
|
39
|
+
/**
|
|
40
|
+
* The Graph API version the adapter was written against.
|
|
41
|
+
*
|
|
42
|
+
* @stability unstable
|
|
43
|
+
* @category constants
|
|
44
|
+
* @since 4.1.5
|
|
45
|
+
*/
|
|
46
|
+
export const defaultBaseUrl = "https://graph.facebook.com/v24.0";
|
|
47
|
+
/**
|
|
48
|
+
* Builds the client from an `HttpClient` and a system user access token.
|
|
49
|
+
*
|
|
50
|
+
* @stability unstable
|
|
51
|
+
* @category constructors
|
|
52
|
+
* @since 4.1.5
|
|
53
|
+
*/
|
|
54
|
+
export const make = /*#__PURE__*/Effect.fnUntraced(function* (options) {
|
|
55
|
+
const http = yield* HttpClient.HttpClient;
|
|
56
|
+
const baseUrl = options.baseUrl ?? defaultBaseUrl;
|
|
57
|
+
const once = (path, callOptions) => {
|
|
58
|
+
const write = callOptions.write === true;
|
|
59
|
+
const fail = reason => Effect.fail(Errors.chatError(path, reason));
|
|
60
|
+
const method = callOptions.method ?? (callOptions.body !== undefined || callOptions.form !== undefined ? "POST" : "GET");
|
|
61
|
+
let request = HttpClientRequest.make(method)(`${baseUrl}/${path}`).pipe(HttpClientRequest.bearerToken(Redacted.value(options.accessToken)));
|
|
62
|
+
if (callOptions.params !== undefined) request = HttpClientRequest.setUrlParams(request, callOptions.params);
|
|
63
|
+
if (callOptions.form !== undefined) request = HttpClientRequest.bodyFormData(request, callOptions.form);else if (callOptions.body !== undefined) request = HttpClientRequest.bodyJsonUnsafe(request, callOptions.body);
|
|
64
|
+
return http.execute(request).pipe(Effect.catch(error => fail(Errors.transport(write, error.reason._tag, error))), Effect.flatMap(response => response.json.pipe(Effect.matchEffect({
|
|
65
|
+
onFailure: error => response.status >= 200 && response.status < 300 ? fail(Errors.invalidResponse(write, "invalid_json", error)) : fail(Errors.reasonForStatus(response.status, {
|
|
66
|
+
write
|
|
67
|
+
})),
|
|
68
|
+
onSuccess: json => {
|
|
69
|
+
if (response.status >= 200 && response.status < 300) return Effect.succeed(json);
|
|
70
|
+
const graph = Errors.graphError(json);
|
|
71
|
+
const cause = Predicate.hasProperty(json, "error") ? json.error : json;
|
|
72
|
+
return fail(graph === undefined ? Errors.reasonForStatus(response.status, {
|
|
73
|
+
write,
|
|
74
|
+
cause
|
|
75
|
+
}) : Errors.reasonForGraphError(graph, {
|
|
76
|
+
write,
|
|
77
|
+
cause
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
}))));
|
|
81
|
+
};
|
|
82
|
+
return WhatsAppGraphApi.of({
|
|
83
|
+
call: (path, callOptions = {}) => {
|
|
84
|
+
const call = once(path, callOptions);
|
|
85
|
+
return callOptions.retry === false ? call : retrySafe(call);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
/**
|
|
90
|
+
* The client as a layer.
|
|
91
|
+
*
|
|
92
|
+
* @stability unstable
|
|
93
|
+
* @category layers
|
|
94
|
+
* @since 4.1.5
|
|
95
|
+
*/
|
|
96
|
+
export const layer = options => Layer.effect(WhatsAppGraphApi, make(options));
|
|
97
|
+
//# sourceMappingURL=WhatsAppGraphApi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WhatsAppGraphApi.js","names":[],"sources":["../src/WhatsAppGraphApi.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;AAQA,SAAS,SAAS,QAAQ,yBAAyB;AAEnD,OAAO,KAAK,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe;AACvC,OAAO,KAAK,UAAU,MAAM,wBAAwB;AACpD,OAAO,KAAK,iBAAiB,MAAM,+BAA+B;AAClE,OAAO,KAAK,KAAK,MAAM,cAAc;AACrC,OAAO,KAAK,SAAS,MAAM,kBAAkB;AAC7C,OAAO,KAAK,QAAQ,MAAM,iBAAiB;AAE3C,OAAO,KAAK,MAAM,MAAM,sBAAsB;AA2B9C;;;;;;;;;;;;;;;;;;;;AAoBA,OAAM,MAAO,gBAAiB,sBAAQ,OAAO,CAAC,OAAO,EAEjD,CAAC,wCAAwC,CAAC;AAE9C;;;;;;;AAOA,OAAO,MAAM,cAAc,GAAG,kCAAkC;AAEhE;;;;;;;AAOA,OAAO,MAAM,IAAI,gBAAG,MAAM,CAAC,UAAU,CAAC,WAAU,OAG/C;EACC,MAAM,IAAI,GAAG,OAAO,UAAU,CAAC,UAAU;EACzC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,cAAc;EAEjD,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,WAAwB,KAAI;IACtD,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,KA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