feat: Adding the option to conditionally camelize response body

This commit is contained in:
Justin 2019-05-25 17:59:11 -04:00 committed by Justin Dalrymple
parent 03e85ef171
commit 5f97193dfd
3 changed files with 7 additions and 4 deletions

View File

@ -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
});
```

View File

@ -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}`;

View File

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