diff --git a/api/ApiMethodFactory.ts b/api/ApiMethodFactory.ts index 8060283..8416257 100644 --- a/api/ApiMethodFactory.ts +++ b/api/ApiMethodFactory.ts @@ -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 = null): Promise => { - const getBody = R.pipe( - R.ifElse( - R.isNil, - R.always(null), + ) => async (data: Nullable = null, headers: HeadersType = {}): Promise => { + const getBody = (body: Nullable) => { + 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, }) } } diff --git a/api/TypeHeader.ts b/api/TypeHeader.ts new file mode 100644 index 0000000..6e076db --- /dev/null +++ b/api/TypeHeader.ts @@ -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