refactor: Prevent leading slashes in API urls (#34680)
This commit is contained in:
parent
8d5b73d70d
commit
c45ce549af
3 changed files with 8 additions and 7 deletions
|
@ -83,6 +83,7 @@ export default function api(withAuthorization = true) {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ApiUrl = `v${1 | 2}/${string}`;
|
||||||
type RequestParamsOrData = Record<string, unknown>;
|
type RequestParamsOrData = Record<string, unknown>;
|
||||||
|
|
||||||
export async function apiRequest<ApiResponse = unknown>(
|
export async function apiRequest<ApiResponse = unknown>(
|
||||||
|
@ -105,28 +106,28 @@ export async function apiRequest<ApiResponse = unknown>(
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function apiRequestGet<ApiResponse = unknown>(
|
export async function apiRequestGet<ApiResponse = unknown>(
|
||||||
url: string,
|
url: ApiUrl,
|
||||||
params?: RequestParamsOrData,
|
params?: RequestParamsOrData,
|
||||||
) {
|
) {
|
||||||
return apiRequest<ApiResponse>('GET', url, { params });
|
return apiRequest<ApiResponse>('GET', url, { params });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function apiRequestPost<ApiResponse = unknown>(
|
export async function apiRequestPost<ApiResponse = unknown>(
|
||||||
url: string,
|
url: ApiUrl,
|
||||||
data?: RequestParamsOrData,
|
data?: RequestParamsOrData,
|
||||||
) {
|
) {
|
||||||
return apiRequest<ApiResponse>('POST', url, { data });
|
return apiRequest<ApiResponse>('POST', url, { data });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function apiRequestPut<ApiResponse = unknown>(
|
export async function apiRequestPut<ApiResponse = unknown>(
|
||||||
url: string,
|
url: ApiUrl,
|
||||||
data?: RequestParamsOrData,
|
data?: RequestParamsOrData,
|
||||||
) {
|
) {
|
||||||
return apiRequest<ApiResponse>('PUT', url, { data });
|
return apiRequest<ApiResponse>('PUT', url, { data });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function apiRequestDelete<ApiResponse = unknown>(
|
export async function apiRequestDelete<ApiResponse = unknown>(
|
||||||
url: string,
|
url: ApiUrl,
|
||||||
params?: RequestParamsOrData,
|
params?: RequestParamsOrData,
|
||||||
) {
|
) {
|
||||||
return apiRequest<ApiResponse>('DELETE', url, { params });
|
return apiRequest<ApiResponse>('DELETE', url, { params });
|
||||||
|
|
|
@ -36,6 +36,6 @@ export const apiGetEndorsedAccounts = (id: string) =>
|
||||||
apiRequestGet<ApiAccountJSON>(`v1/accounts/${id}/endorsements`);
|
apiRequestGet<ApiAccountJSON>(`v1/accounts/${id}/endorsements`);
|
||||||
|
|
||||||
export const apiGetFamiliarFollowers = (id: string) =>
|
export const apiGetFamiliarFollowers = (id: string) =>
|
||||||
apiRequestGet<ApiFamiliarFollowersJSON>('/v1/accounts/familiar_followers', {
|
apiRequestGet<ApiFamiliarFollowersJSON>('v1/accounts/familiar_followers', {
|
||||||
id,
|
id,
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,9 +2,9 @@ import { apiRequestGet, apiRequestPost } from 'mastodon/api';
|
||||||
import type { ApiPollJSON } from 'mastodon/api_types/polls';
|
import type { ApiPollJSON } from 'mastodon/api_types/polls';
|
||||||
|
|
||||||
export const apiGetPoll = (pollId: string) =>
|
export const apiGetPoll = (pollId: string) =>
|
||||||
apiRequestGet<ApiPollJSON>(`/v1/polls/${pollId}`);
|
apiRequestGet<ApiPollJSON>(`v1/polls/${pollId}`);
|
||||||
|
|
||||||
export const apiPollVote = (pollId: string, choices: string[]) =>
|
export const apiPollVote = (pollId: string, choices: string[]) =>
|
||||||
apiRequestPost<ApiPollJSON>(`/v1/polls/${pollId}/votes`, {
|
apiRequestPost<ApiPollJSON>(`v1/polls/${pollId}/votes`, {
|
||||||
choices,
|
choices,
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue