[Glitch] Change `apiRequest` to accept both `params` and `data`

Port 547e97945d
shrike
Renaud Chaput 2024-06-25 15:45:41 +02:00 committed by Claire
parent 6f2771cb32
commit 3a20290915
4 changed files with 51 additions and 12 deletions

View File

@ -59,16 +59,49 @@ export default function api(withAuthorization = true) {
}); });
} }
type RequestParamsOrData = Record<string, unknown>;
export async function apiRequest<ApiResponse = unknown>( export async function apiRequest<ApiResponse = unknown>(
method: Method, method: Method,
url: string, url: string,
params?: Record<string, unknown>, args: {
params?: RequestParamsOrData;
data?: RequestParamsOrData;
} = {},
) { ) {
const { data } = await api().request<ApiResponse>({ const { data } = await api().request<ApiResponse>({
method, method,
url: '/api/' + url, url: '/api/' + url,
data: params, ...args,
}); });
return data; return data;
} }
export async function apiRequestGet<ApiResponse = unknown>(
url: string,
params?: RequestParamsOrData,
) {
return apiRequest<ApiResponse>('GET', url, { params });
}
export async function apiRequestPost<ApiResponse = unknown>(
url: string,
data?: RequestParamsOrData,
) {
return apiRequest<ApiResponse>('POST', url, { data });
}
export async function apiRequestPut<ApiResponse = unknown>(
url: string,
data?: RequestParamsOrData,
) {
return apiRequest<ApiResponse>('PUT', url, { data });
}
export async function apiRequestDelete<ApiResponse = unknown>(
url: string,
params?: RequestParamsOrData,
) {
return apiRequest<ApiResponse>('DELETE', url, { params });
}

View File

@ -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'; import type { ApiRelationshipJSON } from 'flavours/glitch/api_types/relationships';
export const apiSubmitAccountNote = (id: string, value: string) => export const apiSubmitAccountNote = (id: string, value: string) =>
apiRequest<ApiRelationshipJSON>('post', `v1/accounts/${id}/note`, { apiRequestPost<ApiRelationshipJSON>(`v1/accounts/${id}/note`, {
comment: value, data: {
comment: value,
},
}); });

View File

@ -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'; import type { Status, StatusVisibility } from 'flavours/glitch/models/status';
export const apiReblog = (statusId: string, visibility: StatusVisibility) => export const apiReblog = (statusId: string, visibility: StatusVisibility) =>
apiRequest<{ reblog: Status }>('post', `v1/statuses/${statusId}/reblog`, { apiRequestPost<{ reblog: Status }>(`v1/statuses/${statusId}/reblog`, {
visibility, data: {
visibility,
},
}); });
export const apiUnreblog = (statusId: string) => export const apiUnreblog = (statusId: string) =>
apiRequest<Status>('post', `v1/statuses/${statusId}/unreblog`); apiRequestPost<Status>(`v1/statuses/${statusId}/unreblog`);

View File

@ -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'; import type { NotificationPolicyJSON } from 'flavours/glitch/api_types/notification_policies';
export const apiGetNotificationPolicy = () => export const apiGetNotificationPolicy = () =>
apiRequest<NotificationPolicyJSON>('GET', '/v1/notifications/policy'); apiRequestGet<NotificationPolicyJSON>('/v1/notifications/policy');
export const apiUpdateNotificationsPolicy = ( export const apiUpdateNotificationsPolicy = (
policy: Partial<NotificationPolicyJSON>, policy: Partial<NotificationPolicyJSON>,
) => ) =>
apiRequest<NotificationPolicyJSON>('PUT', '/v1/notifications/policy', policy); apiRequestPut<NotificationPolicyJSON>('/v1/notifications/policy', {
data: policy,
});