Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
1eff60e7dd | |||
1d9ad2ef63 | |||
973a884b3e | |||
6f60f8bd94 |
40
.github/workflows/build.yaml
vendored
40
.github/workflows/build.yaml
vendored
@ -37,11 +37,15 @@ jobs:
|
|||||||
key: node_modules
|
key: node_modules
|
||||||
pattern: node_modules
|
pattern: node_modules
|
||||||
|
|
||||||
- name: Cache Artifacts
|
- name: Upload to Registry
|
||||||
uses: ztimson/actions/cache@develop
|
uses: ztimson/actions/npm/publish@develop
|
||||||
|
|
||||||
|
- name: Upload to NPM
|
||||||
|
uses: ztimson/actions/npm/publish@develop
|
||||||
with:
|
with:
|
||||||
key: dist
|
owner: ztimson
|
||||||
pattern: dist
|
registry: https://registry.npmjs.org/
|
||||||
|
token: ${{secrets.NPM_TOKEN}}
|
||||||
tag:
|
tag:
|
||||||
name: Tag Version
|
name: Tag Version
|
||||||
needs: build
|
needs: build
|
||||||
@ -59,31 +63,3 @@ jobs:
|
|||||||
uses: ztimson/actions/tag@develop
|
uses: ztimson/actions/tag@develop
|
||||||
with:
|
with:
|
||||||
tag: ${{env.VERSION}}
|
tag: ${{env.VERSION}}
|
||||||
|
|
||||||
publish:
|
|
||||||
name: Publish
|
|
||||||
needs: build
|
|
||||||
if: ${{github.ref_name}} == 'release'
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
container:
|
|
||||||
image: node
|
|
||||||
volumes:
|
|
||||||
- '/mnt/swarm/gitea/runner/cache:/cache'
|
|
||||||
steps:
|
|
||||||
- name: Clone Repository
|
|
||||||
uses: ztimson/actions/clone@develop
|
|
||||||
|
|
||||||
- name: Restore Artifacts
|
|
||||||
uses: ztimson/actions/cache/restore@develop
|
|
||||||
with:
|
|
||||||
key: dist
|
|
||||||
|
|
||||||
- name: Upload to Registry
|
|
||||||
uses: ztimson/actions/npm/publish@develop
|
|
||||||
|
|
||||||
- name: Upload to NPM
|
|
||||||
uses: ztimson/actions/npm/publish@develop
|
|
||||||
with:
|
|
||||||
owner: ztimson
|
|
||||||
registry: https://registry.npmjs.org/
|
|
||||||
token: ${{secrets.NPM_TOKEN}}
|
|
||||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.10.2",
|
"version": "0.10.4",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.10.2",
|
"version": "0.10.4",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.12",
|
"@types/jest": "^29.5.12",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.10.3",
|
"version": "0.11.2",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@ -12,6 +12,12 @@
|
|||||||
"main": "./dist/utils.cjs",
|
"main": "./dist/utils.cjs",
|
||||||
"module": "./dist/utils.mjs",
|
"module": "./dist/utils.mjs",
|
||||||
"types": "./dist/index.d.ts",
|
"types": "./dist/index.d.ts",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"require": "./dist/utils.cjs",
|
||||||
|
"import": "./dist/utils.mjs"
|
||||||
|
}
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npx tsc && npx vite build",
|
"build": "npx tsc && npx vite build",
|
||||||
"test": "npx jest",
|
"test": "npx jest",
|
||||||
|
32
src/xhr.ts
32
src/xhr.ts
@ -1,17 +1,11 @@
|
|||||||
import {
|
|
||||||
BadRequestError,
|
|
||||||
CustomError,
|
|
||||||
ForbiddenError,
|
|
||||||
InternalServerError,
|
|
||||||
NotFoundError,
|
|
||||||
UnauthorizedError
|
|
||||||
} from './errors.ts';
|
|
||||||
import {clean} from './objects';
|
import {clean} from './objects';
|
||||||
|
|
||||||
export type Interceptor = (request: Response, next: () => void) => void;
|
export type Interceptor = (request: Response, next: () => void) => void;
|
||||||
|
|
||||||
export type RequestOptions = {
|
export type RequestOptions = {
|
||||||
url?: string;
|
url?: string;
|
||||||
|
fragment?: string;
|
||||||
|
query?: {key: string, value: string}[] | {[key: string]: string};
|
||||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
||||||
body?: any;
|
body?: any;
|
||||||
headers?: {[key: string | symbol]: string | null | undefined};
|
headers?: {[key: string | symbol]: string | null | undefined};
|
||||||
@ -55,7 +49,13 @@ export class XHR {
|
|||||||
|
|
||||||
async request<T>(opts: RequestOptions = {}): Promise<T> {
|
async request<T>(opts: RequestOptions = {}): Promise<T> {
|
||||||
if(!this.opts.url && !opts.url) throw new Error('URL needs to be set');
|
if(!this.opts.url && !opts.url) throw new Error('URL needs to be set');
|
||||||
const url = (opts.url?.startsWith('http') ? opts.url : (this.opts.url || '') + (opts.url || '')).replace(/([^:]\/)\/+/g, '$1');
|
let url = (opts.url?.startsWith('http') ? opts.url : (this.opts.url || '') + (opts.url || '')).replace(/([^:]\/)\/+/g, '$1');
|
||||||
|
if(opts.fragment) url.includes('#') ? url.replace(/#.*(\?|\n)/g, (match, arg1) => `#${opts.fragment}${arg1}`) : url += '#' + opts.fragment;
|
||||||
|
if(opts.query) {
|
||||||
|
const q = Array.isArray(opts.query) ? opts.query :
|
||||||
|
Object.keys(opts.query).map(k => ({key: k, value: (<any>opts.query)[k]}))
|
||||||
|
url += (url.includes('?') ? '&' : '?') + q.map(q => `${q.key}=${q.value}`).join('&');
|
||||||
|
}
|
||||||
|
|
||||||
// Prep headers
|
// Prep headers
|
||||||
const headers = <any>clean({
|
const headers = <any>clean({
|
||||||
@ -75,10 +75,16 @@ export class XHR {
|
|||||||
await new Promise<void>(res => fn(resp, () => res()));
|
await new Promise<void>(res => fn(resp, () => res()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!resp.ok) throw new Error(resp.statusText);
|
const decode = async () => {
|
||||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
||||||
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
||||||
return resp;
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = await decode();
|
||||||
|
if(resp.ok) return payload;
|
||||||
|
const text = resp.statusText || (typeof payload == 'string' ? payload : null);
|
||||||
|
throw (text ? new Error(text) : payload);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user