@rebirth-wd/shared 0.0.0-stage → 0.2.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 ADDED
@@ -0,0 +1,13 @@
1
+ # @rebirth-wd/shared
2
+
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - c50bbcd: Dashboard contract (additive): `DashboardEnquiry`, `DashboardEnquiryDetail`, `EnquiryList`, `EnquiryListQuery`, `EnquiryUpdate`, `NoteCreate`, `DashboardSummary`, `DashboardMe`, `DashboardSite`, `FormConfigUpdate`, `InboxView`, `OrgRole`, and `createDashboardClient()` for the authenticated `/v1/dashboard/*` routes.
8
+
9
+ ## 0.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 4aeeefa: First public release of the v1 API contract: `EnquiryCreate`, `EnquiryCreated`, `WidgetConfig`, `SiteFormConfig`, `Journey`, `ApiError`, `estimateFor()`, `fieldErrors()`, and a zod-free browser client at `@rebirth-wd/shared/client`.
package/README.md CHANGED
@@ -1,3 +1,15 @@
1
- # Temporary Holding Version
1
+ # @rebirth-wd/shared
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The Rebirth Pulse API contract: the only thing the widget, dashboard and API share.
4
+
5
+ - `@rebirth-wd/shared`: Zod schemas and types (`EnquiryCreate`, `WidgetConfig`, `SiteFormConfig`, `Journey`, `ApiError`), `estimateFor()`, `fieldErrors()`.
6
+ - `@rebirth-wd/shared/client`: a zod-free fetch client for browsers (`createPulseClient(baseUrl)`), plus the types.
7
+
8
+ Routes are versioned (`/v1/...`). Removing or renaming a field, tightening validation or changing a route's shape is a major release; additive optional fields are minor.
9
+
10
+ | Route | Purpose |
11
+ |---|---|
12
+ | `GET /v1/health` | `{ ok: true, version: "v1" }` |
13
+ | `GET /v1/widget/config?key=pk_...` | Site form config (projects with price bands, budgets, timings, consent text). Only for the site's allowed origins. Edge-cached 5 min. |
14
+ | `POST /v1/enquiries` | Body `EnquiryCreate` as JSON (`text/plain` is accepted, so browsers skip the CORS preflight). `201 EnquiryCreated`, `422 { error: "validation", fields }`, `403` for other origins or a failed Turnstile check. |
15
+ | `GET\|POST /v1/unsubscribe?t=...` | Signed one-click unsubscribe (Spam Act, RFC 8058). |
@@ -0,0 +1,66 @@
1
+ // src/constants.ts
2
+ var API_VERSION = "v1";
3
+ var JOURNEY_MAX_PAGES = 50;
4
+
5
+ // src/client.ts
6
+ var PulseApiError = class extends Error {
7
+ constructor(status, code, fields) {
8
+ super(`Pulse API ${status}: ${code}`);
9
+ this.status = status;
10
+ this.code = code;
11
+ this.fields = fields;
12
+ this.name = "PulseApiError";
13
+ }
14
+ status;
15
+ code;
16
+ fields;
17
+ };
18
+ function createPulseClient(baseUrl, fetchImpl = (...a) => fetch(...a)) {
19
+ const base = `${baseUrl.replace(/\/+$/, "")}/${API_VERSION}`;
20
+ async function call(path, init) {
21
+ const res = await fetchImpl(base + path, { ...init, headers: { accept: "application/json", ...init.headers ?? {} } });
22
+ const body = await res.json().catch(() => ({}));
23
+ if (!res.ok) throw new PulseApiError(res.status, body.error ?? "http_error", body.fields);
24
+ return body;
25
+ }
26
+ return {
27
+ getWidgetConfig: (siteKey, init) => call(`/widget/config?key=${encodeURIComponent(siteKey)}`, { method: "GET", signal: init?.signal ?? null }),
28
+ createEnquiry: (body, init) => call("/enquiries", { method: "POST", body: JSON.stringify(body), headers: { "content-type": "text/plain;charset=UTF-8" }, signal: init?.signal ?? null })
29
+ };
30
+ }
31
+ function createDashboardClient(baseUrl, getToken, fetchImpl = (...a) => fetch(...a)) {
32
+ const base = `${baseUrl.replace(/\/+$/, "")}/${API_VERSION}/dashboard`;
33
+ async function call(path, init = {}) {
34
+ const token = await getToken();
35
+ const headers = { accept: "application/json" };
36
+ if (token) headers.authorization = `Bearer ${token}`;
37
+ if (init.body) headers["content-type"] = "application/json";
38
+ const res = await fetchImpl(base + path, { ...init, headers });
39
+ const body = await res.json().catch(() => ({}));
40
+ if (!res.ok) throw new PulseApiError(res.status, body.error ?? "http_error", body.fields);
41
+ return body;
42
+ }
43
+ const json = (method, body) => ({ method, body: JSON.stringify(body) });
44
+ return {
45
+ me: () => call("/me"),
46
+ summary: () => call("/summary"),
47
+ listEnquiries: (q = {}) => {
48
+ const p = new URLSearchParams();
49
+ for (const [k, v] of Object.entries(q)) if (v !== void 0 && v !== "") p.set(k, String(v));
50
+ const qs = p.toString();
51
+ return call(`/enquiries${qs ? `?${qs}` : ""}`);
52
+ },
53
+ getEnquiry: (id) => call(`/enquiries/${encodeURIComponent(id)}`),
54
+ updateEnquiry: (id, patch) => call(`/enquiries/${encodeURIComponent(id)}`, json("PATCH", patch)),
55
+ addNote: (id, body) => call(`/enquiries/${encodeURIComponent(id)}/notes`, json("POST", { body })),
56
+ updateFormConfig: (siteId, config) => call(`/sites/${encodeURIComponent(siteId)}/form-config`, json("PUT", config))
57
+ };
58
+ }
59
+
60
+ export {
61
+ API_VERSION,
62
+ JOURNEY_MAX_PAGES,
63
+ PulseApiError,
64
+ createPulseClient,
65
+ createDashboardClient
66
+ };
@@ -0,0 +1,438 @@
1
+ import { z } from 'zod';
2
+
3
+ /** Zod-free constants, safe to import from the browser widget without pulling in zod. */
4
+ declare const API_VERSION: "v1";
5
+ declare const JOURNEY_MAX_PAGES = 50;
6
+
7
+ /**
8
+ * Core v1 contract (health, widget config, enquiries). Re-exported from index.ts.
9
+ * @rebirth-wd/shared: the only contract between widget, dashboard and API.
10
+ * Removing or renaming a field, tightening validation or changing a route's shape is a MAJOR bump.
11
+ * Additive optional fields are minor.
12
+ */
13
+
14
+ declare const HealthResponse: z.ZodObject<{
15
+ ok: z.ZodLiteral<true>;
16
+ version: z.ZodLiteral<"v1">;
17
+ }, z.core.$strip>;
18
+ type HealthResponse = z.infer<typeof HealthResponse>;
19
+ /** Public site key printed in the page that embeds the widget. Not a secret. */
20
+ declare const SiteKey: z.ZodString;
21
+ declare const ProjectOption: z.ZodObject<{
22
+ value: z.ZodString;
23
+ label: z.ZodString;
24
+ priceMin: z.ZodOptional<z.ZodNumber>;
25
+ priceMax: z.ZodOptional<z.ZodNumber>;
26
+ }, z.core.$strip>;
27
+ type ProjectOption = z.infer<typeof ProjectOption>;
28
+ declare const SiteFormConfig: z.ZodObject<{
29
+ projects: z.ZodArray<z.ZodObject<{
30
+ value: z.ZodString;
31
+ label: z.ZodString;
32
+ priceMin: z.ZodOptional<z.ZodNumber>;
33
+ priceMax: z.ZodOptional<z.ZodNumber>;
34
+ }, z.core.$strip>>;
35
+ budgets: z.ZodArray<z.ZodString>;
36
+ timings: z.ZodArray<z.ZodString>;
37
+ reassurance: z.ZodOptional<z.ZodString>;
38
+ thanks: z.ZodOptional<z.ZodObject<{
39
+ title: z.ZodString;
40
+ body: z.ZodString;
41
+ }, z.core.$strip>>;
42
+ }, z.core.$strip>;
43
+ type SiteFormConfig = z.infer<typeof SiteFormConfig>;
44
+ /** GET /v1/widget/config?key=pk_... */
45
+ declare const WidgetConfig: z.ZodObject<{
46
+ siteName: z.ZodString;
47
+ privacyUrl: z.ZodString;
48
+ consentText: z.ZodString;
49
+ form: z.ZodObject<{
50
+ projects: z.ZodArray<z.ZodObject<{
51
+ value: z.ZodString;
52
+ label: z.ZodString;
53
+ priceMin: z.ZodOptional<z.ZodNumber>;
54
+ priceMax: z.ZodOptional<z.ZodNumber>;
55
+ }, z.core.$strip>>;
56
+ budgets: z.ZodArray<z.ZodString>;
57
+ timings: z.ZodArray<z.ZodString>;
58
+ reassurance: z.ZodOptional<z.ZodString>;
59
+ thanks: z.ZodOptional<z.ZodObject<{
60
+ title: z.ZodString;
61
+ body: z.ZodString;
62
+ }, z.core.$strip>>;
63
+ }, z.core.$strip>;
64
+ turnstileSiteKey: z.ZodNullable<z.ZodString>;
65
+ }, z.core.$strip>;
66
+ type WidgetConfig = z.infer<typeof WidgetConfig>;
67
+ declare const Journey: z.ZodObject<{
68
+ landing: z.ZodOptional<z.ZodString>;
69
+ referrer: z.ZodOptional<z.ZodString>;
70
+ utm: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
71
+ device: z.ZodOptional<z.ZodEnum<{
72
+ mobile: "mobile";
73
+ tablet: "tablet";
74
+ desktop: "desktop";
75
+ }>>;
76
+ pages: z.ZodArray<z.ZodObject<{
77
+ path: z.ZodString;
78
+ at: z.ZodNumber;
79
+ }, z.core.$strip>>;
80
+ }, z.core.$strip>;
81
+ type Journey = z.infer<typeof Journey>;
82
+ /** POST /v1/enquiries */
83
+ declare const EnquiryCreate: z.ZodObject<{
84
+ siteKey: z.ZodString;
85
+ project: z.ZodString;
86
+ budget: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
87
+ timing: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
88
+ suburb: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
89
+ name: z.ZodString;
90
+ email: z.ZodString;
91
+ phone: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
92
+ message: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
93
+ consent: z.ZodLiteral<true>;
94
+ page: z.ZodPipe<z.ZodOptional<z.ZodString>, z.ZodTransform<string | undefined, string | undefined>>;
95
+ journey: z.ZodOptional<z.ZodObject<{
96
+ landing: z.ZodOptional<z.ZodString>;
97
+ referrer: z.ZodOptional<z.ZodString>;
98
+ utm: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
99
+ device: z.ZodOptional<z.ZodEnum<{
100
+ mobile: "mobile";
101
+ tablet: "tablet";
102
+ desktop: "desktop";
103
+ }>>;
104
+ pages: z.ZodArray<z.ZodObject<{
105
+ path: z.ZodString;
106
+ at: z.ZodNumber;
107
+ }, z.core.$strip>>;
108
+ }, z.core.$strip>>;
109
+ turnstileToken: z.ZodOptional<z.ZodString>;
110
+ company_website: z.ZodOptional<z.ZodString>;
111
+ }, z.core.$strip>;
112
+ type EnquiryCreate = z.input<typeof EnquiryCreate>;
113
+ type EnquiryCreateParsed = z.output<typeof EnquiryCreate>;
114
+ declare const EnquiryCreated: z.ZodObject<{
115
+ id: z.ZodString;
116
+ estimate: z.ZodNullable<z.ZodObject<{
117
+ min: z.ZodNumber;
118
+ max: z.ZodNumber;
119
+ }, z.core.$strip>>;
120
+ }, z.core.$strip>;
121
+ type EnquiryCreated = z.infer<typeof EnquiryCreated>;
122
+ declare const ENQUIRY_STATUSES: readonly ["new", "contacted", "quoted", "won", "lost"];
123
+ declare const EnquiryStatus: z.ZodEnum<{
124
+ new: "new";
125
+ contacted: "contacted";
126
+ quoted: "quoted";
127
+ won: "won";
128
+ lost: "lost";
129
+ }>;
130
+ type EnquiryStatus = z.infer<typeof EnquiryStatus>;
131
+ declare const ApiError: z.ZodObject<{
132
+ error: z.ZodString;
133
+ fields: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
134
+ }, z.core.$strip>;
135
+ type ApiError = z.infer<typeof ApiError>;
136
+ /** Turns a Zod error into `{ field: message }`, first message per field. */
137
+ declare function fieldErrors(error: z.ZodError): Record<string, string>;
138
+ /** Ballpark range for a project, or null when the business set none. */
139
+ declare function estimateFor(form: SiteFormConfig, project: string): {
140
+ min: number;
141
+ max: number;
142
+ } | null;
143
+
144
+ /**
145
+ * Dashboard contract (Phase 6). Routes under /v1/dashboard/*, authenticated with a Clerk session token
146
+ * (`Authorization: Bearer <token>`). The tenant comes only from the token's active Organization (D7, D25).
147
+ */
148
+
149
+ declare const OrgRole: z.ZodEnum<{
150
+ admin: "admin";
151
+ member: "member";
152
+ }>;
153
+ type OrgRole = z.infer<typeof OrgRole>;
154
+ /** One lead as the dashboard sees it. Dates are ISO strings. */
155
+ declare const DashboardEnquiry: z.ZodObject<{
156
+ id: z.ZodString;
157
+ siteId: z.ZodString;
158
+ status: z.ZodEnum<{
159
+ new: "new";
160
+ contacted: "contacted";
161
+ quoted: "quoted";
162
+ won: "won";
163
+ lost: "lost";
164
+ }>;
165
+ project: z.ZodString;
166
+ projectLabel: z.ZodString;
167
+ budget: z.ZodNullable<z.ZodString>;
168
+ timing: z.ZodNullable<z.ZodString>;
169
+ suburb: z.ZodNullable<z.ZodString>;
170
+ name: z.ZodString;
171
+ email: z.ZodString;
172
+ phone: z.ZodNullable<z.ZodString>;
173
+ message: z.ZodNullable<z.ZodString>;
174
+ estimate: z.ZodNullable<z.ZodObject<{
175
+ min: z.ZodNumber;
176
+ max: z.ZodNumber;
177
+ }, z.core.$strip>>;
178
+ value: z.ZodNullable<z.ZodNumber>;
179
+ overdue: z.ZodBoolean;
180
+ createdAt: z.ZodString;
181
+ contactedAt: z.ZodNullable<z.ZodString>;
182
+ }, z.core.$strip>;
183
+ type DashboardEnquiry = z.infer<typeof DashboardEnquiry>;
184
+ /** GET /v1/dashboard/enquiries/:id adds consent evidence, the journey and notes. */
185
+ declare const EnquiryNote: z.ZodObject<{
186
+ id: z.ZodString;
187
+ authorId: z.ZodString;
188
+ body: z.ZodString;
189
+ createdAt: z.ZodString;
190
+ }, z.core.$strip>;
191
+ type EnquiryNote = z.infer<typeof EnquiryNote>;
192
+ declare const DashboardEnquiryDetail: z.ZodObject<{
193
+ id: z.ZodString;
194
+ siteId: z.ZodString;
195
+ status: z.ZodEnum<{
196
+ new: "new";
197
+ contacted: "contacted";
198
+ quoted: "quoted";
199
+ won: "won";
200
+ lost: "lost";
201
+ }>;
202
+ project: z.ZodString;
203
+ projectLabel: z.ZodString;
204
+ budget: z.ZodNullable<z.ZodString>;
205
+ timing: z.ZodNullable<z.ZodString>;
206
+ suburb: z.ZodNullable<z.ZodString>;
207
+ name: z.ZodString;
208
+ email: z.ZodString;
209
+ phone: z.ZodNullable<z.ZodString>;
210
+ message: z.ZodNullable<z.ZodString>;
211
+ estimate: z.ZodNullable<z.ZodObject<{
212
+ min: z.ZodNumber;
213
+ max: z.ZodNumber;
214
+ }, z.core.$strip>>;
215
+ value: z.ZodNullable<z.ZodNumber>;
216
+ overdue: z.ZodBoolean;
217
+ createdAt: z.ZodString;
218
+ contactedAt: z.ZodNullable<z.ZodString>;
219
+ consentText: z.ZodString;
220
+ consentedAt: z.ZodString;
221
+ journey: z.ZodNullable<z.ZodObject<{
222
+ landing: z.ZodOptional<z.ZodOptional<z.ZodString>>;
223
+ referrer: z.ZodOptional<z.ZodOptional<z.ZodString>>;
224
+ utm: z.ZodOptional<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>>;
225
+ device: z.ZodOptional<z.ZodOptional<z.ZodEnum<{
226
+ mobile: "mobile";
227
+ tablet: "tablet";
228
+ desktop: "desktop";
229
+ }>>>;
230
+ pages: z.ZodOptional<z.ZodArray<z.ZodObject<{
231
+ path: z.ZodString;
232
+ at: z.ZodNumber;
233
+ }, z.core.$strip>>>;
234
+ page: z.ZodOptional<z.ZodOptional<z.ZodString>>;
235
+ }, z.core.$strip>>;
236
+ notes: z.ZodArray<z.ZodObject<{
237
+ id: z.ZodString;
238
+ authorId: z.ZodString;
239
+ body: z.ZodString;
240
+ createdAt: z.ZodString;
241
+ }, z.core.$strip>>;
242
+ }, z.core.$strip>;
243
+ type DashboardEnquiryDetail = z.infer<typeof DashboardEnquiryDetail>;
244
+ /** Saved views of the inbox. `todo` = new + contacted + quoted. */
245
+ declare const INBOX_VIEWS: readonly ["todo", "new", "contacted", "quoted", "won", "lost", "all"];
246
+ declare const InboxView: z.ZodEnum<{
247
+ new: "new";
248
+ contacted: "contacted";
249
+ quoted: "quoted";
250
+ won: "won";
251
+ lost: "lost";
252
+ todo: "todo";
253
+ all: "all";
254
+ }>;
255
+ type InboxView = z.infer<typeof InboxView>;
256
+ /** GET /v1/dashboard/enquiries?view=todo&q=bulimba&cursor=...&limit=50 */
257
+ declare const EnquiryListQuery: z.ZodObject<{
258
+ view: z.ZodDefault<z.ZodEnum<{
259
+ new: "new";
260
+ contacted: "contacted";
261
+ quoted: "quoted";
262
+ won: "won";
263
+ lost: "lost";
264
+ todo: "todo";
265
+ all: "all";
266
+ }>>;
267
+ q: z.ZodOptional<z.ZodString>;
268
+ cursor: z.ZodOptional<z.ZodString>;
269
+ limit: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
270
+ }, z.core.$strip>;
271
+ type EnquiryListQuery = z.input<typeof EnquiryListQuery>;
272
+ declare const EnquiryList: z.ZodObject<{
273
+ items: z.ZodArray<z.ZodObject<{
274
+ id: z.ZodString;
275
+ siteId: z.ZodString;
276
+ status: z.ZodEnum<{
277
+ new: "new";
278
+ contacted: "contacted";
279
+ quoted: "quoted";
280
+ won: "won";
281
+ lost: "lost";
282
+ }>;
283
+ project: z.ZodString;
284
+ projectLabel: z.ZodString;
285
+ budget: z.ZodNullable<z.ZodString>;
286
+ timing: z.ZodNullable<z.ZodString>;
287
+ suburb: z.ZodNullable<z.ZodString>;
288
+ name: z.ZodString;
289
+ email: z.ZodString;
290
+ phone: z.ZodNullable<z.ZodString>;
291
+ message: z.ZodNullable<z.ZodString>;
292
+ estimate: z.ZodNullable<z.ZodObject<{
293
+ min: z.ZodNumber;
294
+ max: z.ZodNumber;
295
+ }, z.core.$strip>>;
296
+ value: z.ZodNullable<z.ZodNumber>;
297
+ overdue: z.ZodBoolean;
298
+ createdAt: z.ZodString;
299
+ contactedAt: z.ZodNullable<z.ZodString>;
300
+ }, z.core.$strip>>;
301
+ nextCursor: z.ZodNullable<z.ZodString>;
302
+ }, z.core.$strip>;
303
+ type EnquiryList = z.infer<typeof EnquiryList>;
304
+ /** PATCH /v1/dashboard/enquiries/:id */
305
+ declare const EnquiryUpdate: z.ZodObject<{
306
+ status: z.ZodOptional<z.ZodEnum<{
307
+ new: "new";
308
+ contacted: "contacted";
309
+ quoted: "quoted";
310
+ won: "won";
311
+ lost: "lost";
312
+ }>>;
313
+ value: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
314
+ }, z.core.$strip>;
315
+ type EnquiryUpdate = z.infer<typeof EnquiryUpdate>;
316
+ /** POST /v1/dashboard/enquiries/:id/notes */
317
+ declare const NoteCreate: z.ZodObject<{
318
+ body: z.ZodString;
319
+ }, z.core.$strip>;
320
+ type NoteCreate = z.infer<typeof NoteCreate>;
321
+ /** GET /v1/dashboard/summary: the Today tiles. */
322
+ declare const DashboardSummary: z.ZodObject<{
323
+ newCount: z.ZodNumber;
324
+ overdueCount: z.ZodNumber;
325
+ openCount: z.ZodNumber;
326
+ pipelineValue: z.ZodNumber;
327
+ }, z.core.$strip>;
328
+ type DashboardSummary = z.infer<typeof DashboardSummary>;
329
+ declare const DashboardSite: z.ZodObject<{
330
+ id: z.ZodString;
331
+ name: z.ZodString;
332
+ keys: z.ZodArray<z.ZodString>;
333
+ allowedOrigins: z.ZodArray<z.ZodString>;
334
+ privacyUrl: z.ZodString;
335
+ formConfig: z.ZodObject<{
336
+ projects: z.ZodArray<z.ZodObject<{
337
+ value: z.ZodString;
338
+ label: z.ZodString;
339
+ priceMin: z.ZodOptional<z.ZodNumber>;
340
+ priceMax: z.ZodOptional<z.ZodNumber>;
341
+ }, z.core.$strip>>;
342
+ budgets: z.ZodArray<z.ZodString>;
343
+ timings: z.ZodArray<z.ZodString>;
344
+ reassurance: z.ZodOptional<z.ZodString>;
345
+ thanks: z.ZodOptional<z.ZodObject<{
346
+ title: z.ZodString;
347
+ body: z.ZodString;
348
+ }, z.core.$strip>>;
349
+ }, z.core.$strip>;
350
+ }, z.core.$strip>;
351
+ type DashboardSite = z.infer<typeof DashboardSite>;
352
+ /** GET /v1/dashboard/me */
353
+ declare const DashboardMe: z.ZodObject<{
354
+ tenant: z.ZodObject<{
355
+ id: z.ZodString;
356
+ name: z.ZodString;
357
+ }, z.core.$strip>;
358
+ role: z.ZodEnum<{
359
+ admin: "admin";
360
+ member: "member";
361
+ }>;
362
+ sites: z.ZodArray<z.ZodObject<{
363
+ id: z.ZodString;
364
+ name: z.ZodString;
365
+ keys: z.ZodArray<z.ZodString>;
366
+ allowedOrigins: z.ZodArray<z.ZodString>;
367
+ privacyUrl: z.ZodString;
368
+ formConfig: z.ZodObject<{
369
+ projects: z.ZodArray<z.ZodObject<{
370
+ value: z.ZodString;
371
+ label: z.ZodString;
372
+ priceMin: z.ZodOptional<z.ZodNumber>;
373
+ priceMax: z.ZodOptional<z.ZodNumber>;
374
+ }, z.core.$strip>>;
375
+ budgets: z.ZodArray<z.ZodString>;
376
+ timings: z.ZodArray<z.ZodString>;
377
+ reassurance: z.ZodOptional<z.ZodString>;
378
+ thanks: z.ZodOptional<z.ZodObject<{
379
+ title: z.ZodString;
380
+ body: z.ZodString;
381
+ }, z.core.$strip>>;
382
+ }, z.core.$strip>;
383
+ }, z.core.$strip>>;
384
+ }, z.core.$strip>;
385
+ type DashboardMe = z.infer<typeof DashboardMe>;
386
+ /** PUT /v1/dashboard/sites/:id/form-config (org admins only). Body: SiteFormConfig. Returns DashboardSite. */
387
+ declare const FormConfigUpdate: z.ZodObject<{
388
+ projects: z.ZodArray<z.ZodObject<{
389
+ value: z.ZodString;
390
+ label: z.ZodString;
391
+ priceMin: z.ZodOptional<z.ZodNumber>;
392
+ priceMax: z.ZodOptional<z.ZodNumber>;
393
+ }, z.core.$strip>>;
394
+ budgets: z.ZodArray<z.ZodString>;
395
+ timings: z.ZodArray<z.ZodString>;
396
+ reassurance: z.ZodOptional<z.ZodString>;
397
+ thanks: z.ZodOptional<z.ZodObject<{
398
+ title: z.ZodString;
399
+ body: z.ZodString;
400
+ }, z.core.$strip>>;
401
+ }, z.core.$strip>;
402
+ /** 403 body when the signed-in Organization is not linked to a Pulse tenant yet. */
403
+ declare const OrgNotLinked: z.ZodObject<{
404
+ error: z.ZodLiteral<"org_not_linked">;
405
+ orgId: z.ZodString;
406
+ }, z.core.$strip>;
407
+
408
+ declare class PulseApiError extends Error {
409
+ status: number;
410
+ code: string;
411
+ fields?: Record<string, string> | undefined;
412
+ constructor(status: number, code: string, fields?: Record<string, string> | undefined);
413
+ }
414
+ interface PulseClient {
415
+ getWidgetConfig(siteKey: string, init?: {
416
+ signal?: AbortSignal;
417
+ }): Promise<WidgetConfig>;
418
+ createEnquiry(body: EnquiryCreate, init?: {
419
+ signal?: AbortSignal;
420
+ }): Promise<EnquiryCreated>;
421
+ }
422
+ /** Tiny fetch client for the v1 API. No runtime validation here, to keep the widget small; the API validates.
423
+ * POST bodies are JSON sent as text/plain: a CORS simple request, so browsers skip the preflight. */
424
+ declare function createPulseClient(baseUrl: string, fetchImpl?: typeof fetch): PulseClient;
425
+ interface DashboardClient {
426
+ me(): Promise<DashboardMe>;
427
+ summary(): Promise<DashboardSummary>;
428
+ listEnquiries(query?: EnquiryListQuery): Promise<EnquiryList>;
429
+ getEnquiry(id: string): Promise<DashboardEnquiryDetail>;
430
+ updateEnquiry(id: string, patch: EnquiryUpdate): Promise<DashboardEnquiry>;
431
+ addNote(id: string, body: string): Promise<EnquiryNote>;
432
+ updateFormConfig(siteId: string, config: SiteFormConfig): Promise<DashboardSite>;
433
+ }
434
+ /** Dashboard client. `getToken` returns the current Clerk session token (or null when signed out). */
435
+ declare function createDashboardClient(baseUrl: string, getToken: () => Promise<string | null>, fetchImpl?: typeof fetch): DashboardClient;
436
+
437
+ export { ApiError as A, API_VERSION, DashboardEnquiry as D, ENQUIRY_STATUSES as E, EnquiryCreate, EnquiryCreated, FormConfigUpdate as F, HealthResponse as H, INBOX_VIEWS as I, JOURNEY_MAX_PAGES, Journey, NoteCreate as N, OrgNotLinked as O, ProjectOption, PulseApiError, SiteKey as S, SiteFormConfig, WidgetConfig, DashboardEnquiryDetail as a, DashboardMe as b, DashboardSite as c, createDashboardClient, createPulseClient, DashboardSummary as d, EnquiryList as f, EnquiryListQuery as g, EnquiryNote as h, EnquiryStatus as i, EnquiryUpdate as j, InboxView as k, OrgRole as l, estimateFor as m, fieldErrors as n };
438
+ export type { DashboardClient, PulseClient, EnquiryCreateParsed as e };
package/dist/client.js ADDED
@@ -0,0 +1,14 @@
1
+ import {
2
+ API_VERSION,
3
+ JOURNEY_MAX_PAGES,
4
+ PulseApiError,
5
+ createDashboardClient,
6
+ createPulseClient
7
+ } from "./chunk-NNBGP7NE.js";
8
+ export {
9
+ API_VERSION,
10
+ JOURNEY_MAX_PAGES,
11
+ PulseApiError,
12
+ createDashboardClient,
13
+ createPulseClient
14
+ };
@@ -0,0 +1,2 @@
1
+ export { API_VERSION, A as ApiError, DashboardClient, D as DashboardEnquiry, a as DashboardEnquiryDetail, b as DashboardMe, c as DashboardSite, d as DashboardSummary, E as ENQUIRY_STATUSES, EnquiryCreate, e as EnquiryCreateParsed, EnquiryCreated, f as EnquiryList, g as EnquiryListQuery, h as EnquiryNote, i as EnquiryStatus, j as EnquiryUpdate, F as FormConfigUpdate, H as HealthResponse, I as INBOX_VIEWS, k as InboxView, JOURNEY_MAX_PAGES, Journey, N as NoteCreate, O as OrgNotLinked, l as OrgRole, ProjectOption, PulseApiError, PulseClient, SiteFormConfig, S as SiteKey, WidgetConfig, createDashboardClient, createPulseClient, m as estimateFor, n as fieldErrors } from './client.js';
2
+ import 'zod';
package/dist/index.js ADDED
@@ -0,0 +1,204 @@
1
+ import {
2
+ API_VERSION,
3
+ JOURNEY_MAX_PAGES,
4
+ PulseApiError,
5
+ createDashboardClient,
6
+ createPulseClient
7
+ } from "./chunk-NNBGP7NE.js";
8
+
9
+ // src/core.ts
10
+ import { z } from "zod";
11
+ var HealthResponse = z.object({
12
+ ok: z.literal(true),
13
+ version: z.literal(API_VERSION)
14
+ });
15
+ var SiteKey = z.string().regex(/^pk_[a-z0-9_]{8,64}$/, "invalid site key");
16
+ var ProjectOption = z.object({
17
+ value: z.string().min(1).max(40),
18
+ label: z.string().min(1).max(60),
19
+ /** Ballpark range in whole dollars, from the business's recent jobs. Omit both to show no range. */
20
+ priceMin: z.number().int().nonnegative().optional(),
21
+ priceMax: z.number().int().nonnegative().optional()
22
+ });
23
+ var SiteFormConfig = z.object({
24
+ projects: z.array(ProjectOption).min(1).max(20),
25
+ budgets: z.array(z.string().min(1).max(40)).max(20),
26
+ timings: z.array(z.string().min(1).max(40)).max(20),
27
+ /** Shown under the suburb question, e.g. "Most bathrooms take 3 to 4 weeks." */
28
+ reassurance: z.string().max(200).optional(),
29
+ /** Thank-you heading and body after a successful send. */
30
+ thanks: z.object({ title: z.string().max(80), body: z.string().max(400) }).optional()
31
+ });
32
+ var WidgetConfig = z.object({
33
+ siteName: z.string(),
34
+ privacyUrl: z.string().url(),
35
+ consentText: z.string(),
36
+ form: SiteFormConfig,
37
+ /** When set, the widget must render Turnstile and send its token. */
38
+ turnstileSiteKey: z.string().nullable()
39
+ });
40
+ var Journey = z.object({
41
+ landing: z.string().max(500).optional(),
42
+ referrer: z.string().max(500).optional(),
43
+ utm: z.record(z.string().max(40), z.string().max(200)).optional(),
44
+ device: z.enum(["mobile", "tablet", "desktop"]).optional(),
45
+ pages: z.array(z.object({ path: z.string().max(300), at: z.number().int().nonnegative() })).max(JOURNEY_MAX_PAGES)
46
+ });
47
+ var trimmed = (max) => z.string().trim().max(max);
48
+ var optionalText = (max) => trimmed(max).optional().transform((v) => v ? v : void 0);
49
+ var EnquiryCreate = z.object({
50
+ siteKey: SiteKey,
51
+ project: trimmed(40).min(1, "Choose the type of project."),
52
+ budget: optionalText(40),
53
+ timing: optionalText(40),
54
+ suburb: optionalText(80),
55
+ name: trimmed(100).min(1, "Enter your name."),
56
+ email: z.string().trim().max(200).email("Enter an email address like name@example.com."),
57
+ phone: optionalText(30).refine((v) => !v || /^[+\d][\d\s()-]{7,}$/.test(v), "Enter a phone number, or leave it blank."),
58
+ message: optionalText(3e3),
59
+ /** Must be true. The consent text itself is taken from the site's config on the server. */
60
+ consent: z.literal(true, { error: "Tick the box so we can use your details to reply." }),
61
+ page: optionalText(300),
62
+ journey: Journey.optional(),
63
+ turnstileToken: z.string().max(4096).optional(),
64
+ /** Honeypot. Humans leave it empty. */
65
+ company_website: z.string().max(200).optional()
66
+ });
67
+ var EnquiryCreated = z.object({
68
+ id: z.string().uuid(),
69
+ estimate: z.object({ min: z.number().int(), max: z.number().int() }).nullable()
70
+ });
71
+ var ENQUIRY_STATUSES = ["new", "contacted", "quoted", "won", "lost"];
72
+ var EnquiryStatus = z.enum(ENQUIRY_STATUSES);
73
+ var ApiError = z.object({
74
+ error: z.string(),
75
+ /** Field messages, safe to show to the visitor. */
76
+ fields: z.record(z.string(), z.string()).optional()
77
+ });
78
+ function fieldErrors(error) {
79
+ const out = {};
80
+ for (const issue of error.issues) {
81
+ const key = String(issue.path[0] ?? "_");
82
+ out[key] ??= issue.message;
83
+ }
84
+ return out;
85
+ }
86
+ function estimateFor(form, project) {
87
+ const p = form.projects.find((o) => o.value === project);
88
+ if (!p || p.priceMin == null || p.priceMax == null) return null;
89
+ return { min: p.priceMin, max: p.priceMax };
90
+ }
91
+
92
+ // src/dashboard.ts
93
+ import { z as z2 } from "zod";
94
+ var OrgRole = z2.enum(["admin", "member"]);
95
+ var DashboardEnquiry = z2.object({
96
+ id: z2.string().uuid(),
97
+ siteId: z2.string().uuid(),
98
+ status: EnquiryStatus,
99
+ project: z2.string(),
100
+ projectLabel: z2.string(),
101
+ budget: z2.string().nullable(),
102
+ timing: z2.string().nullable(),
103
+ suburb: z2.string().nullable(),
104
+ name: z2.string(),
105
+ email: z2.string(),
106
+ phone: z2.string().nullable(),
107
+ message: z2.string().nullable(),
108
+ estimate: z2.object({ min: z2.number().int(), max: z2.number().int() }).nullable(),
109
+ /** Job value in whole dollars, set by the business. */
110
+ value: z2.number().int().nullable(),
111
+ /** `new` and older than 24 h (same rule as the follow-up cron). */
112
+ overdue: z2.boolean(),
113
+ createdAt: z2.string(),
114
+ contactedAt: z2.string().nullable()
115
+ });
116
+ var EnquiryNote = z2.object({ id: z2.string().uuid(), authorId: z2.string(), body: z2.string(), createdAt: z2.string() });
117
+ var DashboardEnquiryDetail = DashboardEnquiry.extend({
118
+ consentText: z2.string(),
119
+ consentedAt: z2.string(),
120
+ journey: Journey.extend({ page: z2.string().optional() }).partial().nullable(),
121
+ notes: z2.array(EnquiryNote)
122
+ });
123
+ var INBOX_VIEWS = ["todo", "new", "contacted", "quoted", "won", "lost", "all"];
124
+ var InboxView = z2.enum(INBOX_VIEWS);
125
+ var EnquiryListQuery = z2.object({
126
+ view: InboxView.default("todo"),
127
+ q: z2.string().trim().max(100).optional(),
128
+ cursor: z2.string().max(200).optional(),
129
+ limit: z2.coerce.number().int().min(1).max(100).default(50)
130
+ });
131
+ var EnquiryList = z2.object({ items: z2.array(DashboardEnquiry), nextCursor: z2.string().nullable() });
132
+ var EnquiryUpdate = z2.object({
133
+ status: EnquiryStatus.optional(),
134
+ value: z2.number().int().nonnegative().max(1e8).nullable().optional()
135
+ }).refine((v) => v.status !== void 0 || v.value !== void 0, "Nothing to update.");
136
+ var NoteCreate = z2.object({ body: z2.string().trim().min(1, "Write a note first.").max(4e3) });
137
+ var DashboardSummary = z2.object({
138
+ newCount: z2.number().int(),
139
+ overdueCount: z2.number().int(),
140
+ openCount: z2.number().int(),
141
+ /** Whole dollars: the business's value where set, else the ballpark midpoint, over open leads. */
142
+ pipelineValue: z2.number().int()
143
+ });
144
+ var DashboardSite = z2.object({
145
+ id: z2.string().uuid(),
146
+ name: z2.string(),
147
+ /** Public site keys used in the embed snippet. */
148
+ keys: z2.array(z2.string()),
149
+ allowedOrigins: z2.array(z2.string()),
150
+ privacyUrl: z2.string(),
151
+ formConfig: SiteFormConfig
152
+ });
153
+ var DashboardMe = z2.object({
154
+ tenant: z2.object({ id: z2.string().uuid(), name: z2.string() }),
155
+ role: OrgRole,
156
+ sites: z2.array(DashboardSite)
157
+ });
158
+ var FormConfigUpdate = SiteFormConfig.superRefine((cfg, ctx) => {
159
+ const seen = /* @__PURE__ */ new Set();
160
+ cfg.projects.forEach((p, i) => {
161
+ if (seen.has(p.value)) ctx.addIssue({ code: "custom", path: ["projects", i, "value"], message: "Each project needs a different value." });
162
+ seen.add(p.value);
163
+ if (p.priceMin == null !== (p.priceMax == null))
164
+ ctx.addIssue({ code: "custom", path: ["projects", i, "priceMax"], message: "Set both ends of the price range, or neither." });
165
+ if (p.priceMin != null && p.priceMax != null && p.priceMin > p.priceMax)
166
+ ctx.addIssue({ code: "custom", path: ["projects", i, "priceMax"], message: "The top of the range must be at least the bottom." });
167
+ });
168
+ });
169
+ var OrgNotLinked = z2.object({ error: z2.literal("org_not_linked"), orgId: z2.string() });
170
+ export {
171
+ API_VERSION,
172
+ ApiError,
173
+ DashboardEnquiry,
174
+ DashboardEnquiryDetail,
175
+ DashboardMe,
176
+ DashboardSite,
177
+ DashboardSummary,
178
+ ENQUIRY_STATUSES,
179
+ EnquiryCreate,
180
+ EnquiryCreated,
181
+ EnquiryList,
182
+ EnquiryListQuery,
183
+ EnquiryNote,
184
+ EnquiryStatus,
185
+ EnquiryUpdate,
186
+ FormConfigUpdate,
187
+ HealthResponse,
188
+ INBOX_VIEWS,
189
+ InboxView,
190
+ JOURNEY_MAX_PAGES,
191
+ Journey,
192
+ NoteCreate,
193
+ OrgNotLinked,
194
+ OrgRole,
195
+ ProjectOption,
196
+ PulseApiError,
197
+ SiteFormConfig,
198
+ SiteKey,
199
+ WidgetConfig,
200
+ createDashboardClient,
201
+ createPulseClient,
202
+ estimateFor,
203
+ fieldErrors
204
+ };
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@rebirth-wd/shared",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0",
4
+ "description": "Rebirth Pulse API contract: Zod schemas, types and a tiny fetch client for the v1 API.",
5
+ "license": "UNLICENSED",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ },
12
+ "./client": {
13
+ "types": "./dist/client.d.ts",
14
+ "import": "./dist/client.js"
15
+ },
16
+ "./package.json": "./package.json"
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "CHANGELOG.md"
22
+ ],
23
+ "sideEffects": false,
24
+ "dependencies": {
25
+ "zod": "^4.6.7"
26
+ },
27
+ "devDependencies": {
28
+ "eslint": "^10.16.0",
29
+ "typescript": "~6.0.3",
30
+ "vitest": "^5.1.2",
31
+ "tsup": "^9.0.0",
32
+ "@rebirth-wd/eslint-config": "0.0.0",
33
+ "@rebirth-wd/tsconfig": "0.0.0"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/rebirth-wd/rebirth-pulse.git",
38
+ "directory": "packages/shared"
39
+ },
40
+ "homepage": "https://github.com/rebirth-wd/rebirth-pulse/tree/main/packages/shared#readme",
41
+ "publishConfig": {
42
+ "access": "public",
43
+ "provenance": true
44
+ },
45
+ "scripts": {
46
+ "build": "tsup src/index.ts src/client.ts --format esm --dts --clean",
47
+ "lint": "eslint .",
48
+ "typecheck": "tsc -p tsconfig.json",
49
+ "test": "vitest run"
50
+ }
6
51
  }