-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdac1db
commit 8d2f53c
Showing
15 changed files
with
221 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface ServerErrorModel { | ||
errors: string[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import EventEmitter from 'eventemitter3'; | ||
import { EventTypes } from '../models/EventTypes'; | ||
|
||
const eventEmitter = new EventEmitter(); | ||
|
||
const EventHub = { | ||
on: (event: EventTypes, fn: (value: object) => void) => eventEmitter.on(event, fn), | ||
once: (event: EventTypes, fn: (value: object) => void) => eventEmitter.once(event, fn), | ||
off: (event: EventTypes, fn?: (value: object) => void) => eventEmitter.off(event, fn), | ||
emit: (event: EventTypes, payload: object) => eventEmitter.emit(event, payload) | ||
} | ||
|
||
Object.freeze(EventHub); | ||
|
||
export default EventHub; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,92 @@ | ||
import {settings as appSettings} from './AppSettings'; | ||
import { ServerErrorModel } from '../models/ServerErrorModel'; | ||
import { settings as appSettings } from './AppSettings'; | ||
|
||
export class HttpUtility { | ||
public static makeUrl(relativePart = ''){ | ||
public static makeUrl(relativePart = '') { | ||
return HttpUtility.trimEndSlash(appSettings.baseUrl) + "/" + HttpUtility.trimStartSlash(relativePart); | ||
} | ||
private static trimEndSlash(source: string){ | ||
|
||
private static trimEndSlash(source: string) { | ||
return source.replace(/\/+$/g, ''); | ||
} | ||
|
||
private static trimStartSlash(source: string){ | ||
private static trimStartSlash(source: string) { | ||
return source.replace(/^\/+/g, ''); | ||
} | ||
|
||
public static async post(url = '', data = {}){ | ||
// Default options are marked with * | ||
const response = await fetch(url, { | ||
method: 'POST', // *GET, POST, PUT, DELETE, etc. | ||
mode: 'cors', // no-cors, *cors, same-origin | ||
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached | ||
credentials: 'same-origin', // include, *same-origin, omit | ||
headers: { | ||
'Content-Type': 'application/json' | ||
// 'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
redirect: 'follow', // manual, *follow, error | ||
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url | ||
body: JSON.stringify(data) // body data type must match "Content-Type" header | ||
}); | ||
return response.json(); // parses JSON response into native JavaScript objects | ||
public static async post(url = '', data = {}) { | ||
try { | ||
// Default options are marked with * | ||
const response = await fetch(url, { | ||
method: 'POST', // *GET, POST, PUT, DELETE, etc. | ||
mode: 'cors', // no-cors, *cors, same-origin | ||
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached | ||
credentials: 'same-origin', // include, *same-origin, omit | ||
headers: { | ||
'Content-Type': 'application/json' | ||
// 'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
redirect: 'follow', // manual, *follow, error | ||
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url | ||
body: JSON.stringify(data) // body data type must match "Content-Type" header | ||
}); | ||
console.log(response.ok); | ||
|
||
if (response.ok) { | ||
return response.json(); | ||
} else { | ||
const parsedBody = await response.json(); // parses JSON response into native JavaScript objects | ||
if ((parsedBody as ServerErrorModel)?.errors?.length > 0) { | ||
return Promise.reject(parsedBody); | ||
} else { | ||
return Promise.reject({ | ||
errors: ["Unexpected server error"] | ||
} as ServerErrorModel); | ||
} | ||
} | ||
|
||
} | ||
catch { | ||
return { | ||
errors: ["Network error"] | ||
} as ServerErrorModel; | ||
} | ||
} | ||
|
||
public static async get(url = ''){ | ||
public static async get(url = '') { | ||
// Default options are marked with * | ||
const response = await fetch(url, { | ||
method: 'GET', // *GET, POST, PUT, DELETE, etc. | ||
mode: 'cors', // no-cors, *cors, same-origin | ||
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached | ||
credentials: 'same-origin', // include, *same-origin, omit | ||
headers: { | ||
'Content-Type': 'application/json' | ||
// 'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
redirect: 'follow', // manual, *follow, error | ||
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url | ||
}); | ||
return response.json(); // parses JSON response into native JavaScript objects | ||
try { | ||
const response = await fetch(url, { | ||
method: 'GET', // *GET, POST, PUT, DELETE, etc. | ||
mode: 'cors', // no-cors, *cors, same-origin | ||
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached | ||
credentials: 'same-origin', // include, *same-origin, omit | ||
headers: { | ||
'Content-Type': 'application/json' | ||
// 'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
redirect: 'follow', // manual, *follow, error | ||
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url | ||
}); | ||
|
||
if (response.ok) { | ||
return response.json(); | ||
} else { | ||
const parsedBody = await response.json(); // parses JSON response into native JavaScript objects | ||
if ((parsedBody as ServerErrorModel)?.errors?.length > 0) { | ||
return Promise.reject(parsedBody); | ||
} else { | ||
return Promise.reject({ | ||
errors: ["Unexpected server error"] | ||
} as ServerErrorModel); | ||
} | ||
} | ||
|
||
} | ||
catch { | ||
return Promise.reject({ | ||
errors: ["Network error"] | ||
} as ServerErrorModel); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum EventTypes { | ||
FETCH_ERROR = "FETCH_ERROR" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface ServerErrorModel { | ||
errors: string[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.