Merge pull request #12 from TouchInstinct/feature/request_header
Feature/request header
This commit is contained in:
commit
1226407579
|
|
@ -3,6 +3,7 @@ import * as R from 'ramda'
|
|||
import { sprintf } from 'sprintf-js'
|
||||
import request from './request'
|
||||
import HttpMethod from './HttpMethod'
|
||||
import HeadersType from './TypeHeader'
|
||||
|
||||
class ApiMethodFactory {
|
||||
private readonly apiPrefix: string
|
||||
|
|
@ -63,15 +64,17 @@ class ApiMethodFactory {
|
|||
path: pathKeys = [],
|
||||
query: queryKeys = [],
|
||||
}: { path?: string[], query?: string[] } = {},
|
||||
) => async (data: Nullable<T> = null): Promise<R> => {
|
||||
const getBody = R.pipe(
|
||||
R.ifElse(
|
||||
R.isNil,
|
||||
R.always(null),
|
||||
) => async (data: Nullable<T> = null, headers: HeadersType = {}): Promise<R> => {
|
||||
const getBody = (body: Nullable<T>) => {
|
||||
if (R.isNil(body) || body instanceof FormData) {
|
||||
return body
|
||||
}
|
||||
|
||||
return R.pipe(
|
||||
R.omit(R.concat(pathKeys, queryKeys)),
|
||||
),
|
||||
R.when(R.isEmpty, R.always(null)),
|
||||
)
|
||||
R.when(R.isEmpty, R.always(null)),
|
||||
)(body)
|
||||
}
|
||||
|
||||
const body = getBody(data)
|
||||
const endpoint = this.makeEndpoint(template, data, pathKeys, queryKeys)
|
||||
|
|
@ -80,6 +83,7 @@ class ApiMethodFactory {
|
|||
method: method,
|
||||
url: endpoint,
|
||||
data: body,
|
||||
headers: headers,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
interface HeadersType {
|
||||
'Content-Type'?: ContentType
|
||||
Accept?: string
|
||||
}
|
||||
|
||||
export enum ContentType {
|
||||
MULTIPART = 'multipart/form-data; boundary=boundary',
|
||||
JSON = 'application/json',
|
||||
}
|
||||
|
||||
export default HeadersType
|
||||
Loading…
Reference in New Issue