fix(utils/request): fix options and drop unused copy-paste
This commit is contained in:
parent
6a76b4b06d
commit
f9a93405ab
|
|
@ -241,3 +241,25 @@ describe('# Authorization header', function () {
|
|||
}))
|
||||
})
|
||||
})
|
||||
|
||||
describe('# Headers', function () {
|
||||
it('should add headers', async function () {
|
||||
const options = { headers: { h1: 'h1', h2: 'h2' } }
|
||||
const request = new FetchRequest(options)
|
||||
|
||||
await request.get('/path')
|
||||
|
||||
expect(global.fetch).toBeCalledWith('/path', expect.objectContaining(options))
|
||||
})
|
||||
|
||||
it('should merge headers', async function () {
|
||||
const options = { headers: { h1: 'h1', h2: 'h2' } }
|
||||
const localOptions = { headers: { h1: 'h11', h3: 'h3' } }
|
||||
const expectedOptions = { headers: { h1: 'h11', h2: 'h2', h3: 'h3' } }
|
||||
const request = new FetchRequest(options)
|
||||
|
||||
await request.get('/path', localOptions)
|
||||
|
||||
expect(global.fetch).toBeCalledWith('/path', expect.objectContaining(expectedOptions))
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import parseStorageGet from './parse-storage-get'
|
||||
import merge from 'deepmerge'
|
||||
|
||||
interface FetchRequestOptions {
|
||||
prefix: string;
|
||||
|
|
@ -8,17 +8,17 @@ interface FetchRequestOptions {
|
|||
}
|
||||
|
||||
export default class FetchRequest {
|
||||
defaultOptions: FetchRequestOptions = {
|
||||
private defaultOptions: FetchRequestOptions = {
|
||||
prefix: '',
|
||||
headers: {},
|
||||
params: {},
|
||||
responseInterceptor: (response) => response,
|
||||
}
|
||||
|
||||
public options: FetchRequestOptions
|
||||
private options: FetchRequestOptions
|
||||
|
||||
constructor (options: Partial<FetchRequestOptions> = {}) {
|
||||
this.options = Object.assign({}, this.defaultOptions, options)
|
||||
this.options = merge(this.defaultOptions, options)
|
||||
}
|
||||
|
||||
private generateFinalUrl = (url: string, options: Partial<FetchRequestOptions> = {}) => {
|
||||
|
|
@ -34,6 +34,10 @@ export default class FetchRequest {
|
|||
return finalUrl
|
||||
}
|
||||
|
||||
private generateFinalHeaders = (options: Partial<FetchRequestOptions> = {}) => {
|
||||
return merge(this.options.headers, options.headers ?? {})
|
||||
}
|
||||
|
||||
private handleResponse = (response: Response) => {
|
||||
this.options.responseInterceptor(response)
|
||||
return response.json()
|
||||
|
|
@ -51,73 +55,59 @@ export default class FetchRequest {
|
|||
}
|
||||
|
||||
get<T = any> (url: string, options: Partial<FetchRequestOptions> = {}): Promise<T> {
|
||||
options.headers = options.headers ?? {}
|
||||
const token = parseStorageGet('user')?.token
|
||||
if (token) options.headers.Authorization = `Token ${token}`
|
||||
|
||||
const finalUrl = this.generateFinalUrl(url, options)
|
||||
const headers = this.generateFinalHeaders(options)
|
||||
|
||||
return fetch(finalUrl, {
|
||||
method: 'GET',
|
||||
headers: this.options.headers,
|
||||
headers,
|
||||
})
|
||||
.then(this.handleResponse)
|
||||
}
|
||||
|
||||
post<T = any> (url: string, data: Record<string, any> = {}, options: Partial<FetchRequestOptions> = {}): Promise<T> {
|
||||
options.headers = options.headers ?? {}
|
||||
const token = parseStorageGet('user')?.token
|
||||
if (token) options.headers.Authorization = `Token ${token}`
|
||||
|
||||
const finalUrl = this.generateFinalUrl(url, options)
|
||||
const headers = this.generateFinalHeaders(options)
|
||||
|
||||
return fetch(finalUrl, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
headers: this.options.headers,
|
||||
headers,
|
||||
})
|
||||
.then(this.handleResponse)
|
||||
}
|
||||
|
||||
delete<T = any> (url: string, options: Partial<FetchRequestOptions> = {}): Promise<T> {
|
||||
options.headers = options.headers ?? {}
|
||||
const token = parseStorageGet('user')?.token
|
||||
if (token) options.headers.Authorization = `Token ${token}`
|
||||
|
||||
const finalUrl = this.generateFinalUrl(url, options)
|
||||
const headers = this.generateFinalHeaders(options)
|
||||
|
||||
return fetch(finalUrl, {
|
||||
method: 'DELETE',
|
||||
headers: this.options.headers,
|
||||
headers,
|
||||
})
|
||||
.then(this.handleResponse)
|
||||
}
|
||||
|
||||
put<T = any> (url: string, data: Record<string, any> = {}, options: Partial<FetchRequestOptions> = {}): Promise<T> {
|
||||
options.headers = options.headers ?? {}
|
||||
const token = parseStorageGet('user')?.token
|
||||
if (token) options.headers.Authorization = `Token ${token}`
|
||||
|
||||
const finalUrl = this.generateFinalUrl(url, options)
|
||||
const headers = this.generateFinalHeaders(options)
|
||||
|
||||
return fetch(finalUrl, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data),
|
||||
headers: this.options.headers,
|
||||
headers,
|
||||
})
|
||||
.then(this.handleResponse)
|
||||
}
|
||||
|
||||
patch<T = any> (url: string, data: Record<string, any> = {}, options: Partial<FetchRequestOptions> = {}): Promise<T> {
|
||||
options.headers = options.headers ?? {}
|
||||
const token = parseStorageGet('user')?.token
|
||||
if (token) options.headers.Authorization = `Token ${token}`
|
||||
|
||||
const finalUrl = this.generateFinalUrl(url, options)
|
||||
const headers = this.generateFinalHeaders(options)
|
||||
|
||||
return fetch(finalUrl, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
headers: this.options.headers,
|
||||
headers,
|
||||
})
|
||||
.then(this.handleResponse)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue