@recovr/sdk 1.0.0 → 1.1.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.
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Add a lead from another system (a quiz, a website form, a CRM).
3
+ *
4
+ * RECOVR_TOKEN=rcvr_sk_… npx tsx examples/add-lead.ts <your-lead-id> <email> [first name] [last name]
5
+ *
6
+ * Needs `clients:write` and `contact_details:read`. Recovr matches the person to an existing
7
+ * client by email (or phone and first name), or adds them as a new lead, and starts your Lead
8
+ * received workflows once. `external_id` is your own id for the lead, so running this twice for
9
+ * the same lead (a retry, a re-run of a sync) adds it once.
10
+ */
11
+ import { createRecovrClient } from "@recovr/sdk";
12
+
13
+ const [leadId, email, firstName, lastName] = process.argv.slice(2);
14
+ if (!leadId || !email) {
15
+ throw new Error("Usage: add-lead.ts <your-lead-id> <email> [first name] [last name]");
16
+ }
17
+
18
+ const recovr = createRecovrClient({ token: env("RECOVR_TOKEN"), baseUrl: process.env.RECOVR_BASE_URL });
19
+
20
+ const { data } = await recovr.createLead({
21
+ body: {
22
+ external_id: leadId,
23
+ email,
24
+ first_name: firstName,
25
+ last_name: lastName,
26
+ source_label: "Website quiz",
27
+ fields: { "What's your main goal?": "Build strength", "How many days a week can you train?": "3" },
28
+ },
29
+ });
30
+
31
+ const lead = data.data;
32
+ if (!lead.created) {
33
+ console.log(`Lead ${leadId} was already added (client ${lead.client_id}); nothing changed.`);
34
+ } else {
35
+ console.log(
36
+ `Added lead ${lead.lead_id} for ${lead.client === "existing" ? "an existing" : "a new"} client ${lead.client_id}` +
37
+ (lead.live ? "" : " (older than a day, so no workflows started)"),
38
+ );
39
+ }
40
+
41
+ function env(name: string): string {
42
+ const value = process.env[name];
43
+ if (!value) throw new Error(`Set ${name}`);
44
+ return value;
45
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Add a note to a client from your own system (a CRM, a coaching app, a form tool).
3
+ *
4
+ * RECOVR_TOKEN=rcvr_sk_… npx tsx examples/add-note.ts <booking-platform-client-id> <your-note-id> "Note text"
5
+ *
6
+ * Needs `clients:read` and `notes:write`. The client is found by their booking platform's id,
7
+ * and the note's Idempotency-Key is built from your own note id, so running this twice for the
8
+ * same note (a retry, a re-run of a sync) adds it once.
9
+ */
10
+ import { createRecovrClient } from "@recovr/sdk";
11
+
12
+ const [externalClientId, sourceNoteId, content] = process.argv.slice(2);
13
+ if (!externalClientId || !sourceNoteId || !content) {
14
+ throw new Error("Usage: add-note.ts <booking-platform-client-id> <your-note-id> <text>");
15
+ }
16
+
17
+ const recovr = createRecovrClient({ token: env("RECOVR_TOKEN"), baseUrl: process.env.RECOVR_BASE_URL });
18
+
19
+ const { data: matches } = await recovr.listClients({ query: { external_client_id: externalClientId, limit: 1 } });
20
+ const client = matches.data[0];
21
+ if (!client) throw new Error(`No client with booking-platform id ${externalClientId} at this location`);
22
+
23
+ const { data: created } = await recovr.createClientNote({
24
+ path: { id: client.id },
25
+ body: { content },
26
+ headers: { "Idempotency-Key": `my-crm-note-${sourceNoteId}` },
27
+ });
28
+ console.log(`Added note ${created.data.id} to ${client.name}`);
29
+
30
+ function env(name: string): string {
31
+ const value = process.env[name];
32
+ if (!value) throw new Error(`Set ${name}`);
33
+ return value;
34
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Nightly health-score export: every client at the token's location, one row each, as CSV.
3
+ *
4
+ * RECOVR_TOKEN=rcvr_sk_… npx tsx examples/export-health-scores.ts health-scores.csv
5
+ *
6
+ * Needs `clients:read`. Schedule it with cron, a GitHub Action or your warehouse's loader.
7
+ */
8
+ import { createWriteStream } from "node:fs";
9
+ import { once } from "node:events";
10
+ import { createRecovrClient, paginate } from "@recovr/sdk";
11
+
12
+ const recovr = createRecovrClient({ token: env("RECOVR_TOKEN"), baseUrl: process.env.RECOVR_BASE_URL });
13
+ const output = process.argv[2] ?? `health-scores-${new Date().toISOString().slice(0, 10)}.csv`;
14
+
15
+ const columns = ["id", "name", "client_status", "health_score", "risk_level", "last_attended_on"] as const;
16
+ const file = createWriteStream(output);
17
+ file.write(`${columns.join(",")}\n`);
18
+
19
+ let rows = 0;
20
+ for await (const client of paginate(recovr.listClients, { query: { limit: 200 } })) {
21
+ file.write(`${columns.map((column) => csv(client[column])).join(",")}\n`);
22
+ rows += 1;
23
+ }
24
+ file.end();
25
+ await once(file, "finish");
26
+ console.error(`Wrote ${rows} clients to ${output}`);
27
+
28
+ function csv(value: unknown): string {
29
+ if (value === null || value === undefined) return "";
30
+ const text = String(value);
31
+ return /[",\n]/.test(text) ? `"${text.replaceAll('"', '""')}"` : text;
32
+ }
33
+
34
+ function env(name: string): string {
35
+ const value = process.env[name];
36
+ if (!value) throw new Error(`Set ${name}`);
37
+ return value;
38
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Record why a suspended client is away and when they're expected back, e.g. from your booking
3
+ * system's freeze form. Recovr shows these on the client's profile.
4
+ *
5
+ * RECOVR_TOKEN=rcvr_sk_… npx tsx examples/set-suspension.ts <recovr-client-id> Travel 2026-12-01 "Back after the holidays"
6
+ *
7
+ * Needs `clients:write`. Whether a client is suspended comes from the booking platform; this
8
+ * sets the details around it.
9
+ */
10
+ import { createRecovrClient, RecovrApiError } from "@recovr/sdk";
11
+
12
+ const [id, reason, expectedReturnOn, notes] = process.argv.slice(2);
13
+ if (!id || !reason) throw new Error("Usage: set-suspension.ts <recovr-client-id> <reason> [YYYY-MM-DD] [notes]");
14
+
15
+ const recovr = createRecovrClient({ token: env("RECOVR_TOKEN"), baseUrl: process.env.RECOVR_BASE_URL });
16
+
17
+ try {
18
+ const { data } = await recovr.updateClientSuspension({
19
+ path: { id: Number(id) },
20
+ // Only the fields given: a field left out keeps its saved value, while null would clear it.
21
+ body: { reason, ...(expectedReturnOn ? { expected_return_on: expectedReturnOn } : {}), ...(notes ? { notes } : {}) },
22
+ });
23
+ console.log(`Saved: ${data.data.reason}, expected back ${data.data.expected_return_on ?? "(not set)"}`);
24
+ } catch (error) {
25
+ // `code` is stable: branch on it, show `message` to people.
26
+ if (error instanceof RecovrApiError && error.code === "not_found") {
27
+ console.error(`No client ${id} at this location`);
28
+ process.exitCode = 1;
29
+ } else {
30
+ throw error;
31
+ }
32
+ }
33
+
34
+ function env(name: string): string {
35
+ const value = process.env[name];
36
+ if (!value) throw new Error(`Set ${name}`);
37
+ return value;
38
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@recovr/sdk",
3
- "version": "1.0.0",
4
- "description": "TypeScript client for the Recovr API: read and act on your location's clients, notes and reminders.",
3
+ "version": "1.1.0",
4
+ "description": "TypeScript client for the Recovr API: read and act on your gym's clients, notes, reminders, forms, inbox, leads and reports.",
5
5
  "license": "MIT",
6
6
  "author": "Recovr <support@recovr.com>",
7
7
  "homepage": "https://retention-backend.recovr.com/v1/docs",
@@ -33,7 +33,9 @@
33
33
  },
34
34
  "files": [
35
35
  "dist",
36
+ "examples",
36
37
  "README.md",
38
+ "CHANGELOG.md",
37
39
  "LICENSE"
38
40
  ],
39
41
  "engines": {
@@ -43,16 +45,17 @@
43
45
  "access": "public"
44
46
  },
45
47
  "scripts": {
46
- "generate": "openapi-ts",
48
+ "generate": "openapi-ts && node scripts/strip-trailing-whitespace.mjs",
47
49
  "build": "node scripts/build.mjs",
50
+ "check-dist": "node scripts/check-dist.mjs",
48
51
  "test": "vitest run",
49
52
  "check-types": "tsc -p tsconfig.json --noEmit",
50
- "prepublishOnly": "pnpm run check-types && pnpm run test && pnpm run build"
53
+ "prepublishOnly": "pnpm run check-types && pnpm run test && pnpm run build && pnpm run check-dist"
51
54
  },
52
55
  "devDependencies": {
53
56
  "@hey-api/openapi-ts": "0.99.0",
54
57
  "@types/node": "^24.0.0",
55
58
  "typescript": "~5.9.3",
56
- "vitest": "^4.1.11"
59
+ "vitest": "^4.1.12"
57
60
  }
58
61
  }