@openmeter/sdk 1.0.0-beta.5 → 1.0.0-beta.51

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
@@ -3,7 +3,7 @@
3
3
  ## Install
4
4
 
5
5
  ```sh
6
- npm install --save @openmeter/sdk@beta
6
+ npm install --save @openmeter/sdk
7
7
  ```
8
8
 
9
9
  ## Example
@@ -54,6 +54,20 @@ const event: Event = {
54
54
  await openmeter.events.ingest(event)
55
55
  ```
56
56
 
57
+ ### batch ingest
58
+
59
+ ```ts
60
+ await openmeter.events.ingest([event1, event2, event3])
61
+ ```
62
+
63
+ #### list
64
+
65
+ Retrieve latest raw events. Useful for debugging.
66
+
67
+ ```ts
68
+ const events = await openmeter.events.list()
69
+ ```
70
+
57
71
  ### Meters
58
72
 
59
73
  #### list
@@ -72,17 +86,87 @@ Get one meter by slug.
72
86
  const meter = await openmeter.meters.get('m1')
73
87
  ```
74
88
 
75
- #### values
89
+ #### query
76
90
 
77
- Get back meter values.
91
+ Query meter values.
78
92
 
79
93
  ```ts
80
94
  import { WindowSize } from '@openmeter/sdk'
81
95
 
82
- const values = await openmeter.meters.values('my-meter-slug', {
83
- subject: 'user-1',
96
+ const values = await openmeter.meters.query('my-meter-slug', {
97
+ subject: ['user-1'],
98
+ groupBy: ['method', 'path'],
84
99
  from: new Date('2021-01-01'),
85
100
  to: new Date('2021-01-02'),
86
101
  windowSize: WindowSize.HOUR,
87
102
  })
88
103
  ```
104
+
105
+ #### meter subjects
106
+
107
+ List meter subjects.
108
+
109
+ ```ts
110
+ const subjects = await openmeter.meters.subjects('my-meter-slug')
111
+ ```
112
+
113
+ ### Portal
114
+
115
+ #### createToken
116
+
117
+ Create subject specific tokens.
118
+ Useful to build consumer dashboards.
119
+
120
+ ```ts
121
+ const token = await openmeter.portal.createToken({ subject: 'customer-1' })
122
+ ```
123
+
124
+ #### invalidateTokens
125
+
126
+ Invalidate portal tokens for all or specific subjects.
127
+
128
+ ```ts
129
+ await openmeter.portal.invalidateTokens()
130
+ ```
131
+
132
+ ### Subject
133
+
134
+ Subject mappings. Like display name and metadata.
135
+
136
+ #### upsert
137
+
138
+ Upsert subjects.
139
+
140
+ ```ts
141
+ const subjects = await openmeter.subjects.upsert([
142
+ {
143
+ key: 'customer-1',
144
+ displayName: 'ACME'
145
+ }
146
+ ])
147
+ ```
148
+
149
+ #### list
150
+
151
+ List subjects.
152
+
153
+ ```ts
154
+ const subjects = await openmeter.subjects.list()
155
+ ```
156
+
157
+ #### get
158
+
159
+ Get subject by key.
160
+
161
+ ```ts
162
+ const subjects = await openmeter.subjects.get('customer-1')
163
+ ```
164
+
165
+ #### delete
166
+
167
+ Delete subject by key.
168
+ It doesn't delete corresponding usage.
169
+
170
+ ```ts
171
+ await openmeter.subjects.delete('customer-1')
172
+ ```
@@ -80,7 +80,9 @@ export class BaseClient {
80
80
  continue;
81
81
  }
82
82
  if (Array.isArray(value)) {
83
- searchParams.append(key, value.join(','));
83
+ for (const item of value) {
84
+ searchParams.append(key, item);
85
+ }
84
86
  }
85
87
  else if (value instanceof Date) {
86
88
  searchParams.append(key, value.toISOString());
@@ -1,4 +1,14 @@
1
+ import { components } from '../schemas/openapi.js';
1
2
  import { RequestOptions, BaseClient, OpenMeterConfig } from './client.js';
3
+ export type CloudEvents = components['schemas']['Event'];
4
+ export type IngestedEvent = components['schemas']['IngestedEvent'];
5
+ export type EventsQueryParams = {
6
+ /**
7
+ * @description Limit number of results. Max: 100
8
+ * @example 25
9
+ */
10
+ limit?: number;
11
+ };
2
12
  /**
3
13
  * Usage Event
4
14
  */
@@ -53,7 +63,7 @@ export type Event = {
53
63
  * "path": "/hello"
54
64
  * }
55
65
  */
56
- data: Record<string, string | number | Record<string, string | number>>;
66
+ data: Record<string, unknown>;
57
67
  };
58
68
  export declare class EventsClient extends BaseClient {
59
69
  constructor(config: OpenMeterConfig);
@@ -61,5 +71,9 @@ export declare class EventsClient extends BaseClient {
61
71
  * Ingest usage event in a CloudEvents format
62
72
  * @see https://cloudevents.io
63
73
  */
64
- ingest(usageEvent: Event, options?: RequestOptions): Promise<void>;
74
+ ingest(usageEvent: Event | Event[], options?: RequestOptions): Promise<void>;
75
+ /**
76
+ * List raw events
77
+ */
78
+ list(params?: EventsQueryParams, options?: RequestOptions): Promise<IngestedEvent[]>;
65
79
  }
@@ -9,31 +9,52 @@ export class EventsClient extends BaseClient {
9
9
  * @see https://cloudevents.io
10
10
  */
11
11
  async ingest(usageEvent, options) {
12
- if (usageEvent.datacontenttype &&
13
- usageEvent.datacontenttype !== 'application/json') {
14
- throw new TypeError(`Unsupported datacontenttype: ${usageEvent.datacontenttype}`);
15
- }
16
- // We default where we can to lower the barrier to use CloudEvents
17
- const body = {
18
- specversion: usageEvent.specversion ?? '1.0',
19
- id: usageEvent.id ?? crypto.randomUUID(),
20
- source: usageEvent.source ?? '@openmeter/sdk',
21
- type: usageEvent.type,
22
- subject: usageEvent.subject,
23
- time: usageEvent.time?.toISOString(),
24
- datacontenttype: usageEvent.datacontenttype,
25
- dataschema: usageEvent.dataschema,
26
- data: usageEvent.data,
27
- };
12
+ const isBatch = Array.isArray(usageEvent);
13
+ const cloudEvents = (isBatch ? usageEvent : [usageEvent]).map((ev) => {
14
+ // Validate content type
15
+ if (ev.datacontenttype &&
16
+ ev.datacontenttype !== 'application/json') {
17
+ throw new TypeError(`Unsupported datacontenttype: ${ev.datacontenttype}`);
18
+ }
19
+ // We default where we can to lower the barrier to use CloudEvents
20
+ const cloudEvent = {
21
+ specversion: ev.specversion ?? '1.0',
22
+ id: ev.id ?? crypto.randomUUID(),
23
+ source: ev.source ?? '@openmeter/sdk',
24
+ type: ev.type,
25
+ subject: ev.subject,
26
+ time: ev.time?.toISOString(),
27
+ datacontenttype: ev.datacontenttype,
28
+ dataschema: ev.dataschema,
29
+ data: ev.data,
30
+ };
31
+ return cloudEvent;
32
+ });
33
+ const contentType = isBatch ? 'application/cloudevents-batch+json' : 'application/cloudevents+json';
34
+ const body = isBatch ? JSON.stringify(cloudEvents) : JSON.stringify(cloudEvents[0]);
28
35
  // Making Request
29
36
  return await this.request({
30
37
  path: '/api/v1/events',
31
38
  method: 'POST',
32
- body: JSON.stringify(body),
39
+ body,
33
40
  headers: {
34
- 'Content-Type': 'application/cloudevents+json',
41
+ 'Content-Type': contentType,
35
42
  },
36
43
  options,
37
44
  });
38
45
  }
46
+ /**
47
+ * List raw events
48
+ */
49
+ async list(params, options) {
50
+ const searchParams = params
51
+ ? BaseClient.toURLSearchParams(params)
52
+ : undefined;
53
+ return this.request({
54
+ method: 'GET',
55
+ path: `/api/v1/events`,
56
+ searchParams,
57
+ options,
58
+ });
59
+ }
39
60
  }
@@ -13,7 +13,11 @@ export declare enum MeterAggregation {
13
13
  MAX = "MAX"
14
14
  }
15
15
  export type MeterQueryParams = {
16
- subject?: string;
16
+ /**
17
+ * @description Subject(s) to filter by.
18
+ * @example ["customer-1", "customer-2"]
19
+ */
20
+ subject?: string[];
17
21
  /**
18
22
  * @description Start date.
19
23
  * Must be aligned with the window size.
@@ -26,13 +30,24 @@ export type MeterQueryParams = {
26
30
  * Inclusive.
27
31
  */
28
32
  to?: Date;
29
- /** @description If not specified, a single usage aggregate will be returned for the entirety of the specified period for each subject and group. */
33
+ /**
34
+ * @description Window Size
35
+ * If not specified, a single usage aggregate will be returned for the entirety of
36
+ * the specified period for each subject and group.
37
+ */
30
38
  windowSize?: WindowSizeType;
31
- /** @description If not specified a single aggregate will be returned for each subject and time window. */
39
+ /**
40
+ * @description The value is the name of the time zone as defined in the IANA Time Zone Database (http://www.iana.org/time-zones).
41
+ * If not specified, the UTC timezone will be used.
42
+ */
43
+ windowTimeZone?: string;
44
+ /**
45
+ * @description Group By
46
+ * If not specified a single aggregate will be returned for each subject and time window.
47
+ */
32
48
  groupBy?: string[];
33
49
  };
34
- export type MeterQueryResponse = paths['/api/v1/meters/{meterIdOrSlug}/values']['get']['responses']['200']['content']['application/json'];
35
- export type MeterValue = components['schemas']['MeterValue'];
50
+ export type MeterQueryResponse = paths['/api/v1/meters/{meterIdOrSlug}/query']['get']['responses']['200']['content']['application/json'];
36
51
  export type Meter = components['schemas']['Meter'];
37
52
  export type WindowSizeType = components['schemas']['WindowSize'];
38
53
  export declare class MetersClient extends BaseClient {
@@ -46,7 +61,11 @@ export declare class MetersClient extends BaseClient {
46
61
  */
47
62
  list(options?: RequestOptions): Promise<Meter[]>;
48
63
  /**
49
- * Get aggregated values of a meter
64
+ * Query a meter
65
+ */
66
+ query(slug: string, params?: MeterQueryParams, options?: RequestOptions): Promise<MeterQueryResponse>;
67
+ /**
68
+ * List subjects of a meter
50
69
  */
51
- values(slug: string, params?: MeterQueryParams, options?: RequestOptions): Promise<MeterQueryResponse>;
70
+ subjects(slug: string, options?: RequestOptions): Promise<string[]>;
52
71
  }
@@ -38,17 +38,27 @@ export class MetersClient extends BaseClient {
38
38
  });
39
39
  }
40
40
  /**
41
- * Get aggregated values of a meter
41
+ * Query a meter
42
42
  */
43
- async values(slug, params, options) {
43
+ async query(slug, params, options) {
44
44
  const searchParams = params
45
45
  ? BaseClient.toURLSearchParams(params)
46
46
  : undefined;
47
47
  return this.request({
48
48
  method: 'GET',
49
- path: `/api/v1/meters/${slug}/values`,
49
+ path: `/api/v1/meters/${slug}/query`,
50
50
  searchParams,
51
51
  options,
52
52
  });
53
53
  }
54
+ /**
55
+ * List subjects of a meter
56
+ */
57
+ async subjects(slug, options) {
58
+ return this.request({
59
+ method: 'GET',
60
+ path: `/api/v1/meters/${slug}/subjects`,
61
+ options,
62
+ });
63
+ }
54
64
  }
@@ -0,0 +1,22 @@
1
+ import { components } from '../schemas/openapi.js';
2
+ import { RequestOptions, BaseClient, OpenMeterConfig } from './client.js';
3
+ export type PortalToken = components['schemas']['PortalToken'];
4
+ export declare class PortalClient extends BaseClient {
5
+ constructor(config: OpenMeterConfig);
6
+ /**
7
+ * Create portal token
8
+ * Useful for creating a token sharable with your customer to query their own usage
9
+ */
10
+ createToken(token: {
11
+ subject: string;
12
+ expiresAt?: Date;
13
+ allowedMeterSlugs?: string[];
14
+ }, options?: RequestOptions): Promise<PortalToken>;
15
+ /**
16
+ * Invalidate portal token
17
+ * @note OpenMeter Cloud only feature
18
+ */
19
+ invalidateTokens(invalidate?: {
20
+ subject?: string;
21
+ }, options?: RequestOptions): Promise<void>;
22
+ }
@@ -0,0 +1,36 @@
1
+ import { BaseClient } from './client.js';
2
+ export class PortalClient extends BaseClient {
3
+ constructor(config) {
4
+ super(config);
5
+ }
6
+ /**
7
+ * Create portal token
8
+ * Useful for creating a token sharable with your customer to query their own usage
9
+ */
10
+ async createToken(token, options) {
11
+ return await this.request({
12
+ path: '/api/v1/portal/tokens',
13
+ method: 'POST',
14
+ headers: {
15
+ 'Content-Type': 'application/json',
16
+ },
17
+ body: JSON.stringify(token),
18
+ options,
19
+ });
20
+ }
21
+ /**
22
+ * Invalidate portal token
23
+ * @note OpenMeter Cloud only feature
24
+ */
25
+ async invalidateTokens(invalidate = {}, options) {
26
+ return await this.request({
27
+ path: '/api/v1/portal/tokens/invalidate',
28
+ method: 'POST',
29
+ headers: {
30
+ 'Content-Type': 'application/json',
31
+ },
32
+ body: JSON.stringify(invalidate),
33
+ options,
34
+ });
35
+ }
36
+ }
@@ -0,0 +1,27 @@
1
+ import { components } from '../schemas/openapi.js';
2
+ import { RequestOptions, BaseClient, OpenMeterConfig } from './client.js';
3
+ export type Subject = components['schemas']['Subject'];
4
+ export declare class SubjectClient extends BaseClient {
5
+ constructor(config: OpenMeterConfig);
6
+ /**
7
+ * Upsert subject
8
+ * Useful to map display name and metadata to subjects
9
+ * @note OpenMeter Cloud only feature
10
+ */
11
+ upsert(subject: Omit<Subject, 'id'>[], options?: RequestOptions): Promise<Subject[]>;
12
+ /**
13
+ * Get subject by id or key
14
+ * @note OpenMeter Cloud only feature
15
+ */
16
+ get(idOrKey: string, options?: RequestOptions): Promise<void>;
17
+ /**
18
+ * List subjects
19
+ * @note OpenMeter Cloud only feature
20
+ */
21
+ list(options?: RequestOptions): Promise<void>;
22
+ /**
23
+ * Delete subject by id or key
24
+ * @note OpenMeter Cloud only feature
25
+ */
26
+ delete(idOrKey: string, options?: RequestOptions): Promise<void>;
27
+ }
@@ -0,0 +1,55 @@
1
+ import { BaseClient } from './client.js';
2
+ export class SubjectClient extends BaseClient {
3
+ constructor(config) {
4
+ super(config);
5
+ }
6
+ /**
7
+ * Upsert subject
8
+ * Useful to map display name and metadata to subjects
9
+ * @note OpenMeter Cloud only feature
10
+ */
11
+ async upsert(subject, options) {
12
+ return await this.request({
13
+ path: '/api/v1/subjects',
14
+ method: 'POST',
15
+ headers: {
16
+ 'Content-Type': 'application/json',
17
+ },
18
+ body: JSON.stringify(subject),
19
+ options,
20
+ });
21
+ }
22
+ /**
23
+ * Get subject by id or key
24
+ * @note OpenMeter Cloud only feature
25
+ */
26
+ async get(idOrKey, options) {
27
+ return await this.request({
28
+ path: `/api/v1/subjects/${idOrKey}`,
29
+ method: 'GET',
30
+ options,
31
+ });
32
+ }
33
+ /**
34
+ * List subjects
35
+ * @note OpenMeter Cloud only feature
36
+ */
37
+ async list(options) {
38
+ return await this.request({
39
+ path: '/api/v1/subjects',
40
+ method: 'GET',
41
+ options,
42
+ });
43
+ }
44
+ /**
45
+ * Delete subject by id or key
46
+ * @note OpenMeter Cloud only feature
47
+ */
48
+ async delete(idOrKey, options) {
49
+ return await this.request({
50
+ path: `/api/v1/subjects/${idOrKey}`,
51
+ method: 'DELETE',
52
+ options,
53
+ });
54
+ }
55
+ }
package/dist/index.d.ts CHANGED
@@ -1,11 +1,15 @@
1
1
  import { OpenMeterConfig } from './clients/client.js';
2
2
  import { EventsClient } from './clients/event.js';
3
3
  import { MetersClient } from './clients/meter.js';
4
+ import { PortalClient } from './clients/portal.js';
5
+ import { SubjectClient } from './clients/subject.js';
4
6
  export { OpenMeterConfig, RequestOptions } from './clients/client.js';
5
- export { Event } from './clients/event.js';
6
- export { Meter, MeterValue, MeterAggregation, WindowSize, } from './clients/meter.js';
7
+ export { Event, IngestedEvent } from './clients/event.js';
8
+ export { Meter, MeterAggregation, WindowSize } from './clients/meter.js';
7
9
  export declare class OpenMeter {
8
10
  events: EventsClient;
9
11
  meters: MetersClient;
12
+ portal: PortalClient;
13
+ subjects: SubjectClient;
10
14
  constructor(config: OpenMeterConfig);
11
15
  }
package/dist/index.js CHANGED
@@ -1,11 +1,17 @@
1
1
  import { EventsClient } from './clients/event.js';
2
2
  import { MetersClient } from './clients/meter.js';
3
- export { MeterAggregation, WindowSize, } from './clients/meter.js';
3
+ import { PortalClient } from './clients/portal.js';
4
+ import { SubjectClient } from './clients/subject.js';
5
+ export { MeterAggregation, WindowSize } from './clients/meter.js';
4
6
  export class OpenMeter {
5
7
  events;
6
8
  meters;
9
+ portal;
10
+ subjects;
7
11
  constructor(config) {
8
12
  this.events = new EventsClient(config);
9
13
  this.meters = new MetersClient(config);
14
+ this.portal = new PortalClient(config);
15
+ this.subjects = new SubjectClient(config);
10
16
  }
11
17
  }