Skip to content

Commit

Permalink
feat: add notification (un-)registration endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Aug 17, 2023
1 parent 32042e3 commit 5b40b21
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fetchData, insertParams, stringifyQuery } from './utils'
import type { GetEndpoint, paths, PostEndpoint, Primitive } from './types/api'
import type { DeleteEndpoint, GetEndpoint, paths, PostEndpoint, Primitive } from './types/api'

function makeUrl(
baseUrl: string,
Expand Down Expand Up @@ -33,3 +33,12 @@ export function getEndpoint<T extends keyof paths>(
const url = makeUrl(baseUrl, path as string, params?.path, params?.query)
return fetchData(url)
}

export function deleteEndpoint<T extends keyof paths>(
baseUrl: string,
path: T,
params?: paths[T] extends DeleteEndpoint ? paths[T]['delete']['parameters'] : never,
): Promise<paths[T] extends DeleteEndpoint ? paths[T]['delete']['responses'][200]['schema'] : never> {
const url = makeUrl(baseUrl, path as string, params?.path)
return fetchData(url)
}
29 changes: 28 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEndpoint, postEndpoint } from './endpoint'
import { deleteEndpoint, getEndpoint, postEndpoint } from './endpoint'
import type { operations } from './types/api'
import type {
SafeTransactionEstimation,
Expand Down Expand Up @@ -355,4 +355,31 @@ export function getDelegates(chainId: string, query: DelegatesRequest = {}): Pro
})
}

/**
* Registers a device/Safe for notifications
*/
export function registerNotifications(body: operations['register_notifications']['parameters']['body']): Promise<void> {
return postEndpoint(baseUrl, '/v1/register/notifications', {
body,
})
}

/**
* Unregisters a Safe from notifications
*/
export function unregisterSafeNotifications(chainId: string, address: string, uuid: string): Promise<void> {
return deleteEndpoint(baseUrl, '/v1/chains/{chainId}/notifications/devices/{uuid}/safes/{safe_address}', {
path: { chainId, safe_address: address, uuid },
})
}

/**
* Unregisters a device from notifications
*/
export function unregisterDeviceNotifications(chainId: string, uuid: string): Promise<void> {
return deleteEndpoint(baseUrl, '/v1/chains/{chainId}/notifications/devices/{uuid}', {
path: { chainId, uuid },
})
}

/* eslint-enable @typescript-eslint/explicit-module-boundary-types */
75 changes: 73 additions & 2 deletions src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ import type {
SafeMessageListPage,
} from './safe-messages'
import type { DelegateResponse, DelegatesRequest } from './delegates'
import type { RegisterNotificationsRequest } from './notifications'

export type Primitive = string | number | boolean | null

interface GetParams {
interface DeleteParams {
path?: { [key: string]: Primitive }
}

interface GetParams extends DeleteParams {
query?: { [key: string]: Primitive }
}

Expand Down Expand Up @@ -64,8 +68,15 @@ export interface PostEndpoint extends Endpoint {
}
}

export interface DeleteEndpoint extends Endpoint {
delete: {
parameters: DeleteParams | null
responses: Responses
}
}

interface PathRegistry {
[key: string]: GetEndpoint | PostEndpoint | (GetEndpoint & PostEndpoint)
[key: string]: DeleteEndpoint | GetEndpoint | PostEndpoint | (GetEndpoint & PostEndpoint)
}

export interface paths extends PathRegistry {
Expand Down Expand Up @@ -270,6 +281,29 @@ export interface paths extends PathRegistry {
query: DelegatesRequest
}
}
'/v1/register/notifications': {
post: operations['register_notifications']
parameters: null
}
'/v1/chains/{chainId}/notifications/devices/{uuid}/safes/{safe_address}': {
delete: operations['unregister_safe_notifications']
parameters: {
path: {
uuid: string
chainId: string
safe_address: string
}
}
}
'/v1/chains/{chainId}/notifications/devices/{uuid}': {
delete: operations['unregister_device_notifications']
parameters: {
path: {
uuid: string
chainId: string
}
}
}
}

export interface operations {
Expand Down Expand Up @@ -689,4 +723,41 @@ export interface operations {
}
}
}
register_notifications: {
parameters: {
body: RegisterNotificationsRequest
}
responses: {
200: {
schema: void
}
}
}
unregister_safe_notifications: {
parameters: {
path: {
uuid: string
chainId: string
safe_address: string
}
}
responses: {
200: {
schema: void
}
}
}
unregister_device_notifications: {
parameters: {
path: {
uuid: string
chainId: string
}
}
responses: {
200: {
schema: void
}
}
}
}
22 changes: 22 additions & 0 deletions src/types/notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export enum DeviceType {
ANDROID = 'ANDROID',
IOS = 'IOS',
WEB = 'WEB',
}

type SafeRegistration = {
chainId: string
safes: Array<string>
signatures: Array<string>
}

export type RegisterNotificationsRequest = {
uuid: string
cloudMessagingToken: string
buildNumber: string
bundle: string
deviceType: DeviceType
version: string
timestamp: string
safeRegistrations: Array<SafeRegistration>
}

0 comments on commit 5b40b21

Please sign in to comment.