From d00ddb9c0af7e9ab9d2e62ae19f8fb19969a9823 Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Wed, 19 Jan 2022 13:37:58 +0000 Subject: [PATCH 1/9] feat: add fetch method to client --- src/lib/components/HttpClient.spec.ts | 40 +++++++++++++++++++++++++++ src/lib/components/HttpClient.ts | 11 ++++++++ 2 files changed, 51 insertions(+) diff --git a/src/lib/components/HttpClient.spec.ts b/src/lib/components/HttpClient.spec.ts index 8981e44..1915ead 100644 --- a/src/lib/components/HttpClient.spec.ts +++ b/src/lib/components/HttpClient.spec.ts @@ -68,4 +68,44 @@ describe('HttpClient', () => { data: 'hello world', }); }); + + it('should fetch amd returmn status amd data', async () => { + jest.spyOn(connection, 'request').mockReturnValue( + Promise.resolve({ + status: 200, + data: 'hello world', + headers: {}, + }) + ); + + const response = await httpClient.fetch({ + url: 'https://bigcontent.io', + method: HttpMethod.GET, + data: {}, + }); + + expect(response).toEqual({ + status: 200, + data: 'hello world', + }); + }); + + it('should fetch and throw an error', async () => { + jest.spyOn(connection, 'request').mockReturnValue( + Promise.reject({ + status: 404, + data: 'Not Found', + }) + ); + + try { + await httpClient.fetch({ + url: 'https://bigcontent.io', + method: HttpMethod.GET, + data: {}, + }); + } catch (error: any) { + expect(error.message).toBe('Request Error, Status: 404'); + } + }); }); diff --git a/src/lib/components/HttpClient.ts b/src/lib/components/HttpClient.ts index c63422b..5e8060c 100644 --- a/src/lib/components/HttpClient.ts +++ b/src/lib/components/HttpClient.ts @@ -88,4 +88,15 @@ export class HttpClient { return this.DEFAULT_ERROR; } } + + public async fetch(config: HttpRequest): Promise { + const response = await this.request(config); + if (response.status >= 200 && response.status < 400 || response.status === 410) { + return { + data: response.data, + status: response.status, + }; + } + throw new Error (`Request Error, Status: ${response.status}`); + } } From dcc100a1f3556a6359dd51eedc477299e6278e8f Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Wed, 19 Jan 2022 16:30:21 +0000 Subject: [PATCH 2/9] feat: refactor fetch method in HttpClient --- src/lib/components/HttpClient.spec.ts | 2 +- src/lib/components/HttpClient.ts | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/lib/components/HttpClient.spec.ts b/src/lib/components/HttpClient.spec.ts index 1915ead..621eef6 100644 --- a/src/lib/components/HttpClient.spec.ts +++ b/src/lib/components/HttpClient.spec.ts @@ -105,7 +105,7 @@ describe('HttpClient', () => { data: {}, }); } catch (error: any) { - expect(error.message).toBe('Request Error, Status: 404'); + expect(error.message).toBe('Request failed with status code 404: Not Found'); } }); }); diff --git a/src/lib/components/HttpClient.ts b/src/lib/components/HttpClient.ts index 5e8060c..7954a1a 100644 --- a/src/lib/components/HttpClient.ts +++ b/src/lib/components/HttpClient.ts @@ -91,12 +91,13 @@ export class HttpClient { public async fetch(config: HttpRequest): Promise { const response = await this.request(config); - if (response.status >= 200 && response.status < 400 || response.status === 410) { - return { - data: response.data, - status: response.status, - }; - } - throw new Error (`Request Error, Status: ${response.status}`); + + return this.request(config).then((response) => { + if (response.status >= 200 && response.status < 300) { + return response; + } else { + throw new Error(`Request failed with status code ${response.status}: ${response.data}`); + } + }); } } From 6bace8b40a90d5de4405d2ed25ae07d331d0d61e Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Thu, 20 Jan 2022 12:45:49 +0000 Subject: [PATCH 3/9] feat: stringify non string data in HttpClient --- src/lib/components/HttpClient.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/components/HttpClient.ts b/src/lib/components/HttpClient.ts index 7954a1a..710d6b7 100644 --- a/src/lib/components/HttpClient.ts +++ b/src/lib/components/HttpClient.ts @@ -92,10 +92,13 @@ export class HttpClient { public async fetch(config: HttpRequest): Promise { const response = await this.request(config); - return this.request(config).then((response) => { + return this.request(config).then((response: any) => { if (response.status >= 200 && response.status < 300) { return response; } else { + if (typeof response.data === 'object') { + response.data = JSON.stringify(response.data) + } throw new Error(`Request failed with status code ${response.status}: ${response.data}`); } }); From bebeaf526cfa0197fd10943fc3f16850ce809d94 Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Fri, 21 Jan 2022 09:44:18 +0000 Subject: [PATCH 4/9] feat: add HttpError to client --- src/lib/components/HttpClient.ts | 11 ++++++++--- src/lib/components/HttpError.ts | 12 ++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/lib/components/HttpError.ts diff --git a/src/lib/components/HttpClient.ts b/src/lib/components/HttpClient.ts index 710d6b7..17a5e61 100644 --- a/src/lib/components/HttpClient.ts +++ b/src/lib/components/HttpClient.ts @@ -1,4 +1,5 @@ import { ClientConnection } from 'message-event-channel'; +import { HttpError } from './HttpError'; /** * @hidden @@ -96,10 +97,14 @@ export class HttpClient { if (response.status >= 200 && response.status < 300) { return response; } else { - if (typeof response.data === 'object') { - response.data = JSON.stringify(response.data) + if (typeof response.data !== 'string') { + response.data = JSON.stringify(response.data); } - throw new Error(`Request failed with status code ${response.status}: ${response.data}`); + throw new HttpError( + `Request failed with status code ${response.status}: ${response.data}`, + config, + response + ); } }); } diff --git a/src/lib/components/HttpError.ts b/src/lib/components/HttpError.ts new file mode 100644 index 0000000..508885c --- /dev/null +++ b/src/lib/components/HttpError.ts @@ -0,0 +1,12 @@ +import { HttpRequest, HttpResponse } from './HttpClient'; + +export class HttpError extends Error { + constructor( + message: string, + public readonly request?: HttpRequest, + public readonly response?: HttpResponse + ) { + super(message); + Object.setPrototypeOf(this, new.target.prototype); + } +} From 3197f54f5450583c645cec3d3df019269242dd64 Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Mon, 24 Jan 2022 09:42:43 +0000 Subject: [PATCH 5/9] feat: remove response data from error --- src/lib/components/HttpClient.spec.ts | 2 +- src/lib/components/HttpClient.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/components/HttpClient.spec.ts b/src/lib/components/HttpClient.spec.ts index 621eef6..fa08ecf 100644 --- a/src/lib/components/HttpClient.spec.ts +++ b/src/lib/components/HttpClient.spec.ts @@ -105,7 +105,7 @@ describe('HttpClient', () => { data: {}, }); } catch (error: any) { - expect(error.message).toBe('Request failed with status code 404: Not Found'); + expect(error.message).toBe('Request failed with status code 404'); } }); }); diff --git a/src/lib/components/HttpClient.ts b/src/lib/components/HttpClient.ts index 17a5e61..fa381fa 100644 --- a/src/lib/components/HttpClient.ts +++ b/src/lib/components/HttpClient.ts @@ -101,7 +101,7 @@ export class HttpClient { response.data = JSON.stringify(response.data); } throw new HttpError( - `Request failed with status code ${response.status}: ${response.data}`, + `Request failed with status code ${response.status}`, config, response ); From 01cdf0fb3b6adb3d2d5755c21a2a6615e62d82ec Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Mon, 24 Jan 2022 09:54:32 +0000 Subject: [PATCH 6/9] feat: fix typo --- src/lib/components/HttpClient.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/components/HttpClient.spec.ts b/src/lib/components/HttpClient.spec.ts index fa08ecf..12dc047 100644 --- a/src/lib/components/HttpClient.spec.ts +++ b/src/lib/components/HttpClient.spec.ts @@ -69,7 +69,7 @@ describe('HttpClient', () => { }); }); - it('should fetch amd returmn status amd data', async () => { + it('should fetch amd return status amd data', async () => { jest.spyOn(connection, 'request').mockReturnValue( Promise.resolve({ status: 200, From 83d21c0e6f5f160106bb9c70107ae4d250b0258c Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Mon, 24 Jan 2022 09:54:50 +0000 Subject: [PATCH 7/9] feat: fix typo --- src/lib/components/HttpClient.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/components/HttpClient.spec.ts b/src/lib/components/HttpClient.spec.ts index 12dc047..c48635b 100644 --- a/src/lib/components/HttpClient.spec.ts +++ b/src/lib/components/HttpClient.spec.ts @@ -69,7 +69,7 @@ describe('HttpClient', () => { }); }); - it('should fetch amd return status amd data', async () => { + it('should fetch amd return status and data', async () => { jest.spyOn(connection, 'request').mockReturnValue( Promise.resolve({ status: 200, From 603146ec30f4156e82b271addd73355a89f4d01d Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Mon, 24 Jan 2022 09:55:05 +0000 Subject: [PATCH 8/9] feat: fix typo --- src/lib/components/HttpClient.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/components/HttpClient.spec.ts b/src/lib/components/HttpClient.spec.ts index c48635b..116dade 100644 --- a/src/lib/components/HttpClient.spec.ts +++ b/src/lib/components/HttpClient.spec.ts @@ -69,7 +69,7 @@ describe('HttpClient', () => { }); }); - it('should fetch amd return status and data', async () => { + it('should fetch and return status and data', async () => { jest.spyOn(connection, 'request').mockReturnValue( Promise.resolve({ status: 200, From b3ba60b7b5376004a8a931abe132c4b2b35627fa Mon Sep 17 00:00:00 2001 From: Luke Tyson Date: Mon, 24 Jan 2022 10:46:30 +0000 Subject: [PATCH 9/9] feat: remove type check on response --- src/lib/components/HttpClient.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/lib/components/HttpClient.ts b/src/lib/components/HttpClient.ts index fa381fa..b246e89 100644 --- a/src/lib/components/HttpClient.ts +++ b/src/lib/components/HttpClient.ts @@ -97,9 +97,6 @@ export class HttpClient { if (response.status >= 200 && response.status < 300) { return response; } else { - if (typeof response.data !== 'string') { - response.data = JSON.stringify(response.data); - } throw new HttpError( `Request failed with status code ${response.status}`, config,