From 3a202909157af2f07fee13113a6c55ba3d5a6ff2 Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Tue, 25 Jun 2024 15:45:41 +0200 Subject: [PATCH] [Glitch] Change `apiRequest` to accept both `params` and `data` Port 547e97945df0abc68dc473ed60d2faeb2feb2b06 --- app/javascript/flavours/glitch/api.ts | 37 ++++++++++++++++++- .../flavours/glitch/api/accounts.ts | 8 ++-- .../flavours/glitch/api/interactions.ts | 10 +++-- .../glitch/api/notification_policies.ts | 8 ++-- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/app/javascript/flavours/glitch/api.ts b/app/javascript/flavours/glitch/api.ts index e133125a29..24672290c7 100644 --- a/app/javascript/flavours/glitch/api.ts +++ b/app/javascript/flavours/glitch/api.ts @@ -59,16 +59,49 @@ export default function api(withAuthorization = true) { }); } +type RequestParamsOrData = Record; + export async function apiRequest( method: Method, url: string, - params?: Record, + args: { + params?: RequestParamsOrData; + data?: RequestParamsOrData; + } = {}, ) { const { data } = await api().request({ method, url: '/api/' + url, - data: params, + ...args, }); return data; } + +export async function apiRequestGet( + url: string, + params?: RequestParamsOrData, +) { + return apiRequest('GET', url, { params }); +} + +export async function apiRequestPost( + url: string, + data?: RequestParamsOrData, +) { + return apiRequest('POST', url, { data }); +} + +export async function apiRequestPut( + url: string, + data?: RequestParamsOrData, +) { + return apiRequest('PUT', url, { data }); +} + +export async function apiRequestDelete( + url: string, + params?: RequestParamsOrData, +) { + return apiRequest('DELETE', url, { params }); +} diff --git a/app/javascript/flavours/glitch/api/accounts.ts b/app/javascript/flavours/glitch/api/accounts.ts index 346b3bc38c..f2c9c43319 100644 --- a/app/javascript/flavours/glitch/api/accounts.ts +++ b/app/javascript/flavours/glitch/api/accounts.ts @@ -1,7 +1,9 @@ -import { apiRequest } from 'flavours/glitch/api'; +import { apiRequestPost } from 'flavours/glitch/api'; import type { ApiRelationshipJSON } from 'flavours/glitch/api_types/relationships'; export const apiSubmitAccountNote = (id: string, value: string) => - apiRequest('post', `v1/accounts/${id}/note`, { - comment: value, + apiRequestPost(`v1/accounts/${id}/note`, { + data: { + comment: value, + }, }); diff --git a/app/javascript/flavours/glitch/api/interactions.ts b/app/javascript/flavours/glitch/api/interactions.ts index eaa83b2136..1fb6fd50dd 100644 --- a/app/javascript/flavours/glitch/api/interactions.ts +++ b/app/javascript/flavours/glitch/api/interactions.ts @@ -1,10 +1,12 @@ -import { apiRequest } from 'flavours/glitch/api'; +import { apiRequestPost } from 'flavours/glitch/api'; import type { Status, StatusVisibility } from 'flavours/glitch/models/status'; export const apiReblog = (statusId: string, visibility: StatusVisibility) => - apiRequest<{ reblog: Status }>('post', `v1/statuses/${statusId}/reblog`, { - visibility, + apiRequestPost<{ reblog: Status }>(`v1/statuses/${statusId}/reblog`, { + data: { + visibility, + }, }); export const apiUnreblog = (statusId: string) => - apiRequest('post', `v1/statuses/${statusId}/unreblog`); + apiRequestPost(`v1/statuses/${statusId}/unreblog`); diff --git a/app/javascript/flavours/glitch/api/notification_policies.ts b/app/javascript/flavours/glitch/api/notification_policies.ts index 2bb5dc37ca..8ff1967b9f 100644 --- a/app/javascript/flavours/glitch/api/notification_policies.ts +++ b/app/javascript/flavours/glitch/api/notification_policies.ts @@ -1,10 +1,12 @@ -import { apiRequest } from 'flavours/glitch/api'; +import { apiRequestGet, apiRequestPut } from 'flavours/glitch/api'; import type { NotificationPolicyJSON } from 'flavours/glitch/api_types/notification_policies'; export const apiGetNotificationPolicy = () => - apiRequest('GET', '/v1/notifications/policy'); + apiRequestGet('/v1/notifications/policy'); export const apiUpdateNotificationsPolicy = ( policy: Partial, ) => - apiRequest('PUT', '/v1/notifications/policy', policy); + apiRequestPut('/v1/notifications/policy', { + data: policy, + });