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:
Zakary Timson 2024-04-11 23:19:36 -04:00
parent c8ccc19996
commit 85fc5f3017
7 changed files with 61 additions and 61 deletions

9
package-lock.json generated
View File

@ -1,16 +1,15 @@
{ {
"name": "@ztimson/js-utilities", "name": "@ztimson/utils",
"version": "0.2.1", "version": "0.1.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@ztimson/js-utilities", "name": "@ztimson/utils",
"version": "0.2.1", "version": "0.1.2",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/jest": "^29.5.12", "@types/jest": "^29.5.12",
"@types/node": "^18.14.0",
"jest": "^29.7.0", "jest": "^29.7.0",
"jest-junit": "^16.0.0", "jest-junit": "^16.0.0",
"ts-jest": "^29.1.2", "ts-jest": "^29.1.2",

View File

@ -1,7 +1,7 @@
{ {
"name": "@ztimson/js-utilities", "name": "@ztimson/utils",
"version": "0.4.0", "version": "0.2.0",
"description": "JavaScript Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",
"private": false, "private": false,
@ -9,18 +9,17 @@
"type": "git", "type": "git",
"url": "https://git.zakscode.com/ztimson/js-utilities" "url": "https://git.zakscode.com/ztimson/js-utilities"
}, },
"main": "./dist/js-utilities.js", "main": "./dist/utils.cjs",
"module": "./dist/js-utilities.mjs", "module": "./dist/utils.mjs",
"types": "./dist/src/index.d.ts", "types": "./dist/index.d.ts",
"scripts": { "scripts": {
"build": "npx vite build", "build": "npx tsc && npx vite build",
"test": "npx jest", "test": "npx jest",
"test:coverage": "npx jest --coverage", "test:coverage": "npx jest --coverage",
"watch": "npx vite build --watch" "watch": "npx vite build --watch"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^29.5.12", "@types/jest": "^29.5.12",
"@types/node": "^18.14.0",
"jest": "^29.7.0", "jest": "^29.7.0",
"jest-junit": "^16.0.0", "jest-junit": "^16.0.0",
"ts-jest": "^29.1.2", "ts-jest": "^29.1.2",
@ -28,12 +27,6 @@
"vite": "^5.0.12", "vite": "^5.0.12",
"vite-plugin-dts": "^3.7.2" "vite-plugin-dts": "^3.7.2"
}, },
"exports": {
".": {
"import": "./dist/js-utilities.mjs",
"require": "./dist/js-utilities.js"
}
},
"files": [ "files": [
"dist" "dist"
] ]

View File

@ -1,6 +1,6 @@
import {XHR} from './xhr'; import {XHR} from './xhr';
XHR.addInterceptor((resp: Response, next: () => {}) => { XHR.addInterceptor((resp: Response, next: () => void) => {
if(resp.status == 200) return next(); if(resp.status == 200) return next();
if(resp.status == 400) throw new BadRequestError(resp.statusText); if(resp.status == 400) throw new BadRequestError(resp.statusText);
if(resp.status == 401) throw new UnauthorizedError(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 { export function includes(target: any, values: any, allowMissing = false): boolean {
if(target == undefined) return allowMissing; 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; const type = typeof values;
if(type != typeof target) return false; if(type != typeof target) return false;
if(type == 'object') { if(type == 'object') {
@ -140,3 +140,16 @@ export function isEqual(a: any, b: any): boolean {
if(keys.length != Object.keys(b).length) return false; if(keys.length != Object.keys(b).length) return false;
return Object.keys(a).every(key => isEqual(a[key], b[key])); 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'; import {clean} from './objects';
export type Interceptor = (request: Response, next: () => void) => void; export type Interceptor = (request: Response, next: () => void) => void;
@ -11,27 +10,23 @@ export type RequestOptions = {
[key: string]: any; [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 = { export type XhrOptions = {
headers?: {[key: string | symbol]: string | null | undefined};
interceptors?: Interceptor[]; interceptors?: Interceptor[];
url?: string; url?: string;
} }
export class XHR extends TypedEmitter<XhrEvents> { export class XHR {
private static headers: {[key: string]: string} = {};
private static interceptors: {[key: string]: Interceptor} = {}; private static interceptors: {[key: string]: Interceptor} = {};
private headers: {[key: string]: string} = {} static headers: {[key: string]: string | null | undefined} = {};
private interceptors: {[key: string]: Interceptor} = {} private interceptors: {[key: string]: Interceptor} = {}
headers: {[key: string]: string | null | undefined} = {}
constructor(public readonly opts: XhrOptions = {}) { constructor(public readonly opts: XhrOptions = {}) {
super(); this.headers = opts.headers || {};
if(opts.interceptors) { if(opts.interceptors) {
opts.interceptors.forEach(i => XHR.addInterceptor(i)); opts.interceptors.forEach(i => XHR.addInterceptor(i));
} }
@ -72,17 +67,13 @@ export class XHR extends TypedEmitter<XhrEvents> {
await wait; await wait;
} }
this.emit(`${resp.status}`, resp, opts);
if(!resp.ok) throw Error(resp.statusText); 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('application/json')) return await resp.json();
if(resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text(); if(resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
return resp; return resp;
}).catch((err: Error) => { }).catch((err: Error) => {
this.emit('REJECTED', err, opts);
throw err; throw err;
}); });
this.emit('REQUEST', req, opts)
return req; return req;
} }
} }

View File

@ -1,19 +1,20 @@
{ {
"include": [ "include": ["src"],
"src/**/*"
],
"compilerOptions": { "compilerOptions": {
"composite": true, "target": "ES2020",
"declaration": true, "useDefineForClassFields": true,
"declarationMap": true, "module": "ESNext",
"experimentalDecorators": true, "lib": ["ESNext", "DOM", "DOM.Iterable"],
"esModuleInterop": true, "skipLibCheck": true,
"inlineSourceMap": true,
"lib": ["ESNext", "DOM"], /* Bundler mode */
"module": "ES6", "moduleResolution": "bundler",
"moduleResolution": "Node", "allowImportingTsExtensions": true,
"outDir": "./dist", "resolveJsonModule": true,
"strict": true, "isolatedModules": true,
"target": "ESNext" "noEmit": true,
/* Linting */
"strict": true
} }
} }

View File

@ -6,12 +6,15 @@ export default defineConfig({
build: { build: {
lib: { lib: {
entry: resolve(process.cwd(), 'src/index.ts'), entry: resolve(process.cwd(), 'src/index.ts'),
name: 'js-utilities', name: 'utils',
fileName: (module, entryName) => { fileName: (module, entryName) => {
if(module == 'es') return 'js-utilities.mjs'; if(module == 'es') return 'utils.mjs';
if(module == 'umd') return 'js-utilities.js'; if(module == 'umd') return 'utils.cjs';
} }
}, },
emptyOutDir: true,
minify: true,
sourcemap: true
}, },
plugins: [dts()], plugins: [dts()],
}); });