2023-09-29 11:55:02 +02:00

55 lines
1.3 KiB
TypeScript

import BaseNotary from "../BaseNotary";
import { UserNotification } from "le-coffre-resources/dist/Notary";
// TODO Type get query params -> Where + inclue + orderby
export interface IGetNotificationsParams {
where?: {};
include?: {};
select?: {};
}
export type IPutNotificationsParams = {
read?: boolean;
};
export default class Notifications extends BaseNotary {
private static instance: Notifications;
private baseUrl = this.namespaceUrl.concat("/notifications");
private constructor() {
super();
}
public static getInstance() {
if (!this.instance) {
return new this();
} else {
return this.instance;
}
}
public async get(q?: IGetNotificationsParams): Promise<UserNotification[]> {
const url = new URL(this.baseUrl);
if (q) {
const query = { q };
Object.entries(query).forEach(([key, value]) => url.searchParams.set(key, JSON.stringify(value)));
}
try {
return await this.getRequest<UserNotification[]>(url);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async put(uid: string, body: IPutNotificationsParams): Promise<UserNotification> {
const url = new URL(this.baseUrl.concat(`/${uid}`));
try {
return await this.putRequest<UserNotification>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
}