mbase-sdk 0.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.
- package/LICENSE +21 -0
- package/README.md +361 -0
- package/dist/index.cjs +589 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +557 -0
- package/dist/index.d.ts +557 -0
- package/dist/index.js +574 -0
- package/dist/index.js.map +1 -0
- package/package.json +76 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
type MeterbaseOptions = {
|
|
2
|
+
/** A secret key. It names its own workspace. */
|
|
3
|
+
apiKey: string;
|
|
4
|
+
/** Defaults to the hosted engine. */
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
/** Per attempt, not per call. */
|
|
7
|
+
timeout?: number;
|
|
8
|
+
maxRetries?: number;
|
|
9
|
+
fetch?: typeof globalThis.fetch;
|
|
10
|
+
};
|
|
11
|
+
type RequestOptions = {
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
timeout?: number;
|
|
14
|
+
maxRetries?: number;
|
|
15
|
+
};
|
|
16
|
+
type HttpMethod = "GET" | "POST" | "PATCH" | "DELETE";
|
|
17
|
+
type Request = RequestOptions & {
|
|
18
|
+
method: HttpMethod;
|
|
19
|
+
path: string;
|
|
20
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
21
|
+
body?: unknown;
|
|
22
|
+
/**
|
|
23
|
+
* Replay this call even though its method is not safe. Only a route that
|
|
24
|
+
* carries an idempotency key may set it, and the key has to be fixed before
|
|
25
|
+
* `request` is called — the loop re-sends one body, so a key generated
|
|
26
|
+
* per attempt would count the same usage twice.
|
|
27
|
+
*/
|
|
28
|
+
idempotent?: boolean;
|
|
29
|
+
};
|
|
30
|
+
declare class Client {
|
|
31
|
+
#private;
|
|
32
|
+
constructor(options: MeterbaseOptions);
|
|
33
|
+
request<T>(req: Request): Promise<T>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Enveloped so pagination can arrive without breaking callers. */
|
|
37
|
+
type List<T> = {
|
|
38
|
+
data: T[];
|
|
39
|
+
};
|
|
40
|
+
type Meter = {
|
|
41
|
+
id: string;
|
|
42
|
+
workspace_id: string;
|
|
43
|
+
key: string;
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
unit: string;
|
|
47
|
+
value: string;
|
|
48
|
+
/** Usage-alert percents for this meter. `null` inherits the workspace's
|
|
49
|
+
* defaults; `[]` turns alerts off for the meter. A customer's own list
|
|
50
|
+
* beats this one. */
|
|
51
|
+
thresholds: number[] | null;
|
|
52
|
+
created_at: string;
|
|
53
|
+
updated_at: string;
|
|
54
|
+
archived_at: string | null;
|
|
55
|
+
};
|
|
56
|
+
type MeterCreateParams = {
|
|
57
|
+
key: string;
|
|
58
|
+
name: string;
|
|
59
|
+
description?: string;
|
|
60
|
+
unit?: string;
|
|
61
|
+
value?: string;
|
|
62
|
+
/** Whole percents, 1–1000, at most 32 of them; a mark over 100 is an
|
|
63
|
+
* overage alert. Omitted, the meter inherits the workspace's. */
|
|
64
|
+
thresholds?: number[];
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* An omitted field is left alone; `""` clears an optional one.
|
|
68
|
+
*
|
|
69
|
+
* `thresholds` is the exception on both counts: `null` is a value there, not
|
|
70
|
+
* silence. Omit it to keep the stored list, send `null` to inherit the
|
|
71
|
+
* workspace's again, send `[]` to turn alerts off for this meter.
|
|
72
|
+
*/
|
|
73
|
+
type MeterUpdateParams = {
|
|
74
|
+
key?: string;
|
|
75
|
+
name?: string;
|
|
76
|
+
description?: string;
|
|
77
|
+
unit?: string;
|
|
78
|
+
value?: string;
|
|
79
|
+
thresholds?: number[] | null;
|
|
80
|
+
};
|
|
81
|
+
type MeterListParams = {
|
|
82
|
+
include_archived?: boolean;
|
|
83
|
+
};
|
|
84
|
+
type Customer = {
|
|
85
|
+
id: string;
|
|
86
|
+
workspace_id: string;
|
|
87
|
+
external_id: string;
|
|
88
|
+
name: string;
|
|
89
|
+
metadata: Record<string, unknown>;
|
|
90
|
+
/** Usage-alert percents for this customer. `null` inherits — the meter's
|
|
91
|
+
* list, then the workspace's. `[]` turns alerts off for this customer.
|
|
92
|
+
* The most specific of the three, and it always wins. */
|
|
93
|
+
thresholds: number[] | null;
|
|
94
|
+
created_at: string;
|
|
95
|
+
updated_at: string;
|
|
96
|
+
deleted_at: string | null;
|
|
97
|
+
};
|
|
98
|
+
type CustomerCreateParams = {
|
|
99
|
+
external_id: string;
|
|
100
|
+
name?: string;
|
|
101
|
+
metadata?: Record<string, unknown>;
|
|
102
|
+
/** Whole percents, 1–1000, at most 32 of them; a mark over 100 is an
|
|
103
|
+
* overage alert. Omitted, the customer inherits. */
|
|
104
|
+
thresholds?: number[];
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* An omitted field is left alone. `external_id` cannot be cleared, and
|
|
108
|
+
* `metadata` is replaced wholesale rather than merged.
|
|
109
|
+
*
|
|
110
|
+
* `thresholds` reads `null` as a value, not as silence: omit it to keep the
|
|
111
|
+
* stored list, send `null` to go back to inheriting the meter's or the
|
|
112
|
+
* workspace's, send `[]` to turn alerts off for this customer alone.
|
|
113
|
+
*/
|
|
114
|
+
type CustomerUpdateParams = {
|
|
115
|
+
external_id?: string;
|
|
116
|
+
name?: string;
|
|
117
|
+
metadata?: Record<string, unknown>;
|
|
118
|
+
thresholds?: number[] | null;
|
|
119
|
+
};
|
|
120
|
+
type CustomerListParams = {
|
|
121
|
+
include_deleted?: boolean;
|
|
122
|
+
};
|
|
123
|
+
/** `customer_id` is the tenant's own external id and `meter_id` the meter's
|
|
124
|
+
* key: an SDK never holds Meterbase uuids. */
|
|
125
|
+
type CheckParams = {
|
|
126
|
+
customer_id: string;
|
|
127
|
+
meter_id: string;
|
|
128
|
+
/** What is about to be consumed, `1 … 100000000000`. Defaults to `1`, which
|
|
129
|
+
* asks whether there is any capacity at all. */
|
|
130
|
+
quantity?: number;
|
|
131
|
+
};
|
|
132
|
+
type CheckResult = {
|
|
133
|
+
allowed: boolean;
|
|
134
|
+
/** Unlimited for this meter. `available` is `null`. */
|
|
135
|
+
no_cap: boolean;
|
|
136
|
+
/**
|
|
137
|
+
* The whole capacity: the plan's unused entitlement for the current period,
|
|
138
|
+
* floored at 0, plus every open grant. `null` under `no_cap`, where capacity
|
|
139
|
+
* is not a number.
|
|
140
|
+
*
|
|
141
|
+
* One figure and no breakdown: `check` answers from a single cached integer,
|
|
142
|
+
* and itemising where the capacity came from would cost it that. For the
|
|
143
|
+
* per-grant detail, read `customers.allowances.list`.
|
|
144
|
+
*/
|
|
145
|
+
available: number | null;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* `track` records work that already happened, so it never refuses on capacity
|
|
149
|
+
* — the gate is `check`, which runs before the work. A quantity beyond the
|
|
150
|
+
* customer's capacity is recorded, drives `available` to 0, and the next
|
|
151
|
+
* `check` says no.
|
|
152
|
+
*/
|
|
153
|
+
type TrackParams = {
|
|
154
|
+
customer_id: string;
|
|
155
|
+
meter_id: string;
|
|
156
|
+
/** Integer, `1 … 100000000000`. Defaults to `1`, which is what a counting
|
|
157
|
+
* meter sends. */
|
|
158
|
+
quantity?: number;
|
|
159
|
+
/**
|
|
160
|
+
* RFC 3339, defaulting to now, and it must fall inside the workspace's
|
|
161
|
+
* window — by default 7 days back and 5 minutes ahead. Keeping it apart
|
|
162
|
+
* from `recorded_at` is what lets usage be reported late.
|
|
163
|
+
*/
|
|
164
|
+
occurred_at?: string;
|
|
165
|
+
/**
|
|
166
|
+
* The engine requires one; the SDK generates one per call when it is
|
|
167
|
+
* omitted, and reuses it across that call's retries. Supply your own when
|
|
168
|
+
* the caller already has an id for the work — a job id, a request id — so a
|
|
169
|
+
* retry from further out replays rather than counts twice.
|
|
170
|
+
*/
|
|
171
|
+
idempotency_key?: string;
|
|
172
|
+
};
|
|
173
|
+
/** Enveloped, like the list reads, so the receipt can grow a sibling field
|
|
174
|
+
* without breaking callers. */
|
|
175
|
+
type TrackResult = {
|
|
176
|
+
event: UsageEvent;
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* A receipt. The same shape whether the event is new, a replay, or dated
|
|
180
|
+
* inside a period that has already closed — no capacity figures, no nullable
|
|
181
|
+
* fields, nothing to branch on.
|
|
182
|
+
*/
|
|
183
|
+
type UsageEvent = {
|
|
184
|
+
/** Meterbase's id for the event. A retry of the same key returns this same
|
|
185
|
+
* id. */
|
|
186
|
+
id: string;
|
|
187
|
+
customer_id: string;
|
|
188
|
+
meter_id: string;
|
|
189
|
+
quantity: number;
|
|
190
|
+
/** As supplied, or now. Chooses the period the event counts against. */
|
|
191
|
+
occurred_at: string;
|
|
192
|
+
/** When Meterbase stored it, which is not `occurred_at`. */
|
|
193
|
+
recorded_at: string;
|
|
194
|
+
idempotency_key: string;
|
|
195
|
+
/** `true` when this key had already been recorded: nothing was written, and
|
|
196
|
+
* this is the original event. */
|
|
197
|
+
replayed: boolean;
|
|
198
|
+
};
|
|
199
|
+
/**
|
|
200
|
+
* A plan's billing period. Every recurring allowance it holds is measured
|
|
201
|
+
* against it, and it is fixed when the plan is created. `one_time` is not
|
|
202
|
+
* among them: that is a kind of allowance, not a period.
|
|
203
|
+
*/
|
|
204
|
+
type PlanCycle = "daily" | "weekly" | "monthly" | "quarterly" | "half_yearly" | "yearly";
|
|
205
|
+
type Plan = {
|
|
206
|
+
id: string;
|
|
207
|
+
workspace_id: string;
|
|
208
|
+
key: string;
|
|
209
|
+
name: string;
|
|
210
|
+
description: string;
|
|
211
|
+
cycle: PlanCycle;
|
|
212
|
+
created_at: string;
|
|
213
|
+
updated_at: string;
|
|
214
|
+
archived_at: string | null;
|
|
215
|
+
};
|
|
216
|
+
type PlanCreateParams = {
|
|
217
|
+
key: string;
|
|
218
|
+
name: string;
|
|
219
|
+
description?: string;
|
|
220
|
+
cycle: PlanCycle;
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* An omitted field is left alone; `""` clears the description. There is no
|
|
224
|
+
* `cycle`: moving a plan's period would re-date every period its customers
|
|
225
|
+
* were already measured against, so a different cadence is a different plan.
|
|
226
|
+
*/
|
|
227
|
+
type PlanUpdateParams = {
|
|
228
|
+
key?: string;
|
|
229
|
+
name?: string;
|
|
230
|
+
description?: string;
|
|
231
|
+
};
|
|
232
|
+
type PlanListParams = {
|
|
233
|
+
include_archived?: boolean;
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* Which of a `(plan, meter)` pair's two lineages a version belongs to. Each
|
|
237
|
+
* numbers itself separately.
|
|
238
|
+
*/
|
|
239
|
+
type PlanAllowanceKind = "recurring" | "one_time";
|
|
240
|
+
type ApplyMode = "immediate" | "next_cycle";
|
|
241
|
+
type Reconciliation = "none" | "prorate" | "rebalance";
|
|
242
|
+
/**
|
|
243
|
+
* How long a one-time grant lives once minted. Months are kept apart from
|
|
244
|
+
* days because they are not the same length: three months from Jan 31 dies
|
|
245
|
+
* Apr 30, ninety days dies May 1.
|
|
246
|
+
*/
|
|
247
|
+
type Validity = {
|
|
248
|
+
months: number;
|
|
249
|
+
days: number;
|
|
250
|
+
};
|
|
251
|
+
type PlanAllowance = {
|
|
252
|
+
id: string;
|
|
253
|
+
workspace_id: string;
|
|
254
|
+
plan_id: string;
|
|
255
|
+
meter_id: string;
|
|
256
|
+
/** 1, 2, 3… per lineage. Assigned by the engine, never by the caller. */
|
|
257
|
+
version: number;
|
|
258
|
+
/** Units per period. `0` is legal and grants nothing. `null` under no_cap. */
|
|
259
|
+
amount: number | null;
|
|
260
|
+
no_cap: boolean;
|
|
261
|
+
kind: PlanAllowanceKind;
|
|
262
|
+
/** `one_time` only; `null` is a grant that never expires. */
|
|
263
|
+
valid_for: Validity | null;
|
|
264
|
+
effective_from: string;
|
|
265
|
+
apply_mode: ApplyMode;
|
|
266
|
+
reconciliation: Reconciliation;
|
|
267
|
+
created_at: string;
|
|
268
|
+
};
|
|
269
|
+
/**
|
|
270
|
+
* An allowance says how much, never how often: the period is the plan's
|
|
271
|
+
* cycle. Give exactly one of `amount` or `no_cap`.
|
|
272
|
+
*/
|
|
273
|
+
type PlanAllowanceSetParams = {
|
|
274
|
+
meter_id: string;
|
|
275
|
+
amount?: number;
|
|
276
|
+
no_cap?: boolean;
|
|
277
|
+
/** Defaults to `recurring`. */
|
|
278
|
+
kind?: PlanAllowanceKind;
|
|
279
|
+
/** `one_time` only. */
|
|
280
|
+
valid_for?: Validity;
|
|
281
|
+
/** Defaults to `next_cycle`, except `one_time` which is always immediate. */
|
|
282
|
+
apply_mode?: ApplyMode;
|
|
283
|
+
/** `immediate` only. Defaults to `none`. */
|
|
284
|
+
reconciliation?: Reconciliation;
|
|
285
|
+
};
|
|
286
|
+
type PlanAllowanceListParams = {
|
|
287
|
+
meter_id?: string;
|
|
288
|
+
};
|
|
289
|
+
type Assignment = {
|
|
290
|
+
id: string;
|
|
291
|
+
workspace_id: string;
|
|
292
|
+
customer_id: string;
|
|
293
|
+
plan_id: string;
|
|
294
|
+
/** Ahead of now for a change that has not landed yet. */
|
|
295
|
+
effective_at: string;
|
|
296
|
+
/** As recorded, which is not always as asked: a first plan starts at once. */
|
|
297
|
+
effective: ApplyMode;
|
|
298
|
+
reconciliation: Reconciliation | "reset";
|
|
299
|
+
/** What periods are measured from. A plan change inherits it, so a
|
|
300
|
+
* customer's renewal day never moves under them. */
|
|
301
|
+
anchor_at: string;
|
|
302
|
+
/** Set when a newer instruction replaced this one before it took effect. */
|
|
303
|
+
superseded_at: string | null;
|
|
304
|
+
created_at: string;
|
|
305
|
+
};
|
|
306
|
+
/** Assigning and changing are one operation: the history is append-only. */
|
|
307
|
+
type AssignPlanParams = {
|
|
308
|
+
plan_id: string;
|
|
309
|
+
/** Defaults to `next_cycle` on a change, and is forced to `immediate` on a
|
|
310
|
+
* customer's first plan: there is no period to wait out. */
|
|
311
|
+
effective?: ApplyMode;
|
|
312
|
+
reconciliation?: Reconciliation | "reset";
|
|
313
|
+
/** The customer's first assignment only, and the one chance to stagger
|
|
314
|
+
* their billing day. Defaults to the instant it lands. */
|
|
315
|
+
cycle_anchor?: string;
|
|
316
|
+
};
|
|
317
|
+
/** `plan` is not accepted on a grant: the engine mints those itself when a
|
|
318
|
+
* plan is assigned. */
|
|
319
|
+
type AllowanceSource = "purchased" | "bonus" | "manual";
|
|
320
|
+
type Allowance = {
|
|
321
|
+
id: string;
|
|
322
|
+
workspace_id: string;
|
|
323
|
+
customer_id: string;
|
|
324
|
+
meter_id: string;
|
|
325
|
+
amount: number | null;
|
|
326
|
+
no_cap: boolean;
|
|
327
|
+
consumed: number;
|
|
328
|
+
/** `null` under no_cap. */
|
|
329
|
+
remaining: number | null;
|
|
330
|
+
source: AllowanceSource | "plan";
|
|
331
|
+
metadata: Record<string, unknown>;
|
|
332
|
+
effective_at: string;
|
|
333
|
+
expires_at: string | null;
|
|
334
|
+
revoked_at: string | null;
|
|
335
|
+
exhausted_at: string | null;
|
|
336
|
+
created_at: string;
|
|
337
|
+
/** Null unless `source` is `plan`. Together they answer where the grant
|
|
338
|
+
* came from and why it went away. */
|
|
339
|
+
plan_allowance_version_id: string | null;
|
|
340
|
+
assignment_id: string | null;
|
|
341
|
+
cancelled_at: string | null;
|
|
342
|
+
};
|
|
343
|
+
/** Give exactly one of `amount` or `no_cap`. Unlike a plan allowance, a grant
|
|
344
|
+
* of 0 is refused: a grant that gives nothing is a no-op, not a state. */
|
|
345
|
+
type AllowanceGrantParams = {
|
|
346
|
+
meter_id: string;
|
|
347
|
+
/** At least 1. */
|
|
348
|
+
amount?: number;
|
|
349
|
+
no_cap?: boolean;
|
|
350
|
+
/** Required: the engine will not guess why capacity was handed out. */
|
|
351
|
+
source: AllowanceSource;
|
|
352
|
+
metadata?: Record<string, unknown>;
|
|
353
|
+
/** Defaults to now. Dated ahead, the grant is inert until then. */
|
|
354
|
+
effective_at?: string;
|
|
355
|
+
expires_at?: string;
|
|
356
|
+
};
|
|
357
|
+
type AllowanceListParams = {
|
|
358
|
+
meter_id?: string;
|
|
359
|
+
/** Revoked, expired and exhausted grants as well as open ones. */
|
|
360
|
+
include_closed?: boolean;
|
|
361
|
+
};
|
|
362
|
+
type WhoAmI = {
|
|
363
|
+
workspace: string;
|
|
364
|
+
caller: string;
|
|
365
|
+
kind: "api_key" | "service";
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Capacity handed to one customer on top of their plan. A grant with
|
|
370
|
+
* `source: "plan"` is the engine's own, minted when a plan is assigned, and
|
|
371
|
+
* cannot be created here.
|
|
372
|
+
*/
|
|
373
|
+
declare class CustomerAllowances {
|
|
374
|
+
#private;
|
|
375
|
+
constructor(client: Client);
|
|
376
|
+
grant(customerId: string, params: AllowanceGrantParams, options?: RequestOptions): Promise<Allowance>;
|
|
377
|
+
/** Open grants only, unless `include_closed`. */
|
|
378
|
+
list(customerId: string, params?: AllowanceListParams, options?: RequestOptions): Promise<List<Allowance>>;
|
|
379
|
+
retrieve(customerId: string, allowanceId: string, options?: RequestOptions): Promise<Allowance>;
|
|
380
|
+
/** Withdraws what is left without erasing what was consumed. Idempotent. */
|
|
381
|
+
revoke(customerId: string, allowanceId: string, options?: RequestOptions): Promise<Allowance>;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Which plan a customer holds. The history is append-only: there is no patch
|
|
386
|
+
* and no delete, and assigning a plan to a customer who already has one is
|
|
387
|
+
* the same call as their first.
|
|
388
|
+
*/
|
|
389
|
+
declare class CustomerPlan {
|
|
390
|
+
#private;
|
|
391
|
+
constructor(client: Client);
|
|
392
|
+
/** Returns the instruction as recorded, which is where to read when it lands. */
|
|
393
|
+
assign(customerId: string, params: AssignPlanParams, options?: RequestOptions): Promise<Assignment>;
|
|
394
|
+
/**
|
|
395
|
+
* The plan in force now, which is not always the newest instruction: a
|
|
396
|
+
* change dated ahead does not govern yet. Throws NotFoundError when the
|
|
397
|
+
* customer holds no plan.
|
|
398
|
+
*/
|
|
399
|
+
retrieve(customerId: string, options?: RequestOptions): Promise<Assignment>;
|
|
400
|
+
/** Every instruction, superseded ones included: it is the whole trail. */
|
|
401
|
+
history(customerId: string, options?: RequestOptions): Promise<List<Assignment>>;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
declare class Customers {
|
|
405
|
+
#private;
|
|
406
|
+
/** The plan they hold, and the trail of instructions that got them there. */
|
|
407
|
+
readonly plan: CustomerPlan;
|
|
408
|
+
/** Grants, which sit on top of whatever the plan gives. */
|
|
409
|
+
readonly allowances: CustomerAllowances;
|
|
410
|
+
constructor(client: Client);
|
|
411
|
+
create(params: CustomerCreateParams, options?: RequestOptions): Promise<Customer>;
|
|
412
|
+
list(params?: CustomerListParams, options?: RequestOptions): Promise<List<Customer>>;
|
|
413
|
+
retrieve(id: string, options?: RequestOptions): Promise<Customer>;
|
|
414
|
+
/**
|
|
415
|
+
* Resolves the tenant's own identifier, or null. The engine answers this as
|
|
416
|
+
* a filtered list, so a miss is an empty collection rather than a 404.
|
|
417
|
+
*/
|
|
418
|
+
retrieveByExternalId(externalId: string, options?: RequestOptions): Promise<Customer | null>;
|
|
419
|
+
update(id: string, params: CustomerUpdateParams, options?: RequestOptions): Promise<Customer>;
|
|
420
|
+
/** Soft delete: usage and assignments keep referencing the customer. */
|
|
421
|
+
delete(id: string, options?: RequestOptions): Promise<Customer>;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
declare class Meters {
|
|
425
|
+
#private;
|
|
426
|
+
constructor(client: Client);
|
|
427
|
+
create(params: MeterCreateParams, options?: RequestOptions): Promise<Meter>;
|
|
428
|
+
list(params?: MeterListParams, options?: RequestOptions): Promise<List<Meter>>;
|
|
429
|
+
retrieve(id: string, options?: RequestOptions): Promise<Meter>;
|
|
430
|
+
update(id: string, params: MeterUpdateParams, options?: RequestOptions): Promise<Meter>;
|
|
431
|
+
/** Soft delete: plans and usage keep referencing the meter. Idempotent. */
|
|
432
|
+
archive(id: string, options?: RequestOptions): Promise<Meter>;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* A plan's entitlement, versioned. There is no update and no delete: an edit
|
|
437
|
+
* appends the next version, so which amount applied when stays derivable.
|
|
438
|
+
*/
|
|
439
|
+
declare class PlanAllowances {
|
|
440
|
+
#private;
|
|
441
|
+
constructor(client: Client);
|
|
442
|
+
set(planId: string, params: PlanAllowanceSetParams, options?: RequestOptions): Promise<PlanAllowance>;
|
|
443
|
+
/** Every version of every meter, newest first — history, not just current. */
|
|
444
|
+
list(planId: string, params?: PlanAllowanceListParams, options?: RequestOptions): Promise<List<PlanAllowance>>;
|
|
445
|
+
}
|
|
446
|
+
declare class Plans {
|
|
447
|
+
#private;
|
|
448
|
+
readonly allowances: PlanAllowances;
|
|
449
|
+
constructor(client: Client);
|
|
450
|
+
/** A new plan grants nothing; attach entitlement with `allowances.set`. */
|
|
451
|
+
create(params: PlanCreateParams, options?: RequestOptions): Promise<Plan>;
|
|
452
|
+
list(params?: PlanListParams, options?: RequestOptions): Promise<List<Plan>>;
|
|
453
|
+
retrieve(id: string, options?: RequestOptions): Promise<Plan>;
|
|
454
|
+
update(id: string, params: PlanUpdateParams, options?: RequestOptions): Promise<Plan>;
|
|
455
|
+
/**
|
|
456
|
+
* Stops new assignments without withdrawing capacity from anyone already on
|
|
457
|
+
* the plan, which is why an archived plan still resolves by id. Idempotent.
|
|
458
|
+
*/
|
|
459
|
+
archive(id: string, options?: RequestOptions): Promise<Plan>;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
declare class MeterbaseError extends Error {
|
|
463
|
+
constructor(message: string, options?: {
|
|
464
|
+
cause?: unknown;
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
declare class APIError extends MeterbaseError {
|
|
468
|
+
readonly status: number;
|
|
469
|
+
readonly code: string;
|
|
470
|
+
/** From `X-Request-Id`, when the engine sends one. */
|
|
471
|
+
readonly requestId: string | undefined;
|
|
472
|
+
readonly body: unknown;
|
|
473
|
+
constructor(args: {
|
|
474
|
+
status: number;
|
|
475
|
+
code: string;
|
|
476
|
+
message: string;
|
|
477
|
+
requestId?: string | undefined;
|
|
478
|
+
body?: unknown;
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
declare class AuthenticationError extends APIError {
|
|
482
|
+
}
|
|
483
|
+
declare class PermissionDeniedError extends APIError {
|
|
484
|
+
}
|
|
485
|
+
declare class NotFoundError extends APIError {
|
|
486
|
+
}
|
|
487
|
+
declare class ConflictError extends APIError {
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* `409 idempotency_conflict`: the key is in use for a different meter or
|
|
491
|
+
* quantity. A caller told their key is taken asks what it recorded next, so
|
|
492
|
+
* the engine names the original and this carries it.
|
|
493
|
+
*
|
|
494
|
+
* A `ConflictError` still, so `catch (e) { if (e instanceof ConflictError) }`
|
|
495
|
+
* keeps working.
|
|
496
|
+
*/
|
|
497
|
+
declare class IdempotencyConflictError extends ConflictError {
|
|
498
|
+
/** The event that key already recorded. Undefined only if the engine sent a
|
|
499
|
+
* body this could not be read out of. */
|
|
500
|
+
readonly existingEventId: string | undefined;
|
|
501
|
+
constructor(args: ConstructorParameters<typeof APIError>[0]);
|
|
502
|
+
}
|
|
503
|
+
declare class InvalidRequestError extends APIError {
|
|
504
|
+
}
|
|
505
|
+
declare class RateLimitError extends APIError {
|
|
506
|
+
/** Seconds to wait, from `Retry-After`, when the engine sends one. */
|
|
507
|
+
readonly retryAfter: number | undefined;
|
|
508
|
+
constructor(args: ConstructorParameters<typeof APIError>[0] & {
|
|
509
|
+
retryAfter?: number | undefined;
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
declare class ServerError extends APIError {
|
|
513
|
+
}
|
|
514
|
+
declare class ConnectionError extends MeterbaseError {
|
|
515
|
+
}
|
|
516
|
+
declare class TimeoutError extends ConnectionError {
|
|
517
|
+
}
|
|
518
|
+
declare function errorFromResponse(args: {
|
|
519
|
+
status: number;
|
|
520
|
+
code: string;
|
|
521
|
+
message: string;
|
|
522
|
+
requestId?: string | undefined;
|
|
523
|
+
retryAfter?: number | undefined;
|
|
524
|
+
body?: unknown;
|
|
525
|
+
}): APIError;
|
|
526
|
+
|
|
527
|
+
declare class Meterbase {
|
|
528
|
+
#private;
|
|
529
|
+
readonly customers: Customers;
|
|
530
|
+
readonly meters: Meters;
|
|
531
|
+
readonly plans: Plans;
|
|
532
|
+
constructor(options: MeterbaseOptions);
|
|
533
|
+
/**
|
|
534
|
+
* Asks whether a customer may consume `quantity` of a meter, named by the
|
|
535
|
+
* tenant's own external id and the meter's key. Absence of entitlement is
|
|
536
|
+
* not permission: a customer with no plan and no grant is denied.
|
|
537
|
+
*
|
|
538
|
+
* This is the gate, and the only call that ever refuses. Run it before the
|
|
539
|
+
* work; record the work with `track` afterwards.
|
|
540
|
+
*/
|
|
541
|
+
check(params: CheckParams, options?: RequestOptions): Promise<CheckResult>;
|
|
542
|
+
/**
|
|
543
|
+
* Records usage that already happened — the tokens were spent, the image
|
|
544
|
+
* was generated — so it never refuses on capacity. A quantity beyond the
|
|
545
|
+
* customer's capacity is recorded, drives `available` to 0, and the next
|
|
546
|
+
* `check` says no. The answer is a receipt, not a verdict.
|
|
547
|
+
*
|
|
548
|
+
* `idempotency_key` is generated when omitted, so retrying this call — the
|
|
549
|
+
* SDK's own retries included — replays the original rather than counting
|
|
550
|
+
* the same work twice.
|
|
551
|
+
*/
|
|
552
|
+
track(params: TrackParams, options?: RequestOptions): Promise<TrackResult>;
|
|
553
|
+
/** Reports which workspace this key acts for. */
|
|
554
|
+
whoami(options?: RequestOptions): Promise<WhoAmI>;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
export { APIError, type Allowance, type AllowanceGrantParams, type AllowanceListParams, type AllowanceSource, type ApplyMode, type AssignPlanParams, type Assignment, AuthenticationError, type CheckParams, type CheckResult, ConflictError, ConnectionError, type Customer, CustomerAllowances, type CustomerCreateParams, type CustomerListParams, CustomerPlan, type CustomerUpdateParams, Customers, IdempotencyConflictError, InvalidRequestError, type List, type Meter, type MeterCreateParams, type MeterListParams, type MeterUpdateParams, Meterbase, MeterbaseError, type MeterbaseOptions, Meters, NotFoundError, PermissionDeniedError, type Plan, type PlanAllowance, type PlanAllowanceKind, type PlanAllowanceListParams, type PlanAllowanceSetParams, PlanAllowances, type PlanCreateParams, type PlanCycle, type PlanListParams, type PlanUpdateParams, Plans, RateLimitError, type Reconciliation, type RequestOptions, ServerError, TimeoutError, type TrackParams, type TrackResult, type UsageEvent, type Validity, type WhoAmI, errorFromResponse };
|