Add more methods on blessing.fetch

This commit is contained in:
Pig Fang 2019-09-08 15:20:49 +08:00
parent b7d845ee51
commit a72d46d2f2
5 changed files with 77 additions and 6 deletions

View File

@ -77,9 +77,9 @@ export function get(url: string, params = empty): Promise<any> {
return walkFetch(new Request(`${blessing.base_url}${url}${qs && `?${qs}`}`, init)) return walkFetch(new Request(`${blessing.base_url}${url}${qs && `?${qs}`}`, init))
} }
export function post(url: string, data = empty): Promise<any> { function nonGet(method: string, url: string, data: any): Promise<any> {
emit('beforeFetch', { emit('beforeFetch', {
method: 'POST', method: method.toUpperCase(),
url, url,
data, data,
}) })
@ -88,7 +88,7 @@ export function post(url: string, data = empty): Promise<any> {
const request = new Request(`${blessing.base_url}${url}`, { const request = new Request(`${blessing.base_url}${url}`, {
body: isFormData ? data : JSON.stringify(data), body: isFormData ? data : JSON.stringify(data),
method: 'POST', method: method.toUpperCase(),
...init, ...init,
}) })
!isFormData && request.headers.set('Content-Type', 'application/json') !isFormData && request.headers.set('Content-Type', 'application/json')
@ -96,10 +96,26 @@ export function post(url: string, data = empty): Promise<any> {
return walkFetch(request) return walkFetch(request)
} }
export function post(url: string, data = empty): Promise<any> {
return nonGet('POST', url, data)
}
export function put(url: string, data = empty): Promise<any> {
return nonGet('PUT', url, data)
}
export function del(url: string, data = empty): Promise<any> {
return nonGet('DELETE', url, data)
}
Vue.use(_Vue => { Vue.use(_Vue => {
Object.defineProperty(_Vue.prototype, '$http', { Object.defineProperty(_Vue.prototype, '$http', {
get: () => ({ get, post }), get: () => ({
get, post, put, del,
}),
}) })
}) })
blessing.fetch = { get, post } blessing.fetch = {
get, post, put, del,
}

View File

@ -22,6 +22,8 @@ declare global {
fetch: { fetch: {
get(url: string, params?: object): Promise<object> get(url: string, params?: object): Promise<object>
post(url: string, data?: object): Promise<object> post(url: string, data?: object): Promise<object>
put(url: string, data?: object): Promise<object>
del(url: string, data?: object): Promise<object>
} }
event: { event: {
@ -46,8 +48,9 @@ declare module 'vue/types/vue' {
$http: { $http: {
get(url: string, params?: object) get(url: string, params?: object)
post(url: string, data?: object): { code?: number, message?: string } post(url: string, data?: object): { code?: number, message?: string }
put(url: string, data?: object): { code?: number, message?: string }
del(url: string, data?: object): { code?: number, message?: string }
} }
$route: RegExpExecArray | null $route: RegExpExecArray | null

View File

@ -73,6 +73,46 @@ test('the POST method', async () => {
expect(window.fetch.mock.calls[2][0].body).toBe('{}') expect(window.fetch.mock.calls[2][0].body).toBe('{}')
}) })
test('the PUT method', () => {
const fetch = jest.fn()
window.fetch = fetch
const stub = jest.fn()
on('beforeFetch', stub)
net.put('/abc')
expect(fetch).toBeCalled()
// eslint-disable-next-line prefer-destructuring
const request = fetch.mock.calls[0][0]
expect(request.method).toBe('PUT')
expect(stub).toBeCalledWith({
method: 'PUT',
url: '/abc',
data: {},
})
})
test('the DELETE method', () => {
const fetch = jest.fn()
window.fetch = fetch
const stub = jest.fn()
on('beforeFetch', stub)
net.del('/abc')
expect(fetch).toBeCalled()
// eslint-disable-next-line prefer-destructuring
const request = fetch.mock.calls[0][0]
expect(request.method).toBe('DELETE')
expect(stub).toBeCalledWith({
method: 'DELETE',
url: '/abc',
data: {},
})
})
test('low level fetch', async () => { test('low level fetch', async () => {
const json = jest.fn().mockResolvedValue({}) const json = jest.fn().mockResolvedValue({})
window.fetch = jest.fn() window.fetch = jest.fn()

View File

@ -8,6 +8,8 @@ declare module 'vue/types/vue' {
$http: { $http: {
get: jest.Mock<any> get: jest.Mock<any>
post: jest.Mock<any> post: jest.Mock<any>
put: jest.Mock<any>
del: jest.Mock<any>
}, },
$message: { $message: {
info: jest.Mock<ReturnType<typeof Message>, Parameters<typeof Message>> info: jest.Mock<ReturnType<typeof Message>, Parameters<typeof Message>>

View File

@ -17,3 +17,13 @@ export const post = {} as jest.Mock<
ReturnType<typeof net.post>, ReturnType<typeof net.post>,
Parameters<typeof net.post> Parameters<typeof net.post>
> >
export const put = {} as jest.Mock<
ReturnType<typeof net.post>,
Parameters<typeof net.post>
>
export const del = {} as jest.Mock<
ReturnType<typeof net.post>,
Parameters<typeof net.post>
>