Skip to content

Commit

Permalink
refactor(core): CHECKOUT-2899 Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchin committed Jul 5, 2018
1 parent f7894a1 commit a1489be
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 59 deletions.
13 changes: 3 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
"description": "HTTP request client for browsers",
"license": "MIT",
"main": "lib/index.js",
"typings": "index.d.ts",
"types": "lib/index.d.ts",
"files": [
"lib/",
"index.d.ts"
"lib/"
],
"engines": {
"node": "^6.10.0"
},
"repository": {
"type": "git",
"url": "git://github.com/bigcommerce/request-sender-js.git"
Expand All @@ -24,7 +20,7 @@
"scripts": {
"prebuild": "rm -rf lib",
"build": "tsc --outDir lib --project tsconfig-build.json",
"lint": "eslint src --config eslintrc.json",
"lint": "tslint 'src/**/*.ts' --config tslint.json --project tsconfig.json && tsc --noEmit",
"prepare": "npm run build",
"prerelease": "git fetch --tags && npm run validate-commits && npm run lint && npm test",
"release": "standard-version",
Expand All @@ -48,9 +44,6 @@
"@bigcommerce/validate-commits": "^2.0.2",
"@types/jest": "^22.2.3",
"@types/node": "^10.1.4",
"eslint": "2.8.0",
"eslint-config-airbnb": "6.0.2",
"eslint-plugin-react": "4.1.0",
"jest": "^21.2.1",
"standard-version": "^4.2.0",
"ts-jest": "^21.2.3",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cookie from 'js-cookie';

import PayloadTransformer from './payload-transformer';
import RequestFactory from './request-factory';
import RequestSender from './request-sender';
Expand Down
7 changes: 3 additions & 4 deletions src/payload-transformer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import PayloadTransformer from './payload-transformer';
import Timeout from './timeout';

describe('PayloadTransformer', () => {
let payloadTransformer: PayloadTransformer;
Expand Down Expand Up @@ -37,14 +36,14 @@ describe('PayloadTransformer', () => {
),
response: '{ "message": "foobar" }',
status: 200,
statusText: 'OK',
statusText: 'OK',
} as any;

expect(payloadTransformer.toResponse(xhr)).toEqual({
body: { message: 'foobar' },
headers: {
'content-type': 'application/json',
'content-language': 'en',
'content-type': 'application/json',
},
status: 200,
statusText: 'OK',
Expand All @@ -64,8 +63,8 @@ describe('PayloadTransformer', () => {
expect(payloadTransformer.toResponse(xhr)).toEqual({
body: { message: 'foobar' },
headers: {
'content-type': 'application/problem+json',
'content-language': 'en',
'content-type': 'application/problem+json',
},
status: 400,
statusText: 'Bad request',
Expand Down
17 changes: 11 additions & 6 deletions src/payload-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const JSON_CONTENT_TYPE_REGEXP = /application\/(\w+\+)?json/;

export default class PayloadTransformer {
toRequestBody(options: RequestOptions): any {
const contentType = this._getHeader(options.headers, 'Content-Type');
const contentType = options.headers ? this._getHeader(options.headers, 'Content-Type') : '';

if (options.body && JSON_CONTENT_TYPE_REGEXP.test(contentType)) {
return JSON.stringify(options.body);
Expand All @@ -17,7 +17,12 @@ export default class PayloadTransformer {

toResponse(xhr: XMLHttpRequest): Response {
const headers = this._parseResponseHeaders(xhr.getAllResponseHeaders());
const body = this._parseResponseBody('response' in xhr ? xhr.response : (xhr as any).responseText, headers); // Using `responseText` to support legacy IE

// Using `responseText` to support legacy IE
const body = this._parseResponseBody(
'response' in xhr ? xhr.response : (xhr as any).responseText,
headers
);

return {
body,
Expand All @@ -27,7 +32,7 @@ export default class PayloadTransformer {
};
}

_parseResponseBody(body: string, headers: Headers): any {
private _parseResponseBody(body: string, headers: Headers): any {
const contentType = this._getHeader(headers, 'Content-Type');

if (body && JSON_CONTENT_TYPE_REGEXP.test(contentType)) {
Expand All @@ -37,12 +42,12 @@ export default class PayloadTransformer {
return body;
}

_parseResponseHeaders(rawHeaders: string): Headers {
private _parseResponseHeaders(rawHeaders: string): Headers {
const lines = rawHeaders ? rawHeaders.replace(/\r?\n[\t ]+/g, ' ').split(/\r?\n/) : [];

return lines.reduce((headers, line) => {
const parts = line.split(':');
const key = parts.shift().trim();
const key = (parts.shift() || '').trim();

if (!key) {
return headers;
Expand All @@ -55,7 +60,7 @@ export default class PayloadTransformer {
}, {});
}

_getHeader(headers: Headers, key: string): string {
private _getHeader(headers: Headers, key: string): string {
if (!headers || !key) {
return '';
}
Expand Down
11 changes: 5 additions & 6 deletions src/request-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import RequestFactory from './request-factory';
import Timeout from './timeout';

describe('RequestFactory', () => {
let requestFactory: RequestFactory;
Expand All @@ -8,7 +7,7 @@ describe('RequestFactory', () => {
beforeEach(() => {
url = 'http://foobar/v1/endpoint';

(<any>global).XMLHttpRequest = function XMLHttpRequestMock() {
(global as any).XMLHttpRequest = function XMLHttpRequestMock() {
this.open = jest.fn();
this.setRequestHeader = jest.fn();
};
Expand All @@ -26,12 +25,12 @@ describe('RequestFactory', () => {
it('configures XHR object with options', () => {
const xhr = requestFactory.createRequest(url, {
credentials: true,
timeout: 2000,
method: 'GET',
headers: {
'Accept': 'application/json, text/plain, */*',
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
method: 'GET',
timeout: 2000,
} as any);

expect(xhr.withCredentials).toEqual(true);
Expand All @@ -45,8 +44,8 @@ describe('RequestFactory', () => {
const xhr = requestFactory.createRequest(url, {
method: 'GET',
params: {
foobar: 'foobar',
bar: 'bar',
foobar: 'foobar',
},
});

Expand Down
21 changes: 10 additions & 11 deletions src/request-factory.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import * as queryString from 'query-string';

import { Headers } from './headers';
import { RequestOptions } from './request-options';

export default class RequestFactory {
createRequest(url: string, options: RequestOptions = {}): XMLHttpRequest {
createRequest(url: string, options?: RequestOptions): XMLHttpRequest {
const xhr = new XMLHttpRequest();

this._configureRequest(xhr, url, options);

return xhr;
}

_configureRequest(xhr: XMLHttpRequest, url: string, options: RequestOptions): void {
xhr.open(options.method, this._formatUrl(url, options.params), true);
private _configureRequest(xhr: XMLHttpRequest, url: string, options: RequestOptions = {}): void {
xhr.open(options.method || 'GET', this._formatUrl(url, options.params), true);

this._configureRequestHeaders(xhr, options.headers);
if (options.headers) {
this._configureRequestHeaders(xhr, options.headers);
}

if (typeof options.credentials === 'boolean') {
xhr.withCredentials = options.credentials;
Expand All @@ -25,17 +28,13 @@ export default class RequestFactory {
}
}

_configureRequestHeaders(xhr: XMLHttpRequest, headers: Headers): void {
if (!headers) {
return;
}

Object.keys(headers).forEach((key) => {
private _configureRequestHeaders(xhr: XMLHttpRequest, headers: Headers): void {
Object.keys(headers).forEach(key => {
xhr.setRequestHeader(key, headers[key]);
});
}

_formatUrl(url: string, params: Object): string {
private _formatUrl(url: string, params?: object): string {
if (!params || Object.keys(params).length === 0) {
return url;
}
Expand Down
11 changes: 6 additions & 5 deletions src/request-sender.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as cookie from 'js-cookie';
import { getErrorResponse, getResponse, getTimeoutResponse } from './responses.mock';

import PayloadTransformer from './payload-transformer';
import RequestFactory from './request-factory';
import RequestSender from './request-sender';
import { getErrorResponse, getResponse, getTimeoutResponse } from './responses.mock';

describe('RequestSender', () => {
let payloadTransformer: PayloadTransformer;
Expand Down Expand Up @@ -32,7 +33,7 @@ describe('RequestSender', () => {
expect(requestFactory.createRequest).toHaveBeenCalledWith(url, {
credentials: true,
headers: {
'Accept': 'application/json, text/plain, */*',
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
method: 'GET',
Expand All @@ -44,7 +45,7 @@ describe('RequestSender', () => {
body: 'foobar',
credentials: false,
headers: {
'Accept': 'text/plain',
Accept: 'text/plain',
'Content-Type': 'text/plain',
},
method: 'POST',
Expand All @@ -69,8 +70,8 @@ describe('RequestSender', () => {
expect(requestFactory.createRequest).toHaveBeenCalledWith(url, {
credentials: true,
headers: {
'Accept': 'text/plain',
'Authorization': 'Basic YWxhZGRpbjpvcGVuc2VzYW1l',
Accept: 'text/plain',
Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l',
'Content-Type': 'application/json',
},
method: 'POST',
Expand Down
9 changes: 5 additions & 4 deletions src/request-sender.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CookiesStatic } from 'js-cookie';
import { merge } from 'lodash';

import isPromise from './is-promise';
import PayloadTransformer from './payload-transformer';
import RequestFactory from './request-factory';
Expand Down Expand Up @@ -67,19 +68,19 @@ export default class RequestSender {
return this.sendRequest(url, { ...options, method: 'DELETE' });
}

_mergeDefaultOptions(options: RequestOptions = {}): RequestOptions {
private _mergeDefaultOptions(options?: RequestOptions): RequestOptions {
const defaultOptions: Partial<RequestOptions> = {
credentials: true,
method: 'GET',
headers: {
'Accept': 'application/json, text/plain, */*',
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
method: 'GET',
};

const csrfToken = this._cookie.get('XSRF-TOKEN');

if (csrfToken) {
if (csrfToken && defaultOptions.headers) {
defaultOptions.headers['X-XSRF-TOKEN'] = csrfToken;
}

Expand Down
1 change: 0 additions & 1 deletion src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ export interface Response<T = any> {
status: number;
statusText: string;
}

8 changes: 4 additions & 4 deletions src/responses.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export function getResponse(
): Response {
return {
body,
status,
statusText,
headers: {
'content-type': 'application/json',
...headers,
},
status,
statusText,
};
}

Expand All @@ -26,12 +26,12 @@ export function getErrorResponse(
): Response {
return {
body,
status,
statusText,
headers: {
'content-type': 'application/json',
...headers,
},
status,
statusText,
};
}

Expand Down
8 changes: 4 additions & 4 deletions src/timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Timeout', () => {
jest.useRealTimers();
});

it('triggers callback when complete', (done) => {
it('triggers callback when complete', done => {
const timeout = new Timeout();
const callback = jest.fn();

Expand All @@ -22,7 +22,7 @@ describe('Timeout', () => {
});
});

it('does not trigger callback again after completion', (done) => {
it('does not trigger callback again after completion', done => {
const timeout = new Timeout();
const callback = jest.fn();

Expand All @@ -36,7 +36,7 @@ describe('Timeout', () => {
});
});

it('triggers callback after delay', (done) => {
it('triggers callback after delay', done => {
const timeout = new Timeout(10);
const callback = jest.fn();

Expand All @@ -51,7 +51,7 @@ describe('Timeout', () => {
});
});

it('does not trigger callback again after manual completion', (done) => {
it('does not trigger callback again after manual completion', done => {
const timeout = new Timeout(10);
const callback = jest.fn();

Expand Down
10 changes: 6 additions & 4 deletions src/timeout.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
export default class Timeout {
private _timeoutToken?: number;
private _promise: Promise<any>;
private _resolve: () => void;
private _timeoutToken?: number;

constructor(
private _delay?: number
) {
this._promise = new Promise((resolve) => {
// tslint:disable-next-line:no-empty
this._resolve = () => {};

this._promise = new Promise(resolve => {
this._resolve = resolve;
});
}
}

onComplete(callback: () => void): void {
this._promise.then(callback);
Expand All @@ -29,4 +32,3 @@ export default class Timeout {
}
}
}

3 changes: 3 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@bigcommerce/tslint-config"
}

0 comments on commit a1489be

Please sign in to comment.