mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-02-05 14:20:08 +08:00
Move emitting to each fetcher
This commit is contained in:
parent
f0d4e5bfdb
commit
4c40900910
@ -24,8 +24,6 @@ function retrieveToken() {
|
||||
export async function walkFetch(request) {
|
||||
request.headers.set('X-CSRF-TOKEN', retrieveToken());
|
||||
|
||||
emit('beforeFetch', request);
|
||||
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
if (response.ok) {
|
||||
@ -39,12 +37,24 @@ export async function walkFetch(request) {
|
||||
}
|
||||
|
||||
export async function get(url, params = empty) {
|
||||
emit('beforeFetch', {
|
||||
method: 'GET',
|
||||
url,
|
||||
data: params
|
||||
});
|
||||
|
||||
const qs = queryStringify(params);
|
||||
|
||||
return walkFetch(new Request(`${blessing.base_url}${url}${qs && '?' + qs}`, init));
|
||||
}
|
||||
|
||||
export async function post(url, data = empty) {
|
||||
emit('beforeFetch', {
|
||||
method: 'POST',
|
||||
url,
|
||||
data
|
||||
});
|
||||
|
||||
return walkFetch(new Request(`${blessing.base_url}${url}`, {
|
||||
body: JSON.stringify(data),
|
||||
method: 'POST',
|
||||
|
@ -17,7 +17,15 @@ test('the GET method', async () => {
|
||||
json
|
||||
});
|
||||
|
||||
const stub = jest.fn();
|
||||
on('beforeFetch', stub);
|
||||
|
||||
await net.get('/abc', { a: 'b' });
|
||||
expect(stub).toBeCalledWith({
|
||||
method: 'GET',
|
||||
url: '/abc',
|
||||
data: { a: 'b' }
|
||||
});
|
||||
expect(window.fetch.mock.calls[0][0].url).toBe('/abc?a=b');
|
||||
expect(json).toBeCalled();
|
||||
|
||||
@ -36,7 +44,15 @@ test('the POST method', async () => {
|
||||
meta.content = 'token';
|
||||
document.head.appendChild(meta);
|
||||
|
||||
const stub = jest.fn();
|
||||
on('beforeFetch', stub);
|
||||
|
||||
await net.post('/abc', { a: 'b' });
|
||||
expect(stub).toBeCalledWith({
|
||||
method: 'POST',
|
||||
url: '/abc',
|
||||
data: { a: 'b' }
|
||||
});
|
||||
const request = window.fetch.mock.calls[0][0];
|
||||
expect(request.url).toBe('/abc');
|
||||
expect(request.method).toBe('POST');
|
||||
@ -60,13 +76,10 @@ test('low level fetch', async () => {
|
||||
json
|
||||
});
|
||||
|
||||
const stub = jest.fn();
|
||||
on('beforeFetch', stub);
|
||||
const request = { headers: new Map() };
|
||||
|
||||
await net.walkFetch(request);
|
||||
expect(showAjaxError.mock.calls[0][0]).toBeInstanceOf(Error);
|
||||
expect(stub).toBeCalledWith(request);
|
||||
|
||||
await net.walkFetch(request);
|
||||
expect(showAjaxError).toBeCalledWith('404');
|
||||
|
Loading…
Reference in New Issue
Block a user