@contractspec/lib.metering 3.7.6 → 3.7.7

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,198 +1,78 @@
1
1
  # @contractspec/lib.metering
2
2
 
3
- Website: https://contractspec.io/
3
+ Website: https://contractspec.io
4
4
 
5
+ **Usage metering and billing core module for ContractSpec applications.**
5
6
 
6
- Usage metering and billing core module for ContractSpec applications.
7
-
8
- ## Overview
7
+ ## What It Provides
9
8
 
10
- This module provides a reusable metering system that can be used to track usage-based metrics across all ContractSpec applications. It supports:
9
+ - **Layer**: lib.
10
+ - **Consumers**: bundles.
11
+ - `src/contracts/` contains contract specs, operations, entities, and registry exports.
12
+ - `src/docs/` contains docblocks and documentation-facing exports.
13
+ - `src/contracts/` contains contract specs, operations, entities, and registry exports.
14
+ - `src/docs/` contains docblocks and documentation-facing exports.
11
15
 
12
- - **Metric Definitions**: Define what metrics to track
13
- - **Usage Recording**: Record usage events
14
- - **Aggregation**: Roll up usage into summaries
15
- - **Thresholds & Alerts**: Monitor usage against limits
16
- - **Billing Integration**: Connect usage to billing/plans
16
+ ## Installation
17
17
 
18
- ## Entities
18
+ `npm install @contractspec/lib.metering`
19
19
 
20
- ### MetricDefinition
20
+ or
21
21
 
22
- Defines a trackable metric.
23
-
24
- | Field | Type | Description |
25
- |-------|------|-------------|
26
- | id | string | Unique identifier |
27
- | key | string | Metric key (e.g., `api_calls`, `storage_gb`) |
28
- | name | string | Human-readable name |
29
- | description | string | Metric description |
30
- | unit | string | Unit of measurement |
31
- | aggregationType | enum | How to aggregate (count, sum, avg, max, min) |
32
- | resetPeriod | enum | When to reset (never, hourly, daily, monthly) |
33
- | orgId | string | Organization scope (null = global) |
34
-
35
- ### UsageRecord
36
-
37
- Individual usage event.
38
-
39
- | Field | Type | Description |
40
- |-------|------|-------------|
41
- | id | string | Unique identifier |
42
- | metricKey | string | Metric being recorded |
43
- | subjectType | string | Subject type (org, user, project) |
44
- | subjectId | string | Subject identifier |
45
- | quantity | decimal | Usage quantity |
46
- | timestamp | datetime | When usage occurred |
47
- | metadata | json | Additional context |
48
-
49
- ### UsageSummary
50
-
51
- Pre-aggregated usage summary.
52
-
53
- | Field | Type | Description |
54
- |-------|------|-------------|
55
- | id | string | Unique identifier |
56
- | metricKey | string | Metric key |
57
- | subjectType | string | Subject type |
58
- | subjectId | string | Subject identifier |
59
- | periodType | enum | Period type (hourly, daily, monthly) |
60
- | periodStart | datetime | Period start time |
61
- | periodEnd | datetime | Period end time |
62
- | totalQuantity | decimal | Aggregated quantity |
63
- | recordCount | int | Number of records aggregated |
64
-
65
- ### UsageThreshold
66
-
67
- Threshold configuration for alerts.
68
-
69
- | Field | Type | Description |
70
- |-------|------|-------------|
71
- | id | string | Unique identifier |
72
- | metricKey | string | Metric to monitor |
73
- | subjectType | string | Subject type |
74
- | subjectId | string | Subject identifier |
75
- | threshold | decimal | Threshold value |
76
- | action | enum | Action when exceeded (alert, block, none) |
77
- | notifyEmails | json | Email addresses to notify |
78
-
79
- ## Contracts
80
-
81
- ### Commands
82
-
83
- - `metric.define` - Define a new metric
84
- - `metric.update` - Update metric definition
85
- - `metric.delete` - Delete a metric
86
- - `usage.record` - Record a usage event
87
- - `usage.recordBatch` - Record multiple usage events
88
- - `threshold.create` - Create a usage threshold
89
- - `threshold.update` - Update a threshold
90
- - `threshold.delete` - Delete a threshold
91
-
92
- ### Queries
93
-
94
- - `metric.get` - Get metric definition
95
- - `metric.list` - List all metrics
96
- - `usage.get` - Get usage for a subject
97
- - `usage.getSummary` - Get aggregated usage summary
98
- - `usage.export` - Export usage data
99
- - `threshold.list` - List thresholds
100
-
101
- ## Events
102
-
103
- - `metric.defined` - Metric was defined
104
- - `usage.recorded` - Usage was recorded
105
- - `usage.aggregated` - Usage was aggregated into summary
106
- - `threshold.exceeded` - Usage exceeded a threshold
107
- - `threshold.approaching` - Usage approaching threshold (80%)
22
+ `bun add @contractspec/lib.metering`
108
23
 
109
24
  ## Usage
110
25
 
111
- ```typescript
112
- import {
113
- MetricDefinitionEntity,
114
- RecordUsageContract,
115
- UsageAggregator
116
- } from '@contractspec/lib.metering';
117
-
118
- // Define a metric
119
- await meteringService.defineMetric({
120
- key: 'api_calls',
121
- name: 'API Calls',
122
- unit: 'calls',
123
- aggregationType: 'COUNT',
124
- resetPeriod: 'MONTHLY',
125
- });
126
-
127
- // Record usage
128
- await meteringService.recordUsage({
129
- metricKey: 'api_calls',
130
- subjectType: 'org',
131
- subjectId: 'org-123',
132
- quantity: 1,
133
- });
134
-
135
- // Get usage summary
136
- const summary = await meteringService.getUsageSummary({
137
- metricKey: 'api_calls',
138
- subjectType: 'org',
139
- subjectId: 'org-123',
140
- periodType: 'MONTHLY',
141
- periodStart: new Date('2024-01-01'),
142
- });
143
- ```
144
-
145
- ## Aggregation
146
-
147
- The module includes an aggregation engine that periodically rolls up usage records:
148
-
149
- ```typescript
150
- import { UsageAggregator } from '@contractspec/lib.metering/aggregation';
151
-
152
- const aggregator = new UsageAggregator({
153
- storage: usageStorage,
154
- });
155
-
156
- // Run hourly aggregation
157
- await aggregator.aggregate({
158
- periodType: 'HOURLY',
159
- periodStart: new Date(),
160
- });
161
- ```
162
-
163
- ## Integration
164
-
165
- This module integrates with:
166
-
167
- - `@contractspec/lib.jobs` - Scheduled aggregation jobs
168
- - `@contractspec/module.notifications` - Threshold alerts
169
- - `@contractspec/lib.identity-rbac` - Subject resolution
170
-
171
- ## Schema Contribution
172
-
173
- ```typescript
174
- import { meteringSchemaContribution } from '@contractspec/lib.metering';
175
-
176
- export const schemaComposition = {
177
- modules: [
178
- meteringSchemaContribution,
179
- // ... other modules
180
- ],
181
- };
182
- ```
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
26
+ Import the root entrypoint from `@contractspec/lib.metering`, or choose a documented subpath when you only need one part of the package surface.
27
+
28
+ ## Architecture
29
+
30
+ - `src/aggregation` is part of the package's public or composition surface.
31
+ - `src/analytics` is part of the package's public or composition surface.
32
+ - `src/contracts/` contains contract specs, operations, entities, and registry exports.
33
+ - `src/docs/` contains docblocks and documentation-facing exports.
34
+ - `src/entities/` contains domain entities and value objects.
35
+ - `src/events.ts` is package-level event definitions.
36
+ - `src/index.ts` is the root public barrel and package entrypoint.
37
+
38
+ ## Public Entry Points
39
+
40
+ - Export `.` resolves through `./src/index.ts`.
41
+ - Export `./aggregation` resolves through `./src/aggregation/index.ts`.
42
+ - Export `./analytics/posthog-metering` resolves through `./src/analytics/posthog-metering.ts`.
43
+ - Export `./analytics/posthog-metering-reader` resolves through `./src/analytics/posthog-metering-reader.ts`.
44
+ - Export `./contracts` resolves through `./src/contracts/index.ts`.
45
+ - Export `./docs` resolves through `./src/docs/index.ts`.
46
+ - Export `./docs/metering.docblock` resolves through `./src/docs/metering.docblock.ts`.
47
+ - Export `./entities` resolves through `./src/entities/index.ts`.
48
+ - Export `./events` resolves through `./src/events.ts`.
49
+ - Export `./metering.capability` resolves through `./src/metering.capability.ts`.
50
+ - The package publishes 11 total export subpaths; keep docs aligned with `package.json`.
51
+
52
+ ## Local Commands
53
+
54
+ - `bun run dev` — contractspec-bun-build dev
55
+ - `bun run build` — bun run prebuild && bun run build:bundle && bun run build:types
56
+ - `bun run lint` — bun lint:fix
57
+ - `bun run lint:check` — biome check .
58
+ - `bun run lint:fix` — biome check --write --unsafe --only=nursery/useSortedClasses . && biome check --write .
59
+ - `bun run typecheck` — tsc --noEmit
60
+ - `bun run publish:pkg` — bun publish --tolerate-republish --ignore-scripts --verbose
61
+ - `bun run publish:pkg:canary` — bun publish:pkg --tag canary
62
+ - `bun run clean` rimraf dist .turbo
63
+ - `bun run build:bundle` — contractspec-bun-build transpile
64
+ - `bun run build:types` — contractspec-bun-build types
65
+ - `bun run prebuild` contractspec-bun-build prebuild
66
+
67
+ ## Recent Updates
68
+
69
+ - Replace eslint+prettier by biomejs to optimize speed.
70
+ - Stability.
71
+ - Resolve lint, build, and type errors across nine packages.
72
+ - Add AI provider ranking system with ranking-driven model selection.
73
+
74
+ ## Notes
75
+
76
+ - Aggregation logic must stay deterministic — non-determinism causes billing discrepancies.
77
+ - Billing-related schemas are compliance-sensitive; changes require review.
78
+ - Capability contract (`metering.capability`) is public API — treat as a breaking-change surface.
@@ -1,6 +1,6 @@
1
1
  // src/contracts/index.ts
2
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
3
2
  import { defineCommand, defineQuery } from "@contractspec/lib.contracts-spec";
3
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
4
  var OWNERS = ["platform.metering"];
5
5
  var MetricDefinitionModel = defineSchemaModel({
6
6
  name: "MetricDefinition",
@@ -1,6 +1,6 @@
1
1
  // src/events.ts
2
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
3
2
  import { defineEvent } from "@contractspec/lib.contracts-spec";
3
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
4
  var MetricDefinedPayload = defineSchemaModel({
5
5
  name: "MetricDefinedEventPayload",
6
6
  description: "Payload when a metric is defined",
@@ -565,8 +565,8 @@ class PosthogMeteringReporter {
565
565
  }
566
566
 
567
567
  // src/contracts/index.ts
568
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
569
568
  import { defineCommand, defineQuery } from "@contractspec/lib.contracts-spec";
569
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
570
570
  var OWNERS = ["platform.metering"];
571
571
  var MetricDefinitionModel = defineSchemaModel({
572
572
  name: "MetricDefinition",
@@ -1564,8 +1564,8 @@ var meteringSchemaContribution = {
1564
1564
  };
1565
1565
 
1566
1566
  // src/events.ts
1567
- import { ScalarTypeEnum as ScalarTypeEnum2, defineSchemaModel as defineSchemaModel2 } from "@contractspec/lib.schema";
1568
1567
  import { defineEvent } from "@contractspec/lib.contracts-spec";
1568
+ import { defineSchemaModel as defineSchemaModel2, ScalarTypeEnum as ScalarTypeEnum2 } from "@contractspec/lib.schema";
1569
1569
  var MetricDefinedPayload = defineSchemaModel2({
1570
1570
  name: "MetricDefinedEventPayload",
1571
1571
  description: "Payload when a metric is defined",
@@ -1,7 +1,7 @@
1
1
  // @bun
2
2
  // src/contracts/index.ts
3
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
4
3
  import { defineCommand, defineQuery } from "@contractspec/lib.contracts-spec";
4
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
5
5
  var OWNERS = ["platform.metering"];
6
6
  var MetricDefinitionModel = defineSchemaModel({
7
7
  name: "MetricDefinition",
package/dist/events.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // @bun
2
2
  // src/events.ts
3
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
4
3
  import { defineEvent } from "@contractspec/lib.contracts-spec";
4
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
5
5
  var MetricDefinedPayload = defineSchemaModel({
6
6
  name: "MetricDefinedEventPayload",
7
7
  description: "Payload when a metric is defined",
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- export * from './entities';
2
- export * from './contracts';
3
- export * from './events';
4
1
  export * from './aggregation';
5
- export * from './metering.feature';
6
2
  export * from './analytics/posthog-metering';
7
3
  export * from './analytics/posthog-metering-reader';
4
+ export * from './contracts';
5
+ export * from './entities';
6
+ export * from './events';
7
+ export * from './metering.feature';
8
8
  import './docs';
package/dist/index.js CHANGED
@@ -566,8 +566,8 @@ class PosthogMeteringReporter {
566
566
  }
567
567
 
568
568
  // src/contracts/index.ts
569
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
570
569
  import { defineCommand, defineQuery } from "@contractspec/lib.contracts-spec";
570
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
571
571
  var OWNERS = ["platform.metering"];
572
572
  var MetricDefinitionModel = defineSchemaModel({
573
573
  name: "MetricDefinition",
@@ -1565,8 +1565,8 @@ var meteringSchemaContribution = {
1565
1565
  };
1566
1566
 
1567
1567
  // src/events.ts
1568
- import { ScalarTypeEnum as ScalarTypeEnum2, defineSchemaModel as defineSchemaModel2 } from "@contractspec/lib.schema";
1569
1568
  import { defineEvent } from "@contractspec/lib.contracts-spec";
1569
+ import { defineSchemaModel as defineSchemaModel2, ScalarTypeEnum as ScalarTypeEnum2 } from "@contractspec/lib.schema";
1570
1570
  var MetricDefinedPayload = defineSchemaModel2({
1571
1571
  name: "MetricDefinedEventPayload",
1572
1572
  description: "Payload when a metric is defined",
@@ -1,6 +1,6 @@
1
1
  // src/contracts/index.ts
2
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
3
2
  import { defineCommand, defineQuery } from "@contractspec/lib.contracts-spec";
3
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
4
  var OWNERS = ["platform.metering"];
5
5
  var MetricDefinitionModel = defineSchemaModel({
6
6
  name: "MetricDefinition",
@@ -1,6 +1,6 @@
1
1
  // src/events.ts
2
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
3
2
  import { defineEvent } from "@contractspec/lib.contracts-spec";
3
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
4
  var MetricDefinedPayload = defineSchemaModel({
5
5
  name: "MetricDefinedEventPayload",
6
6
  description: "Payload when a metric is defined",
@@ -565,8 +565,8 @@ class PosthogMeteringReporter {
565
565
  }
566
566
 
567
567
  // src/contracts/index.ts
568
- import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
569
568
  import { defineCommand, defineQuery } from "@contractspec/lib.contracts-spec";
569
+ import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
570
570
  var OWNERS = ["platform.metering"];
571
571
  var MetricDefinitionModel = defineSchemaModel({
572
572
  name: "MetricDefinition",
@@ -1564,8 +1564,8 @@ var meteringSchemaContribution = {
1564
1564
  };
1565
1565
 
1566
1566
  // src/events.ts
1567
- import { ScalarTypeEnum as ScalarTypeEnum2, defineSchemaModel as defineSchemaModel2 } from "@contractspec/lib.schema";
1568
1567
  import { defineEvent } from "@contractspec/lib.contracts-spec";
1568
+ import { defineSchemaModel as defineSchemaModel2, ScalarTypeEnum as ScalarTypeEnum2 } from "@contractspec/lib.schema";
1569
1569
  var MetricDefinedPayload = defineSchemaModel2({
1570
1570
  name: "MetricDefinedEventPayload",
1571
1571
  description: "Payload when a metric is defined",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contractspec/lib.metering",
3
- "version": "3.7.6",
3
+ "version": "3.7.7",
4
4
  "description": "Usage metering and billing core module for ContractSpec applications",
5
5
  "keywords": [
6
6
  "contractspec",
@@ -21,15 +21,15 @@
21
21
  "dev": "contractspec-bun-build dev",
22
22
  "clean": "rimraf dist .turbo",
23
23
  "lint": "bun lint:fix",
24
- "lint:fix": "eslint src --fix",
25
- "lint:check": "eslint src",
24
+ "lint:fix": "biome check --write --unsafe --only=nursery/useSortedClasses . && biome check --write .",
25
+ "lint:check": "biome check .",
26
26
  "prebuild": "contractspec-bun-build prebuild",
27
27
  "typecheck": "tsc --noEmit"
28
28
  },
29
29
  "dependencies": {
30
30
  "@contractspec/lib.schema": "3.7.6",
31
- "@contractspec/lib.contracts-spec": "3.7.6",
32
- "@contractspec/lib.contracts-integrations": "3.7.6",
31
+ "@contractspec/lib.contracts-spec": "4.0.0",
32
+ "@contractspec/lib.contracts-integrations": "3.7.7",
33
33
  "zod": "^4.3.5"
34
34
  },
35
35
  "devDependencies": {