@contractspec/lib.metering 3.7.17 → 3.7.18

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.
Files changed (34) hide show
  1. package/dist/aggregation/index.js +1 -265
  2. package/dist/analytics/posthog-metering-reader.js +4 -266
  3. package/dist/analytics/posthog-metering.js +1 -45
  4. package/dist/browser/aggregation/index.js +1 -265
  5. package/dist/browser/analytics/posthog-metering-reader.js +4 -266
  6. package/dist/browser/analytics/posthog-metering.js +1 -45
  7. package/dist/browser/contracts/index.js +1 -617
  8. package/dist/browser/docs/index.js +4 -18
  9. package/dist/browser/docs/metering.docblock.js +4 -18
  10. package/dist/browser/entities/index.js +1 -350
  11. package/dist/browser/events.js +1 -269
  12. package/dist/browser/index.js +7 -1877
  13. package/dist/browser/metering.capability.js +1 -31
  14. package/dist/browser/metering.feature.js +1 -53
  15. package/dist/contracts/index.js +1 -617
  16. package/dist/docs/index.js +4 -18
  17. package/dist/docs/metering.docblock.js +4 -18
  18. package/dist/entities/index.js +1 -350
  19. package/dist/events.js +1 -269
  20. package/dist/index.js +7 -1877
  21. package/dist/metering.capability.js +1 -31
  22. package/dist/metering.feature.js +1 -53
  23. package/dist/node/aggregation/index.js +1 -265
  24. package/dist/node/analytics/posthog-metering-reader.js +4 -266
  25. package/dist/node/analytics/posthog-metering.js +1 -45
  26. package/dist/node/contracts/index.js +1 -617
  27. package/dist/node/docs/index.js +4 -18
  28. package/dist/node/docs/metering.docblock.js +4 -18
  29. package/dist/node/entities/index.js +1 -350
  30. package/dist/node/events.js +1 -269
  31. package/dist/node/index.js +7 -1877
  32. package/dist/node/metering.capability.js +1 -31
  33. package/dist/node/metering.feature.js +1 -53
  34. package/package.json +6 -6
@@ -1,617 +1 @@
1
- // src/contracts/index.ts
2
- import { defineCommand, defineQuery } from "@contractspec/lib.contracts-spec";
3
- import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
- var OWNERS = ["platform.metering"];
5
- var MetricDefinitionModel = defineSchemaModel({
6
- name: "MetricDefinition",
7
- description: "Represents a metric definition",
8
- fields: {
9
- id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
10
- key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
11
- name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
12
- description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
13
- unit: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
14
- aggregationType: {
15
- type: ScalarTypeEnum.String_unsecure(),
16
- isOptional: false
17
- },
18
- resetPeriod: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
19
- category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
20
- isActive: { type: ScalarTypeEnum.Boolean(), isOptional: false },
21
- orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
22
- createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
23
- updatedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
24
- }
25
- });
26
- var UsageRecordModel = defineSchemaModel({
27
- name: "UsageRecord",
28
- description: "Represents a usage record",
29
- fields: {
30
- id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
31
- metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
32
- subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
33
- subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
34
- quantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
35
- source: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
36
- resourceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
37
- resourceType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
38
- metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
39
- timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: false },
40
- createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
41
- }
42
- });
43
- var UsageSummaryModel = defineSchemaModel({
44
- name: "UsageSummary",
45
- description: "Represents aggregated usage",
46
- fields: {
47
- id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
48
- metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
49
- subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
50
- subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
51
- periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
52
- periodStart: { type: ScalarTypeEnum.DateTime(), isOptional: false },
53
- periodEnd: { type: ScalarTypeEnum.DateTime(), isOptional: false },
54
- totalQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
55
- recordCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
56
- minQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
57
- maxQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
58
- avgQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true }
59
- }
60
- });
61
- var UsageThresholdModel = defineSchemaModel({
62
- name: "UsageThreshold",
63
- description: "Represents a usage threshold",
64
- fields: {
65
- id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
66
- metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
67
- subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
68
- subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
69
- name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
70
- threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
71
- warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
72
- periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
73
- action: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
74
- currentValue: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
75
- isActive: { type: ScalarTypeEnum.Boolean(), isOptional: false },
76
- createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
77
- }
78
- });
79
- var DefineMetricInput = defineSchemaModel({
80
- name: "DefineMetricInput",
81
- description: "Input for defining a metric",
82
- fields: {
83
- key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
84
- name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
85
- description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
86
- unit: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
87
- aggregationType: {
88
- type: ScalarTypeEnum.String_unsecure(),
89
- isOptional: true
90
- },
91
- resetPeriod: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
92
- category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
93
- orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
94
- metadata: { type: ScalarTypeEnum.JSON(), isOptional: true }
95
- }
96
- });
97
- var UpdateMetricInput = defineSchemaModel({
98
- name: "UpdateMetricInput",
99
- description: "Input for updating a metric",
100
- fields: {
101
- metricId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
102
- name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
103
- description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
104
- category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
105
- isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true },
106
- metadata: { type: ScalarTypeEnum.JSON(), isOptional: true }
107
- }
108
- });
109
- var DeleteMetricInput = defineSchemaModel({
110
- name: "DeleteMetricInput",
111
- description: "Input for deleting a metric",
112
- fields: {
113
- metricId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
114
- }
115
- });
116
- var GetMetricInput = defineSchemaModel({
117
- name: "GetMetricInput",
118
- description: "Input for getting a metric",
119
- fields: {
120
- key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
121
- orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
122
- }
123
- });
124
- var ListMetricsInput = defineSchemaModel({
125
- name: "ListMetricsInput",
126
- description: "Input for listing metrics",
127
- fields: {
128
- orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
129
- category: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
130
- isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true },
131
- limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
132
- offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
133
- }
134
- });
135
- var ListMetricsOutput = defineSchemaModel({
136
- name: "ListMetricsOutput",
137
- description: "Output for listing metrics",
138
- fields: {
139
- metrics: { type: MetricDefinitionModel, isArray: true, isOptional: false },
140
- total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
141
- }
142
- });
143
- var RecordUsageInput = defineSchemaModel({
144
- name: "RecordUsageInput",
145
- description: "Input for recording usage",
146
- fields: {
147
- metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
148
- subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
149
- subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
150
- quantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
151
- timestamp: { type: ScalarTypeEnum.DateTime(), isOptional: true },
152
- source: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
153
- resourceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
154
- resourceType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
155
- metadata: { type: ScalarTypeEnum.JSON(), isOptional: true },
156
- idempotencyKey: {
157
- type: ScalarTypeEnum.String_unsecure(),
158
- isOptional: true
159
- }
160
- }
161
- });
162
- var RecordBatchUsageInput = defineSchemaModel({
163
- name: "RecordBatchUsageInput",
164
- description: "Input for recording batch usage",
165
- fields: {
166
- records: { type: RecordUsageInput, isArray: true, isOptional: false }
167
- }
168
- });
169
- var RecordBatchUsageOutput = defineSchemaModel({
170
- name: "RecordBatchUsageOutput",
171
- description: "Output for recording batch usage",
172
- fields: {
173
- recordedCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
174
- skippedCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
175
- recordIds: { type: ScalarTypeEnum.JSON(), isOptional: false }
176
- }
177
- });
178
- var GetUsageInput = defineSchemaModel({
179
- name: "GetUsageInput",
180
- description: "Input for getting usage",
181
- fields: {
182
- metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
183
- subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
184
- subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
185
- startDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
186
- endDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
187
- limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
188
- offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
189
- }
190
- });
191
- var GetUsageOutput = defineSchemaModel({
192
- name: "GetUsageOutput",
193
- description: "Output for getting usage",
194
- fields: {
195
- records: { type: UsageRecordModel, isArray: true, isOptional: false },
196
- total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
197
- totalQuantity: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false }
198
- }
199
- });
200
- var GetUsageSummaryInput = defineSchemaModel({
201
- name: "GetUsageSummaryInput",
202
- description: "Input for getting usage summary",
203
- fields: {
204
- metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
205
- subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
206
- subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
207
- periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
208
- startDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
209
- endDate: { type: ScalarTypeEnum.DateTime(), isOptional: true }
210
- }
211
- });
212
- var GetUsageSummaryOutput = defineSchemaModel({
213
- name: "GetUsageSummaryOutput",
214
- description: "Output for getting usage summary",
215
- fields: {
216
- summaries: { type: UsageSummaryModel, isArray: true, isOptional: false },
217
- total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
218
- }
219
- });
220
- var CreateThresholdInput = defineSchemaModel({
221
- name: "CreateThresholdInput",
222
- description: "Input for creating a threshold",
223
- fields: {
224
- metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
225
- subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
226
- subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
227
- name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
228
- threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
229
- warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
230
- periodType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
231
- action: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
232
- notifyEmails: { type: ScalarTypeEnum.JSON(), isOptional: true },
233
- notifyWebhook: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
234
- }
235
- });
236
- var UpdateThresholdInput = defineSchemaModel({
237
- name: "UpdateThresholdInput",
238
- description: "Input for updating a threshold",
239
- fields: {
240
- thresholdId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
241
- name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
242
- threshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
243
- warnThreshold: { type: ScalarTypeEnum.Float_unsecure(), isOptional: true },
244
- action: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
245
- notifyEmails: { type: ScalarTypeEnum.JSON(), isOptional: true },
246
- notifyWebhook: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
247
- isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true }
248
- }
249
- });
250
- var DeleteThresholdInput = defineSchemaModel({
251
- name: "DeleteThresholdInput",
252
- description: "Input for deleting a threshold",
253
- fields: {
254
- thresholdId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
255
- }
256
- });
257
- var ListThresholdsInput = defineSchemaModel({
258
- name: "ListThresholdsInput",
259
- description: "Input for listing thresholds",
260
- fields: {
261
- metricKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
262
- subjectType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
263
- subjectId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
264
- isActive: { type: ScalarTypeEnum.Boolean(), isOptional: true }
265
- }
266
- });
267
- var ListThresholdsOutput = defineSchemaModel({
268
- name: "ListThresholdsOutput",
269
- description: "Output for listing thresholds",
270
- fields: {
271
- thresholds: { type: UsageThresholdModel, isArray: true, isOptional: false },
272
- total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
273
- }
274
- });
275
- var SuccessOutput = defineSchemaModel({
276
- name: "SuccessOutput",
277
- description: "Generic success output",
278
- fields: {
279
- success: { type: ScalarTypeEnum.Boolean(), isOptional: false }
280
- }
281
- });
282
- var DefineMetricContract = defineCommand({
283
- meta: {
284
- key: "metric.define",
285
- version: "1.0.0",
286
- stability: "stable",
287
- owners: [...OWNERS],
288
- tags: ["metering", "metric", "define"],
289
- description: "Define a new usage metric.",
290
- goal: "Create a new metric for tracking usage.",
291
- context: "Called when setting up metering."
292
- },
293
- io: {
294
- input: DefineMetricInput,
295
- output: MetricDefinitionModel,
296
- errors: {
297
- METRIC_KEY_EXISTS: {
298
- description: "Metric key already exists",
299
- http: 409,
300
- gqlCode: "METRIC_KEY_EXISTS",
301
- when: "A metric with this key already exists"
302
- }
303
- }
304
- },
305
- policy: {
306
- auth: "admin"
307
- }
308
- });
309
- var UpdateMetricContract = defineCommand({
310
- meta: {
311
- key: "metric.update",
312
- version: "1.0.0",
313
- stability: "stable",
314
- owners: [...OWNERS],
315
- tags: ["metering", "metric", "update"],
316
- description: "Update a metric definition.",
317
- goal: "Modify metric configuration.",
318
- context: "Called when updating metric settings."
319
- },
320
- io: {
321
- input: UpdateMetricInput,
322
- output: MetricDefinitionModel,
323
- errors: {
324
- METRIC_NOT_FOUND: {
325
- description: "Metric does not exist",
326
- http: 404,
327
- gqlCode: "METRIC_NOT_FOUND",
328
- when: "Metric ID is invalid"
329
- }
330
- }
331
- },
332
- policy: {
333
- auth: "admin"
334
- }
335
- });
336
- var DeleteMetricContract = defineCommand({
337
- meta: {
338
- key: "metric.delete",
339
- version: "1.0.0",
340
- stability: "stable",
341
- owners: [...OWNERS],
342
- tags: ["metering", "metric", "delete"],
343
- description: "Delete a metric definition.",
344
- goal: "Remove a metric and its data.",
345
- context: "Called when removing a metric."
346
- },
347
- io: {
348
- input: DeleteMetricInput,
349
- output: SuccessOutput,
350
- errors: {
351
- METRIC_NOT_FOUND: {
352
- description: "Metric does not exist",
353
- http: 404,
354
- gqlCode: "METRIC_NOT_FOUND",
355
- when: "Metric ID is invalid"
356
- }
357
- }
358
- },
359
- policy: {
360
- auth: "admin"
361
- }
362
- });
363
- var GetMetricContract = defineQuery({
364
- meta: {
365
- key: "metric.get",
366
- version: "1.0.0",
367
- stability: "stable",
368
- owners: [...OWNERS],
369
- tags: ["metering", "metric", "get"],
370
- description: "Get a metric by key.",
371
- goal: "Retrieve metric definition.",
372
- context: "Called to inspect metric details."
373
- },
374
- io: {
375
- input: GetMetricInput,
376
- output: MetricDefinitionModel,
377
- errors: {
378
- METRIC_NOT_FOUND: {
379
- description: "Metric does not exist",
380
- http: 404,
381
- gqlCode: "METRIC_NOT_FOUND",
382
- when: "Metric key is invalid"
383
- }
384
- }
385
- },
386
- policy: {
387
- auth: "user"
388
- }
389
- });
390
- var ListMetricsContract = defineQuery({
391
- meta: {
392
- key: "metric.list",
393
- version: "1.0.0",
394
- stability: "stable",
395
- owners: [...OWNERS],
396
- tags: ["metering", "metric", "list"],
397
- description: "List all metrics.",
398
- goal: "View configured metrics.",
399
- context: "Called to browse metrics."
400
- },
401
- io: {
402
- input: ListMetricsInput,
403
- output: ListMetricsOutput
404
- },
405
- policy: {
406
- auth: "user"
407
- }
408
- });
409
- var RecordUsageContract = defineCommand({
410
- meta: {
411
- key: "usage.record",
412
- version: "1.0.0",
413
- stability: "stable",
414
- owners: [...OWNERS],
415
- tags: ["metering", "usage", "record"],
416
- description: "Record a usage event.",
417
- goal: "Track usage for billing and monitoring.",
418
- context: "Called when usage occurs."
419
- },
420
- io: {
421
- input: RecordUsageInput,
422
- output: UsageRecordModel,
423
- errors: {
424
- METRIC_NOT_FOUND: {
425
- description: "Metric does not exist",
426
- http: 404,
427
- gqlCode: "METRIC_NOT_FOUND",
428
- when: "Metric key is invalid"
429
- },
430
- DUPLICATE_RECORD: {
431
- description: "Record already exists",
432
- http: 409,
433
- gqlCode: "DUPLICATE_RECORD",
434
- when: "Idempotency key already used"
435
- }
436
- }
437
- },
438
- policy: {
439
- auth: "admin"
440
- }
441
- });
442
- var RecordBatchUsageContract = defineCommand({
443
- meta: {
444
- key: "usage.recordBatch",
445
- version: "1.0.0",
446
- stability: "stable",
447
- owners: [...OWNERS],
448
- tags: ["metering", "usage", "batch"],
449
- description: "Record multiple usage events.",
450
- goal: "Efficiently track bulk usage.",
451
- context: "Called for batch processing."
452
- },
453
- io: {
454
- input: RecordBatchUsageInput,
455
- output: RecordBatchUsageOutput
456
- },
457
- policy: {
458
- auth: "admin"
459
- }
460
- });
461
- var GetUsageContract = defineQuery({
462
- meta: {
463
- key: "usage.get",
464
- version: "1.0.0",
465
- stability: "stable",
466
- owners: [...OWNERS],
467
- tags: ["metering", "usage", "get"],
468
- description: "Get usage records for a subject.",
469
- goal: "View detailed usage history.",
470
- context: "Called to analyze usage."
471
- },
472
- io: {
473
- input: GetUsageInput,
474
- output: GetUsageOutput
475
- },
476
- policy: {
477
- auth: "user"
478
- }
479
- });
480
- var GetUsageSummaryContract = defineQuery({
481
- meta: {
482
- key: "usage.getSummary",
483
- version: "1.0.0",
484
- stability: "stable",
485
- owners: [...OWNERS],
486
- tags: ["metering", "usage", "summary"],
487
- description: "Get aggregated usage summary.",
488
- goal: "View usage totals for billing.",
489
- context: "Called for billing and reporting."
490
- },
491
- io: {
492
- input: GetUsageSummaryInput,
493
- output: GetUsageSummaryOutput
494
- },
495
- policy: {
496
- auth: "user"
497
- }
498
- });
499
- var CreateThresholdContract = defineCommand({
500
- meta: {
501
- key: "threshold.create",
502
- version: "1.0.0",
503
- stability: "stable",
504
- owners: [...OWNERS],
505
- tags: ["metering", "threshold", "create"],
506
- description: "Create a usage threshold.",
507
- goal: "Set up usage limits and alerts.",
508
- context: "Called when configuring limits."
509
- },
510
- io: {
511
- input: CreateThresholdInput,
512
- output: UsageThresholdModel,
513
- errors: {
514
- METRIC_NOT_FOUND: {
515
- description: "Metric does not exist",
516
- http: 404,
517
- gqlCode: "METRIC_NOT_FOUND",
518
- when: "Metric key is invalid"
519
- }
520
- }
521
- },
522
- policy: {
523
- auth: "admin"
524
- }
525
- });
526
- var UpdateThresholdContract = defineCommand({
527
- meta: {
528
- key: "threshold.update",
529
- version: "1.0.0",
530
- stability: "stable",
531
- owners: [...OWNERS],
532
- tags: ["metering", "threshold", "update"],
533
- description: "Update a threshold.",
534
- goal: "Modify threshold configuration.",
535
- context: "Called when adjusting limits."
536
- },
537
- io: {
538
- input: UpdateThresholdInput,
539
- output: UsageThresholdModel,
540
- errors: {
541
- THRESHOLD_NOT_FOUND: {
542
- description: "Threshold does not exist",
543
- http: 404,
544
- gqlCode: "THRESHOLD_NOT_FOUND",
545
- when: "Threshold ID is invalid"
546
- }
547
- }
548
- },
549
- policy: {
550
- auth: "admin"
551
- }
552
- });
553
- var DeleteThresholdContract = defineCommand({
554
- meta: {
555
- key: "threshold.delete",
556
- version: "1.0.0",
557
- stability: "stable",
558
- owners: [...OWNERS],
559
- tags: ["metering", "threshold", "delete"],
560
- description: "Delete a threshold.",
561
- goal: "Remove a usage threshold.",
562
- context: "Called when removing limits."
563
- },
564
- io: {
565
- input: DeleteThresholdInput,
566
- output: SuccessOutput,
567
- errors: {
568
- THRESHOLD_NOT_FOUND: {
569
- description: "Threshold does not exist",
570
- http: 404,
571
- gqlCode: "THRESHOLD_NOT_FOUND",
572
- when: "Threshold ID is invalid"
573
- }
574
- }
575
- },
576
- policy: {
577
- auth: "admin"
578
- }
579
- });
580
- var ListThresholdsContract = defineQuery({
581
- meta: {
582
- key: "threshold.list",
583
- version: "1.0.0",
584
- stability: "stable",
585
- owners: [...OWNERS],
586
- tags: ["metering", "threshold", "list"],
587
- description: "List usage thresholds.",
588
- goal: "View configured limits.",
589
- context: "Called to browse thresholds."
590
- },
591
- io: {
592
- input: ListThresholdsInput,
593
- output: ListThresholdsOutput
594
- },
595
- policy: {
596
- auth: "user"
597
- }
598
- });
599
- export {
600
- UsageThresholdModel,
601
- UsageSummaryModel,
602
- UsageRecordModel,
603
- UpdateThresholdContract,
604
- UpdateMetricContract,
605
- RecordUsageContract,
606
- RecordBatchUsageContract,
607
- MetricDefinitionModel,
608
- ListThresholdsContract,
609
- ListMetricsContract,
610
- GetUsageSummaryContract,
611
- GetUsageContract,
612
- GetMetricContract,
613
- DeleteThresholdContract,
614
- DeleteMetricContract,
615
- DefineMetricContract,
616
- CreateThresholdContract
617
- };
1
+ import{defineCommand as v,defineQuery as w}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as k,ScalarTypeEnum as j}from"@contractspec/lib.schema";var q=["platform.metering"],x=k({name:"MetricDefinition",description:"Represents a metric definition",fields:{id:{type:j.String_unsecure(),isOptional:!1},key:{type:j.String_unsecure(),isOptional:!1},name:{type:j.String_unsecure(),isOptional:!1},description:{type:j.String_unsecure(),isOptional:!0},unit:{type:j.String_unsecure(),isOptional:!1},aggregationType:{type:j.String_unsecure(),isOptional:!1},resetPeriod:{type:j.String_unsecure(),isOptional:!1},category:{type:j.String_unsecure(),isOptional:!0},isActive:{type:j.Boolean(),isOptional:!1},orgId:{type:j.String_unsecure(),isOptional:!0},createdAt:{type:j.DateTime(),isOptional:!1},updatedAt:{type:j.DateTime(),isOptional:!1}}}),A=k({name:"UsageRecord",description:"Represents a usage record",fields:{id:{type:j.String_unsecure(),isOptional:!1},metricKey:{type:j.String_unsecure(),isOptional:!1},subjectType:{type:j.String_unsecure(),isOptional:!1},subjectId:{type:j.String_unsecure(),isOptional:!1},quantity:{type:j.Float_unsecure(),isOptional:!1},source:{type:j.String_unsecure(),isOptional:!0},resourceId:{type:j.String_unsecure(),isOptional:!0},resourceType:{type:j.String_unsecure(),isOptional:!0},metadata:{type:j.JSON(),isOptional:!0},timestamp:{type:j.DateTime(),isOptional:!1},createdAt:{type:j.DateTime(),isOptional:!1}}}),J=k({name:"UsageSummary",description:"Represents aggregated usage",fields:{id:{type:j.String_unsecure(),isOptional:!1},metricKey:{type:j.String_unsecure(),isOptional:!1},subjectType:{type:j.String_unsecure(),isOptional:!1},subjectId:{type:j.String_unsecure(),isOptional:!1},periodType:{type:j.String_unsecure(),isOptional:!1},periodStart:{type:j.DateTime(),isOptional:!1},periodEnd:{type:j.DateTime(),isOptional:!1},totalQuantity:{type:j.Float_unsecure(),isOptional:!1},recordCount:{type:j.Int_unsecure(),isOptional:!1},minQuantity:{type:j.Float_unsecure(),isOptional:!0},maxQuantity:{type:j.Float_unsecure(),isOptional:!0},avgQuantity:{type:j.Float_unsecure(),isOptional:!0}}}),z=k({name:"UsageThreshold",description:"Represents a usage threshold",fields:{id:{type:j.String_unsecure(),isOptional:!1},metricKey:{type:j.String_unsecure(),isOptional:!1},subjectType:{type:j.String_unsecure(),isOptional:!0},subjectId:{type:j.String_unsecure(),isOptional:!0},name:{type:j.String_unsecure(),isOptional:!1},threshold:{type:j.Float_unsecure(),isOptional:!1},warnThreshold:{type:j.Float_unsecure(),isOptional:!0},periodType:{type:j.String_unsecure(),isOptional:!1},action:{type:j.String_unsecure(),isOptional:!1},currentValue:{type:j.Float_unsecure(),isOptional:!1},isActive:{type:j.Boolean(),isOptional:!1},createdAt:{type:j.DateTime(),isOptional:!1}}}),K=k({name:"DefineMetricInput",description:"Input for defining a metric",fields:{key:{type:j.String_unsecure(),isOptional:!1},name:{type:j.String_unsecure(),isOptional:!1},description:{type:j.String_unsecure(),isOptional:!0},unit:{type:j.String_unsecure(),isOptional:!1},aggregationType:{type:j.String_unsecure(),isOptional:!0},resetPeriod:{type:j.String_unsecure(),isOptional:!0},category:{type:j.String_unsecure(),isOptional:!0},orgId:{type:j.String_unsecure(),isOptional:!0},metadata:{type:j.JSON(),isOptional:!0}}}),P=k({name:"UpdateMetricInput",description:"Input for updating a metric",fields:{metricId:{type:j.String_unsecure(),isOptional:!1},name:{type:j.String_unsecure(),isOptional:!0},description:{type:j.String_unsecure(),isOptional:!0},category:{type:j.String_unsecure(),isOptional:!0},isActive:{type:j.Boolean(),isOptional:!0},metadata:{type:j.JSON(),isOptional:!0}}}),V=k({name:"DeleteMetricInput",description:"Input for deleting a metric",fields:{metricId:{type:j.String_unsecure(),isOptional:!1}}}),X=k({name:"GetMetricInput",description:"Input for getting a metric",fields:{key:{type:j.String_unsecure(),isOptional:!1},orgId:{type:j.String_unsecure(),isOptional:!0}}}),Y=k({name:"ListMetricsInput",description:"Input for listing metrics",fields:{orgId:{type:j.String_unsecure(),isOptional:!0},category:{type:j.String_unsecure(),isOptional:!0},isActive:{type:j.Boolean(),isOptional:!0},limit:{type:j.Int_unsecure(),isOptional:!0},offset:{type:j.Int_unsecure(),isOptional:!0}}}),Z=k({name:"ListMetricsOutput",description:"Output for listing metrics",fields:{metrics:{type:x,isArray:!0,isOptional:!1},total:{type:j.Int_unsecure(),isOptional:!1}}}),F=k({name:"RecordUsageInput",description:"Input for recording usage",fields:{metricKey:{type:j.String_unsecure(),isOptional:!1},subjectType:{type:j.String_unsecure(),isOptional:!1},subjectId:{type:j.String_unsecure(),isOptional:!1},quantity:{type:j.Float_unsecure(),isOptional:!1},timestamp:{type:j.DateTime(),isOptional:!0},source:{type:j.String_unsecure(),isOptional:!0},resourceId:{type:j.String_unsecure(),isOptional:!0},resourceType:{type:j.String_unsecure(),isOptional:!0},metadata:{type:j.JSON(),isOptional:!0},idempotencyKey:{type:j.String_unsecure(),isOptional:!0}}}),_=k({name:"RecordBatchUsageInput",description:"Input for recording batch usage",fields:{records:{type:F,isArray:!0,isOptional:!1}}}),$=k({name:"RecordBatchUsageOutput",description:"Output for recording batch usage",fields:{recordedCount:{type:j.Int_unsecure(),isOptional:!1},skippedCount:{type:j.Int_unsecure(),isOptional:!1},recordIds:{type:j.JSON(),isOptional:!1}}}),b=k({name:"GetUsageInput",description:"Input for getting usage",fields:{metricKey:{type:j.String_unsecure(),isOptional:!1},subjectType:{type:j.String_unsecure(),isOptional:!1},subjectId:{type:j.String_unsecure(),isOptional:!1},startDate:{type:j.DateTime(),isOptional:!1},endDate:{type:j.DateTime(),isOptional:!1},limit:{type:j.Int_unsecure(),isOptional:!0},offset:{type:j.Int_unsecure(),isOptional:!0}}}),B=k({name:"GetUsageOutput",description:"Output for getting usage",fields:{records:{type:A,isArray:!0,isOptional:!1},total:{type:j.Int_unsecure(),isOptional:!1},totalQuantity:{type:j.Float_unsecure(),isOptional:!1}}}),L=k({name:"GetUsageSummaryInput",description:"Input for getting usage summary",fields:{metricKey:{type:j.String_unsecure(),isOptional:!1},subjectType:{type:j.String_unsecure(),isOptional:!1},subjectId:{type:j.String_unsecure(),isOptional:!1},periodType:{type:j.String_unsecure(),isOptional:!1},startDate:{type:j.DateTime(),isOptional:!1},endDate:{type:j.DateTime(),isOptional:!0}}}),G=k({name:"GetUsageSummaryOutput",description:"Output for getting usage summary",fields:{summaries:{type:J,isArray:!0,isOptional:!1},total:{type:j.Int_unsecure(),isOptional:!1}}}),Q=k({name:"CreateThresholdInput",description:"Input for creating a threshold",fields:{metricKey:{type:j.String_unsecure(),isOptional:!1},subjectType:{type:j.String_unsecure(),isOptional:!0},subjectId:{type:j.String_unsecure(),isOptional:!0},name:{type:j.String_unsecure(),isOptional:!1},threshold:{type:j.Float_unsecure(),isOptional:!1},warnThreshold:{type:j.Float_unsecure(),isOptional:!0},periodType:{type:j.String_unsecure(),isOptional:!0},action:{type:j.String_unsecure(),isOptional:!0},notifyEmails:{type:j.JSON(),isOptional:!0},notifyWebhook:{type:j.String_unsecure(),isOptional:!0}}}),D=k({name:"UpdateThresholdInput",description:"Input for updating a threshold",fields:{thresholdId:{type:j.String_unsecure(),isOptional:!1},name:{type:j.String_unsecure(),isOptional:!0},threshold:{type:j.Float_unsecure(),isOptional:!0},warnThreshold:{type:j.Float_unsecure(),isOptional:!0},action:{type:j.String_unsecure(),isOptional:!0},notifyEmails:{type:j.JSON(),isOptional:!0},notifyWebhook:{type:j.String_unsecure(),isOptional:!0},isActive:{type:j.Boolean(),isOptional:!0}}}),C=k({name:"DeleteThresholdInput",description:"Input for deleting a threshold",fields:{thresholdId:{type:j.String_unsecure(),isOptional:!1}}}),N=k({name:"ListThresholdsInput",description:"Input for listing thresholds",fields:{metricKey:{type:j.String_unsecure(),isOptional:!0},subjectType:{type:j.String_unsecure(),isOptional:!0},subjectId:{type:j.String_unsecure(),isOptional:!0},isActive:{type:j.Boolean(),isOptional:!0}}}),W=k({name:"ListThresholdsOutput",description:"Output for listing thresholds",fields:{thresholds:{type:z,isArray:!0,isOptional:!1},total:{type:j.Int_unsecure(),isOptional:!1}}}),H=k({name:"SuccessOutput",description:"Generic success output",fields:{success:{type:j.Boolean(),isOptional:!1}}}),U=v({meta:{key:"metric.define",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","metric","define"],description:"Define a new usage metric.",goal:"Create a new metric for tracking usage.",context:"Called when setting up metering."},io:{input:K,output:x,errors:{METRIC_KEY_EXISTS:{description:"Metric key already exists",http:409,gqlCode:"METRIC_KEY_EXISTS",when:"A metric with this key already exists"}}},policy:{auth:"admin"}}),R=v({meta:{key:"metric.update",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","metric","update"],description:"Update a metric definition.",goal:"Modify metric configuration.",context:"Called when updating metric settings."},io:{input:P,output:x,errors:{METRIC_NOT_FOUND:{description:"Metric does not exist",http:404,gqlCode:"METRIC_NOT_FOUND",when:"Metric ID is invalid"}}},policy:{auth:"admin"}}),O=v({meta:{key:"metric.delete",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","metric","delete"],description:"Delete a metric definition.",goal:"Remove a metric and its data.",context:"Called when removing a metric."},io:{input:V,output:H,errors:{METRIC_NOT_FOUND:{description:"Metric does not exist",http:404,gqlCode:"METRIC_NOT_FOUND",when:"Metric ID is invalid"}}},policy:{auth:"admin"}}),s=w({meta:{key:"metric.get",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","metric","get"],description:"Get a metric by key.",goal:"Retrieve metric definition.",context:"Called to inspect metric details."},io:{input:X,output:x,errors:{METRIC_NOT_FOUND:{description:"Metric does not exist",http:404,gqlCode:"METRIC_NOT_FOUND",when:"Metric key is invalid"}}},policy:{auth:"user"}}),f=w({meta:{key:"metric.list",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","metric","list"],description:"List all metrics.",goal:"View configured metrics.",context:"Called to browse metrics."},io:{input:Y,output:Z},policy:{auth:"user"}}),h=v({meta:{key:"usage.record",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","usage","record"],description:"Record a usage event.",goal:"Track usage for billing and monitoring.",context:"Called when usage occurs."},io:{input:F,output:A,errors:{METRIC_NOT_FOUND:{description:"Metric does not exist",http:404,gqlCode:"METRIC_NOT_FOUND",when:"Metric key is invalid"},DUPLICATE_RECORD:{description:"Record already exists",http:409,gqlCode:"DUPLICATE_RECORD",when:"Idempotency key already used"}}},policy:{auth:"admin"}}),M=v({meta:{key:"usage.recordBatch",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","usage","batch"],description:"Record multiple usage events.",goal:"Efficiently track bulk usage.",context:"Called for batch processing."},io:{input:_,output:$},policy:{auth:"admin"}}),o=w({meta:{key:"usage.get",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","usage","get"],description:"Get usage records for a subject.",goal:"View detailed usage history.",context:"Called to analyze usage."},io:{input:b,output:B},policy:{auth:"user"}}),t=w({meta:{key:"usage.getSummary",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","usage","summary"],description:"Get aggregated usage summary.",goal:"View usage totals for billing.",context:"Called for billing and reporting."},io:{input:L,output:G},policy:{auth:"user"}}),i=v({meta:{key:"threshold.create",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","threshold","create"],description:"Create a usage threshold.",goal:"Set up usage limits and alerts.",context:"Called when configuring limits."},io:{input:Q,output:z,errors:{METRIC_NOT_FOUND:{description:"Metric does not exist",http:404,gqlCode:"METRIC_NOT_FOUND",when:"Metric key is invalid"}}},policy:{auth:"admin"}}),d=v({meta:{key:"threshold.update",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","threshold","update"],description:"Update a threshold.",goal:"Modify threshold configuration.",context:"Called when adjusting limits."},io:{input:D,output:z,errors:{THRESHOLD_NOT_FOUND:{description:"Threshold does not exist",http:404,gqlCode:"THRESHOLD_NOT_FOUND",when:"Threshold ID is invalid"}}},policy:{auth:"admin"}}),y=v({meta:{key:"threshold.delete",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","threshold","delete"],description:"Delete a threshold.",goal:"Remove a usage threshold.",context:"Called when removing limits."},io:{input:C,output:H,errors:{THRESHOLD_NOT_FOUND:{description:"Threshold does not exist",http:404,gqlCode:"THRESHOLD_NOT_FOUND",when:"Threshold ID is invalid"}}},policy:{auth:"admin"}}),T=w({meta:{key:"threshold.list",version:"1.0.0",stability:"stable",owners:[...q],tags:["metering","threshold","list"],description:"List usage thresholds.",goal:"View configured limits.",context:"Called to browse thresholds."},io:{input:N,output:W},policy:{auth:"user"}});export{z as UsageThresholdModel,J as UsageSummaryModel,A as UsageRecordModel,d as UpdateThresholdContract,R as UpdateMetricContract,h as RecordUsageContract,M as RecordBatchUsageContract,x as MetricDefinitionModel,T as ListThresholdsContract,f as ListMetricsContract,t as GetUsageSummaryContract,o as GetUsageContract,s as GetMetricContract,y as DeleteThresholdContract,O as DeleteMetricContract,U as DefineMetricContract,i as CreateThresholdContract};
@@ -1,15 +1,4 @@
1
- // src/docs/metering.docblock.ts
2
- import { registerDocBlocks } from "@contractspec/lib.contracts-spec/docs";
3
- var meteringDocBlocks = [
4
- {
5
- id: "docs.metering.usage",
6
- title: "Usage Metering & Billing Core",
7
- summary: "Reusable usage/metering layer with metric definitions, usage ingestion, aggregation, thresholds, and alerts for billing or quotas.",
8
- kind: "reference",
9
- visibility: "public",
10
- route: "/docs/metering/usage",
11
- tags: ["metering", "usage", "billing", "quotas"],
12
- body: `## Capabilities
1
+ import{registerDocBlocks as f}from"@contractspec/lib.contracts-spec/docs";var h=[{id:"docs.metering.usage",title:"Usage Metering & Billing Core",summary:"Reusable usage/metering layer with metric definitions, usage ingestion, aggregation, thresholds, and alerts for billing or quotas.",kind:"reference",visibility:"public",route:"/docs/metering/usage",tags:["metering","usage","billing","quotas"],body:`## Capabilities
13
2
 
14
3
  - **Entities**: MetricDefinition, UsageRecord, UsageSummary, UsageThreshold, UsageAlert.
15
4
  - **Contracts**: define/list metrics; record usage (batch + idempotent); retrieve usage by subject; manage thresholds and alerts.
@@ -34,7 +23,7 @@ var meteringDocBlocks = [
34
23
 
35
24
  ## Example
36
25
 
37
- ${"```"}ts
26
+ \`\`\`ts
38
27
  import { meteringSchemaContribution } from '@contractspec/lib.metering';
39
28
  import { aggregateUsage } from '@contractspec/lib.metering/aggregation';
40
29
 
@@ -49,14 +38,11 @@ await aggregateUsage({
49
38
  usage: usageRepository,
50
39
  period: 'DAILY',
51
40
  });
52
- ${"```"},
41
+ \`\`\`,
53
42
 
54
43
  ## Guardrails
55
44
 
56
45
  - Keep metric keys stable; store quantities as decimals for currency/units.
57
46
  - Use idempotency keys for external ingestion; avoid PII in metric metadata.
58
47
  - Scope by org/user for multi-tenant isolation; emit audit + analytics events on changes.
59
- `
60
- }
61
- ];
62
- registerDocBlocks(meteringDocBlocks);
48
+ `}];f(h);