Created
September 2, 2018 12:08
-
-
Save yayoc/1dabb1728a5492374e88a178d37cf7f2 to your computer and use it in GitHub Desktop.
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
namespace JSONAPIClientUtils { | |
export const queryParams = (params: { [key: string]: any }) => { | |
return Object.keys(params) | |
.map(k => encodeURIComponent(k) + "=" + encodeURIComponent(params[k])) | |
.join("&"); | |
}; | |
const DEFAULT_HEADERS = { | |
Accept: "application/json", | |
"Content-Type": "application/json" | |
}; | |
export const mergeHeaders = ( | |
headers: object | undefined | |
): Record<string, string> => { | |
if (!headers) { | |
return DEFAULT_HEADERS; | |
} | |
return Object.assign({}, DEFAULT_HEADERS, headers); | |
}; | |
} | |
enum HTTP_METHOD { | |
GET = "GET", | |
POST = "POST", | |
PUT = "PUT", | |
PATCH = "PATCH", | |
DELETE = "DELETE" | |
} | |
interface JSONAPIClientInterface { | |
readonly baseUrl: string; | |
get<T>(endpoint: string, headers?: object, params?: object): Promise<T>; | |
post<T>(endpoint: string, headers?: object, body?: object): Promise<T>; | |
put<T>(endpoint: string, headers?: object, body?: object): Promise<T>; | |
patch<T>(endpoint: string, headers?: object, body?: object): Promise<T>; | |
delete<T>(endpoint: string, headers?: object): Promise<T>; | |
} | |
export class JSONAPIClient implements JSONAPIClientInterface { | |
readonly baseUrl: string; | |
constructor(baseUrl: string) { | |
this.baseUrl = baseUrl; | |
} | |
get<T>(endpoint: string, headers?: object, params?: object): Promise<T> { | |
let url = [this.baseUrl, endpoint].join("/"); | |
if (params) { | |
url += | |
(url.indexOf("?") === -1 ? "?" : "&") + | |
JSONAPIClientUtils.queryParams(params); | |
} | |
return fetch(url, { | |
headers: JSONAPIClientUtils.mergeHeaders(headers), | |
method: HTTP_METHOD.GET | |
}).then(res => res.json()); | |
} | |
post<T>(endpoint: string, headers?: object, body?: object): Promise<T> { | |
let url = [this.baseUrl, endpoint].join("/"); | |
return fetch(url, { | |
headers: JSONAPIClientUtils.mergeHeaders(headers), | |
method: HTTP_METHOD.POST, | |
body: JSON.stringify(body) | |
}).then(res => res.json()); | |
} | |
put<T>(endpoint: string, headers?: object, body?: object): Promise<T> { | |
let url = [this.baseUrl, endpoint].join("/"); | |
return fetch(url, { | |
headers: JSONAPIClientUtils.mergeHeaders(headers), | |
method: HTTP_METHOD.PUT, | |
body: JSON.stringify(body) | |
}).then(res => res.json()); | |
} | |
patch<T>(endpoint: string, headers?: object, body?: object): Promise<T> { | |
let url = [this.baseUrl, endpoint].join("/"); | |
return fetch(url, { | |
headers: JSONAPIClientUtils.mergeHeaders(headers), | |
method: HTTP_METHOD.PATCH, | |
body: JSON.stringify(body) | |
}).then(res => res.json()); | |
} | |
delete<T>(endpoint: string, headers?: object): Promise<T> { | |
let url = [this.baseUrl, endpoint].join("/"); | |
return fetch(url, { | |
headers: JSONAPIClientUtils.mergeHeaders(headers), | |
method: HTTP_METHOD.DELETE | |
}).then(res => res.json()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment