diff --git a/src/endpoint.ts b/src/endpoint.ts index 558dfbeb..c1964eff 100644 --- a/src/endpoint.ts +++ b/src/endpoint.ts @@ -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, @@ -33,3 +33,12 @@ export function getEndpoint( const url = makeUrl(baseUrl, path as string, params?.path, params?.query) return fetchData(url) } + +export function deleteEndpoint( + baseUrl: string, + path: T, + params?: paths[T] extends DeleteEndpoint ? paths[T]['delete']['parameters'] : never, +): Promise { + const url = makeUrl(baseUrl, path as string, params?.path) + return fetchData(url) +} diff --git a/src/index.ts b/src/index.ts index da5d78f0..b3714e4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { getEndpoint, postEndpoint } from './endpoint' +import { deleteEndpoint, getEndpoint, postEndpoint } from './endpoint' import type { operations } from './types/api' import type { SafeTransactionEstimation, @@ -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 { + return postEndpoint(baseUrl, '/v1/register/notifications', { + body, + }) +} + +/** + * Unregisters a Safe from notifications + */ +export function unregisterSafeNotifications(chainId: string, address: string, uuid: string): Promise { + 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 { + return deleteEndpoint(baseUrl, '/v1/chains/{chainId}/notifications/devices/{uuid}', { + path: { chainId, uuid }, + }) +} + /* eslint-enable @typescript-eslint/explicit-module-boundary-types */ diff --git a/src/types/api.ts b/src/types/api.ts index e9e328e7..d8ee5a20 100644 --- a/src/types/api.ts +++ b/src/types/api.ts @@ -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 } } @@ -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 { @@ -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 { @@ -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 + } + } + } } diff --git a/src/types/notifications.ts b/src/types/notifications.ts new file mode 100644 index 00000000..908bca0a --- /dev/null +++ b/src/types/notifications.ts @@ -0,0 +1,22 @@ +export enum DeviceType { + ANDROID = 'ANDROID', + IOS = 'IOS', + WEB = 'WEB', +} + +type SafeRegistration = { + chainId: string + safes: Array + signatures: Array +} + +export type RegisterNotificationsRequest = { + uuid: string + cloudMessagingToken: string + buildNumber: string + bundle: string + deviceType: DeviceType + version: string + timestamp: string + safeRegistrations: Array +}