Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
a0f0699a85 | |||
adcd6eaf79 | |||
afb6ca0803 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.15.3",
|
"version": "0.15.6",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
@ -172,3 +172,41 @@ export class GatewayTimeoutError extends CustomError {
|
|||||||
return (<any>err).constructor.code == this.code;
|
return (<any>err).constructor.code == this.code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the correct error object from a status code
|
||||||
|
* @param {number} code Will be converted to respective error (ex. 404 -> NotFoundError)
|
||||||
|
* @param {string} message Override default error message
|
||||||
|
* @return {CustomError} The proper error type
|
||||||
|
*/
|
||||||
|
export function errorFromCode(code: number, message?: string) {
|
||||||
|
if(code >= 200 && code < 300) return null;
|
||||||
|
switch(code) {
|
||||||
|
case 400:
|
||||||
|
return new BadRequestError(message);
|
||||||
|
case 401:
|
||||||
|
return new UnauthorizedError(message);
|
||||||
|
case 402:
|
||||||
|
return new PaymentRequiredError(message);
|
||||||
|
case 403:
|
||||||
|
return new ForbiddenError(message);
|
||||||
|
case 404:
|
||||||
|
return new NotFoundError(message);
|
||||||
|
case 405:
|
||||||
|
return new MethodNotAllowedError(message);
|
||||||
|
case 406:
|
||||||
|
return new NotAcceptableError(message);
|
||||||
|
case 500:
|
||||||
|
return new InternalServerError(message);
|
||||||
|
case 501:
|
||||||
|
return new NotImplementedError(message);
|
||||||
|
case 502:
|
||||||
|
return new BadGatewayError(message);
|
||||||
|
case 503:
|
||||||
|
return new ServiceUnavailableError(message);
|
||||||
|
case 504:
|
||||||
|
return new GatewayTimeoutError(message);
|
||||||
|
default:
|
||||||
|
return new CustomError(message, code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -10,7 +10,7 @@ export type HttpRequestOptions = {
|
|||||||
decode?: boolean;
|
decode?: boolean;
|
||||||
fragment?: string;
|
fragment?: string;
|
||||||
headers?: {[key: string | symbol]: string | null | undefined};
|
headers?: {[key: string | symbol]: string | null | undefined};
|
||||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
method?: 'GET' | 'POST' | 'PATCH' | 'PUT' | 'DELETE';
|
||||||
query?: {key: string, value: string}[] | {[key: string]: string};
|
query?: {key: string, value: string}[] | {[key: string]: string};
|
||||||
url?: string;
|
url?: string;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
|
@ -6,6 +6,14 @@ export function createHex(length: number) {
|
|||||||
return Array(length).fill(null).map(() => Math.round(Math.random() * 0xF).toString(16)).join('');
|
return Array(length).fill(null).map(() => Math.round(Math.random() * 0xF).toString(16)).join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatBytes(bytes: number, decimals = 2) {
|
||||||
|
if (bytes === 0) return '0 Bytes';
|
||||||
|
const k = 1024;
|
||||||
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String of all letters
|
* String of all letters
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user