Added mixin
Some checks failed
Build / Build NPM Project (push) Failing after 0s
Build / Tag Version (push) Has been skipped
Build / Publish (push) Has been skipped

This commit is contained in:
2024-04-11 23:19:36 -04:00
parent c8ccc19996
commit 85fc5f3017
7 changed files with 61 additions and 61 deletions

View File

@ -1,6 +1,6 @@
import {XHR} from './xhr';
XHR.addInterceptor((resp: Response, next: () => {}) => {
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);

View File

@ -115,7 +115,7 @@ export function flattenObj(obj: any, parent?: any, result: any = {}) {
*/
export function includes(target: any, values: any, allowMissing = false): boolean {
if(target == undefined) return allowMissing;
if(Array.isArray(values)) return values.findIndex((e, i) => !includes(target[i], values[i], allowMissing)) == -1;
if(Array.isArray(values)) return values.findIndex((e: any, i: number) => !includes(target[i], values[i], allowMissing)) == -1;
const type = typeof values;
if(type != typeof target) return false;
if(type == 'object') {
@ -140,3 +140,16 @@ export function isEqual(a: any, b: any): boolean {
if(keys.length != Object.keys(b).length) return false;
return Object.keys(a).every(key => isEqual(a[key], b[key]));
}
export function mixin(target: any, constructors: any[]) {
constructors.forEach(c => {
Object.getOwnPropertyNames(c.prototype).forEach((name) => {
Object.defineProperty(
target.prototype,
name,
Object.getOwnPropertyDescriptor(c.prototype, name) ||
Object.create(null)
);
});
});
}

View File

@ -1,4 +1,3 @@
import {TypedEmitter, type TypedEvents} from './emitter';
import {clean} from './objects';
export type Interceptor = (request: Response, next: () => void) => void;
@ -11,27 +10,23 @@ export type RequestOptions = {
[key: string]: any;
}
export type XhrEvents = TypedEvents & {
'REQUEST': (request: Promise<any>, options: RequestOptions) => any;
'RESPONSE': (response: Response, options: RequestOptions) => any;
'REJECTED': (response: Error, options: RequestOptions) => any;
};
export type XhrOptions = {
headers?: {[key: string | symbol]: string | null | undefined};
interceptors?: Interceptor[];
url?: string;
}
export class XHR extends TypedEmitter<XhrEvents> {
private static headers: {[key: string]: string} = {};
export class XHR {
private static interceptors: {[key: string]: Interceptor} = {};
private headers: {[key: string]: string} = {}
static headers: {[key: string]: string | null | undefined} = {};
private interceptors: {[key: string]: Interceptor} = {}
headers: {[key: string]: string | null | undefined} = {}
constructor(public readonly opts: XhrOptions = {}) {
super();
this.headers = opts.headers || {};
if(opts.interceptors) {
opts.interceptors.forEach(i => XHR.addInterceptor(i));
}
@ -72,17 +67,13 @@ export class XHR extends TypedEmitter<XhrEvents> {
await wait;
}
this.emit(`${resp.status}`, resp, opts);
if(!resp.ok) throw Error(resp.statusText);
this.emit('RESPONSE', resp, opts);
if(resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
if(resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
return resp;
}).catch((err: Error) => {
this.emit('REJECTED', err, opts);
throw err;
});
this.emit('REQUEST', req, opts)
return req;
}
}