effect-cf 0.14.0 → 0.16.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.
- package/CHANGELOG.md +12 -0
- package/README.md +110 -1
- package/dist/index.d.mts +322 -100
- package/dist/index.mjs +414 -103
- package/package.json +1 -1
- package/src/AnalyticsEngine.ts +846 -0
- package/src/Email.ts +134 -0
- package/src/index.ts +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# effect-cf
|
|
2
2
|
|
|
3
|
+
## 0.16.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#51](https://github.com/danieljvdm/effect-cf/pull/51) [`9a6abd2`](https://github.com/danieljvdm/effect-cf/commit/9a6abd27366c71867a420f872459b3d5fc1b2143) Thanks [@owensbla](https://github.com/owensbla)! - Add Effect-native Cloudflare Analytics Engine helpers. `AnalyticsEngine.Tag(...)` now provides validated dataset writes, configurable invalid-write policy, and batch write helpers. `AnalyticsEngine.QueryTag(...)` / `makeQueryClient(...)` provide SQL API querying backed by Effect `HttpClient`, config and redacted API token support, typed result envelopes, and row decoding through Effect schemas.
|
|
8
|
+
|
|
9
|
+
## 0.15.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#47](https://github.com/danieljvdm/effect-cf/pull/47) [`debc5e1`](https://github.com/danieljvdm/effect-cf/commit/debc5e12cee68c85c5ea99096e2660aa0ec90b6f) Thanks [@owensbla](https://github.com/owensbla)! - Add an Effect-native Cloudflare Send Email binding wrapper. `Email.Tag(...)` now provides a typed client for `send_email` bindings with `send(...)`, `unsafeRaw`, binding validation, and `EmailOperationError` failure mapping.
|
|
14
|
+
|
|
3
15
|
## 0.14.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# effect-cf
|
|
2
2
|
|
|
3
|
-
Effect-native Cloudflare primitives for Workers, Durable Objects, bindings, KV, D1, Queues, Workflows, and Durable Object storage.
|
|
3
|
+
Effect-native Cloudflare primitives for Workers, Durable Objects, bindings, KV, D1, Queues, Email, Analytics Engine, Workflows, and Durable Object storage.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -35,6 +35,8 @@ Runtime creation belongs at Cloudflare entrypoints, not inside binding helpers.
|
|
|
35
35
|
- `R2` - typed R2 bucket binding helper with Effect-wrapped object and multipart operations
|
|
36
36
|
- `Hyperdrive` - typed Hyperdrive binding helper for connection strings and optional Postgres SQL integration
|
|
37
37
|
- `Images` - typed Cloudflare Images binding helper with transformation APIs and optional hosted image operations
|
|
38
|
+
- `Email` - typed Cloudflare Send Email binding helper for `send_email` bindings
|
|
39
|
+
- `AnalyticsEngine` - typed Cloudflare Analytics Engine write bindings and SQL API query helpers
|
|
38
40
|
- `Queue` - typed Queue producer/consumer tags plus client and error types
|
|
39
41
|
- `Workflow` - typed Workflow entrypoints, steps, starter clients, and instance types
|
|
40
42
|
- `Rpc` - Cloudflare RPC type helpers and scoped disposal utilities
|
|
@@ -206,6 +208,113 @@ export const resizeAvatar = (image: Images.ImageInputValue) =>
|
|
|
206
208
|
});
|
|
207
209
|
```
|
|
208
210
|
|
|
211
|
+
## Email Example
|
|
212
|
+
|
|
213
|
+
Email tags expose Cloudflare Send Email bindings as Effect-wrapped `send(...)` operations.
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
import { Effect } from "effect";
|
|
217
|
+
import { Email } from "effect-cf";
|
|
218
|
+
|
|
219
|
+
class TransactionalEmail extends Email.Tag<TransactionalEmail>()("TransactionalEmail") {}
|
|
220
|
+
|
|
221
|
+
export const TransactionalEmailLayer = TransactionalEmail.layer({ binding: "EMAIL" });
|
|
222
|
+
|
|
223
|
+
export const sendWelcomeEmail = (to: string) =>
|
|
224
|
+
Effect.gen(function* () {
|
|
225
|
+
const email = yield* TransactionalEmail;
|
|
226
|
+
|
|
227
|
+
return yield* email.send({
|
|
228
|
+
from: { name: "Example", email: "team@example.com" },
|
|
229
|
+
to,
|
|
230
|
+
subject: "Welcome to Example",
|
|
231
|
+
text: "Welcome to Example",
|
|
232
|
+
html: "<p>Welcome to Example</p>",
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Analytics Engine Example
|
|
238
|
+
|
|
239
|
+
Analytics Engine tags expose Cloudflare dataset bindings as Effect-wrapped `writeDataPoint(...)` operations. Writes use Cloudflare's native non-blocking runtime API and are validated against Workers Analytics Engine limits before reaching the binding.
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
import { Effect } from "effect";
|
|
243
|
+
import { AnalyticsEngine } from "effect-cf";
|
|
244
|
+
|
|
245
|
+
class RequestAnalytics extends AnalyticsEngine.Tag<RequestAnalytics>()("RequestAnalytics") {}
|
|
246
|
+
|
|
247
|
+
export const RequestAnalyticsLayer = RequestAnalytics.layer({
|
|
248
|
+
binding: "REQUEST_ANALYTICS",
|
|
249
|
+
write: {
|
|
250
|
+
onInvalid: "error",
|
|
251
|
+
batchSize: 100,
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
export const recordPageView = (request: Request) =>
|
|
256
|
+
Effect.gen(function* () {
|
|
257
|
+
const analytics = yield* RequestAnalytics;
|
|
258
|
+
const url = new URL(request.url);
|
|
259
|
+
|
|
260
|
+
yield* analytics.writeDataPoint({
|
|
261
|
+
indexes: [url.hostname],
|
|
262
|
+
blobs: [url.pathname, request.headers.get("cf-connecting-country") ?? "unknown"],
|
|
263
|
+
doubles: [1],
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
yield* analytics.writeBatch(
|
|
267
|
+
[
|
|
268
|
+
{
|
|
269
|
+
indexes: [url.hostname],
|
|
270
|
+
blobs: [url.pathname, "page-view"],
|
|
271
|
+
doubles: [1],
|
|
272
|
+
},
|
|
273
|
+
],
|
|
274
|
+
{ onInvalid: "drop" },
|
|
275
|
+
);
|
|
276
|
+
});
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Define the dataset binding in the consuming Worker's `wrangler.jsonc` with `analytics_engine_datasets`, then provide `RequestAnalytics.layer({ binding: "REQUEST_ANALYTICS" })` from the Worker layer. Invalid writes fail with `AnalyticsEngineWriteValidationError` by default. Use `write: { onInvalid: "drop" }` on the layer or `{ onInvalid: "drop" }` per call when dropping invalid points is preferable.
|
|
280
|
+
|
|
281
|
+
Analytics Engine query tags wrap Cloudflare's HTTP SQL API. Configuration can stay in Effect `Config`, including a redacted API token, the outbound transport is an Effect `HttpClient` dependency, and rows can be decoded with Effect schemas at the query boundary. Use `fetchLayerConfig(...)` as shorthand when the platform fetch-backed client is enough.
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
import { Effect, Layer, Schema as S } from "effect";
|
|
285
|
+
import { FetchHttpClient } from "effect/unstable/http";
|
|
286
|
+
import { AnalyticsEngine, WorkerConfig } from "effect-cf";
|
|
287
|
+
|
|
288
|
+
class AnalyticsQuery extends AnalyticsEngine.QueryTag<AnalyticsQuery>()("AnalyticsQuery") {}
|
|
289
|
+
|
|
290
|
+
export const AnalyticsQueryLayer = AnalyticsQuery.layerConfig(
|
|
291
|
+
AnalyticsEngine.queryConfig({
|
|
292
|
+
accountId: WorkerConfig.string("CLOUDFLARE_ACCOUNT_ID"),
|
|
293
|
+
apiToken: WorkerConfig.redacted("CLOUDFLARE_API_TOKEN"),
|
|
294
|
+
}),
|
|
295
|
+
).pipe(Layer.provide(FetchHttpClient.layer));
|
|
296
|
+
|
|
297
|
+
const PageView = S.Struct({
|
|
298
|
+
path: S.String,
|
|
299
|
+
views: S.Number,
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
export const topPages = Effect.gen(function* () {
|
|
303
|
+
const analytics = yield* AnalyticsQuery;
|
|
304
|
+
|
|
305
|
+
return yield* analytics.queryRows(
|
|
306
|
+
PageView,
|
|
307
|
+
`
|
|
308
|
+
SELECT blob1 AS path, SUM(_sample_interval) AS views
|
|
309
|
+
FROM request_metrics
|
|
310
|
+
GROUP BY path
|
|
311
|
+
ORDER BY views DESC
|
|
312
|
+
LIMIT 10
|
|
313
|
+
`,
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
```
|
|
317
|
+
|
|
209
318
|
## Workflow Example
|
|
210
319
|
|
|
211
320
|
Workflow definitions type the payload and result. Runtime handlers can access `Workflow.WorkflowEvent`, use durable `Workflow.step(...)`, and use normal binding services inside steps.
|