Added mixin
This commit is contained in:
@ -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);
|
||||
|
@ -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)
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
23
src/xhr.ts
23
src/xhr.ts
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user