couriercrow 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -2
  3. package/bin/couriercrow.js +5 -0
  4. package/bin/crow.js +14 -0
  5. package/bundle/migrations/0001_initial.sql +463 -0
  6. package/bundle/version.json +3 -0
  7. package/bundle/web/_headers +6 -0
  8. package/bundle/web/assets/index-Bim8rHvG.css +2 -0
  9. package/bundle/web/assets/index-YpJn_3e5.js +13 -0
  10. package/bundle/web/index.html +14 -0
  11. package/bundle/worker/README.md +1 -0
  12. package/bundle/worker/worker.js +57841 -0
  13. package/bundle/wrangler.jsonc +64 -0
  14. package/package.json +61 -4
  15. package/src/app-credentials.ts +413 -0
  16. package/src/bin.ts +578 -0
  17. package/src/build-config.ts +264 -0
  18. package/src/build-id.ts +39 -0
  19. package/src/cloudflare/cloudflare-client.ts +361 -0
  20. package/src/cloudflare/cloudflare-live-access.ts +454 -0
  21. package/src/cloudflare/cloudflare-live-account.ts +95 -0
  22. package/src/cloudflare/cloudflare-live-api.ts +87 -0
  23. package/src/cloudflare/cloudflare-live-checks.ts +151 -0
  24. package/src/cloudflare/cloudflare-live-zone.ts +363 -0
  25. package/src/cloudflare/cloudflare-live.ts +270 -0
  26. package/src/cloudflare/cloudflare-state.ts +152 -0
  27. package/src/cloudflare/first-run.ts +322 -0
  28. package/src/credentials-show.ts +49 -0
  29. package/src/deploy.ts +807 -0
  30. package/src/doctor.ts +504 -0
  31. package/src/environment.ts +141 -0
  32. package/src/index.ts +4 -0
  33. package/src/init.ts +332 -0
  34. package/src/load-config.ts +47 -0
  35. package/src/mailbox-api.ts +345 -0
  36. package/src/permissions.ts +133 -0
  37. package/src/plan.ts +517 -0
  38. package/src/printable.ts +8 -0
  39. package/src/product.ts +63 -0
  40. package/src/purge.ts +93 -0
  41. package/src/render.ts +406 -0
  42. package/src/rotate-app-secret.ts +53 -0
  43. package/src/runtime.ts +33 -0
  44. package/src/set-secret.ts +63 -0
  45. package/src/terminal.ts +163 -0
  46. package/src/wrangler.ts +168 -0
@@ -0,0 +1,454 @@
1
+ import { EmailAddress } from '@couriercrow/core'
2
+ import { Effect, Option, Schema } from 'effect'
3
+
4
+ import {
5
+ applicationUpdateBody,
6
+ CloudflareError,
7
+ coverageOf,
8
+ mailboxApplicationName,
9
+ unsubscribeApplicationName,
10
+ unsubscribeDomain
11
+ } from './cloudflare-client'
12
+ import type {
13
+ AccessPolicyInput,
14
+ AccessRule,
15
+ ApplicationCoverage,
16
+ CloudflareShape
17
+ } from './cloudflare-client'
18
+ import {
19
+ accountError,
20
+ accountNotFound,
21
+ segment
22
+ } from './cloudflare-live-api'
23
+ import type { LiveApi } from './cloudflare-live-api'
24
+ import { peoplePolicyName } from './first-run'
25
+ import { accountPermissions } from '../permissions'
26
+
27
+ // Access sessions last a month.
28
+ const sessionDuration = '720h'
29
+
30
+ const rule = Schema.Struct({
31
+ email: Schema.optional(Schema.Struct({ email: Schema.String })),
32
+ service_token: Schema.optional(Schema.Struct({ token_id: Schema.String }))
33
+ })
34
+ const include = Schema.optional(Schema.Array(rule))
35
+
36
+ // Only what the plan needs to read the people who are allowed in.
37
+ const policyForPlan = Schema.Struct({
38
+ decision: Schema.String,
39
+ include,
40
+ name: Schema.optional(Schema.String)
41
+ })
42
+ const policyWithName = Schema.Struct({
43
+ decision: Schema.String,
44
+ id: Schema.String,
45
+ include,
46
+ name: Schema.String
47
+ })
48
+ const application = Schema.Struct({
49
+ aud: Schema.String,
50
+ domain: Schema.optional(Schema.String),
51
+ id: Schema.String
52
+ })
53
+ const applicationForPlan = Schema.Struct({
54
+ domain: Schema.optional(Schema.String),
55
+ id: Schema.String
56
+ })
57
+ const applicationNamed = Schema.Struct({
58
+ domain: Schema.optional(Schema.String),
59
+ id: Schema.String,
60
+ name: Schema.optional(Schema.String)
61
+ })
62
+ // The whole application as Cloudflare answers it, so an update can send back
63
+ // every field it did not mean to change.
64
+ const applicationRecord = Schema.Record(Schema.String, Schema.Unknown)
65
+ const organization = Schema.Struct({ auth_domain: Schema.String })
66
+ const serviceToken = Schema.Struct({
67
+ client_id: Schema.String,
68
+ client_secret: Schema.String,
69
+ id: Schema.String
70
+ })
71
+ const serviceTokenItem = Schema.Struct({
72
+ client_id: Schema.String,
73
+ id: Schema.String,
74
+ name: Schema.String
75
+ })
76
+
77
+ const decodeEmailAddress = Schema.decodeUnknownOption(EmailAddress)
78
+
79
+ function encodeRule(item: AccessRule): unknown {
80
+ switch (item._tag) {
81
+ case 'Email':
82
+ return { email: { email: item.email } }
83
+ case 'Everyone':
84
+ return { everyone: {} }
85
+ case 'ServiceToken':
86
+ return { service_token: { token_id: item.tokenId } }
87
+ }
88
+ }
89
+
90
+ // A self-hosted application for the unsubscribe page alone. It is not shown
91
+ // in the App Launcher. Its bypass policy lets everyone through, so
92
+ // recipients who are not signed in reach the page. A path application is
93
+ // more specific than the app's own, so it wins for /u/*.
94
+ function unsubscribeApplicationBody(hostname: string): unknown {
95
+ return {
96
+ app_launcher_visible: false,
97
+ domain: unsubscribeDomain(hostname),
98
+ name: unsubscribeApplicationName,
99
+ type: 'self_hosted'
100
+ }
101
+ }
102
+
103
+ // A self-hosted application with a one month session. The session cookie is
104
+ // SameSite=Lax, so another site cannot make a signed-in browser post to the
105
+ // API with it.
106
+ function applicationBody(hostname: string): unknown {
107
+ return {
108
+ domain: hostname,
109
+ name: mailboxApplicationName,
110
+ same_site_cookie_attribute: 'lax',
111
+ session_duration: sessionDuration,
112
+ type: 'self_hosted'
113
+ }
114
+ }
115
+
116
+ function encodePolicy(policy: AccessPolicyInput): unknown {
117
+ return {
118
+ decision: policy.decision,
119
+ include: policy.include.map(encodeRule),
120
+ name: policy.name
121
+ }
122
+ }
123
+
124
+ export function accessMethods(
125
+ api: LiveApi
126
+ ): Pick<
127
+ CloudflareShape,
128
+ | 'accessApplicationCoverage'
129
+ | 'accessApplicationSettings'
130
+ | 'accessPolicies'
131
+ | 'accessPolicyEmails'
132
+ | 'accessTeamDomain'
133
+ | 'createAccessApplication'
134
+ | 'createAccessPolicy'
135
+ | 'createServiceToken'
136
+ | 'createUnsubscribeApplication'
137
+ | 'deleteAccessApplication'
138
+ | 'deleteAccessPolicy'
139
+ | 'deleteServiceToken'
140
+ | 'findAccessApplication'
141
+ | 'mailboxApplications'
142
+ | 'revokeAccessSessions'
143
+ | 'serviceTokens'
144
+ | 'unsubscribeApplications'
145
+ | 'updateAccessApplication'
146
+ | 'updateAccessPolicy'
147
+ > {
148
+ const accessError = accountError(
149
+ 'Access applications',
150
+ accountPermissions.accessApplications
151
+ )
152
+ const tokensError = accountError(
153
+ 'Access service tokens',
154
+ accountPermissions.accessServiceTokens
155
+ )
156
+ const apps = `${api.account}/access/apps`
157
+
158
+ // Exact hostname, all pages.
159
+ const applicationsFor = <Item extends Schema.Constraint>(
160
+ item: Item,
161
+ hostname: string
162
+ ) =>
163
+ api.getAll(apps, item, 'Access applications', {
164
+ domain: hostname,
165
+ exact: 'true'
166
+ })
167
+
168
+ const readApplication = (applicationId: string) =>
169
+ api
170
+ .get(`${apps}/${segment(applicationId)}`, applicationRecord)
171
+ .pipe(Effect.map((page) => page.result))
172
+
173
+ return {
174
+ // GET /accounts/:account/access/apps/:app
175
+ accessApplicationCoverage: (applicationId) =>
176
+ readApplication(applicationId).pipe(
177
+ Effect.map((record) => Option.some(coverageOf(record))),
178
+ Effect.catchIf(
179
+ (error) => error.kind === 'notFound',
180
+ () => Effect.succeed(Option.none<ApplicationCoverage>())
181
+ ),
182
+ Effect.mapError(accessError)
183
+ ),
184
+
185
+ // GET /accounts/:account/access/apps?domain=<hostname>&exact=true, then
186
+ // GET /accounts/:account/access/apps/:app for its settings.
187
+ accessApplicationSettings: (hostname) =>
188
+ Effect.gen(function* () {
189
+ const applications = yield* applicationsFor(applicationForPlan, hostname)
190
+ const found = applications.find(
191
+ (candidate) => candidate.domain === hostname
192
+ )
193
+
194
+ if (found === undefined) {
195
+ return Option.none()
196
+ }
197
+
198
+ const record = yield* readApplication(found.id)
199
+ const cookie = record['same_site_cookie_attribute']
200
+
201
+ return Option.some({
202
+ id: found.id,
203
+ sameSiteCookie:
204
+ cookie === undefined
205
+ ? Option.none()
206
+ : Option.some(typeof cookie === 'string' ? cookie : null)
207
+ })
208
+ }).pipe(Effect.mapError(accessError)),
209
+
210
+ // GET /accounts/:account/access/apps/:app/policies
211
+ accessPolicies: (applicationId) =>
212
+ api
213
+ .getAll(
214
+ `${apps}/${segment(applicationId)}/policies`,
215
+ policyWithName,
216
+ 'Access policies'
217
+ )
218
+ .pipe(
219
+ Effect.map((policies) =>
220
+ policies.map((policy) => ({
221
+ decision: policy.decision,
222
+ id: policy.id,
223
+ name: policy.name,
224
+ serviceTokenIds: (policy.include ?? []).flatMap((item) =>
225
+ item.service_token === undefined
226
+ ? []
227
+ : [item.service_token.token_id]
228
+ )
229
+ }))
230
+ ),
231
+ Effect.mapError(accessError)
232
+ ),
233
+
234
+ // GET /accounts/:account/access/apps?domain=<hostname>&exact=true, then
235
+ // GET /accounts/:account/access/apps/:app/policies. Both paged.
236
+ accessPolicyEmails: () =>
237
+ Effect.gen(function* () {
238
+ const applications = yield* applicationsFor(
239
+ applicationForPlan,
240
+ api.hostname
241
+ )
242
+ const found = applications.find(
243
+ (candidate) => candidate.domain === api.hostname
244
+ )
245
+
246
+ if (found === undefined) {
247
+ return []
248
+ }
249
+
250
+ const policies = yield* api.getAll(
251
+ `${apps}/${segment(found.id)}/policies`,
252
+ policyForPlan,
253
+ 'Access policies'
254
+ )
255
+
256
+ // Only the policy deploy writes. An allow policy added by hand stays
257
+ // as it is, so reading it would show the same removals forever.
258
+ return policies
259
+ .filter(
260
+ (policy) =>
261
+ policy.decision === 'allow' && policy.name === peoplePolicyName
262
+ )
263
+ .flatMap((policy) => policy.include ?? [])
264
+ .flatMap((item) => {
265
+ const email =
266
+ item.email === undefined
267
+ ? undefined
268
+ : decodeEmailAddress(item.email.email)
269
+
270
+ return email?._tag === 'Some' ? [email.value] : []
271
+ })
272
+ }).pipe(Effect.mapError(accountNotFound)),
273
+
274
+ // GET /accounts/:account/access/organizations
275
+ accessTeamDomain: () =>
276
+ api.get(`${api.account}/access/organizations`, organization).pipe(
277
+ Effect.map((page) => page.result.auth_domain),
278
+ Effect.mapError((error) =>
279
+ error.kind === 'forbidden'
280
+ ? new CloudflareError({
281
+ kind: 'forbidden',
282
+ message: `The Cloudflare API token cannot read the Access organization. Add the permission ${accountPermissions.accessOrganization} to the token and run deploy again.`
283
+ })
284
+ : accountNotFound(error)
285
+ )
286
+ ),
287
+
288
+ // POST /accounts/:account/access/apps
289
+ createAccessApplication: (hostname) =>
290
+ api
291
+ .write('POST', apps, applicationBody(hostname), application)
292
+ .pipe(
293
+ Effect.map((created) => ({ audience: created.aud, id: created.id })),
294
+ Effect.mapError(accessError)
295
+ ),
296
+
297
+ // POST /accounts/:account/access/apps/:app/policies
298
+ createAccessPolicy: (applicationId, policy) =>
299
+ api
300
+ .write(
301
+ 'POST',
302
+ `${apps}/${segment(applicationId)}/policies`,
303
+ encodePolicy(policy),
304
+ Schema.Unknown
305
+ )
306
+ .pipe(Effect.asVoid, Effect.mapError(accessError)),
307
+
308
+ // POST /accounts/:account/access/service_tokens. Never expires: the
309
+ // deploy credentials stop working silently otherwise.
310
+ createServiceToken: (name) =>
311
+ api
312
+ .write(
313
+ 'POST',
314
+ `${api.account}/access/service_tokens`,
315
+ { duration: 'forever', name },
316
+ serviceToken
317
+ )
318
+ .pipe(
319
+ Effect.map((created) => ({
320
+ clientId: created.client_id,
321
+ clientSecret: created.client_secret,
322
+ id: created.id
323
+ })),
324
+ Effect.mapError(tokensError)
325
+ ),
326
+
327
+ // POST /accounts/:account/access/apps
328
+ createUnsubscribeApplication: (hostname) =>
329
+ api
330
+ .write('POST', apps, unsubscribeApplicationBody(hostname), application)
331
+ .pipe(
332
+ Effect.map((created) => ({ audience: created.aud, id: created.id })),
333
+ Effect.mapError(accessError)
334
+ ),
335
+
336
+ // DELETE /accounts/:account/access/apps/:app
337
+ deleteAccessApplication: (applicationId) =>
338
+ api.remove(`${apps}/${segment(applicationId)}`).pipe(Effect.mapError(accessError)),
339
+
340
+ // DELETE /accounts/:account/access/apps/:app/policies/:policy
341
+ deleteAccessPolicy: (applicationId, policy) =>
342
+ api
343
+ .remove(`${apps}/${segment(applicationId)}/policies/${segment(policy)}`)
344
+ .pipe(Effect.mapError(accessError)),
345
+
346
+ // DELETE /accounts/:account/access/service_tokens/:token
347
+ deleteServiceToken: (token) =>
348
+ api
349
+ .remove(`${api.account}/access/service_tokens/${segment(token)}`)
350
+ .pipe(Effect.mapError(tokensError)),
351
+
352
+ // GET /accounts/:account/access/apps?domain=<hostname>&exact=true
353
+ findAccessApplication: (hostname) =>
354
+ applicationsFor(application, hostname).pipe(
355
+ Effect.map((applications) =>
356
+ Option.fromNullishOr(
357
+ applications.find((candidate) => candidate.domain === hostname)
358
+ ).pipe(
359
+ Option.map((found) => ({ audience: found.aud, id: found.id }))
360
+ )
361
+ ),
362
+ Effect.mapError(accessError)
363
+ ),
364
+
365
+ // GET /accounts/:account/access/apps, all pages: ours by name, without
366
+ // a path.
367
+ mailboxApplications: () =>
368
+ api.getAll(apps, applicationNamed, 'Access applications').pipe(
369
+ Effect.map((applications) =>
370
+ applications.flatMap((candidate) =>
371
+ candidate.name === mailboxApplicationName &&
372
+ candidate.domain !== undefined &&
373
+ candidate.domain !== '' &&
374
+ !candidate.domain.includes('/')
375
+ ? [{ domain: candidate.domain, id: candidate.id }]
376
+ : []
377
+ )
378
+ ),
379
+ Effect.mapError(accessError)
380
+ ),
381
+
382
+ // POST /accounts/:account/access/organizations/revoke_user
383
+ revokeAccessSessions: (email) =>
384
+ api
385
+ .write(
386
+ 'POST',
387
+ `${api.account}/access/organizations/revoke_user`,
388
+ { email },
389
+ Schema.Unknown
390
+ )
391
+ .pipe(Effect.asVoid, Effect.mapError(accessError)),
392
+
393
+ // GET /accounts/:account/access/service_tokens
394
+ serviceTokens: () =>
395
+ api
396
+ .getAll(
397
+ `${api.account}/access/service_tokens`,
398
+ serviceTokenItem,
399
+ 'Access service tokens'
400
+ )
401
+ .pipe(
402
+ Effect.map((tokens) =>
403
+ tokens.map((token) => ({
404
+ clientId: token.client_id,
405
+ id: token.id,
406
+ name: token.name
407
+ }))
408
+ ),
409
+ Effect.mapError(tokensError)
410
+ ),
411
+
412
+ // GET /accounts/:account/access/apps, all pages: ours by name and path.
413
+ unsubscribeApplications: () =>
414
+ api.getAll(apps, applicationNamed, 'Access applications').pipe(
415
+ Effect.map((applications) =>
416
+ applications.flatMap((candidate) =>
417
+ candidate.name === unsubscribeApplicationName &&
418
+ candidate.domain !== undefined &&
419
+ candidate.domain.endsWith('/u/*')
420
+ ? [{ domain: candidate.domain, id: candidate.id }]
421
+ : []
422
+ )
423
+ ),
424
+ Effect.mapError(accessError)
425
+ ),
426
+
427
+ // GET, then PUT /accounts/:account/access/apps/:app. The API has no
428
+ // partial update, so the application is read and its writable fields
429
+ // are sent back, with only the session cookie changed. Settings changed
430
+ // by hand stay. Policies are not sent; they stay attached.
431
+ updateAccessApplication: (applicationId) =>
432
+ Effect.gen(function* () {
433
+ const record = yield* readApplication(applicationId)
434
+
435
+ yield* api.write(
436
+ 'PUT',
437
+ `${apps}/${segment(applicationId)}`,
438
+ applicationUpdateBody(record),
439
+ Schema.Unknown
440
+ )
441
+ }).pipe(Effect.mapError(accessError)),
442
+
443
+ // PUT /accounts/:account/access/apps/:app/policies/:policy
444
+ updateAccessPolicy: (applicationId, policy, input) =>
445
+ api
446
+ .write(
447
+ 'PUT',
448
+ `${apps}/${segment(applicationId)}/policies/${segment(policy)}`,
449
+ encodePolicy(input),
450
+ Schema.Unknown
451
+ )
452
+ .pipe(Effect.asVoid, Effect.mapError(accessError))
453
+ }
454
+ }
@@ -0,0 +1,95 @@
1
+ import { Effect, Option, Schema } from 'effect'
2
+
3
+ import type { CloudflareShape } from './cloudflare-client'
4
+ import { accountError, segment } from './cloudflare-live-api'
5
+ import type { LiveApi } from './cloudflare-live-api'
6
+ import { accountPermissions } from '../permissions'
7
+
8
+ const databaseItem = Schema.Struct({ name: Schema.String, uuid: Schema.String })
9
+ const bucketItem = Schema.Struct({ name: Schema.String })
10
+
11
+ export function accountMethods(
12
+ api: LiveApi
13
+ ): Pick<
14
+ CloudflareShape,
15
+ | 'createBucket'
16
+ | 'createDatabase'
17
+ | 'findBucket'
18
+ | 'findDatabase'
19
+ | 'workerExists'
20
+ > {
21
+ const databasesError = accountError(
22
+ 'D1 databases',
23
+ accountPermissions.databases
24
+ )
25
+ const bucketsError = accountError('R2 buckets', accountPermissions.buckets)
26
+
27
+ return {
28
+ // POST /accounts/:account/r2/buckets
29
+ createBucket: (name) =>
30
+ api
31
+ .write('POST', `${api.account}/r2/buckets`, { name }, Schema.Unknown)
32
+ .pipe(Effect.asVoid, Effect.mapError(bucketsError)),
33
+
34
+ // POST /accounts/:account/d1/database
35
+ createDatabase: (name) =>
36
+ api
37
+ .write(
38
+ 'POST',
39
+ `${api.account}/d1/database`,
40
+ { name },
41
+ Schema.Struct({ uuid: Schema.String })
42
+ )
43
+ .pipe(
44
+ Effect.map((created) => created.uuid),
45
+ Effect.mapError(databasesError)
46
+ ),
47
+
48
+ // GET /accounts/:account/r2/buckets/:name, 404 when there is none. The
49
+ // name is exact by the path.
50
+ findBucket: (name) =>
51
+ api.get(`${api.account}/r2/buckets/${segment(name)}`, bucketItem).pipe(
52
+ Effect.map(() => true),
53
+ Effect.catch((error) =>
54
+ error.kind === 'notFound' ? Effect.succeed(false) : Effect.fail(error)
55
+ ),
56
+ Effect.mapError(bucketsError)
57
+ ),
58
+
59
+ // GET /accounts/:account/d1/database?name=<name>, all pages, exact name
60
+ findDatabase: (name) =>
61
+ api
62
+ .getAll(
63
+ `${api.account}/d1/database`,
64
+ databaseItem,
65
+ 'D1 databases',
66
+ { name }
67
+ )
68
+ .pipe(
69
+ Effect.map((databases) =>
70
+ Option.fromNullishOr(
71
+ databases.find((candidate) => candidate.name === name)
72
+ ).pipe(Option.map((found) => found.uuid))
73
+ ),
74
+ Effect.mapError(databasesError)
75
+ ),
76
+
77
+ // GET /accounts/:account/workers/scripts/:name/settings, 404 when there
78
+ // is no Worker with that name
79
+ workerExists: (name) =>
80
+ api
81
+ .get(
82
+ `${api.account}/workers/scripts/${segment(name)}/settings`,
83
+ Schema.Unknown
84
+ )
85
+ .pipe(
86
+ Effect.map(() => true),
87
+ Effect.catch((error) =>
88
+ error.kind === 'notFound'
89
+ ? Effect.succeed(false)
90
+ : Effect.fail(error)
91
+ ),
92
+ Effect.mapError(accountError('Workers', accountPermissions.workers))
93
+ )
94
+ }
95
+ }
@@ -0,0 +1,87 @@
1
+ import type { Effect, Schema } from 'effect'
2
+
3
+ import { CloudflareError } from './cloudflare-client'
4
+
5
+ export interface CloudflarePage<Value> {
6
+ result: Value
7
+ totalPages: number
8
+ }
9
+
10
+ export const paidPlanMessage =
11
+ "Sending mail needs the Workers Paid plan ($5 a month). Upgrade the account in the Cloudflare dashboard, then run deploy again. Courier Crow needs that plan for D1's query limits too: without it the daily clean-up and received mail with many attachments fail."
12
+
13
+ // Lets one call say what an error means. Returns undefined to leave the
14
+ // error to the general rules.
15
+ export type Interpret = (
16
+ status: number,
17
+ messages: readonly string[]
18
+ ) => CloudflareError | undefined
19
+
20
+ // The HTTP calls the live methods are built from. Every call has a 30 second
21
+ // timeout and its response decoded.
22
+ export interface LiveApi {
23
+ // /accounts/<id>
24
+ account: string
25
+ accountId: string
26
+ // The hostname of the Courier Crow app.
27
+ hostname: string
28
+ get: <Value extends Schema.Constraint>(
29
+ path: string,
30
+ schema: Value,
31
+ urlParams?: Record<string, string>
32
+ ) => Effect.Effect<
33
+ CloudflarePage<Value['Type']>,
34
+ CloudflareError,
35
+ Value['DecodingServices']
36
+ >
37
+ // A list endpoint, read page by page. `what` names the list in the error
38
+ // for a list that never ends.
39
+ getAll: <Item extends Schema.Constraint>(
40
+ path: string,
41
+ item: Item,
42
+ what: string,
43
+ urlParams?: Record<string, string>
44
+ ) => Effect.Effect<
45
+ Item['Type'][],
46
+ CloudflareError,
47
+ Item['DecodingServices']
48
+ >
49
+ remove: (path: string) => Effect.Effect<void, CloudflareError>
50
+ write: <Value extends Schema.Constraint>(
51
+ method: 'POST' | 'PUT',
52
+ path: string,
53
+ body: unknown,
54
+ schema: Value,
55
+ interpret?: Interpret
56
+ ) => Effect.Effect<
57
+ Value['Type'],
58
+ CloudflareError,
59
+ Value['DecodingServices']
60
+ >
61
+ }
62
+
63
+ // Ids go into URL paths.
64
+ export function segment(value: string): string {
65
+ return encodeURIComponent(value)
66
+ }
67
+
68
+ export function accountNotFound(error: CloudflareError): CloudflareError {
69
+ return error.kind === 'notFound'
70
+ ? new CloudflareError({
71
+ kind: 'notFound',
72
+ message:
73
+ 'Cloudflare could not find the account (HTTP 404). Check CLOUDFLARE_ACCOUNT_ID.'
74
+ })
75
+ : error
76
+ }
77
+
78
+ // A forbidden answer names what could not be managed and the permission.
79
+ export const accountError =
80
+ (what: string, permission: string) =>
81
+ (error: CloudflareError): CloudflareError =>
82
+ error.kind === 'forbidden'
83
+ ? new CloudflareError({
84
+ kind: 'forbidden',
85
+ message: `The Cloudflare API token cannot manage ${what} in this account. Add the permission ${permission} to the token and run deploy again.`
86
+ })
87
+ : accountNotFound(error)