@hausofweb/meter 0.1.0-beta.2 → 0.2.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
@@ -5,10 +5,7 @@ Reports SMS/email usage from Haus of Web client sites to the admin control plane
5
5
  ```ts
6
6
  import { createMeterClient } from "@hausofweb/meter";
7
7
 
8
- const meter = createMeterClient({
9
- apiKey: process.env.HOW_METER_API_KEY!,
10
- baseUrl: process.env.HOW_ADMIN_URL,
11
- });
8
+ const meter = createMeterClient({ baseUrl: process.env.HOW_ADMIN_URL });
12
9
 
13
10
  const result = await meter.record({
14
11
  eventName: "sms_event",
@@ -19,6 +16,8 @@ if (!result.ok) {
19
16
  }
20
17
  ```
21
18
 
19
+ The API key is the site's `HOW_API_KEY`, the one key admin issues for every Haus of Web service; pass `apiKey` to supply it some other way. When `HOW_API_KEY` is unset the client reads `HOW_METER_API_KEY`, the name older deploys have. With neither, `record` returns `{ ok: false }` naming both variables and sends nothing.
20
+
22
21
  `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
22
 
24
23
  Releases are published by pushing a `meter-v<version>` tag matching `version` in `package.json`.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export type MeterEventName = "sms_event" | "email_event";
2
2
  export interface MeterClientOptions {
3
- apiKey: string;
3
+ apiKey?: string;
4
4
  baseUrl?: string;
5
5
  fetch?: typeof fetch;
6
6
  retries?: number;
@@ -19,6 +19,6 @@ export type RecordResult = {
19
19
  error: string;
20
20
  ok: false;
21
21
  };
22
- export declare function createMeterClient({ apiKey, baseUrl, fetch: fetchImpl, retries, timeoutMs, }: MeterClientOptions): {
22
+ export declare function createMeterClient({ apiKey, baseUrl, fetch: fetchImpl, retries, timeoutMs, }?: MeterClientOptions): {
23
23
  record: (input: RecordInput) => Promise<RecordResult>;
24
24
  };
package/dist/index.js CHANGED
@@ -5,7 +5,14 @@ function backoffWithJitter(attempt) {
5
5
  const ceiling = BASE_DELAY_MS * 2 ** attempt;
6
6
  return ceiling / 2 + Math.random() * (ceiling / 2);
7
7
  }
8
- export function createMeterClient({ apiKey, baseUrl = "https://admin.hausofweb.com", fetch: fetchImpl = globalThis.fetch, retries = 3, timeoutMs = 5000, }) {
8
+ const MISSING_API_KEY = "No API key: pass apiKey or set HOW_API_KEY (or the older HOW_METER_API_KEY)";
9
+ // HOW-1100: HOW_METER_API_KEY is the name sites on older deploys have; drop
10
+ // the fallback once every site has HOW_API_KEY.
11
+ function apiKeyFromEnv() {
12
+ const env = typeof process === "undefined" ? undefined : process.env;
13
+ return env?.HOW_API_KEY || env?.HOW_METER_API_KEY || undefined;
14
+ }
15
+ export function createMeterClient({ apiKey = apiKeyFromEnv(), baseUrl = "https://admin.hausofweb.com", fetch: fetchImpl = globalThis.fetch, retries = 3, timeoutMs = 5000, } = {}) {
9
16
  async function attempt(body) {
10
17
  let response;
11
18
  try {
@@ -53,6 +60,9 @@ export function createMeterClient({ apiKey, baseUrl = "https://admin.hausofweb.c
53
60
  }
54
61
  return {
55
62
  async record({ eventName, identifier, value = 1 }) {
63
+ if (!apiKey) {
64
+ return { error: MISSING_API_KEY, ok: false };
65
+ }
56
66
  const body = JSON.stringify({ event_name: eventName, identifier, value });
57
67
  let lastError = "Unknown error";
58
68
  for (let i = 0; i <= retries; i += 1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hausofweb/meter",
3
- "version": "0.1.0-beta.2",
3
+ "version": "0.2.0-beta.1",
4
4
  "description": "Report SMS/email usage to Haus of Web for Stripe metered billing",
5
5
  "repository": {
6
6
  "type": "git",
@@ -23,7 +23,7 @@
23
23
  "test": "node --test \"src/**/*.test.ts\""
24
24
  },
25
25
  "devDependencies": {
26
- "@types/node": "^24.0.0",
26
+ "@types/node": "^26.0.0",
27
27
  "tsconfig": "workspace:*",
28
28
  "typescript": "^7.0.0"
29
29
  },