Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
1d9ad2ef63 | |||
973a884b3e | |||
6f60f8bd94 | |||
07606cd996 | |||
3ca956e531 |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.4.0",
|
||||
"version": "0.10.4",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.4.0",
|
||||
"version": "0.10.4",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.10.0",
|
||||
"version": "0.11.1",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
@ -12,6 +12,12 @@
|
||||
"main": "./dist/utils.cjs",
|
||||
"module": "./dist/utils.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./dist/utils.cjs",
|
||||
"import": "./dist/utils.mjs"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"build": "npx tsc && npx vite build",
|
||||
"test": "npx jest",
|
||||
|
@ -1,15 +1,3 @@
|
||||
import {XHR} from './xhr';
|
||||
|
||||
XHR.addInterceptor((resp: Response, next: () => void) => {
|
||||
if(resp.status == 200) return next();
|
||||
if(resp.status == 400) throw new BadRequestError(resp.statusText);
|
||||
if(resp.status == 401) throw new UnauthorizedError(resp.statusText);
|
||||
if(resp.status == 403) throw new ForbiddenError(resp.statusText);
|
||||
if(resp.status == 404) throw new NotFoundError(resp.statusText);
|
||||
if(resp.status == 500) throw new InternalServerError(resp.statusText);
|
||||
throw new CustomError(resp.statusText, resp.status);
|
||||
});
|
||||
|
||||
export class CustomError extends Error {
|
||||
static code = 500;
|
||||
|
||||
|
24
src/xhr.ts
24
src/xhr.ts
@ -4,6 +4,8 @@ export type Interceptor = (request: Response, next: () => void) => void;
|
||||
|
||||
export type RequestOptions = {
|
||||
url?: string;
|
||||
fragment?: string;
|
||||
query?: {key: string, value: string}[] | {[key: string]: string};
|
||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
||||
body?: any;
|
||||
headers?: {[key: string | symbol]: string | null | undefined};
|
||||
@ -47,7 +49,13 @@ export class XHR {
|
||||
|
||||
async request<T>(opts: RequestOptions = {}): Promise<T> {
|
||||
if(!this.opts.url && !opts.url) throw new Error('URL needs to be set');
|
||||
const url = (opts.url?.startsWith('http') ? opts.url : (this.opts.url || '') + (opts.url || '')).replace(/([^:]\/)\/+/g, '$1');
|
||||
let url = (opts.url?.startsWith('http') ? opts.url : (this.opts.url || '') + (opts.url || '')).replace(/([^:]\/)\/+/g, '$1');
|
||||
if(opts.fragment) url.includes('#') ? url.replace(/#.*(\?|\n)/g, (match, arg1) => `#${opts.fragment}${arg1}`) : url += '#' + opts.fragment;
|
||||
if(opts.query) {
|
||||
const q = Array.isArray(opts.query) ? opts.query :
|
||||
Object.keys(opts.query).map(k => ({key: k, value: (<any>opts.query)[k]}))
|
||||
url += (url.includes('?') ? '&' : '?') + q.map(q => `${q.key}=${q.value}`).join('&');
|
||||
}
|
||||
|
||||
// Prep headers
|
||||
const headers = <any>clean({
|
||||
@ -67,10 +75,16 @@ export class XHR {
|
||||
await new Promise<void>(res => fn(resp, () => res()));
|
||||
}
|
||||
|
||||
if(!resp.ok) throw new Error(resp.statusText);
|
||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
||||
return resp;
|
||||
const decode = async () => {
|
||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
||||
return resp;
|
||||
}
|
||||
|
||||
const payload = await decode();
|
||||
if(resp.ok) return payload;
|
||||
const text = resp.statusText || (typeof payload == 'string' ? payload : null);
|
||||
throw (text ? new Error(text) : payload);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user