Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a fallback param to utility functions #107

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/utils-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,46 @@ import {
reasonPhraseToStatusCode,
} from './utils';

const getDefaultFallback = (message: string) => <T>(param?: T): any => {
if (!param) {
throw new Error(`${message} ${param}`);
}

return param;
}

const defaultReasonPhraseFallback = getDefaultFallback(`Status code does not exist:`);

const defaultStatusCodeFallback = getDefaultFallback(`Reason phrase does not exist:`);

/**
* Returns the reason phrase for the given status code.
* If the given status code does not exist, an error is thrown.
* If the given status code does not exist, a fallback function is called passing the status code provided,
* which by default throws an error indicating that the status code it's missing.
*
* @param {number|string} statusCode The HTTP status code
* @param {function} [fallback] Optional fallback function that it's called if the status code is missing
* @returns {string} The associated reason phrase (e.g. "Bad Request", "OK")
* */
export function getReasonPhrase(statusCode: (number | string)): (string) {
export function getReasonPhrase(statusCode: (number | string), fallback = defaultReasonPhraseFallback): (string) {
const result = statusCodeToReasonPhrase[statusCode.toString()];
if (!result) {
throw new Error(`Status code does not exist: ${statusCode}`);
}
return result;

return result ? result : fallback(statusCode);
}

/**
* Returns the status code for the given reason phrase.
* If the given reason phrase does not exist, undefined is returned.
* If the given reason phrase does not exist, a fallback function is called passing the reason phrase provided,
* which by default throws an error indicating that the reason code it's missing.
*
* @param {string} reasonPhrase The HTTP reason phrase (e.g. "Bad Request", "OK")
* @param {function} [fallback] Optional fallback function that it's called if the reason phrase is missing
* @returns {string} The associated status code
* */
export function getStatusCode(reasonPhrase: string): (number) {
export function getStatusCode(reasonPhrase: string, fallback = defaultStatusCodeFallback): (number) {
const result = reasonPhraseToStatusCode[reasonPhrase];
if (!result) {
throw new Error(`Reason phrase does not exist: ${reasonPhrase}`);
}
return result;

return result ? result : fallback(reasonPhrase);
}

/**
Expand Down