@bernierllc/nevarmail 0.28.0 → 0.30.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/README.md CHANGED
@@ -20,7 +20,7 @@ org may have been mailing into a `403` for months with nothing on either side no
20
20
  npm install @bernierllc/nevarmail
21
21
  ```
22
22
 
23
- Requires Node 20+ (uses the global `fetch`). Zero dependencies.
23
+ Server-side only; requires Node 20+ (uses global `fetch` and `crypto.randomUUID`). Never expose API credentials in browser code. Zero dependencies.
24
24
 
25
25
  ## Integration status
26
26
 
@@ -164,6 +164,7 @@ the two, not both). The slug must exist in exactly one project the key can open:
164
164
  | `listCampaigns({ projectId?, page?, perPage? })` / `getCampaign(id, { projectId? })` — read-only | `GET /api/v1/campaigns`, `GET /api/v1/campaigns/{id}` | `campaigns:read` |
165
165
  | `getCampaignAnalytics(id, { projectId? })` — counts plus delivery/open/click/bounce/unsubscribe rates (0 to 1) | `GET /api/v1/campaigns/{id}/analytics` | `analytics:read` |
166
166
  | `getAnalyticsSummary({ period? } \| { start, end })` — org-wide counts and rates for a trailing `7d`/`30d`/`90d` (default `30d`) or an inclusive UTC `YYYY-MM-DD` range | `GET /api/v1/analytics/summary` | `analytics:read` |
167
+ | `getUsage({ start, end })` — metered outbound email counts (billable and test-mode apart) for an inclusive UTC `YYYY-MM-DD` range of at most 366 days, current hour included; counts only, no cost | `GET /api/v1/usage` | `usage:read` |
167
168
  | `listLists` / `createList` / `getList` | `/api/v1/lists` | `contacts:read` / `contacts:write` |
168
169
  | `addListContacts` | `POST /api/v1/lists/{id}/contacts` | `contacts:write` |
169
170
  | `removeListContact` | `DELETE /api/v1/lists/{id}/contacts?email=` (one list only; the contact is kept) | `contacts:write` |
@@ -309,3 +310,45 @@ new NevarMail({
309
310
  fetch: undefined, // inject one for tests or a custom runtime
310
311
  });
311
312
  ```
313
+
314
+ ### Use-case groups and inherited settings
315
+
316
+ Create reusable project defaults with `createUseCaseGroup`, then create a use case with only its name and group ID. Groups allow suppressable, transactional, or mixed use cases; mixed groups have a default kind and separate tracking defaults per kind.
317
+
318
+ ```ts
319
+ const group = await mail.createUseCaseGroup({
320
+ name: "Account emails", projectId,
321
+ allowedKind: "transactional", defaultSuppressable: false,
322
+ defaults: {
323
+ shared: { fromName: { value: "Accounts" } },
324
+ suppressable: {}, transactional: { clickTracking: { value: false } },
325
+ },
326
+ });
327
+ const login = await mail.createUseCase({ name: "Login", groupId: group.id });
328
+ await mail.updateUseCase(login.id, {
329
+ expectedRevision: login.settingsRevision,
330
+ settings: { set: { replyToEmail: null }, inherit: ["fromName"] },
331
+ });
332
+ ```
333
+
334
+ An omitted override inherits; explicit `false` disables; explicit `null` clears a nullable field. `settings` on responses includes effective/configured values, source and override status. An override equal to the group value remains an override. Group defaults stay live for inheritors.
335
+
336
+ Use `listUseCaseGroups`, `getUseCaseGroup`, and `deleteUseCaseGroup` to manage groups. Delete returns `{id, deleted: true}` and refuses a group containing any members, including archived ones. To edit defaults, call `previewUseCaseGroupUpdate(id, {changes})`, review affected members, then `updateUseCaseGroup` with those changes, the preview's revision and token.
337
+
338
+ ```ts
339
+ const target = { groupId: destinationId }; // null means Ungrouped
340
+ const preview = await mail.previewUseCaseGroupMove(login.id, target);
341
+ // Show preview.rows and both outcomes to the user before applying.
342
+ const idempotencyKey = globalThis.crypto.randomUUID();
343
+ const moved = await mail.moveUseCaseToGroup(login.id, {
344
+ ...target, previewToken: preview.previewToken,
345
+ policy: "inherit_destination", // or "preserve_effective"
346
+ idempotencyKey,
347
+ });
348
+ ```
349
+
350
+ Moves retain existing overrides. `preserve_effective` additionally snapshots inherited values as overrides. A kind change supplies `suppressable` through the same preview/apply flow, not an ordinary metadata PATCH. Tokens expire after 15 minutes and are bound to the actor/scope; use the same credential for preview and apply. A stale preview returns 409 and is not retried: fetch another comparison and obtain confirmation again.
351
+
352
+ The move key is a UUID in the request body. Omission generates one once per SDK call, preserved across automatic retries. Retain and reuse an explicit key when retrying a whole call after an ambiguous network result. A committed retry returns its original result even after token expiry; reusing the key for a different move is a conflict.
353
+
354
+ Production transitions require `confirmation: {productionSending: true}` and server permissions/readiness. Enabling tracking for transactional mail requires `transactionalTracking: "transactional-tracking-v1"` after the user sees the disclosure. Never auto-confirm these choices. Tracking is resolved server-side; `SendEmailInput` has no tracking overrides. Keep API credentials server-side: app browser components use session routes.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { NevarMailError } from "./errors.js";
2
+ export type * from "./use-case-groups.js";
3
+ import type { UseCaseSettingOverrides, UseCaseSettingsPatch, ResolvedUseCaseSettings, UseCaseSettingsConfirmation, UseCaseGroup, ListUseCaseGroupsOptions, CreateUseCaseGroupInput, UpdateUseCaseGroupInput, PreviewUseCaseGroupUpdateInput, UseCaseGroupUpdatePreview, PreviewUseCaseGroupMoveInput, UseCaseGroupMovePreview, MoveUseCaseToGroupInput, UseCaseGroupMoveResult } from "./use-case-groups.js";
2
4
  /** The v1 success envelope. `request()` unwraps `data` and returns it. */
3
5
  export interface V1Pagination {
4
6
  page: number;
@@ -269,6 +271,17 @@ export interface AnalyticsSummary {
269
271
  /** True when part of `totalSent` is a campaign's provider-reported recipient estimate. */
270
272
  sentIncludesCampaignEstimates: boolean;
271
273
  }
274
+ /** Metered outbound email counts for an inclusive UTC date range. */
275
+ export interface Usage {
276
+ /** First day of the range (YYYY-MM-DD, UTC). */
277
+ start: string;
278
+ /** Last day of the range (YYYY-MM-DD, UTC, inclusive). */
279
+ end: string;
280
+ /** Billable outbound emails sent in the range, including the current hour. */
281
+ outboundEmails: number;
282
+ /** Test-mode outbound emails sent in the range; never billed. */
283
+ outboundEmailsTest: number;
284
+ }
272
285
  export interface TemplateVariable {
273
286
  name: string;
274
287
  type: string;
@@ -459,6 +472,10 @@ export type UpdateSenderInput = Partial<Omit<CreateSenderInput, "projectId" | "f
459
472
  };
460
473
  /** A use case: the message category a project files mail under (`use_case_id` / `useCase` on a send). */
461
474
  export interface UseCase {
475
+ groupId?: string | null;
476
+ settingsRevision?: number;
477
+ overrides?: UseCaseSettingOverrides;
478
+ settings?: ResolvedUseCaseSettings;
462
479
  id: string;
463
480
  orgId: string;
464
481
  projectId: string;
@@ -490,17 +507,26 @@ export interface ListUseCasesOptions extends PageOptions {
490
507
  includeArchived?: boolean;
491
508
  }
492
509
  export interface CreateUseCaseInput {
510
+ /** Name plus groupId inherits its project, kind and settings. */
511
+ groupId?: string;
512
+ settings?: UseCaseSettingsPatch;
513
+ confirmation?: UseCaseSettingsConfirmation;
493
514
  name: string;
494
515
  /** Same rule as `createSender`: omit it only when the org has one project (400 `PROJECT_REQUIRED` otherwise). */
495
516
  projectId?: string;
496
517
  description?: string;
497
- /** Defaults to true. */
518
+ /** Uses the group default when grouped; otherwise defaults to true. */
498
519
  suppressable?: boolean;
499
520
  category?: string;
500
521
  }
501
522
  export interface UpdateUseCaseInput {
523
+ settings?: UseCaseSettingsPatch;
524
+ /** Required when settings is supplied. */
525
+ expectedRevision?: number;
526
+ confirmation?: UseCaseSettingsConfirmation;
502
527
  name?: string;
503
528
  description?: string;
529
+ /** Only the unchanged kind is accepted here; change kind via previewUseCaseGroupMove and moveUseCaseToGroup. */
504
530
  suppressable?: boolean;
505
531
  category?: string;
506
532
  /** The complete set of lists to link; lists not named are unlinked. */
@@ -926,6 +952,17 @@ export declare class NevarMail {
926
952
  * on its project only.
927
953
  */
928
954
  getAnalyticsSummary(window?: AnalyticsSummaryWindow): Promise<AnalyticsSummary>;
955
+ /**
956
+ * GET /api/v1/usage — scope `usage:read`. `start` and `end` are YYYY-MM-DD
957
+ * UTC dates, `end` inclusive, at most 366 days apart. Counts only: billing
958
+ * prices per month and tier, so no cost is returned. 400
959
+ * `VALIDATION_ERROR` for an invalid range; 403 `PROJECT_PINNED_CREDENTIAL` for a key
960
+ * pinned to a project, since usage is metered per organization.
961
+ */
962
+ getUsage(range: {
963
+ start: string;
964
+ end: string;
965
+ }): Promise<Usage>;
929
966
  /** GET /api/v1/lists — paged. */
930
967
  listLists(options?: PageOptions): Promise<Paginated<SubscriberList>>;
931
968
  /** POST /api/v1/lists — 409 DUPLICATE_NAME on a name the org already uses. */
@@ -1095,6 +1132,18 @@ export declare class NevarMail {
1095
1132
  deleteSender(id: string): Promise<{
1096
1133
  deleted: boolean;
1097
1134
  }>;
1135
+ /** List accessible groups, optionally restricted to a compatible use-case kind. */
1136
+ listUseCaseGroups(options?: ListUseCaseGroupsOptions): Promise<Paginated<UseCaseGroup>>;
1137
+ createUseCaseGroup(input: CreateUseCaseGroupInput): Promise<UseCaseGroup>;
1138
+ getUseCaseGroup(id: string): Promise<UseCaseGroup>;
1139
+ previewUseCaseGroupUpdate(id: string, input: PreviewUseCaseGroupUpdateInput): Promise<UseCaseGroupUpdatePreview>;
1140
+ updateUseCaseGroup(id: string, input: UpdateUseCaseGroupInput): Promise<UseCaseGroup>;
1141
+ deleteUseCaseGroup(id: string): Promise<{
1142
+ id: string;
1143
+ deleted: true;
1144
+ }>;
1145
+ previewUseCaseGroupMove(id: string, input: PreviewUseCaseGroupMoveInput): Promise<UseCaseGroupMovePreview>;
1146
+ moveUseCaseToGroup(id: string, input: MoveUseCaseToGroupInput): Promise<UseCaseGroupMoveResult>;
1098
1147
  /** GET /api/v1/use-cases — paged; every project the key can open, or `projectId`'s. */
1099
1148
  listUseCases(options?: ListUseCasesOptions): Promise<Paginated<UseCase>>;
1100
1149
  /** POST /api/v1/use-cases — 409 DUPLICATE_NAME when the name is taken in the project. */
package/dist/index.js CHANGED
@@ -375,6 +375,18 @@ export class NevarMail {
375
375
  query: { period: window.period, start: window.start, end: window.end },
376
376
  });
377
377
  }
378
+ /**
379
+ * GET /api/v1/usage — scope `usage:read`. `start` and `end` are YYYY-MM-DD
380
+ * UTC dates, `end` inclusive, at most 366 days apart. Counts only: billing
381
+ * prices per month and tier, so no cost is returned. 400
382
+ * `VALIDATION_ERROR` for an invalid range; 403 `PROJECT_PINNED_CREDENTIAL` for a key
383
+ * pinned to a project, since usage is metered per organization.
384
+ */
385
+ getUsage(range) {
386
+ return this.request("GET", "/api/v1/usage", {
387
+ query: { start: range.start, end: range.end },
388
+ });
389
+ }
378
390
  // ── Lists & contacts ───────────────────────────────────────────────────
379
391
  /** GET /api/v1/lists — paged. */
380
392
  async listLists(options = {}) {
@@ -535,6 +547,34 @@ export class NevarMail {
535
547
  deleteSender(id) {
536
548
  return this.request("DELETE", `/api/v1/senders/${encodeURIComponent(id)}`);
537
549
  }
550
+ /** List accessible groups, optionally restricted to a compatible use-case kind. */
551
+ listUseCaseGroups(options = {}) {
552
+ return this.requestPaginated("GET", "/api/v1/use-case-groups", { query: { ...options } });
553
+ }
554
+ createUseCaseGroup(input) {
555
+ return this.request("POST", "/api/v1/use-case-groups", { body: input });
556
+ }
557
+ getUseCaseGroup(id) {
558
+ return this.request("GET", `/api/v1/use-case-groups/${encodeURIComponent(id)}`);
559
+ }
560
+ previewUseCaseGroupUpdate(id, input) {
561
+ return this.request("POST", `/api/v1/use-case-groups/${encodeURIComponent(id)}/preview`, { body: input });
562
+ }
563
+ updateUseCaseGroup(id, input) {
564
+ return this.request("PATCH", `/api/v1/use-case-groups/${encodeURIComponent(id)}`, { body: input });
565
+ }
566
+ deleteUseCaseGroup(id) {
567
+ return this.request("DELETE", `/api/v1/use-case-groups/${encodeURIComponent(id)}`);
568
+ }
569
+ previewUseCaseGroupMove(id, input) {
570
+ return this.request("POST", `/api/v1/use-cases/${encodeURIComponent(id)}/group-move-preview`, { body: input });
571
+ }
572
+ moveUseCaseToGroup(id, input) {
573
+ // Construct once outside request's retry loop; callers can also retain a
574
+ // UUID across whole-call retries after an ambiguous network result.
575
+ const body = { ...input, idempotencyKey: input.idempotencyKey ?? globalThis.crypto.randomUUID() };
576
+ return this.request("POST", `/api/v1/use-cases/${encodeURIComponent(id)}/group-move`, { body });
577
+ }
538
578
  // ── Use cases ──────────────────────────────────────────────────────────
539
579
  /** GET /api/v1/use-cases — paged; every project the key can open, or `projectId`'s. */
540
580
  listUseCases(options = {}) {
@@ -0,0 +1,160 @@
1
+ /** Sending configuration resolved by NevarMail, never by the SDK. */
2
+ export interface UseCaseSettingValues {
3
+ domainId: string | null;
4
+ senderId: string | null;
5
+ fromName: string | null;
6
+ replyToEmail: string | null;
7
+ replyToName: string | null;
8
+ templateId: string | null;
9
+ sendMode: "test" | "production";
10
+ priority: "low" | "normal" | "high";
11
+ clickTracking: boolean;
12
+ openTracking: boolean;
13
+ googleAnalytics: boolean;
14
+ utmSource: string | null;
15
+ utmMedium: string | null;
16
+ utmCampaign: string | null;
17
+ utmTerm: string | null;
18
+ utmContent: string | null;
19
+ }
20
+ export type UseCaseSettingKey = keyof UseCaseSettingValues;
21
+ export type UseCaseSettingOverrides = {
22
+ [K in UseCaseSettingKey]?: {
23
+ value: UseCaseSettingValues[K];
24
+ };
25
+ };
26
+ export interface UseCaseSettingsPatch {
27
+ set?: Partial<UseCaseSettingValues>;
28
+ inherit?: UseCaseSettingKey[];
29
+ }
30
+ export type UseCaseSettingSource = "use_case" | "group" | "project" | "sender" | "product";
31
+ export type ResolvedUseCaseSetting<K extends UseCaseSettingKey = UseCaseSettingKey> = {
32
+ value: UseCaseSettingValues[K];
33
+ configuredValue: UseCaseSettingValues[K];
34
+ source: UseCaseSettingSource;
35
+ sourceId: string | null;
36
+ overridden: boolean;
37
+ constrainedBy: "project_analytics_disabled" | "test_credential" | null;
38
+ };
39
+ export type ResolvedUseCaseSettings = {
40
+ [K in UseCaseSettingKey]: ResolvedUseCaseSetting<K>;
41
+ };
42
+ export type UseCaseTrackingSettingKey = "clickTracking" | "openTracking" | "googleAnalytics" | "utmSource" | "utmMedium" | "utmCampaign" | "utmTerm" | "utmContent";
43
+ export interface UseCaseGroupDefaults {
44
+ shared: Omit<UseCaseSettingOverrides, UseCaseTrackingSettingKey>;
45
+ suppressable: Pick<UseCaseSettingOverrides, UseCaseTrackingSettingKey>;
46
+ transactional: Pick<UseCaseSettingOverrides, UseCaseTrackingSettingKey>;
47
+ }
48
+ export type UseCaseGroupKind = "suppressable" | "transactional" | "mixed";
49
+ export interface UseCaseSettingsConfirmation {
50
+ transactionalTracking?: "transactional-tracking-v1";
51
+ productionSending?: true;
52
+ }
53
+ export interface UseCaseGroupFields {
54
+ name: string;
55
+ allowedKind: UseCaseGroupKind;
56
+ defaultSuppressable: boolean;
57
+ defaults: UseCaseGroupDefaults;
58
+ }
59
+ export interface UseCaseGroup extends UseCaseGroupFields {
60
+ id: string;
61
+ orgId: string;
62
+ projectId: string;
63
+ revision: number;
64
+ createdAt: string;
65
+ updatedAt: string;
66
+ memberCount?: number;
67
+ archivedCount?: number;
68
+ members?: {
69
+ id: string;
70
+ name: string;
71
+ suppressable: boolean;
72
+ archivedAt: string | null;
73
+ }[];
74
+ }
75
+ export interface ListUseCaseGroupsOptions {
76
+ projectId?: string;
77
+ suppressable?: boolean;
78
+ page?: number;
79
+ perPage?: number;
80
+ }
81
+ export interface CreateUseCaseGroupInput extends UseCaseGroupFields {
82
+ projectId?: string;
83
+ confirmation?: UseCaseSettingsConfirmation;
84
+ }
85
+ export interface PreviewUseCaseGroupUpdateInput {
86
+ changes: Partial<UseCaseGroupFields>;
87
+ }
88
+ export interface UpdateUseCaseGroupInput extends PreviewUseCaseGroupUpdateInput {
89
+ expectedRevision: number;
90
+ /** Opaque, actor-scoped token, max 4096 characters; expires after 15 minutes. */
91
+ previewToken: string;
92
+ confirmation?: UseCaseSettingsConfirmation;
93
+ }
94
+ export interface UseCaseGroupUpdatePreview {
95
+ groupId: string;
96
+ revision: number;
97
+ changes: UseCaseGroupFields;
98
+ noOp: boolean;
99
+ projectMode: "test" | "production";
100
+ projectUpdatedAt: string;
101
+ affectedCount: number;
102
+ previewToken: string;
103
+ members: {
104
+ id: string;
105
+ name: string;
106
+ archived: boolean;
107
+ suppressable: boolean;
108
+ revision: number;
109
+ before: ResolvedUseCaseSettings;
110
+ after: ResolvedUseCaseSettings;
111
+ changedFields: UseCaseSettingKey[];
112
+ }[];
113
+ }
114
+ export interface PreviewUseCaseGroupMoveInput {
115
+ /** Null moves to Ungrouped. */
116
+ groupId: string | null;
117
+ /** A kind change uses this same comparison, even without changing groups. */
118
+ suppressable?: boolean;
119
+ }
120
+ export type UseCaseGroupMovePolicy = "inherit_destination" | "preserve_effective";
121
+ export interface MoveUseCaseToGroupInput extends PreviewUseCaseGroupMoveInput {
122
+ policy: UseCaseGroupMovePolicy;
123
+ /** Preview and apply must use the same credential. */
124
+ previewToken: string;
125
+ /** UUID. Omitted: generated once per call. Supplied values (including empty strings) are sent unchanged for server validation. Supply the same UUID for whole-call retries. */
126
+ idempotencyKey?: string;
127
+ confirmation?: UseCaseSettingsConfirmation;
128
+ }
129
+ export type UseCaseGroupMoveComparison = {
130
+ [K in UseCaseSettingKey]: {
131
+ key: K;
132
+ label: string;
133
+ current: ResolvedUseCaseSetting<K>;
134
+ destinationDefault: ResolvedUseCaseSetting<K>;
135
+ overridden: boolean;
136
+ inheritDestination: ResolvedUseCaseSetting<K>;
137
+ preserveEffective: ResolvedUseCaseSetting<K>;
138
+ };
139
+ }[UseCaseSettingKey];
140
+ export interface UseCaseGroupMovePreview {
141
+ useCaseId: string;
142
+ revision: number;
143
+ sourceGroupId: string | null;
144
+ destinationGroupId: string | null;
145
+ suppressable: boolean;
146
+ noOp: boolean;
147
+ rows: UseCaseGroupMoveComparison[];
148
+ current: ResolvedUseCaseSettings;
149
+ results: Record<UseCaseGroupMovePolicy, ResolvedUseCaseSettings>;
150
+ previewToken: string;
151
+ }
152
+ export interface UseCaseGroupMoveResult {
153
+ id: string;
154
+ groupId: string | null;
155
+ suppressable: boolean;
156
+ settingsRevision: number;
157
+ overrides: UseCaseSettingOverrides;
158
+ settings: ResolvedUseCaseSettings;
159
+ }
160
+ export type UseCaseGroupErrorCode = "GROUP_MOVE_STALE" | "GROUP_UPDATE_STALE" | "USE_CASE_SETTINGS_STALE" | "IDEMPOTENCY_KEY_CONFLICT" | "GROUP_NOT_EMPTY" | "GROUP_KIND_INCOMPATIBLE" | "GROUP_NAME_EXISTS" | "GROUP_PROJECT_MISMATCH" | "SETTING_REFERENCE_UNAVAILABLE" | "SENDER_DOMAIN_MISMATCH" | "REVISION_REQUIRED" | "USE_CASE_KIND_PREVIEW_REQUIRED" | "TRANSACTIONAL_TRACKING_CONFIRMATION_REQUIRED" | "PRODUCTION_CONFIRMATION_REQUIRED";
@@ -0,0 +1,6 @@
1
+ /*
2
+ Copyright (c) 2026 Bernier LLC
3
+ Licensed for use within the scope of the project it was delivered for.
4
+ Redistribution requires written consent from Bernier LLC.
5
+ */
6
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bernierllc/nevarmail",
3
- "version": "0.28.0",
3
+ "version": "0.30.0",
4
4
  "description": "Official client for the NevarMail v1 API. Typed, retries 429/5xx, throws NevarMailError on every non-2xx.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",