From 85fc5f30176b44ca4978348cedb55d77b3cf8f67 Mon Sep 17 00:00:00 2001 From: ztimson Date: Thu, 11 Apr 2024 23:19:36 -0400 Subject: [PATCH] Added mixin --- package-lock.json | 9 ++++----- package.json | 21 +++++++-------------- src/errors.ts | 2 +- src/objects.ts | 15 ++++++++++++++- src/xhr.ts | 23 +++++++---------------- tsconfig.json | 39 ++++++++++++++++++++------------------- vite.config.ts | 13 ++++++++----- 7 files changed, 61 insertions(+), 61 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5245e53..6e4776e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 297c431..e7c8499 100644 --- a/package.json +++ b/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" ] diff --git a/src/errors.ts b/src/errors.ts index d603cb1..e99b8bd 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -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); diff --git a/src/objects.ts b/src/objects.ts index 2733e31..8bb2118 100644 --- a/src/objects.ts +++ b/src/objects.ts @@ -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) + ); + }); + }); +} diff --git a/src/xhr.ts b/src/xhr.ts index 9288b4e..23cbc9a 100644 --- a/src/xhr.ts +++ b/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, 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 { - 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 { 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 resp.text(); return resp; }).catch((err: Error) => { - this.emit('REJECTED', err, opts); throw err; }); - this.emit('REQUEST', req, opts) return req; } } diff --git a/tsconfig.json b/tsconfig.json index 4751071..d2cb5fc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,19 +1,20 @@ -{ - "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" - } -} +{ + "include": ["src"], + "compilerOptions": { + "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 + } +} diff --git a/vite.config.ts b/vite.config.ts index 03bab9b..381ec01 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -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()], });