notifications: refactor backend routes to avoid using root route

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-12-10 00:43:57 +01:00
parent a61a67cd37
commit fc15b77b46
9 changed files with 136 additions and 116 deletions
@@ -48,7 +48,7 @@ describe('NotificationsClient', () => {
it('should fetch notifications from correct endpoint', async () => {
server.use(
rest.get(`${mockBaseUrl}/`, (_, res, ctx) =>
rest.get(`${mockBaseUrl}/notifications`, (_, res, ctx) =>
res(ctx.json(expectedResp)),
),
);
@@ -58,7 +58,7 @@ describe('NotificationsClient', () => {
it('should fetch notifications with options', async () => {
server.use(
rest.get(`${mockBaseUrl}/`, (req, res, ctx) => {
rest.get(`${mockBaseUrl}/notifications`, (req, res, ctx) => {
expect(req.url.search).toBe(
'?limit=10&offset=0&search=find+me&read=true&createdAfter=1970-01-01T00%3A00%3A00.005Z',
);
@@ -77,7 +77,7 @@ describe('NotificationsClient', () => {
it('should omit unselected fetch options', async () => {
server.use(
rest.get(`${mockBaseUrl}/`, (req, res, ctx) => {
rest.get(`${mockBaseUrl}/notifications`, (req, res, ctx) => {
expect(req.url.search).toBe('?limit=10');
return res(ctx.json(expectedResp));
}),
@@ -91,7 +91,7 @@ describe('NotificationsClient', () => {
it('should fetch single notification', async () => {
server.use(
rest.get(`${mockBaseUrl}/:id`, (req, res, ctx) => {
rest.get(`${mockBaseUrl}/notifications/:id`, (req, res, ctx) => {
expect(req.params.id).toBe('acdaa8ca-262b-43c1-b74b-de06e5f3b3c7');
return res(ctx.json(testNotification));
}),
@@ -115,12 +115,15 @@ describe('NotificationsClient', () => {
it('should update notifications', async () => {
server.use(
rest.post(`${mockBaseUrl}/update`, async (req, res, ctx) => {
expect(await req.json()).toEqual({
ids: ['acdaa8ca-262b-43c1-b74b-de06e5f3b3c7'],
});
return res(ctx.json(expectedResp));
}),
rest.post(
`${mockBaseUrl}/notifications/update`,
async (req, res, ctx) => {
expect(await req.json()).toEqual({
ids: ['acdaa8ca-262b-43c1-b74b-de06e5f3b3c7'],
});
return res(ctx.json(expectedResp));
},
),
);
const response = await client.updateNotifications({
ids: ['acdaa8ca-262b-43c1-b74b-de06e5f3b3c7'],
@@ -71,23 +71,26 @@ export class NotificationsClient implements NotificationsApi {
if (options?.minimumSeverity !== undefined) {
queryString.append('minimumSeverity', options.minimumSeverity);
}
const urlSegment = `?${queryString}`;
return await this.request<GetNotificationsResponse>(urlSegment);
return await this.request<GetNotificationsResponse>(
`/notifications?${queryString}`,
);
}
async getNotification(id: string): Promise<Notification> {
return await this.request<Notification>(`${id}`);
return await this.request<Notification>(
`/notifications/${encodeURIComponent(id)}`,
);
}
async getStatus(): Promise<NotificationStatus> {
return await this.request<NotificationStatus>('status');
return await this.request<NotificationStatus>('/status');
}
async updateNotifications(
options: UpdateNotificationsOptions,
): Promise<Notification[]> {
return await this.request<Notification[]>('update', {
return await this.request<Notification[]>('/notifications/update', {
method: 'POST',
body: JSON.stringify(options),
headers: { 'Content-Type': 'application/json' },
@@ -95,29 +98,27 @@ export class NotificationsClient implements NotificationsApi {
}
async getNotificationSettings(): Promise<NotificationSettings> {
return await this.request<NotificationSettings>('settings');
return await this.request<NotificationSettings>('/settings');
}
async updateNotificationSettings(
settings: NotificationSettings,
): Promise<NotificationSettings> {
return await this.request<NotificationSettings>('settings', {
return await this.request<NotificationSettings>('/settings', {
method: 'POST',
body: JSON.stringify(settings),
headers: { 'Content-Type': 'application/json' },
});
}
private async request<T>(path: string, init?: any): Promise<T> {
const baseUrl = `${await this.discoveryApi.getBaseUrl('notifications')}/`;
const url = new URL(path, baseUrl);
private async request<T>(path: string, init?: RequestInit): Promise<T> {
const baseUrl = await this.discoveryApi.getBaseUrl('notifications');
const res = await this.fetchApi.fetch(`${baseUrl}${path}`, init);
const response = await this.fetchApi.fetch(url.toString(), init);
if (!response.ok) {
throw await ResponseError.fromResponse(response);
if (!res.ok) {
throw await ResponseError.fromResponse(res);
}
return response.json() as Promise<T>;
return res.json() as Promise<T>;
}
}