Skip to content

Commit

Permalink
Merge pull request #1422 from openmeterio/feat/node-sdk-notifications
Browse files Browse the repository at this point in the history
Feat/node sdk notifications
  • Loading branch information
tothandras authored Aug 23, 2024
2 parents d589e56 + f3314ff commit 7f5fb01
Show file tree
Hide file tree
Showing 4 changed files with 550 additions and 298 deletions.
242 changes: 242 additions & 0 deletions api/client/node/clients/notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
import { components, operations } from '../schemas/openapi.js'
import { RequestOptions, BaseClient, OpenMeterConfig } from './client.js'

export type NotificationChannel = components['schemas']['NotificationChannel']
export type NotificationChannelCreateRequest =
components['schemas']['NotificationChannelCreateRequest']
export type ListNotificationChannelsQueryParams =
operations['listNotificationChannels']['parameters']['query']
export type NotificationChannelsResponse =
components['schemas']['NotificationChannelsResponse']

export type NotificationRule = components['schemas']['NotificationRule']
export type NotificationRuleCreateRequest =
components['schemas']['NotificationRuleCreateRequest']
export type ListNotificationRulesQueryParams =
operations['listNotificationRules']['parameters']['query']
export type NotificationRulesResponse =
components['schemas']['NotificationRulesResponse']

export type NotificationEvent = components['schemas']['NotificationEvent']
export type ListNotificationEventsQueryParams =
operations['listNotificationEvents']['parameters']['query']
export type NotificationEventsResponse =
components['schemas']['NotificationEventsResponse']

export class NotificationClient extends BaseClient {
public channels: NotificationChannelsClient
public rules: NotificationRulesClient
public events: NotificationEventsClient

constructor(config: OpenMeterConfig) {
super(config)

this.channels = new NotificationChannelsClient(config)
this.rules = new NotificationRulesClient(config)
this.events = new NotificationEventsClient(config)
}
}

class NotificationChannelsClient extends BaseClient {
constructor(config: OpenMeterConfig) {
super(config)
}

/**
* List notification channels.
* @example
* const channels = await openmeter.notification.channels.list()
*/
public async list(
params?: ListNotificationChannelsQueryParams,
options?: RequestOptions
): Promise<NotificationChannelsResponse> {
const searchParams = params
? BaseClient.toURLSearchParams(params)
: undefined
return await this.request({
path: '/api/v1/notification/channels',
method: 'GET',
searchParams,
options,
})
}

/**
* Get notification channel.
* @example
* const channel = await openmeter.notification.channels.get('01J5Z602369ZDS9J60N3DV7SGE')
*/
public async get(
id: string,
options?: RequestOptions
): Promise<NotificationChannel> {
return await this.request({
path: `/api/v1/notification/channels/${id}`,
method: 'GET',
options,
})
}

/**
* Delete notification channel.
* @example
* await openmeter.notification.channels.delete('01J5Z602369ZDS9J60N3DV7SGE')
*/
public async delete(id: string, options?: RequestOptions): Promise<void> {
await this.request({
path: `/api/v1/notification/channels/${id}`,
method: 'DELETE',
options,
})
}

/**
* Create notification channel.
* @example
* const channel = await openmeter.notification.channels.create({
* type: 'WEBHOOK',
* name: 'My webhook channel',
* url: 'https://example.com/webhook',
* customHeaders: {
* 'User-Agent': 'OpenMeter'
* },
* })
*/
public async create(
body: NotificationChannelCreateRequest,
options?: RequestOptions
): Promise<NotificationChannel> {
return await this.request({
path: '/api/v1/notification/channels',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
options,
})
}
}

class NotificationRulesClient extends BaseClient {
constructor(config: OpenMeterConfig) {
super(config)
}

/**
* List notification rules.
* @example
* const rules = await openmeter.notification.rules.list()
*/
public async list(
params?: ListNotificationRulesQueryParams,
options?: RequestOptions
): Promise<NotificationRulesResponse> {
const searchParams = params
? BaseClient.toURLSearchParams(params)
: undefined
return await this.request({
path: '/api/v1/notification/rules',
method: 'GET',
searchParams,
options,
})
}

/**
* Get notification rule.
* @example
* const rule = await openmeter.notification.rules.get('01J5Z602369ZDS9J60N3DV7SGE')
*/
public async get(
id: string,
options?: RequestOptions
): Promise<NotificationChannel> {
return await this.request({
path: `/api/v1/notification/rules/${id}`,
method: 'GET',
options,
})
}

/**
* Delete notification rule.
* @example
* await openmeter.notification.rules.delete('01J5Z602369ZDS9J60N3DV7SGE')
*/
public async delete(id: string, options?: RequestOptions): Promise<void> {
await this.request({
path: `/api/v1/notification/rules/${id}`,
method: 'DELETE',
options,
})
}

/**
* Create notification rule.
* @example
* const rule = await openmeter.notification.rules.create({
* type: 'entitlements.balance.threshold',
* name: 'My rule',
* channels: ['01J5Z602369ZDS9J60N3DV7SGE'],
* thresholds: [{ value: 90, type: 'PERCENT' }],
* })
*/
public async create(
body: NotificationRuleCreateRequest,
options?: RequestOptions
): Promise<NotificationChannel> {
return await this.request({
path: '/api/v1/notification/rules',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
options,
})
}
}

class NotificationEventsClient extends BaseClient {
constructor(config: OpenMeterConfig) {
super(config)
}

/**
* List notification events.
* @example
* const events = await openmeter.notification.events.list()
*/
public async list(
params?: ListNotificationEventsQueryParams,
options?: RequestOptions
): Promise<NotificationEventsResponse> {
const searchParams = params
? BaseClient.toURLSearchParams(params)
: undefined
return await this.request({
path: '/api/v1/notification/events',
method: 'GET',
searchParams,
options,
})
}

/**
* Get notification event.
* @example
* const event = await openmeter.notification.events.get('01J5Z602369ZDS9J60N3DV7SGE')
*/
public async get(
id: string,
options?: RequestOptions
): Promise<NotificationEvent> {
return await this.request({
path: `/api/v1/notification/events/${id}`,
method: 'GET',
options,
})
}
}
4 changes: 4 additions & 0 deletions api/client/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { EventsClient } from './clients/event.js'
import { FeatureClient } from './clients/feature.js'
import { GrantClient } from './clients/grant.js'
import { MetersClient } from './clients/meter.js'
import { NotificationClient } from './clients/notifications.js'
import { PortalClient } from './clients/portal.js'
import { SubjectClient } from './clients/subject.js'

export { OpenMeterConfig, RequestOptions } from './clients/client.js'
export { Event, IngestedEvent, CloudEvent } from './clients/event.js'
export { Meter, MeterAggregation, WindowSize } from './clients/meter.js'
export * from './clients/notifications.js'

export class OpenMeter {
public events: EventsClient
Expand All @@ -19,6 +21,7 @@ export class OpenMeter {
public features: FeatureClient
public entitlements: EntitlementClient
public grants: GrantClient
public notification: NotificationClient

constructor(config: OpenMeterConfig) {
this.events = new EventsClient(config)
Expand All @@ -28,5 +31,6 @@ export class OpenMeter {
this.features = new FeatureClient(config)
this.entitlements = new EntitlementClient(config)
this.grants = new GrantClient(config)
this.notification = new NotificationClient(config)
}
}
6 changes: 3 additions & 3 deletions api/client/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@
"prettier": "3.3.3",
"rimraf": "6.0.1",
"tslib": "2.6.3",
"typescript": "5.5.3",
"vitest": "2.0.3"
"typescript": "5.5.4",
"vitest": "2.0.5"
},
"browserslist": [
"current node"
],
"dependencies": {
"undici": "^6.19.2"
"undici": "^6.19.8"
}
}
Loading

0 comments on commit 7f5fb01

Please sign in to comment.