Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
added util class
Browse files Browse the repository at this point in the history
  • Loading branch information
Keimeno committed Jan 8, 2020
1 parent b5e7622 commit 84ea12f
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import 'unfetch/polyfill';
import NeojaxHeaders from 'types/headers';
import { NeojaxConvolutedResponse } from 'types/response';
import NeojaxOptions from 'types/options';

class Util {
/**
* Parses fetch headers into useable NeojaxHeaders
*
* @param headers
*/
public static parseHeadersToNeojaxHeaders(headers: Headers): NeojaxHeaders {
const newHeaders: NeojaxHeaders = {};

headers.forEach((value: string, name: string) => {
newHeaders[name] = value;
});

return newHeaders;
}

/**
* This is the function, that sends out every request.
*
* NeojaxOptions priority:
* 1. NeojaxOptions specified in every single request
* 2. NeojaxOptions set when creating a new instance through the create method
* 3. Defaultoptions specified in class
*
* @param url
* @param method
* @param data
* @param options
*/
public static async sendRequest(
url: string,
method: string,
data: object | null,
defaultOptions: NeojaxOptions,
options?: NeojaxOptions,
): Promise<NeojaxConvolutedResponse> {
const finalUrl = this.retrieveUrl(defaultOptions.baseUrl || '', (options || {}).baseUrl || '', url);
const headers = this.retrieveHeaders(defaultOptions.headers || {}, (options || {}).headers || {});

// builds init
const init: RequestInit = {
method,
headers: {
...headers
}
};

if (data) {
init.body = JSON.stringify(data);
}

// get back the response
const response = await fetch(finalUrl, init);

// get the headers
const resHeaders = Util.parseHeadersToNeojaxHeaders(response.headers);

return {
status: response.status,
headers: resHeaders,
url: response.url,
success: response.ok,
message: response.statusText,
data: await this.retrieveData(response)
};
}

/**
* retrieves the data from the response object
*
* @param response
*/
private static async retrieveData(response: Response): Promise<object | string> {
try {
return await response.json();
} catch (e) {
try {
return await response.text();
} catch (e) {
// return an empty string, so that users can check
// if it's falsy or not.
return '';
}
}
}

/**
* retrieve the requested url.
* this depends on the options priority
*
* @param defaultUrl
* @param optionsUrl
* @param url
*/
private static retrieveUrl(defaultUrl: string, optionsUrl: string, url: string): string {
if (optionsUrl) return optionsUrl + url;
return defaultUrl + url;
}

/**
* returns the headers, that are set for this request
* this as well depends on the headers priority
*
* @param defaultHeaders
* @param optionsHeaders
*/
private static retrieveHeaders(defaultHeaders: NeojaxHeaders, optionsHeaders: NeojaxHeaders): NeojaxHeaders {
return {
...defaultHeaders,
...optionsHeaders
};
}
}

export default Util;

0 comments on commit 84ea12f

Please sign in to comment.