From 850070ed31771e389bd910f04b0f56a00542808d Mon Sep 17 00:00:00 2001 From: Desyatnikov Grigorii Date: Mon, 27 Dec 2021 22:46:08 +0300 Subject: [PATCH 1/2] add request header --- api/ApiMethodFactory.ts | 4 +++- api/TypeHeader.ts | 12 ++++++++++++ api/request.ts | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 api/TypeHeader.ts diff --git a/api/ApiMethodFactory.ts b/api/ApiMethodFactory.ts index 8060283..1ec8fbe 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,7 +64,7 @@ class ApiMethodFactory { path: pathKeys = [], query: queryKeys = [], }: { path?: string[], query?: string[] } = {}, - ) => async (data: Nullable = null): Promise => { + ) => async (data: Nullable = null, headers: HeadersType = {}): Promise => { const getBody = R.pipe( R.ifElse( R.isNil, @@ -80,6 +81,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..53e1303 --- /dev/null +++ b/api/TypeHeader.ts @@ -0,0 +1,12 @@ +interface HeadersType { + 'Content-Type'?: ContentType + Accept?: string +} + +export enum ContentType { + // MULTIPART = 'multipart/form-data; boundary=boundary', + MULTIPART = 'multipart/form-data', + JSON = 'application/json', +} + +export default HeadersType diff --git a/api/request.ts b/api/request.ts index 8ef08a2..8cacf4d 100644 --- a/api/request.ts +++ b/api/request.ts @@ -7,6 +7,8 @@ const retrieve = async ( hasRetriedAfterAuthentication = false, ): Promise => { try { + console.log(props); + const { data } = await axios(props) return data From c9a0b721392add89e48066303837946071dc6505 Mon Sep 17 00:00:00 2001 From: Desyatnikov Grigorii Date: Tue, 28 Dec 2021 16:30:32 +0300 Subject: [PATCH 2/2] add request header --- api/ApiMethodFactory.ts | 16 +++++++++------- api/TypeHeader.ts | 3 +-- api/request.ts | 2 -- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/api/ApiMethodFactory.ts b/api/ApiMethodFactory.ts index 1ec8fbe..8416257 100644 --- a/api/ApiMethodFactory.ts +++ b/api/ApiMethodFactory.ts @@ -65,14 +65,16 @@ class ApiMethodFactory { query: queryKeys = [], }: { path?: string[], query?: string[] } = {}, ) => async (data: Nullable = null, headers: HeadersType = {}): Promise => { - const getBody = R.pipe( - R.ifElse( - R.isNil, - R.always(null), + 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) diff --git a/api/TypeHeader.ts b/api/TypeHeader.ts index 53e1303..6e076db 100644 --- a/api/TypeHeader.ts +++ b/api/TypeHeader.ts @@ -4,8 +4,7 @@ interface HeadersType { } export enum ContentType { - // MULTIPART = 'multipart/form-data; boundary=boundary', - MULTIPART = 'multipart/form-data', + MULTIPART = 'multipart/form-data; boundary=boundary', JSON = 'application/json', } diff --git a/api/request.ts b/api/request.ts index 8cacf4d..8ef08a2 100644 --- a/api/request.ts +++ b/api/request.ts @@ -7,8 +7,6 @@ const retrieve = async ( hasRetriedAfterAuthentication = false, ): Promise => { try { - console.log(props); - const { data } = await axios(props) return data