Skip to content

Commit 5f97193

Browse files
committed
feat: Adding the option to conditionally camelize response body
1 parent 03e85ef commit 5f97193

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ import { Projects } from 'gitlab';
183183
const service = new Projects({
184184
host: 'http://example.com', // Defaults to https://gitlab.com
185185
token: 'abcdefghij123456', // Can be created in your profile.
186+
camelize = false, //Response Key Camelize. Camelizes all response body keys. Optional, Default: false
186187
});
187188
```
188189

src/infrastructure/BaseService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface BaseModelOptions {
66
public readonly url: string;
77
token?: string;
88
oauthToken?: string;
9-
useXMLHttpRequest?: boolean;
9+
public readonly camelize: boolean;
1010
version?: string;
1111
sudo?: string | number;
1212
rejectUnauthorized?: boolean;
@@ -29,6 +29,7 @@ class BaseModel {
2929
host = 'https://gitlab.com',
3030
url = '',
3131
version = 'v4',
32+
camelize = false,
3233
rejectUnauthorized = true,
3334
}: BaseModelContructorOptions) {
3435
}: BaseServiceOptions) {
@@ -38,6 +39,7 @@ class BaseModel {
3839
? XMLHttpRequester : (Request as temporaryAny as XhrStaticPromisified);
3940
this.useXMLHttpRequest = useXMLHttpRequest;
4041
this.rejectUnauthorized = rejectUnauthorized;
42+
this.camelize = camelize;
4143

4244
// Handle auth tokens
4345
if (oauthToken) this.headers.authorization = `Bearer ${oauthToken}`;

src/infrastructure/RequestHelper.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Humps from 'humps';
2-
import LinkParser from 'parse-link-header';
2+
import { camelizeKeys } from 'humps';
33
import QS from 'qs';
44
import URLJoin from 'url-join';
55
import StreamableRequest from 'request';
@@ -46,8 +46,8 @@ function defaultRequest(
4646

4747
if (body) params.body = Humps.decamelizeKeys(body);
4848

49-
if (qs) {
50-
if (useXMLHttpRequest) {
49+
// Camelize response body if specified
50+
if (service.camelize) body = camelizeKeys(body);
5151
// The xhr package doesn't have a way of passing in a qs object until v3
5252
params.url = URLJoin(params.url, `?${QS.stringify(Humps.decamelizeKeys(qs), { arrayFormat: 'brackets' })}`);
5353
} else {

0 commit comments

Comments
 (0)