Added mixin
This commit is contained in:
parent
c8ccc19996
commit
85fc5f3017
9
package-lock.json
generated
9
package-lock.json
generated
@ -1,16 +1,15 @@
|
||||
{
|
||||
"name": "@ztimson/js-utilities",
|
||||
"version": "0.2.1",
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.1.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@ztimson/js-utilities",
|
||||
"version": "0.2.1",
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.1.2",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "^18.14.0",
|
||||
"jest": "^29.7.0",
|
||||
"jest-junit": "^16.0.0",
|
||||
"ts-jest": "^29.1.2",
|
||||
|
21
package.json
21
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@ztimson/js-utilities",
|
||||
"version": "0.4.0",
|
||||
"description": "JavaScript Utility library",
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.2.0",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
"private": false,
|
||||
@ -9,18 +9,17 @@
|
||||
"type": "git",
|
||||
"url": "https://git.zakscode.com/ztimson/js-utilities"
|
||||
},
|
||||
"main": "./dist/js-utilities.js",
|
||||
"module": "./dist/js-utilities.mjs",
|
||||
"types": "./dist/src/index.d.ts",
|
||||
"main": "./dist/utils.cjs",
|
||||
"module": "./dist/utils.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "npx vite build",
|
||||
"build": "npx tsc && npx vite build",
|
||||
"test": "npx jest",
|
||||
"test:coverage": "npx jest --coverage",
|
||||
"watch": "npx vite build --watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.12",
|
||||
"@types/node": "^18.14.0",
|
||||
"jest": "^29.7.0",
|
||||
"jest-junit": "^16.0.0",
|
||||
"ts-jest": "^29.1.2",
|
||||
@ -28,12 +27,6 @@
|
||||
"vite": "^5.0.12",
|
||||
"vite-plugin-dts": "^3.7.2"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/js-utilities.mjs",
|
||||
"require": "./dist/js-utilities.js"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
]
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,20 @@
|
||||
{
|
||||
"include": [
|
||||
"src/**/*"
|
||||
],
|
||||
"include": ["src"],
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"experimentalDecorators": true,
|
||||
"esModuleInterop": true,
|
||||
"inlineSourceMap": true,
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"module": "ES6",
|
||||
"moduleResolution": "Node",
|
||||
"outDir": "./dist",
|
||||
"strict": true,
|
||||
"target": "ESNext"
|
||||
"target": "ES2020",
|
||||
"useDefineForClassFields": true,
|
||||
"module": "ESNext",
|
||||
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
||||
"skipLibCheck": true,
|
||||
|
||||
/* Bundler mode */
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
|
||||
/* Linting */
|
||||
"strict": true
|
||||
}
|
||||
}
|
||||
|
@ -1,17 +1,20 @@
|
||||
import { resolve } from 'path';
|
||||
import { defineConfig } from 'vite';
|
||||
import {resolve} from 'path';
|
||||
import {defineConfig} from 'vite';
|
||||
import dts from 'vite-plugin-dts';
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
lib: {
|
||||
entry: resolve(process.cwd(), 'src/index.ts'),
|
||||
name: 'js-utilities',
|
||||
name: 'utils',
|
||||
fileName: (module, entryName) => {
|
||||
if(module == 'es') return 'js-utilities.mjs';
|
||||
if(module == 'umd') return 'js-utilities.js';
|
||||
if(module == 'es') return 'utils.mjs';
|
||||
if(module == 'umd') return 'utils.cjs';
|
||||
}
|
||||
},
|
||||
emptyOutDir: true,
|
||||
minify: true,
|
||||
sourcemap: true
|
||||
},
|
||||
plugins: [dts()],
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user