Merge pull request #12 from TouchInstinct/feature/request_header

Feature/request header
This commit is contained in:
grigoriishveps 2021-12-28 16:33:51 +03:00 committed by GitHub
commit 1226407579
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View File

@ -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,
})
}
}

11
api/TypeHeader.ts Normal file
View File

@ -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