From 5f97193dfdea608abb8ca7761d798815bbdab948 Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 25 May 2019 17:59:11 -0400 Subject: [PATCH] feat: Adding the option to conditionally camelize response body --- README.md | 1 + src/infrastructure/BaseService.ts | 4 +++- src/infrastructure/RequestHelper.ts | 6 +++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index eab9dbfb..f0d03cae 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ import { Projects } from 'gitlab'; const service = new Projects({ host: 'http://example.com', // Defaults to https://gitlab.com token: 'abcdefghij123456', // Can be created in your profile. + camelize = false, //Response Key Camelize. Camelizes all response body keys. Optional, Default: false }); ``` diff --git a/src/infrastructure/BaseService.ts b/src/infrastructure/BaseService.ts index 53b530f8..3b713859 100644 --- a/src/infrastructure/BaseService.ts +++ b/src/infrastructure/BaseService.ts @@ -6,7 +6,7 @@ interface BaseModelOptions { public readonly url: string; token?: string; oauthToken?: string; - useXMLHttpRequest?: boolean; + public readonly camelize: boolean; version?: string; sudo?: string | number; rejectUnauthorized?: boolean; @@ -29,6 +29,7 @@ class BaseModel { host = 'https://gitlab.com', url = '', version = 'v4', + camelize = false, rejectUnauthorized = true, }: BaseModelContructorOptions) { }: BaseServiceOptions) { @@ -38,6 +39,7 @@ class BaseModel { ? XMLHttpRequester : (Request as temporaryAny as XhrStaticPromisified); this.useXMLHttpRequest = useXMLHttpRequest; this.rejectUnauthorized = rejectUnauthorized; + this.camelize = camelize; // Handle auth tokens if (oauthToken) this.headers.authorization = `Bearer ${oauthToken}`; diff --git a/src/infrastructure/RequestHelper.ts b/src/infrastructure/RequestHelper.ts index cb136fc1..c83ec810 100644 --- a/src/infrastructure/RequestHelper.ts +++ b/src/infrastructure/RequestHelper.ts @@ -1,5 +1,5 @@ import Humps from 'humps'; -import LinkParser from 'parse-link-header'; +import { camelizeKeys } from 'humps'; import QS from 'qs'; import URLJoin from 'url-join'; import StreamableRequest from 'request'; @@ -46,8 +46,8 @@ function defaultRequest( if (body) params.body = Humps.decamelizeKeys(body); - if (qs) { - if (useXMLHttpRequest) { + // Camelize response body if specified + if (service.camelize) body = camelizeKeys(body); // The xhr package doesn't have a way of passing in a qs object until v3 params.url = URLJoin(params.url, `?${QS.stringify(Humps.decamelizeKeys(qs), { arrayFormat: 'brackets' })}`); } else {