@hausofweb/meter 0.0.0-stage → 0.1.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,24 @@
1
- # Temporary Holding Version
1
+ # @hausofweb/meter
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
+ Reports SMS/email usage from Haus of Web client sites to the admin control plane, which bills it through Stripe Billing Meters. Zero dependencies; Node 20+ and Edge runtimes.
4
+
5
+ ```ts
6
+ import { createMeterClient } from "@hausofweb/meter";
7
+
8
+ const meter = createMeterClient({
9
+ apiKey: process.env.HOW_METER_API_KEY!,
10
+ baseUrl: process.env.HOW_ADMIN_URL,
11
+ });
12
+
13
+ const result = await meter.record({
14
+ eventName: "sms_event",
15
+ identifier: twilioMessage.sid,
16
+ });
17
+ if (!result.ok) {
18
+ console.error("Usage not recorded", result.error);
19
+ }
20
+ ```
21
+
22
+ `identifier` must be unique per billable event and stable across retries (e.g. the provider's message id): the admin dedupes on it, so a repeated identifier is answered `{ ok: true, duplicate: true }` and never billed twice. That is also what lets `record` retry network errors, 5xx and 429 (with backoff and jitter, `retries` times, default 3). Other 4xx are returned without retrying. `record` never throws.
23
+
24
+ Releases are published by pushing a `meter-v<version>` tag matching `version` in `package.json`.
@@ -0,0 +1,24 @@
1
+ export type MeterEventName = "sms_event" | "email_event";
2
+ export interface MeterClientOptions {
3
+ apiKey: string;
4
+ baseUrl?: string;
5
+ fetch?: typeof fetch;
6
+ retries?: number;
7
+ timeoutMs?: number;
8
+ }
9
+ export interface RecordInput {
10
+ eventName: MeterEventName;
11
+ identifier: string;
12
+ value?: number;
13
+ }
14
+ export type RecordResult = {
15
+ duplicate: boolean;
16
+ ok: true;
17
+ status: string;
18
+ } | {
19
+ error: string;
20
+ ok: false;
21
+ };
22
+ export declare function createMeterClient({ apiKey, baseUrl, fetch: fetchImpl, retries, timeoutMs, }: MeterClientOptions): {
23
+ record: (input: RecordInput) => Promise<RecordResult>;
24
+ };
package/dist/index.js ADDED
@@ -0,0 +1,77 @@
1
+ const BASE_DELAY_MS = 200;
2
+ const isRetryableStatus = (status) => status === 429 || status >= 500;
3
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
4
+ function backoffWithJitter(attempt) {
5
+ const ceiling = BASE_DELAY_MS * 2 ** attempt;
6
+ return ceiling / 2 + Math.random() * (ceiling / 2);
7
+ }
8
+ export function createMeterClient({ apiKey, baseUrl = "https://admin.hausofweb.com", fetch: fetchImpl = globalThis.fetch, retries = 3, timeoutMs = 5000, }) {
9
+ async function attempt(body) {
10
+ let response;
11
+ try {
12
+ response = await fetchImpl(new URL("/api/v1/meter-events", baseUrl), {
13
+ body,
14
+ headers: {
15
+ Authorization: `Bearer ${apiKey}`,
16
+ "Content-Type": "application/json",
17
+ },
18
+ method: "POST",
19
+ signal: AbortSignal.timeout(timeoutMs),
20
+ });
21
+ }
22
+ catch (error) {
23
+ return {
24
+ error: error instanceof Error ? error.message : String(error),
25
+ retry: true,
26
+ };
27
+ }
28
+ const text = await response.text().catch(() => "");
29
+ if (isRetryableStatus(response.status)) {
30
+ return { error: `HTTP ${response.status}: ${text}`, retry: true };
31
+ }
32
+ if (!response.ok) {
33
+ return {
34
+ result: { error: `HTTP ${response.status}: ${text}`, ok: false },
35
+ retry: false,
36
+ };
37
+ }
38
+ let parsed = {};
39
+ try {
40
+ parsed = JSON.parse(text);
41
+ }
42
+ catch {
43
+ // A 2xx without a JSON body still means the event was accepted.
44
+ }
45
+ return {
46
+ result: {
47
+ duplicate: parsed.duplicate === true,
48
+ ok: true,
49
+ status: parsed.status ?? "pending",
50
+ },
51
+ retry: false,
52
+ };
53
+ }
54
+ return {
55
+ async record({ eventName, identifier, value = 1 }) {
56
+ const body = JSON.stringify({ event_name: eventName, identifier, value });
57
+ let lastError = "Unknown error";
58
+ for (let i = 0; i <= retries; i += 1) {
59
+ if (i > 0) {
60
+ // biome-ignore lint/performance/noAwaitInLoops: retries are sequential by design.
61
+ await sleep(backoffWithJitter(i - 1));
62
+ }
63
+ try {
64
+ const outcome = await attempt(body);
65
+ if (!outcome.retry) {
66
+ return outcome.result;
67
+ }
68
+ lastError = outcome.error;
69
+ }
70
+ catch (error) {
71
+ lastError = error instanceof Error ? error.message : String(error);
72
+ }
73
+ }
74
+ return { error: lastError, ok: false };
75
+ },
76
+ };
77
+ }
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@hausofweb/meter",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-beta.1",
4
+ "description": "Report SMS/email usage to Haus of Web for Stripe metered billing",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/hausofweb/admin.git",
8
+ "directory": "packages/meter"
9
+ },
10
+ "license": "MIT",
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc -p tsconfig.build.json",
23
+ "test": "node --test \"src/**/*.test.ts\""
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^24.0.0",
27
+ "tsconfig": "workspace:*",
28
+ "typescript": "^7.0.0"
29
+ },
30
+ "engines": {
31
+ "node": ">=20"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ }
36
+ }