Skip to content

Commit f3d75d2

Browse files
committed
removing undefined from headers refactoring
1 parent e831ec1 commit f3d75d2

File tree

8 files changed

+148
-35
lines changed

8 files changed

+148
-35
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Jira.js changelog
22

3+
### 1.7.2
4+
5+
- FIX: console.log removed
6+
7+
### 1.7.1
8+
9+
- FIX: Headers fixes
10+
311
### 1.7.0
412

513
- IMPROVEMENT: Readme examples updated

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
"devDependencies": {
2929
"@types/jest": "^26.0.14",
3030
"@types/node": "^14.11.2",
31-
"@types/sinon": "^9.0.6",
31+
"@types/sinon": "^9.0.7",
3232
"@typescript-eslint/eslint-plugin": "^4.3.0",
3333
"@typescript-eslint/parser": "^4.3.0",
3434
"eslint": "^7.10.0",
3535
"eslint-config-airbnb-typescript": "^11.0.0",
3636
"eslint-import-resolver-typescript": "^2.3.0",
3737
"eslint-plugin-import": "^2.22.1",
3838
"jest": "^26.4.2",
39-
"sinon": "^9.0.3",
40-
"ts-jest": "^26.4.0",
39+
"sinon": "^9.1.0",
40+
"ts-jest": "^26.4.1",
4141
"typedoc": "^0.19.2",
4242
"typescript": "^4.0.3"
4343
},

src/helpers/index.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ import * as jwt from 'atlassian-jwt';
33
import * as url from 'url';
44
import { Config } from '../config';
55

6-
export const removeEmptyValues = (obj: { [key: string]: unknown }) => Object.entries(obj)
7-
.forEach(([key, val]) => {
8-
if (val && typeof val === 'object') {
9-
// @ts-ignore
10-
removeEmptyValues(val);
11-
} else if (val == null) {
12-
// eslint-disable-next-line no-param-reassign
13-
delete obj[key];
14-
}
15-
});
16-
176
export const getAuthentication = (
187
config: Config,
198
request: AxiosRequestConfig,

src/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
44
import { Callback } from './callback';
55
import { Config } from './config';
6-
import { getAuthentication, removeEmptyValues } from './helpers';
6+
import { getAuthentication } from './helpers';
7+
import * as Utils from './utils';
78
import {
89
ApplicationRoles,
910
AppProperties,
@@ -188,12 +189,10 @@ export class Client {
188189
private requestInstance: AxiosInstance;
189190

190191
constructor(private readonly config: Config) {
191-
const headers = {
192+
const headers = Utils.removeUndefinedElements({
192193
...config.baseRequestConfig?.headers,
193194
'x-atlassian-force-account-id': config.strictGDPR,
194-
};
195-
196-
removeEmptyValues(headers);
195+
});
197196

198197
this.requestInstance = axios.create({
199198
timeout: config.timeout,
@@ -293,12 +292,10 @@ export class Client {
293292

294293
public async sendRequest(request: AxiosRequestConfig, callback?: Callback): Promise<any> {
295294
try {
296-
request.headers = {
295+
request.headers = Utils.removeUndefinedElements({
297296
Authorization: getAuthentication(this.config, request),
298297
...request.headers,
299-
};
300-
301-
removeEmptyValues(request.headers);
298+
});
302299

303300
const response = await this.requestInstance.request(request);
304301

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './removeUndefinedElements';

src/utils/removeUndefinedElements.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function removeUndefinedFromArray<T>(array: Array<T>): Array<T> {
2+
return array.filter((element) => element !== undefined);
3+
}
4+
5+
function removeUndefinedFromObject<T>(object: Record<string, T>): Record<string, T> {
6+
return Object.entries(object).reduce((accumulator, current) => {
7+
const [key, value] = current;
8+
9+
if (value === undefined) {
10+
return accumulator;
11+
}
12+
13+
const isObject = typeof value === 'object';
14+
const isNull = value === null;
15+
const isSet = value instanceof Set;
16+
const isMap = value instanceof Map;
17+
18+
if (
19+
!isNull
20+
&& isObject
21+
&& !isSet
22+
&& !isMap
23+
) {
24+
return {
25+
...accumulator,
26+
[key]: Array.isArray(value)
27+
? removeUndefinedFromArray(value)
28+
: removeUndefinedFromObject(value as Record<string, unknown>),
29+
};
30+
}
31+
32+
return {
33+
...accumulator,
34+
[key]: value,
35+
};
36+
}, {});
37+
}
38+
39+
export function removeUndefinedElements<T>(o: Array<T>): Array<T>;
40+
export function removeUndefinedElements<T>(o: Record<string, T>): Record<string, T>;
41+
export function removeUndefinedElements(o: any): any {
42+
switch (typeof o) {
43+
case 'object':
44+
if (Array.isArray(o)) {
45+
return removeUndefinedFromArray(o);
46+
}
47+
48+
return removeUndefinedFromObject(o);
49+
default:
50+
return o;
51+
}
52+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { removeUndefinedElements } from '../../src/utils';
2+
3+
describe('Utils: removeUndefinedElements', () => {
4+
it('should return empty object for empty object', () => {
5+
const originalObject: {} = {};
6+
const expectedObject: {} = {};
7+
8+
const actualObject = removeUndefinedElements(originalObject);
9+
10+
expect(actualObject).toEqual(expectedObject);
11+
});
12+
13+
it('should return empty array for empty array', () => {
14+
const originalArray: [] = [];
15+
const expectedArray: [] = [];
16+
17+
const actualArray = removeUndefinedElements(originalArray);
18+
19+
expect(actualArray).toEqual(expectedArray);
20+
});
21+
22+
it('shouldn\'t remove any element', () => {
23+
const originalObject = {
24+
key1: 'stringValue',
25+
emptyString: '',
26+
nullValue: null,
27+
falseBooleanValue: false,
28+
trueBooleanValue: true,
29+
map: new Map(),
30+
set: new Set(),
31+
emptyArray: [],
32+
number: 0,
33+
arrayWithNullElements: [null, null],
34+
arrayWithEmptyString: [''],
35+
arrayWithStrings: ['string1', 'string2'],
36+
};
37+
38+
const expectedObject = {
39+
key1: 'stringValue',
40+
emptyString: '',
41+
nullValue: null,
42+
falseBooleanValue: false,
43+
trueBooleanValue: true,
44+
map: new Map(),
45+
set: new Set(),
46+
emptyArray: [],
47+
number: 0,
48+
arrayWithNullElements: [null, null],
49+
arrayWithEmptyString: [''],
50+
arrayWithStrings: ['string1', 'string2'],
51+
};
52+
53+
const actualObject = removeUndefinedElements(originalObject);
54+
55+
expect(actualObject).toEqual(expectedObject);
56+
});
57+
58+
it('should remove undefined elements from array', () => {
59+
const originalArray = [undefined, 'someString', 3, undefined, '', null, undefined, undefined];
60+
const expectedArray = ['someString', 3, '', null];
61+
62+
const actualArray = removeUndefinedElements(originalArray);
63+
64+
expect(actualArray).toEqual(expectedArray);
65+
});
66+
});

yarn.lock

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -703,10 +703,10 @@
703703
"@types/express-serve-static-core" "*"
704704
"@types/mime" "*"
705705

706-
"@types/sinon@^9.0.6":
707-
version "9.0.6"
708-
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-9.0.6.tgz#fb4b6883fe0417e6a1ac5d9753bdcb7016dd4dd0"
709-
integrity sha512-j3GK0fiHgn8fe7sqOpInMjm0A2Tary1NBZ8gbI/sZ0C0JxYeO+nh8H0/pW/0l94vNWcH1FnZOZu/cOvIfNZTrg==
706+
"@types/sinon@^9.0.7":
707+
version "9.0.7"
708+
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-9.0.7.tgz#c277e19cf9eb0c71106e785650f1e5c299262302"
709+
integrity sha512-uyFiy2gp4P/FK9pmU3WIbT5ZzH54hCswwRkQFhxX7xl8jzhW3g+xOkVqk5YP4cIO//Few8UDAX0MtzFpqBEqwA==
710710
dependencies:
711711
"@types/sinonjs__fake-timers" "*"
712712

@@ -4137,10 +4137,10 @@ signal-exit@^3.0.0, signal-exit@^3.0.2:
41374137
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c"
41384138
integrity sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA==
41394139

4140-
sinon@^9.0.3:
4141-
version "9.0.3"
4142-
resolved "https://registry.yarnpkg.com/sinon/-/sinon-9.0.3.tgz#bffc3ec28c936332cd2a41833547b9eed201ecff"
4143-
integrity sha512-IKo9MIM111+smz9JGwLmw5U1075n1YXeAq8YeSFlndCLhAL5KGn6bLgu7b/4AYHTV/LcEMcRm2wU2YiL55/6Pg==
4140+
sinon@^9.1.0:
4141+
version "9.1.0"
4142+
resolved "https://registry.yarnpkg.com/sinon/-/sinon-9.1.0.tgz#4afc90707c8e360fe051398eed2d3b197980ffc3"
4143+
integrity sha512-9zQShgaeylYH6qtsnNXlTvv0FGTTckuDfHBi+qhgj5PvW2r2WslHZpgc3uy3e/ZAoPkqaOASPi+juU6EdYRYxA==
41444144
dependencies:
41454145
"@sinonjs/commons" "^1.7.2"
41464146
"@sinonjs/fake-timers" "^6.0.1"
@@ -4522,10 +4522,10 @@ tr46@^2.0.2:
45224522
dependencies:
45234523
punycode "^2.1.1"
45244524

4525-
ts-jest@^26.4.0:
4526-
version "26.4.0"
4527-
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.4.0.tgz#903c7827f3d3bc33efc2f91be294b164400c32e3"
4528-
integrity sha512-ofBzoCqf6Nv/PoWb/ByV3VNKy2KJSikamOBxvR3E6eVdIw10GwAXoyvMWXXjZJK2s6S27ZE8fI+JBTnGaovl6Q==
4525+
ts-jest@^26.4.1:
4526+
version "26.4.1"
4527+
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.4.1.tgz#08ec0d3fc2c3a39e4a46eae5610b69fafa6babd0"
4528+
integrity sha512-F4aFq01aS6mnAAa0DljNmKr/Kk9y4HVZ1m6/rtJ0ED56cuxINGq3Q9eVAh+z5vcYKe5qnTMvv90vE8vUMFxomg==
45294529
dependencies:
45304530
"@types/jest" "26.x"
45314531
bs-logger "0.x"

0 commit comments

Comments
 (0)