@easy-cms/core 0.44.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +325 -24
  2. package/dist/index.js +597 -95
  3. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -12,6 +12,11 @@ interface AuthUser {
12
12
  * backups, roles, other users' API keys).
13
13
  */
14
14
  readonly scoped?: boolean;
15
+ /**
16
+ * A member of the site, e.g. a customer (`auth.members.roles`): signs in on the site, never to
17
+ * the admin, and `isLoggedIn` doesn't count them.
18
+ */
19
+ readonly member?: boolean;
15
20
  readonly [field: string]: unknown;
16
21
  }
17
22
  /**
@@ -71,7 +76,13 @@ interface FieldAccess {
71
76
  readonly update?: FieldAccessFn;
72
77
  }
73
78
  declare const anyone: Access;
79
+ /**
80
+ * A signed-in user of the admin: not a site member (`auth.members`). What collections allow by
81
+ * default.
82
+ */
74
83
  declare const isLoggedIn: Access;
84
+ /** Anyone signed in, site members (e.g. customers) included. */
85
+ declare const isSignedIn: Access;
75
86
  declare const isAdmin: Access;
76
87
 
77
88
  /** What a preview token opens: one document of a collection, or one global. */
@@ -81,8 +92,11 @@ type PreviewTarget = {
81
92
  } | {
82
93
  readonly global: string;
83
94
  };
84
- /** Why a password link was sent: a forgotten password, or an invitation to a new account. */
85
- type PasswordPurpose = 'reset' | 'invite';
95
+ /**
96
+ * Why a link was sent: a forgotten password, an invitation to a new account, or (`verify`) to
97
+ * confirm the email of an account made by signing up.
98
+ */
99
+ type PasswordPurpose = 'reset' | 'invite' | 'verify';
86
100
 
87
101
  /** The cookie that carries a sign-in through the provider and back. */
88
102
  declare const SSO_COOKIE = "ecms-sso";
@@ -195,6 +209,29 @@ interface Session {
195
209
  readonly csrfToken: string;
196
210
  readonly expiresAt: string;
197
211
  }
212
+ interface SignupArgs {
213
+ readonly email: string;
214
+ readonly password: string;
215
+ readonly name?: string | undefined;
216
+ /** From `GET <api>/users/signup`: the form was loaded a moment ago. */
217
+ readonly token?: unknown;
218
+ /** The hidden field; bots fill it in. */
219
+ readonly honeypot?: unknown;
220
+ /** The Cloudflare Turnstile response, with `signup.turnstile`. */
221
+ readonly turnstile?: unknown;
222
+ readonly ip?: string | undefined;
223
+ /** The request's origin: used for the link only in development, without a site URL. */
224
+ readonly origin?: string | undefined;
225
+ readonly locale?: string | undefined;
226
+ /** The request's context (`onRequest`), e.g. the tenant: given to the users' hooks. */
227
+ readonly context?: RequestContext;
228
+ }
229
+ /** What signing up did: a link was emailed to confirm, or (without `verifyEmail`) a session. */
230
+ type SignupResult = {
231
+ readonly verify: true;
232
+ } | (Session & {
233
+ readonly verify: false;
234
+ });
198
235
  /** Where a password link is opened from, and the language of its email. */
199
236
  interface PasswordLinkOptions {
200
237
  /** The request's origin: used for the link only in development, when `serverURL` is not set. */
@@ -223,6 +260,29 @@ declare class Auth {
223
260
  setupCode?: string;
224
261
  ip?: string | undefined;
225
262
  }): Promise<Session>;
263
+ /** What a sign-up form needs: a token proving when it was loaded, and Turnstile's site key. */
264
+ signupForm(): {
265
+ token: string;
266
+ turnstile: string | null;
267
+ verifyEmail: boolean;
268
+ };
269
+ /**
270
+ * Creates a member's account (`auth.members.signup`). With `verifyEmail` (the default), emails
271
+ * a link to confirm the address and answers the same whether or not the email has an account;
272
+ * otherwise starts a session. Throws `ValidationError` for spam checks and bad input.
273
+ */
274
+ signup(args: SignupArgs): Promise<SignupResult>;
275
+ /** Confirms a member's email from the link, and signs them in. */
276
+ verifyEmail(args: {
277
+ token: string;
278
+ ip?: string | undefined;
279
+ }): Promise<Session>;
280
+ private mailVerifyLink;
281
+ /**
282
+ * A link to one of the site's pages for members (`auth.members.pages`), else to the admin's
283
+ * page of the same purpose.
284
+ */
285
+ private memberLink;
226
286
  hasUsers(): Promise<boolean>;
227
287
  /** Ends the session for a signed token. Unknown tokens are ignored. */
228
288
  logout(signedToken: string): Promise<void>;
@@ -374,12 +434,35 @@ interface Database {
374
434
  collection: string;
375
435
  data: Record<string, unknown>;
376
436
  }): Promise<RawDocument>;
377
- /** `data` holds the whole document after merging. Runs in one transaction. */
437
+ /**
438
+ * `data` holds the whole document after merging. Runs in one transaction. With `where`, only
439
+ * when the stored document matches it at the moment of writing (compare and set): `null` when
440
+ * it doesn't, and nothing is written.
441
+ */
378
442
  update(args: {
379
443
  collection: string;
380
444
  id: ID;
381
445
  data: Record<string, unknown>;
382
446
  }): Promise<RawDocument>;
447
+ update(args: {
448
+ collection: string;
449
+ id: ID;
450
+ data: Record<string, unknown>;
451
+ where: Where;
452
+ }): Promise<RawDocument | null>;
453
+ /**
454
+ * Adds `by` to a top-level number field in one statement, safe against concurrent calls; an
455
+ * empty value counts as 0. With `min` / `max`, only when the result stays within them. Returns
456
+ * the new value, or `null` when it would leave the bounds or the document is gone.
457
+ */
458
+ increment(args: {
459
+ collection: string;
460
+ id: ID;
461
+ field: string;
462
+ by: number;
463
+ min?: number | undefined;
464
+ max?: number | undefined;
465
+ }): Promise<number | null>;
383
466
  delete(args: {
384
467
  collection: string;
385
468
  id: ID;
@@ -623,8 +706,13 @@ type WithPluginFields<C extends Config, T extends {
623
706
  }, Key extends 'fields' | 'globalFields'> = [PluginFields<C, T['slug'], Key>] extends [never] ? T : Omit<T, 'fields'> & {
624
707
  readonly fields: readonly (T['fields'][number] | PluginFields<C, T['slug'], Key>)[];
625
708
  };
709
+ /**
710
+ * The config's own collections. A literal config without `collections` has none (only plugins
711
+ * add some); a loosely typed `Config` has any.
712
+ */
713
+ type OwnCollections<C extends Config> = 'collections' extends keyof C ? NonNullable<C['collections']>[number] : never;
626
714
  /** Every collection of a config, including the built-in ones and those plugins add. */
627
- type AllCollections<C extends Config> = NonNullable<C['collections']>[number] | BuiltinUsersCollection | BuiltinMediaCollection | ApiKeysOf<C> | FoldersOf<C> | PluginCollections<C>;
715
+ type AllCollections<C extends Config> = OwnCollections<C> | BuiltinUsersCollection | BuiltinMediaCollection | ApiKeysOf<C> | FoldersOf<C> | PluginCollections<C>;
628
716
  /** The built-in media collection; its documents are typed as `MediaDocument`. */
629
717
  interface BuiltinMediaCollection {
630
718
  readonly slug: 'media';
@@ -766,7 +854,7 @@ type InferGlobal<T extends FieldsOwner, C extends Config = Config> = Simplify<
766
854
  } ? {
767
855
  status: 'draft' | 'published';
768
856
  } : unknown) & FieldsValue<T['fields'], C>>;
769
- type CollectionSlug<C extends Config> = NonNullable<C['collections']>[number]['slug'] | 'users' | 'media' | ApiKeysOf<C>['slug'] | FoldersOf<C>['slug'] | PluginCollections<C>['slug'];
857
+ type CollectionSlug<C extends Config> = OwnCollections<C>['slug'] | 'users' | 'media' | ApiKeysOf<C>['slug'] | FoldersOf<C>['slug'] | PluginCollections<C>['slug'];
770
858
  type GlobalSlug<C extends Config> = NonNullable<C['globals']>[number]['slug'];
771
859
  /** Document type of a collection, e.g. `CollectionDocument<typeof config, 'posts'>`. */
772
860
  type CollectionDocument<C extends Config, S extends CollectionSlug<C>> = S extends 'media' ? MediaDocument : InferCollection<CollectionBySlug<C, S>, C>;
@@ -1087,9 +1175,15 @@ declare class MediaFolders {
1087
1175
  * The folder with this key (`folder: 'banners'` on upload fields), made at the top level when
1088
1176
  * there is none yet.
1089
1177
  */
1090
- keyed(key: string): Promise<ID>;
1178
+ /**
1179
+ * The folder with this key, made when first needed. `scope`: the values the key is unique
1180
+ * within (`uniqueWithin` of the `key` field, e.g. the tenant), given to a new folder too.
1181
+ * `null` when there is none and `create` is false.
1182
+ */
1183
+ keyed(key: string, scope?: Readonly<Record<string, unknown>>): Promise<ID>;
1184
+ keyed(key: string, scope: Readonly<Record<string, unknown>>, create: boolean): Promise<ID | null>;
1091
1185
  /** Whether a file in `folder` is in the folder with this key, or below it. */
1092
- inKeyed(key: string, folder: ID | null): Promise<boolean>;
1186
+ inKeyed(key: string, folder: ID | null, scope?: Readonly<Record<string, unknown>>): Promise<boolean>;
1093
1187
  /** The user's level in a folder (`null`: the top level). */
1094
1188
  level(user: AuthUser | null, id: ID | null): Promise<FolderLevel | 'none'>;
1095
1189
  /**
@@ -1231,6 +1325,18 @@ interface UpdateOptions extends DepthOptions {
1231
1325
  * stays, and no version is added to the history.
1232
1326
  */
1233
1327
  readonly live?: boolean;
1328
+ /**
1329
+ * Saves only when the stored document still matches this at the moment of writing (compare and
1330
+ * set), e.g. `{ status: { equals: 'pending' } }` so that one of two concurrent calls wins.
1331
+ * `update()` then returns `null` when it doesn't match, after its `before*` hooks ran.
1332
+ */
1333
+ readonly where?: Where;
1334
+ }
1335
+ interface IncrementOptions {
1336
+ /** Only when the result stays at or above this, e.g. `0` for stock. */
1337
+ readonly min?: number;
1338
+ /** Only when the result stays at or below this. */
1339
+ readonly max?: number;
1234
1340
  }
1235
1341
  interface ReadOptions extends DepthOptions {
1236
1342
  /**
@@ -1255,8 +1361,8 @@ interface CreateEasyCMSOptions {
1255
1361
  readonly cwd?: string;
1256
1362
  /**
1257
1363
  * Run due scheduled jobs and webhook retries every minute in this process. Default: on when
1258
- * a collection or global has `schedule`, or `webhooks` is set. Turn it off where a cron calls
1259
- * `runJobs` (or `GET <api>/jobs/run`) instead.
1364
+ * a collection or global has `schedule`, or `webhooks`, `email` or `jobs` is set. Turn it off
1365
+ * where a cron calls `runJobs` (or `GET <api>/jobs/run`) instead.
1260
1366
  */
1261
1367
  readonly scheduler?: boolean;
1262
1368
  /** Default `verify` when `NODE_ENV=production`, otherwise `push`. */
@@ -1420,7 +1526,18 @@ declare class EasyCMS<C extends Config = Config> {
1420
1526
  /** Files at the top level are public. */
1421
1527
  private settleTopLevel;
1422
1528
  private settleFiles;
1423
- update<S extends Slug<C>>(collection: S, id: ID, data: Update<C, S>, options?: UpdateOptions): Promise<Doc<C, S>>;
1529
+ update<S extends Slug<C>>(collection: S, id: ID, data: Update<C, S>, options: UpdateOptions & {
1530
+ where: Where;
1531
+ }): Promise<Doc<C, S> | null>;
1532
+ update<S extends Slug<C>>(collection: S, id: ID, data: Update<C, S>, options?: UpdateOptions & {
1533
+ where?: undefined;
1534
+ }): Promise<Doc<C, S>>;
1535
+ /**
1536
+ * Adds `by` (negative to take away) to a top-level number field in one database statement, so
1537
+ * concurrent calls never lose a change, e.g. stock or a counter. Skips hooks, validation, access
1538
+ * and versions. Returns the new value, or `null` when `min` / `max` would be passed.
1539
+ */
1540
+ increment<S extends Slug<C>>(collection: S, id: ID, field: string, by: number, options?: IncrementOptions): Promise<number | null>;
1424
1541
  /**
1425
1542
  * Takes a document off the site: its status becomes `draft`. With versions and drafts, a plain
1426
1543
  * `update` with `status: 'draft'` only saves a draft and leaves the published document live;
@@ -1550,7 +1667,21 @@ declare class EasyCMS<C extends Config = Config> {
1550
1667
  sent: number;
1551
1668
  failed: number;
1552
1669
  };
1670
+ jobs: {
1671
+ ran: number;
1672
+ failed: number;
1673
+ };
1553
1674
  }>;
1675
+ /** `jobs` of the config that are due; when each last ran is kept in the database. */
1676
+ private runConfigJobs;
1677
+ /**
1678
+ * Sends an event of the app or a plugin (`events` in the config, e.g. `order.paid`) to the
1679
+ * webhooks that list it. Returns at once; deliveries are queued and retried like content changes.
1680
+ */
1681
+ emit(event: string, data: Record<string, unknown>, about?: {
1682
+ collection: string;
1683
+ id: ID;
1684
+ }): void;
1554
1685
  /**
1555
1686
  * Tries webhook deliveries that are due for another attempt. Deliveries are kept in the
1556
1687
  * database until they succeed, so they survive restarts.
@@ -1648,7 +1779,7 @@ declare class EasyCMS<C extends Config = Config> {
1648
1779
  /** Replaces published documents that have a newer draft with that draft. */
1649
1780
  private withDrafts;
1650
1781
  /** Sends webhook events for a change; `before` is the stored status before it, if any. */
1651
- private emit;
1782
+ private emitChange;
1652
1783
  /**
1653
1784
  * Writes the audit log entry for a change (`audit`): publishing, unpublishing and restoring
1654
1785
  * are told apart from other saves.
@@ -1673,6 +1804,23 @@ declare class EasyCMS<C extends Config = Config> {
1673
1804
  private makeSlugsUnique;
1674
1805
  private checkUnique;
1675
1806
  private isTaken;
1807
+ /**
1808
+ * What folder keys are unique within for a call (`uniqueWithin` of the `key` field of media
1809
+ * folders, e.g. the tenant): the value a folder made by this call would get from its hooks.
1810
+ */
1811
+ private folderScope;
1812
+ /**
1813
+ * Where a unique field's values must differ for a call (`uniqueWithin`, e.g. the tenant): the
1814
+ * value a document created by this call would get there from its hooks, as `{ [field]: value }`;
1815
+ * `{}` when the field is unique everywhere. For finding documents by such a field, e.g. a page
1816
+ * by its path in the current tenant.
1817
+ */
1818
+ uniqueScope(collection: string, field: string, options?: Pick<AccessOptions, 'user' | 'context'>): Promise<Record<string, unknown>>;
1819
+ /**
1820
+ * @internal The id of the media folder with this key (`folder` of upload fields), made when
1821
+ * needed; `null` when the call has no scope to make it in (e.g. all tenants) and none exists.
1822
+ */
1823
+ keyedFolder(key: string, options?: AccessOptions): Promise<ID | null>;
1676
1824
  /** Each reference is one its relationship's `filterOptions` allow. */
1677
1825
  private checkFilterOptions;
1678
1826
  private checkReferences;
@@ -1711,6 +1859,23 @@ interface FieldAdmin {
1711
1859
  readonly after?: readonly AdminComponent[];
1712
1860
  /** Shows the value in the admin's lists (e.g. a color swatch). Default: the plain value. */
1713
1861
  readonly cell?: AdminComponent;
1862
+ /**
1863
+ * A column of the list at first (each user can change it): `true`, or per request, e.g.
1864
+ * only when the admin shows every tenant.
1865
+ */
1866
+ readonly column?: boolean | ((args: AdminFieldArgs) => boolean | Promise<boolean>);
1867
+ /** Relationships: offer to create the related document in place. Default true. */
1868
+ readonly allowCreate?: boolean;
1869
+ /**
1870
+ * The value a new document's form starts with, worked out for each user, e.g. from the
1871
+ * request's context. The server's own default (`defaultValue`, hooks) still applies.
1872
+ */
1873
+ readonly defaultValue?: (args: AdminFieldArgs) => unknown;
1874
+ }
1875
+ /** What per-request admin options of a field receive. */
1876
+ interface AdminFieldArgs {
1877
+ readonly user: AuthUser;
1878
+ readonly context: RequestContext;
1714
1879
  }
1715
1880
  /**
1716
1881
  * Field types that packages add (`fieldTypes` in the config), for the types inferred from the
@@ -1741,11 +1906,11 @@ interface BaseField<TType extends string, TValue> {
1741
1906
  readonly required?: boolean;
1742
1907
  readonly unique?: boolean;
1743
1908
  /**
1744
- * With `unique` (or on a slug field): the name of a sibling field (e.g. `parent` or `tenant`)
1909
+ * With `unique` (or on a slug field): a sibling field (e.g. `parent` or `tenant`), or several,
1745
1910
  * whose documents only need to differ among themselves, so `/about/team` and `/careers/team`
1746
- * can both be `team`. A single, unlocalized relationship, select, text or number field.
1911
+ * can both be `team`. Single, unlocalized relationship, select, text or number fields.
1747
1912
  */
1748
- readonly uniqueWithin?: string;
1913
+ readonly uniqueWithin?: string | readonly string[];
1749
1914
  readonly index?: boolean;
1750
1915
  readonly defaultValue?: TValue;
1751
1916
  readonly validate?: FieldValidate<TValue>;
@@ -1825,6 +1990,8 @@ interface UploadField extends BaseField<'upload', never> {
1825
1990
  readonly folder?: string;
1826
1991
  /** With `folder`: only files in that folder (and its subfolders) may be chosen. */
1827
1992
  readonly folderOnly?: boolean;
1993
+ /** Which media documents may be chosen, as for relationships: offered and checked on save. */
1994
+ readonly filterOptions?: FilterOptions;
1828
1995
  }
1829
1996
  interface FilterOptionsArgs {
1830
1997
  /** The document being edited; `undefined` while it is being created. */
@@ -1876,6 +2043,8 @@ interface BlocksField extends BaseField<'blocks', never> {
1876
2043
  readonly minRows?: number;
1877
2044
  readonly maxRows?: number;
1878
2045
  }
2046
+ /** The fields `uniqueWithin` names, as a list. */
2047
+ declare const uniqueWithinOf: (field: Field) => readonly string[];
1879
2048
  /** Fields with `hasMany`: their value is a list of options, documents or files. */
1880
2049
  declare const isHasMany: (field: Field) => boolean;
1881
2050
  /** Whether a MIME type is one of `patterns` (`image/*` matches every image). */
@@ -1896,6 +2065,12 @@ interface AuditConfig {
1896
2065
  readonly values?: boolean;
1897
2066
  /** Failed logins within an hour that the dashboard warns about. Default 20. */
1898
2067
  readonly failedLogins?: number;
2068
+ /**
2069
+ * The part of the site an entry belongs to, from the request's context, e.g. its tenant
2070
+ * (usually set by a plugin). Users whose role holds in a part only (`scoped`) see that part's
2071
+ * entries; others see the chosen part's, or all.
2072
+ */
2073
+ readonly scope?: ((context: RequestContext) => string | null | undefined) | null;
1899
2074
  }
1900
2075
  /** A field that changed: values before and after, unless they are not kept. */
1901
2076
  interface AuditChange {
@@ -1943,6 +2118,8 @@ interface AuditFilter {
1943
2118
  /** ISO dates. */
1944
2119
  from?: string | null;
1945
2120
  to?: string | null;
2121
+ /** Only entries of this part of the site (`scope`). */
2122
+ scope?: string | null;
1946
2123
  }
1947
2124
  /** `POST <api>/admin/audit/verify`. */
1948
2125
  interface AuditVerification {
@@ -1979,12 +2156,14 @@ declare class AuditLog {
1979
2156
  email?: string | null;
1980
2157
  changes?: AuditChange[] | null;
1981
2158
  detail?: Record<string, unknown> | null;
2159
+ /** The call's context, when it is not the request's. */
2160
+ context?: RequestContext;
1982
2161
  }): Promise<void>;
1983
2162
  /**
1984
2163
  * A change to a document or global: what changed between `before` and `after`. Internal
1985
2164
  * collections (sessions, versions…) are not logged.
1986
2165
  */
1987
- content(config: CollectionConfig | GlobalConfig, global: boolean, action: string, after: Record<string, unknown> | undefined, before: Record<string, unknown> | undefined, user: AuthUser | null): Promise<void>;
2166
+ content(config: CollectionConfig | GlobalConfig, global: boolean, action: string, after: Record<string, unknown> | undefined, before: Record<string, unknown> | undefined, user: AuthUser | null, context?: RequestContext): Promise<void>;
1988
2167
  /** Fields whose stored value changed. Hidden ones never (the password only as "changed"). */
1989
2168
  diff(fields: readonly Field[], before: Record<string, unknown>, after: Record<string, unknown>): AuditChange[];
1990
2169
  list(filter: AuditFilter, page?: number): Promise<AuditPage>;
@@ -2009,6 +2188,8 @@ declare class AuditLog {
2009
2188
  invalid: number;
2010
2189
  at: string;
2011
2190
  })[]>;
2191
+ /** The part of the site a context is in (`scope`), or `null`. */
2192
+ scopeOf(context: RequestContext | undefined): string | null;
2012
2193
  private sign;
2013
2194
  private lastVerification;
2014
2195
  private where;
@@ -2041,6 +2222,7 @@ declare const DEFAULT_PASSWORD_EMAILS: {
2041
2222
  resetPassword: PasswordEmailFn;
2042
2223
  invite: PasswordEmailFn;
2043
2224
  passwordChanged: PasswordEmailFn;
2225
+ verifyEmail: PasswordEmailFn;
2044
2226
  };
2045
2227
 
2046
2228
  /**
@@ -2187,8 +2369,11 @@ type WebhookEvent = (typeof WEBHOOK_EVENTS)[number];
2187
2369
  interface WebhookConfig {
2188
2370
  /** Where to POST events. */
2189
2371
  readonly url: string;
2190
- /** Events to send. Default: all. */
2191
- readonly events?: readonly WebhookEvent[];
2372
+ /**
2373
+ * Events to send. Default: every content change. Events of the app or its plugins (`events`
2374
+ * in the config, e.g. `order.paid`) are sent only when listed here.
2375
+ */
2376
+ readonly events?: readonly (WebhookEvent | (string & {}))[];
2192
2377
  /** Collections to send events for. Default: all; `[]` for none. */
2193
2378
  readonly collections?: readonly string[];
2194
2379
  /** Globals to send events for. Default: all; `[]` for none. */
@@ -2200,11 +2385,15 @@ interface WebhookConfig {
2200
2385
  }
2201
2386
  /** What a webhook receives as JSON. */
2202
2387
  interface WebhookPayload {
2203
- readonly event: WebhookEvent;
2388
+ /** A content change (`WebhookEvent`), or an event of the app or a plugin, e.g. `order.paid`. */
2389
+ readonly event: string;
2204
2390
  readonly collection?: string;
2205
2391
  readonly global?: string;
2206
2392
  readonly id?: ID;
2207
- /** The stored document (every locale, no hidden fields); for `delete`, as it was. */
2393
+ /**
2394
+ * The stored document (every locale, no hidden fields); for `delete`, as it was. For an event of
2395
+ * the app or a plugin: what `cms.emit()` was given.
2396
+ */
2208
2397
  readonly doc: Record<string, unknown>;
2209
2398
  /** ISO time of the change. */
2210
2399
  readonly timestamp: string;
@@ -2290,15 +2479,26 @@ interface ContainerAdmin {
2290
2479
  interface CollectionAdmin extends ContainerAdmin {
2291
2480
  /**
2292
2481
  * `settings` lists the collection under Settings in the admin menu (with Users and API keys)
2293
- * instead of with the content, e.g. for redirects or forms settings.
2482
+ * instead of with the content, e.g. for redirects or forms settings. A label (e.g.
2483
+ * `{ en: 'Shop', th: 'ร้านค้า' }`) lists it under a heading of that name, after the content.
2294
2484
  */
2295
- readonly group?: 'settings';
2485
+ readonly group?: 'settings' | Label;
2296
2486
  readonly list?: CollectionListAdmin;
2297
2487
  /**
2298
2488
  * With `auth.rbac`: the relationship field to `users` that names a document's owner, e.g.
2299
2489
  * `author`, for roles given "own documents only". Default: who created it (`createdBy`).
2300
2490
  */
2301
2491
  readonly ownerField?: string;
2492
+ /**
2493
+ * Deleting asks more than "Are you sure?", for documents that take much with them (e.g. a
2494
+ * tenant): `typeTitle` has the user type the document's title; `impact` is a path under the
2495
+ * API, called with `?id=`, that returns `{ message }` (a label) saying what will be lost.
2496
+ * Documents are then deleted one at a time, not from a selection.
2497
+ */
2498
+ readonly confirmDelete?: {
2499
+ readonly typeTitle?: boolean;
2500
+ readonly impact?: string;
2501
+ };
2302
2502
  }
2303
2503
  interface CollectionListAdmin {
2304
2504
  /**
@@ -2515,6 +2715,44 @@ interface RoutesConfig {
2515
2715
  /** Where the REST API is served. Default `/api/cms`. */
2516
2716
  readonly api?: string;
2517
2717
  }
2718
+ /**
2719
+ * People who sign in on the site rather than to the admin, e.g. customers of a shop.
2720
+ */
2721
+ interface MembersConfig {
2722
+ /**
2723
+ * Their roles, also listed in `roles`. Members never get into the admin, and access that
2724
+ * defaults to logged-in users (`isLoggedIn`) doesn't count them: give them access in the
2725
+ * collections they use, e.g. with `isSignedIn`.
2726
+ */
2727
+ readonly roles: readonly string[];
2728
+ /** Visitors create their own account: `POST <api>/users/signup`. */
2729
+ readonly signup?: MembersSignup;
2730
+ /**
2731
+ * The site's pages that open the links of members' emails, given `?token=`: paths on
2732
+ * `admin.siteUrl` (else `serverURL`), or full URLs. Without them, links go to the admin's pages.
2733
+ */
2734
+ readonly pages?: {
2735
+ /** Confirms the email of a new account: `POST <api>/users/verify-email` with the token. */
2736
+ readonly verifyEmail?: string;
2737
+ /** Sets a forgotten password: `POST <api>/users/reset-password` with the token. */
2738
+ readonly resetPassword?: string;
2739
+ };
2740
+ /** Your own text for the email that confirms an address. Default: English or Thai. */
2741
+ readonly emails?: {
2742
+ readonly verifyEmail?: PasswordEmailFn;
2743
+ };
2744
+ }
2745
+ interface MembersSignup {
2746
+ /** The role new accounts get: one of `members.roles`. */
2747
+ readonly role: string;
2748
+ /** New accounts confirm their email with a link before signing in. Default `true`. */
2749
+ readonly verifyEmail?: boolean;
2750
+ /** Cloudflare Turnstile, checked on every sign-up when set. */
2751
+ readonly turnstile?: {
2752
+ readonly siteKey: string;
2753
+ readonly secretKey: string;
2754
+ };
2755
+ }
2518
2756
  interface AuthConfig {
2519
2757
  /**
2520
2758
  * Roles a user can have. Must include `admin`. Default `['admin', 'editor']`. With `rbac`, these
@@ -2568,6 +2806,11 @@ interface AuthConfig {
2568
2806
  * no code is asked.
2569
2807
  */
2570
2808
  readonly setupCode?: string;
2809
+ /**
2810
+ * People who sign in on the site, not to the admin, e.g. customers: their roles, signing up,
2811
+ * and the pages their email links open.
2812
+ */
2813
+ readonly members?: MembersConfig;
2571
2814
  /** Your own text for the password emails, e.g. in your brand's voice. Default: English or Thai. */
2572
2815
  readonly emails?: {
2573
2816
  readonly resetPassword?: PasswordEmailFn;
@@ -2611,9 +2854,22 @@ interface EndpointRequest {
2611
2854
  json(): Promise<Record<string, unknown>>;
2612
2855
  }
2613
2856
  /**
2614
- * A custom REST endpoint, served under `routes.api`. Writes from the browser pass the same
2615
- * CSRF check as the built-in endpoints.
2857
+ * Work that runs with the scheduled jobs: every minute in a server, or each time a cron calls
2858
+ * `GET <api>/jobs/run`. E.g. a plugin's clean-up.
2616
2859
  */
2860
+ interface JobConfig {
2861
+ /** Unique, e.g. `ecommerce:carts`. */
2862
+ readonly name: string;
2863
+ /**
2864
+ * At most this often, in seconds. Default: every run. The last run is kept in the database, so
2865
+ * it holds across processes and serverless invocations.
2866
+ */
2867
+ readonly every?: number;
2868
+ readonly run: (args: {
2869
+ readonly cms: EasyCMS;
2870
+ readonly now: Date;
2871
+ }) => MaybePromise<unknown>;
2872
+ }
2617
2873
  /** An `easy-cms <name> [args…]` command. Built-in commands keep their names. */
2618
2874
  interface CliCommand {
2619
2875
  /** Lowercase, e.g. `nested:rebuild`. */
@@ -2630,6 +2886,10 @@ interface CliCommand {
2630
2886
  readonly log: (line: string) => void;
2631
2887
  }) => MaybePromise<number | undefined>;
2632
2888
  }
2889
+ /**
2890
+ * A custom REST endpoint, served under `routes.api`. Writes from the browser pass the same
2891
+ * CSRF check as the built-in endpoints.
2892
+ */
2633
2893
  interface Endpoint {
2634
2894
  /**
2635
2895
  * Path under the API, e.g. `/seo/generate` or `/stats/:collection`. The first segment must
@@ -2738,6 +2998,13 @@ interface Config {
2738
2998
  readonly cronSecret?: string;
2739
2999
  /** Endpoints notified when content changes, e.g. to rebuild a static site. */
2740
3000
  readonly webhooks?: readonly WebhookConfig[];
3001
+ /**
3002
+ * Events of the app or its plugins besides content changes, e.g. `order.paid`: sent with
3003
+ * `cms.emit()` to the webhooks that list them in `events`. Named `<area>.<what>`.
3004
+ */
3005
+ readonly events?: readonly string[];
3006
+ /** Work run with the scheduled jobs, e.g. a plugin's clean-up. */
3007
+ readonly jobs?: readonly JobConfig[];
2741
3008
  /** Content in several languages: fields with `localized: true` hold one value per locale. */
2742
3009
  readonly localization?: LocalizationConfig | null;
2743
3010
  readonly routes?: RoutesConfig;
@@ -2799,6 +3066,8 @@ interface ResolvedConfig extends Omit<Config, 'cors' | 'localization' | 'routes'
2799
3066
  readonly globals: readonly GlobalConfig[];
2800
3067
  readonly endpoints: readonly Endpoint[];
2801
3068
  readonly commands: readonly CliCommand[];
3069
+ readonly events: readonly string[];
3070
+ readonly jobs: readonly JobConfig[];
2802
3071
  readonly fieldTypes: readonly FieldTypeDefinition[];
2803
3072
  /** The config's plugins in order, with their `info` where they give one. */
2804
3073
  readonly installedPlugins: readonly Partial<PluginInfo>[];
@@ -3199,6 +3468,10 @@ interface AdminField {
3199
3468
  after?: AdminComponentRef[];
3200
3469
  cell?: AdminComponentRef;
3201
3470
  };
3471
+ /** A column of the list at first (`admin.column`). */
3472
+ column?: boolean;
3473
+ /** A relationship that offers no "Create" in place (`admin.allowCreate: false`). */
3474
+ noCreate?: boolean;
3202
3475
  /** A field of an added type (`fieldTypes`), e.g. `color`; `type` is the base it's stored as. */
3203
3476
  customType?: string;
3204
3477
  }
@@ -3223,6 +3496,8 @@ interface AdminCollection {
3223
3496
  sidebar?: AdminComponentRef[];
3224
3497
  /** Listed under Settings in the menu (users, API keys, `admin.group: 'settings'`). */
3225
3498
  group?: 'settings';
3499
+ /** Listed under a heading of its own in the menu (`admin.group` with a label). */
3500
+ section?: Label;
3226
3501
  /** The list is a tree along this relationship field (`admin.list.tree`). */
3227
3502
  tree?: string;
3228
3503
  /** The list's default order (`admin.list.sort`). */
@@ -3236,6 +3511,11 @@ interface AdminCollection {
3236
3511
  };
3237
3512
  /** With `auth.rbac`: the field naming a document's owner (`createdBy` or `admin.ownerField`). */
3238
3513
  owner?: string;
3514
+ /** Deleting asks to type the title, and says what goes too (`admin.confirmDelete`). */
3515
+ confirmDelete?: {
3516
+ typeTitle?: boolean;
3517
+ impact?: string;
3518
+ };
3239
3519
  }
3240
3520
  interface AdminGlobal {
3241
3521
  slug: string;
@@ -3420,6 +3700,27 @@ declare function createRestHandler<C extends Config>(instance: EasyCMS<C>, optio
3420
3700
  declare function createRootEndpointHandler<C extends Config>(instance: EasyCMS<C>, options?: Pick<RestHandlerOptions, 'getClientIp'>): (request: Request) => Promise<Response | undefined>;
3421
3701
  declare function readCookie(request: Request, name: string): string | undefined;
3422
3702
 
3703
+ /** A token that proves the form `purpose` names was loaded at `now`: `<time>.<signature>`. */
3704
+ declare function formToken(secret: string, purpose: string, now?: number): string;
3705
+ /**
3706
+ * Checks a token: `ok` when the form was loaded at least `minTime` ms ago (people take a few
3707
+ * seconds; bots don't) and less than a day ago.
3708
+ */
3709
+ declare function checkFormToken(secret: string, purpose: string, token: unknown, minTime: number, now?: number): 'ok' | 'invalid' | 'too-fast' | 'expired';
3710
+ /**
3711
+ * Rate-limit keys for `subject` (e.g. an IP and a form) in the current and previous window of
3712
+ * `window` seconds. A hash with the window in it: no IP is kept, and keys from different
3713
+ * windows can't be linked.
3714
+ */
3715
+ declare function rateKeys(secret: string, subject: string, window: number, now?: number): {
3716
+ current: string;
3717
+ previous: string;
3718
+ };
3719
+ /** The hidden field bots fill in: a common name the form doesn't use. */
3720
+ declare function honeypotName(names: readonly string[]): string;
3721
+ /** Checks a Cloudflare Turnstile response with Cloudflare. */
3722
+ declare function verifyTurnstile(secretKey: string, response: unknown, ip: string | undefined, fetcher?: typeof fetch): Promise<boolean>;
3723
+
3423
3724
  /** Best-effort English singular: "categories" → "category". */
3424
3725
  declare function singularize(word: string): string;
3425
3726
  /**
@@ -3437,4 +3738,4 @@ declare function validateConfig(config: Config): ConfigIssue[];
3437
3738
  /** The version of `@easy-cms/core`, set when the package is built. */
3438
3739
  declare const VERSION: string;
3439
3740
 
3440
- export { ADMIN_ICONS, API_KEYS, API_KEY_GLOBAL_OPERATIONS, API_KEY_OPERATIONS, type Access, type AccessArgs, type AccessOptions, type AdminAttention, type AdminBackup, type AdminBackups, type AdminBrand, type AdminCollection, type AdminComponent, type AdminComponentRef, type AdminConfig, type AdminDeliveries, type AdminDelivery, type AdminEmail, type AdminEmailDelivery, type AdminField, type AdminGlobal, type AdminIcon, type AdminLocale, type AdminPage, type AdminPageRef, type AdminRole, type AdminRoleChange, type AdminRoleField, type AdminRoleTarget, type AdminRoleView, type AdminRoles, type AdminSchema, type AdminSso, type AdminStatus, type AdminSwitcher, type AdminViewAccess, type AdminWebhookDelivery, type AdminWidgetRef, type AfterChangeHook, type AfterDeleteHook, type AfterReadHook, type ApiHandlerOptions, type ApiKeyContext, type ApiKeyGlobalOperation, type ApiKeyOperation, type ApiKeyPermissions, type ArrayField, type AuditChange, type AuditConfig, type AuditEntry, AuditLog, type AuditPage, type AuditVerification, Auth, type AuthConfig, type AuthProvider, type AuthProviderProfile, type AuthProviderRequest, type AuthUser, BUILTIN_COLLECTIONS, type BackupState, type BackupsConfig, type BeforeChangeHook, type BeforeDeleteHook, type BeforeValidateHook, type Block, type BlocksField, type BooleanField, type BuiltinApiKeysCollection, type BuiltinMediaCollection, type BuiltinMediaFoldersCollection, type BuiltinUsersCollection, type BundleHints, CONFIG_FILE_NAMES, CSRF_COOKIE, CSRF_HEADER, type CliCommand, type CollectionAccess, type CollectionAdmin, type CollectionConfig, type CollectionDocument, type CollectionHooks, type CollectionListAdmin, type CollectionSlug, type Config, ConfigError, type ConfigIssue, type ContainerAdmin, type CopyProgress, type CopyResult, type CreateEasyCMSOptions, type CreateInput, type CreateMigrationResult, type CustomField, type CustomFieldTypes, DEFAULT_ADMIN_PATH, DEFAULT_API_PATH, DEFAULT_DEPTH, DEFAULT_MAX_FILE_SIZE, DEFAULT_MAX_VERSIONS, DEFAULT_PASSWORD_EMAILS, DEFAULT_TOKEN_EXPIRATION, type DashboardWidget, type Database, type DatabaseAdapter, type DatabaseInitArgs, type DatabaseTransfer, type DateField, type DeliveryKind, type DeliveryState, type DepthOptions, type DirectUpload, type DocumentOf, EXTENSIONS, EasyCMS, EasyCMSError, type EmailAdapter, type EmailCheck, type EmailField, type EmailMessage, type EmailSetting, type Endpoint, type EndpointRequest, FIELD_TYPES, FIELD_TYPE_BASES, FOLDER_LEVELS, type Field, type FieldAccess, type FieldAccessFn, type FieldAdmin, type FieldError, type FieldRule, type FieldType, type FieldTypeBase, type FieldTypeDefinition, type FieldTypeValidateContext, type FieldValidate, type FieldValidateContext, type FieldValue, type FieldsInput, type FieldsValue, type FilterOptions, type FilterOptionsArgs, type FindArgs, type FindOptions, type FolderLevel, type FolderPermissions, ForbiddenError, type GlobalAccess, type GlobalConfig, type GlobalDocument, type GlobalHooks, type GlobalInput, type GlobalSlug, type GroupField, type ID, INTERNAL_COLLECTIONS, type ImageSize, type InferCollection, type InferGlobal, type JsonField, type Label, type LivePreview, type LoadConfigOptions, type LocalStorageOptions, type LocalizationConfig, type Logger, type LoginArgs, MAX_DEPTH, MEDIA, MEDIA_FOLDERS, MIME_GROUPS, MIN_PASSWORD_LENGTH, MIN_SECRET_LENGTH, type MediaDocument, MediaFolders, type MediaSize, type MigrationInfo, NotFoundError, type NumberField, type OnRequest, type OnRequestArgs, type Operation, type PaginatedDocs, type PasswordEmail, type PasswordEmailArgs, type PasswordEmailFn, type PasswordLinkOptions, PayloadTooLargeError, type Plugin, type PluginInfo, type PluginTypes, type PreviewTarget, type PreviewURL, QueryError, ROLE_OPERATIONS, type RawDocument, type ReadOptions, type RelationshipField, type RequestContext, type ResolvedConfig, type RestHandler, type RestHandlerOptions, type RichTextDocument, type RichTextField, type Role, type RoleOperation, type RolePermissions, Roles, type RoutesConfig, SESSION_COOKIE, SSO_COOKIE, type ScheduledJob, SchemaError, type SchemaMode, type SelectField, type SelectOption, type Session, type SlugField, type SlugOf, type SsoOutcome, type SsoProviderRef, type StorageAdapter, type StoredFile, type TextField, type TextareaField, TooManyRequestsError, type TypedPlugin, UnauthorizedError, type UpdateInput, type UpdateOptions, type UploadConfig, type UploadField, type UploadFromURLConfig, type UserIdentity, VERSION, ValidationError, type Version, type VersionSummary, type VersionsConfig, WEBHOOK_EVENTS, type WebhookConfig, type WebhookEvent, type WebhookPayload, type Where, type WhereOperators, anyone, applyDefaults, applyPlugins, configSignature, consoleEmail, consoleLogger, copyDatabase, createApiHandler, createEasyCMS, createRestHandler, createRootEndpointHandler, defineConfig, defineFieldType, definePlugin, expandMimeTypes, fillMissing, findConfigFile, generateSlugs, generateTypes, hasRows, hashPassword, imageDimensions, importConfig, isAdmin, isHasMany, isLoggedIn, isSystemAdmin, keyAllows, loadConfig, localStorage, mergeForUpdate, mimeAllowed, mimeAllowedBy, parseId, populate, readCookie, resolveAdminModule, resolveConfig, rowFields, sharedEasyCMS, silentLogger, singularize, slugify, sniffMimeType, validateConfig, validateFields, verifyPassword, writeBackupFile };
3741
+ export { ADMIN_ICONS, API_KEYS, API_KEY_GLOBAL_OPERATIONS, API_KEY_OPERATIONS, type Access, type AccessArgs, type AccessOptions, type AdminAttention, type AdminBackup, type AdminBackups, type AdminBrand, type AdminCollection, type AdminComponent, type AdminComponentRef, type AdminConfig, type AdminDeliveries, type AdminDelivery, type AdminEmail, type AdminEmailDelivery, type AdminField, type AdminFieldArgs, type AdminGlobal, type AdminIcon, type AdminLocale, type AdminPage, type AdminPageRef, type AdminRole, type AdminRoleChange, type AdminRoleField, type AdminRoleTarget, type AdminRoleView, type AdminRoles, type AdminSchema, type AdminSso, type AdminStatus, type AdminSwitcher, type AdminViewAccess, type AdminWebhookDelivery, type AdminWidgetRef, type AfterChangeHook, type AfterDeleteHook, type AfterReadHook, type ApiHandlerOptions, type ApiKeyContext, type ApiKeyGlobalOperation, type ApiKeyOperation, type ApiKeyPermissions, type ArrayField, type AuditChange, type AuditConfig, type AuditEntry, AuditLog, type AuditPage, type AuditVerification, Auth, type AuthConfig, type AuthProvider, type AuthProviderProfile, type AuthProviderRequest, type AuthUser, BUILTIN_COLLECTIONS, type BackupState, type BackupsConfig, type BeforeChangeHook, type BeforeDeleteHook, type BeforeValidateHook, type Block, type BlocksField, type BooleanField, type BuiltinApiKeysCollection, type BuiltinMediaCollection, type BuiltinMediaFoldersCollection, type BuiltinUsersCollection, type BundleHints, CONFIG_FILE_NAMES, CSRF_COOKIE, CSRF_HEADER, type CliCommand, type CollectionAccess, type CollectionAdmin, type CollectionConfig, type CollectionDocument, type CollectionHooks, type CollectionListAdmin, type CollectionSlug, type Config, ConfigError, type ConfigIssue, type ContainerAdmin, type CopyProgress, type CopyResult, type CreateEasyCMSOptions, type CreateInput, type CreateMigrationResult, type CustomField, type CustomFieldTypes, DEFAULT_ADMIN_PATH, DEFAULT_API_PATH, DEFAULT_DEPTH, DEFAULT_MAX_FILE_SIZE, DEFAULT_MAX_VERSIONS, DEFAULT_PASSWORD_EMAILS, DEFAULT_TOKEN_EXPIRATION, type DashboardWidget, type Database, type DatabaseAdapter, type DatabaseInitArgs, type DatabaseTransfer, type DateField, type DeliveryKind, type DeliveryState, type DepthOptions, type DirectUpload, type DocumentOf, EXTENSIONS, EasyCMS, EasyCMSError, type EmailAdapter, type EmailCheck, type EmailField, type EmailMessage, type EmailSetting, type Endpoint, type EndpointRequest, FIELD_TYPES, FIELD_TYPE_BASES, FOLDER_LEVELS, type Field, type FieldAccess, type FieldAccessFn, type FieldAdmin, type FieldError, type FieldRule, type FieldType, type FieldTypeBase, type FieldTypeDefinition, type FieldTypeValidateContext, type FieldValidate, type FieldValidateContext, type FieldValue, type FieldsInput, type FieldsValue, type FilterOptions, type FilterOptionsArgs, type FindArgs, type FindOptions, type FolderLevel, type FolderPermissions, ForbiddenError, type GlobalAccess, type GlobalConfig, type GlobalDocument, type GlobalHooks, type GlobalInput, type GlobalSlug, type GroupField, type ID, INTERNAL_COLLECTIONS, type ImageSize, type IncrementOptions, type InferCollection, type InferGlobal, type JobConfig, type JsonField, type Label, type LivePreview, type LoadConfigOptions, type LocalStorageOptions, type LocalizationConfig, type Logger, type LoginArgs, MAX_DEPTH, MEDIA, MEDIA_FOLDERS, MIME_GROUPS, MIN_PASSWORD_LENGTH, MIN_SECRET_LENGTH, type MediaDocument, MediaFolders, type MediaSize, type MembersConfig, type MembersSignup, type MigrationInfo, NotFoundError, type NumberField, type OnRequest, type OnRequestArgs, type Operation, type PaginatedDocs, type PasswordEmail, type PasswordEmailArgs, type PasswordEmailFn, type PasswordLinkOptions, PayloadTooLargeError, type Plugin, type PluginInfo, type PluginTypes, type PreviewTarget, type PreviewURL, QueryError, ROLE_OPERATIONS, type RawDocument, type ReadOptions, type RelationshipField, type RequestContext, type ResolvedConfig, type RestHandler, type RestHandlerOptions, type RichTextDocument, type RichTextField, type Role, type RoleOperation, type RolePermissions, Roles, type RoutesConfig, SESSION_COOKIE, SSO_COOKIE, type ScheduledJob, SchemaError, type SchemaMode, type SelectField, type SelectOption, type Session, type SignupArgs, type SignupResult, type SlugField, type SlugOf, type SsoOutcome, type SsoProviderRef, type StorageAdapter, type StoredFile, type TextField, type TextareaField, TooManyRequestsError, type TypedPlugin, UnauthorizedError, type UpdateInput, type UpdateOptions, type UploadConfig, type UploadField, type UploadFromURLConfig, type UserIdentity, VERSION, ValidationError, type Version, type VersionSummary, type VersionsConfig, WEBHOOK_EVENTS, type WebhookConfig, type WebhookEvent, type WebhookPayload, type Where, type WhereOperators, anyone, applyDefaults, applyPlugins, checkFormToken, configSignature, consoleEmail, consoleLogger, copyDatabase, createApiHandler, createEasyCMS, createRestHandler, createRootEndpointHandler, defineConfig, defineFieldType, definePlugin, expandMimeTypes, fillMissing, findConfigFile, formToken, generateSlugs, generateTypes, hasRows, hashPassword, honeypotName, imageDimensions, importConfig, isAdmin, isHasMany, isLoggedIn, isSignedIn, isSystemAdmin, keyAllows, loadConfig, localStorage, mergeForUpdate, mimeAllowed, mimeAllowedBy, parseId, populate, rateKeys, readCookie, resolveAdminModule, resolveConfig, rowFields, sharedEasyCMS, silentLogger, singularize, slugify, sniffMimeType, uniqueWithinOf, validateConfig, validateFields, verifyPassword, verifyTurnstile, writeBackupFile };