Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
535fc0271e | |||
d587b92ea4 | |||
26c6084052 | |||
874bba59d3 | |||
948fba3a2c | |||
a168b03caf | |||
75160b7dbc | |||
af887a0bfb | |||
7626c20f08 | |||
6998616445 | |||
627757ff6d |
10
package.json
10
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.14.0",
|
"version": "0.14.11",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@ -9,13 +9,13 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.zakscode.com/ztimson/js-utilities"
|
"url": "https://git.zakscode.com/ztimson/js-utilities"
|
||||||
},
|
},
|
||||||
"main": "./dist/utils.cjs",
|
"main": "./dist/index.cjs",
|
||||||
"module": "./dist/utils.mjs",
|
"module": "./dist/index.mjs",
|
||||||
"types": "./dist/index.d.ts",
|
"types": "./dist/index.d.ts",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": {
|
".": {
|
||||||
"import": "./dist/utils.mjs",
|
"import": "./dist/index.mjs",
|
||||||
"require": "./dist/utils.cjs",
|
"require": "./dist/index.cjs",
|
||||||
"types": "./dist/index.d.ts"
|
"types": "./dist/index.d.ts"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
16
src/files.ts
16
src/files.ts
@ -1,4 +1,4 @@
|
|||||||
import {PromiseProgress} from './promise-progress.ts';
|
import {PromiseProgress} from './promise-progress';
|
||||||
|
|
||||||
export function download(href: any, name: string) {
|
export function download(href: any, name: string) {
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
@ -31,24 +31,24 @@ export function fileBrowser(options: {accept?: string, multiple?: boolean} = {})
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function uploadWithProgress(options: {
|
export function uploadWithProgress<T>(options: {
|
||||||
url: string;
|
url: string;
|
||||||
file: File;
|
files: File[];
|
||||||
headers?: {[key: string]: string};
|
headers?: {[key: string]: string};
|
||||||
withCredentials?: boolean;
|
withCredentials?: boolean;
|
||||||
}) {
|
}): PromiseProgress<T> {
|
||||||
return new PromiseProgress((res, rej, prog) => {
|
return new PromiseProgress<T>((res, rej, prog) => {
|
||||||
const xhr = new XMLHttpRequest();
|
const xhr = new XMLHttpRequest();
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', options.file);
|
options.files.forEach(f => formData.append('file', f));
|
||||||
|
|
||||||
xhr.withCredentials = !!options.withCredentials
|
xhr.withCredentials = !!options.withCredentials
|
||||||
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
||||||
xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
xhr.upload.addEventListener('progress', (event) => event.lengthComputable ? prog(event.loaded / event.total) : null);
|
||||||
xhr.upload.addEventListener('load', (resp) => res(resp));
|
xhr.upload.addEventListener('load', (resp) => res(<any>resp));
|
||||||
xhr.upload.addEventListener('error', (err) => rej(err));
|
xhr.upload.addEventListener('error', (err) => rej(err));
|
||||||
|
|
||||||
xhr.open('POST', options.url);
|
xhr.open('POST', options.url);
|
||||||
|
Object.entries(options.headers || {}).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
||||||
xhr.send(formData);
|
xhr.send(formData);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
47
src/http.ts
47
src/http.ts
@ -1,17 +1,18 @@
|
|||||||
import {clean} from './objects';
|
import {clean} from './objects';
|
||||||
import {PromiseProgress} from './promise-progress';
|
import {PromiseProgress} from './promise-progress';
|
||||||
|
|
||||||
export type DecodedResponse<T> = Response & {data?: T}
|
export type DecodedResponse<T> = Response & {data: T | null}
|
||||||
|
|
||||||
export type HttpInterceptor = (response: Response, next: () => void) => void;
|
export type HttpInterceptor = (response: Response, next: () => void) => void;
|
||||||
|
|
||||||
export type HttpRequestOptions = {
|
export type HttpRequestOptions = {
|
||||||
url?: string;
|
|
||||||
fragment?: string;
|
|
||||||
query?: {key: string, value: string}[] | {[key: string]: string};
|
|
||||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
||||||
body?: any;
|
body?: any;
|
||||||
|
decode?: boolean;
|
||||||
|
fragment?: string;
|
||||||
headers?: {[key: string | symbol]: string | null | undefined};
|
headers?: {[key: string | symbol]: string | null | undefined};
|
||||||
|
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
||||||
|
query?: {key: string, value: string}[] | {[key: string]: string};
|
||||||
|
url?: string;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +71,7 @@ export class Http {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if(typeof opts.body == 'object' && opts.body != null && headers['Content-Type'] == 'application/json')
|
if(typeof opts.body == 'object' && opts.body != null && headers['Content-Type'] == 'application/json')
|
||||||
opts.body = JSON.stringify(opts.json);
|
opts.body = JSON.stringify(opts.body);
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
return new PromiseProgress((res, rej, prog) => {
|
return new PromiseProgress((res, rej, prog) => {
|
||||||
@ -78,14 +79,11 @@ export class Http {
|
|||||||
headers,
|
headers,
|
||||||
method: opts.method || (opts.body ? 'POST' : 'GET'),
|
method: opts.method || (opts.body ? 'POST' : 'GET'),
|
||||||
body: opts.body
|
body: opts.body
|
||||||
}).then(async (resp: DecodedResponse<T>) => {
|
}).then(async (resp: any) => {
|
||||||
console.log('done!');
|
|
||||||
for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) {
|
for(let fn of [...Object.values(Http.interceptors), ...Object.values(this.interceptors)]) {
|
||||||
await new Promise<void>(res => fn(resp, () => res()));
|
await new Promise<void>(res => fn(resp, () => res()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!resp.ok) rej(resp);
|
|
||||||
|
|
||||||
const contentLength = resp.headers.get('Content-Length');
|
const contentLength = resp.headers.get('Content-Length');
|
||||||
const total = contentLength ? parseInt(contentLength, 10) : 0;
|
const total = contentLength ? parseInt(contentLength, 10) : 0;
|
||||||
let loaded = 0;
|
let loaded = 0;
|
||||||
@ -94,28 +92,29 @@ export class Http {
|
|||||||
const stream = new ReadableStream({
|
const stream = new ReadableStream({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
function push() {
|
function push() {
|
||||||
reader?.read().then(({done, value}) => {
|
reader?.read().then((event: any) => {
|
||||||
if(done) return controller.close();
|
if(event.done) return controller.close();
|
||||||
loaded += value.byteLength;
|
loaded += event.value.byteLength;
|
||||||
prog(loaded / total);
|
prog(loaded / total);
|
||||||
controller.enqueue(value);
|
controller.enqueue(event.value);
|
||||||
push();
|
push();
|
||||||
}).catch(error => {
|
}).catch((error: any) => controller.error(error));
|
||||||
controller.error(error);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
push();
|
push();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = new Response(stream);
|
resp.data = new Response(stream);
|
||||||
|
if(opts.decode == null || opts.decode) {
|
||||||
|
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
||||||
|
if(content?.includes('form')) resp.data = <T>await resp.data.formData();
|
||||||
|
else if(content?.includes('json')) resp.data = <T>await resp.data.json();
|
||||||
|
else if(content?.includes('text')) resp.data = <T>await resp.data.text();
|
||||||
|
else if(content?.includes('application')) resp.data = <T>await resp.data.blob();
|
||||||
|
}
|
||||||
|
|
||||||
const content = resp.headers.get('Content-Type')?.toLowerCase();
|
if(resp.ok) res(resp);
|
||||||
if(content?.includes('json')) resp.data = <T>await data.json();
|
else rej(resp);
|
||||||
else if(content?.includes('text')) resp.data = <T>await data.text();
|
|
||||||
else if(content?.includes('form')) resp.data = <T>await data.formData();
|
|
||||||
else if(content?.includes('application')) resp.data = <T>await data.blob();
|
|
||||||
res(resp);
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ export default defineConfig({
|
|||||||
entry: resolve(process.cwd(), 'src/index.ts'),
|
entry: resolve(process.cwd(), 'src/index.ts'),
|
||||||
name: 'utils',
|
name: 'utils',
|
||||||
fileName: (module, entryName) => {
|
fileName: (module, entryName) => {
|
||||||
if(module == 'es') return 'utils.mjs';
|
if(module == 'es') return 'index.mjs';
|
||||||
if(module == 'umd') return 'utils.cjs';
|
if(module == 'umd') return 'index.cjs';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
emptyOutDir: true,
|
emptyOutDir: true,
|
||||||
|
Reference in New Issue
Block a user