parent
46f4a1b3c5
commit
153d852f76
@ -109,35 +109,38 @@ export function get<T = any>(url: string, params = empty): Promise<T> {
|
||||
return walkFetch(new Request(`${blessing.base_url}${url}?${qs}`, init))
|
||||
}
|
||||
|
||||
function nonGet<T = any>(method: string, url: string, data: any): Promise<T> {
|
||||
function nonGet<T = any>(
|
||||
method: string,
|
||||
url: string,
|
||||
data?: FormData | object,
|
||||
): Promise<T> {
|
||||
emit('beforeFetch', {
|
||||
method: method.toUpperCase(),
|
||||
url,
|
||||
data,
|
||||
})
|
||||
|
||||
const isFormData = data instanceof FormData
|
||||
|
||||
const request = new Request(`${blessing.base_url}${url}`, {
|
||||
body: isFormData ? data : JSON.stringify(data),
|
||||
body: data instanceof FormData ? data : JSON.stringify(data),
|
||||
method: method.toUpperCase(),
|
||||
...init,
|
||||
})
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
!isFormData && request.headers.set('Content-Type', 'application/json')
|
||||
if (!(data instanceof FormData)) {
|
||||
request.headers.set('Content-Type', 'application/json')
|
||||
}
|
||||
|
||||
return walkFetch(request)
|
||||
}
|
||||
|
||||
export function post<T = any>(url: string, data = empty): Promise<T> {
|
||||
export function post<T = any>(url: string, data?: object): Promise<T> {
|
||||
return nonGet<T>('POST', url, data)
|
||||
}
|
||||
|
||||
export function put<T = any>(url: string, data = empty): Promise<T> {
|
||||
export function put<T = any>(url: string, data?: object): Promise<T> {
|
||||
return nonGet<T>('PUT', url, data)
|
||||
}
|
||||
|
||||
export function del<T = any>(url: string, data = empty): Promise<T> {
|
||||
export function del<T = any>(url: string, data?: object): Promise<T> {
|
||||
return nonGet<T>('DELETE', url, data)
|
||||
}
|
||||
|
||||
|
@ -91,9 +91,16 @@ const Players: React.FC = () => {
|
||||
return
|
||||
}
|
||||
|
||||
// workaround for AliCDN, issue#184
|
||||
const search = new URLSearchParams()
|
||||
if (skin) {
|
||||
search.append('skin', 'true')
|
||||
}
|
||||
if (cape) {
|
||||
search.append('cape', 'true')
|
||||
}
|
||||
const { code, message } = await fetch.del<fetch.ResponseBody>(
|
||||
urls.user.player.clear(selected),
|
||||
{ type: [skin && 'skin', cape && 'cape'].filter(Boolean) },
|
||||
`${urls.user.player.clear(selected)}?${search}`,
|
||||
)
|
||||
if (code === 0) {
|
||||
toast.success(message)
|
||||
|
@ -69,7 +69,7 @@ test('the POST method', async () => {
|
||||
expect(request.headers.get('Content-Type')).toBe('application/json')
|
||||
|
||||
await net.post('/abc')
|
||||
expect(window.fetch.mock.calls[2][0].body).toBe('{}')
|
||||
expect(window.fetch.mock.calls[2][0].body).toBeUndefined()
|
||||
})
|
||||
|
||||
test('the PUT method', () => {
|
||||
@ -88,7 +88,7 @@ test('the PUT method', () => {
|
||||
expect(stub).toBeCalledWith({
|
||||
method: 'PUT',
|
||||
url: '/abc',
|
||||
data: {},
|
||||
data: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
@ -108,7 +108,7 @@ test('the DELETE method', () => {
|
||||
expect(stub).toBeCalledWith({
|
||||
method: 'DELETE',
|
||||
url: '/abc',
|
||||
data: {},
|
||||
data: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -363,9 +363,9 @@ describe('reset texture', () => {
|
||||
fireEvent.click(getByLabelText(t('general.cape')))
|
||||
fireEvent.click(getByText(t('general.confirm')))
|
||||
await waitFor(() =>
|
||||
expect(fetch.del).toBeCalledWith(urls.user.player.clear(fixture.pid), {
|
||||
type: ['skin', 'cape'],
|
||||
}),
|
||||
expect(fetch.del).toBeCalledWith(
|
||||
`${urls.user.player.clear(fixture.pid)}?skin=true&cape=true`,
|
||||
),
|
||||
)
|
||||
expect(queryByText('success')).toBeInTheDocument()
|
||||
expect(getByRole('status')).toHaveClass('alert-success')
|
||||
@ -383,9 +383,9 @@ describe('reset texture', () => {
|
||||
fireEvent.click(getByLabelText(t('general.skin')))
|
||||
fireEvent.click(getByText(t('general.confirm')))
|
||||
await waitFor(() =>
|
||||
expect(fetch.del).toBeCalledWith(urls.user.player.clear(fixture.pid), {
|
||||
type: ['skin'],
|
||||
}),
|
||||
expect(fetch.del).toBeCalledWith(
|
||||
`${urls.user.player.clear(fixture.pid)}?skin=true`,
|
||||
),
|
||||
)
|
||||
expect(queryByText('success')).toBeInTheDocument()
|
||||
expect(getByRole('status')).toHaveClass('alert-success')
|
||||
@ -403,9 +403,9 @@ describe('reset texture', () => {
|
||||
fireEvent.click(getByLabelText(t('general.cape')))
|
||||
fireEvent.click(getByText(t('general.confirm')))
|
||||
await waitFor(() =>
|
||||
expect(fetch.del).toBeCalledWith(urls.user.player.clear(fixture.pid), {
|
||||
type: ['cape'],
|
||||
}),
|
||||
expect(fetch.del).toBeCalledWith(
|
||||
`${urls.user.player.clear(fixture.pid)}?cape=true`,
|
||||
),
|
||||
)
|
||||
expect(queryByText('success')).toBeInTheDocument()
|
||||
expect(getByRole('status')).toHaveClass('alert-success')
|
||||
@ -434,9 +434,9 @@ describe('reset texture', () => {
|
||||
fireEvent.click(getByLabelText(t('general.skin')))
|
||||
fireEvent.click(getByText(t('general.confirm')))
|
||||
await waitFor(() =>
|
||||
expect(fetch.del).toBeCalledWith(urls.user.player.clear(fixture.pid), {
|
||||
type: ['skin'],
|
||||
}),
|
||||
expect(fetch.del).toBeCalledWith(
|
||||
`${urls.user.player.clear(fixture.pid)}?skin=true`,
|
||||
),
|
||||
)
|
||||
expect(queryByText('failed')).toBeInTheDocument()
|
||||
expect(getByRole('alert')).toHaveClass('alert-danger')
|
||||
|
Loading…
Reference in New Issue
Block a user