@ecosy/core 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/http.d.ts +3 -3
- package/dist/http.js +1 -1
- package/dist/http.mjs +1 -1
- package/dist/subscriber.d.ts +1 -1
- package/package.json +1 -1
package/dist/http.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type FileListLike } from "
|
|
1
|
+
import { type FileListLike } from "./utilities";
|
|
2
2
|
/** Default base URL for HTTP requests, sourced from `API_URL` env variable. */
|
|
3
|
-
export declare const DEFAULT_BASE_URL: string;
|
|
3
|
+
export declare const DEFAULT_BASE_URL: string | undefined;
|
|
4
4
|
/** Authentication token storage key, sourced from `API_AUTH_TOKEN_KEY` env variable. */
|
|
5
5
|
export declare const API_AUTH_TOKEN_KEY: string | undefined;
|
|
6
6
|
/** Authentication header name, sourced from `API_AUTH_HEADER_KEY` env variable. */
|
|
@@ -105,7 +105,7 @@ export declare class Http {
|
|
|
105
105
|
private storage;
|
|
106
106
|
private static interceptors;
|
|
107
107
|
private interceptors;
|
|
108
|
-
constructor(baseURL?: string);
|
|
108
|
+
constructor(baseURL?: string | undefined);
|
|
109
109
|
/** Register a global interceptor (applies to all `Http` instances). */
|
|
110
110
|
static on(...params: HttpInterceptorParameters): void;
|
|
111
111
|
/** Remove a global interceptor. */
|
package/dist/http.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";require("./utilities/clone.js");var t=require("./utilities/filelist.js"),e=require("./utilities/flatten.js"),s=require("./utilities/object-to-formdata.js"),
|
|
1
|
+
"use strict";require("./utilities/clone.js");var t=require("./utilities/filelist.js"),e=require("./utilities/flatten.js"),s=require("./utilities/object.js"),r=require("./utilities/object-to-formdata.js"),o=require("./serialize.js");function n(t,e){var r;return"undefined"!=typeof process&&s.isLiteralObject(process.env)&&null!==(r=process.env[t])&&void 0!==r?r:e}const a=n("API_URL","/"),i=n("API_AUTH_TOKEN_KEY"),c=n("API_AUTH_HEADER_KEY"),p=n("API_AUTH_HEADER_TYPE");var u;exports.HttpMethod=void 0,(u=exports.HttpMethod||(exports.HttpMethod={})).GET="GET",u.POST="POST",u.PUT="PUT",u.DELETE="DELETE",u.PATCH="PATCH",u.HEAD="HEAD",u.OPTIONS="OPTIONS";class d{constructor(t=a){this.baseURL=t,this.defaultHeaders={"Content-Type":"application/json"},this.method=d.method,this.storage=d.storage,this.interceptors={request:[],response:[],transform:[],error:[]}}static on(...t){const[e,s]=t,r=d.interceptors[e];r.includes(s)||r.push(s),d.interceptors[e]=r}static off(...t){const[e,s]=t,r=d.interceptors[e];d.interceptors[e]=r.filter(t=>t!==s)}setStorage(t){this.storage=t}isValidQuery(t){return"string"==typeof t||t instanceof URLSearchParams||(Array.isArray(t)?t.every(t=>Array.isArray(t)&&2===t.length&&"string"==typeof t[0]&&o.Serialize.Primitive.isPrimitive(t[1])):"object"==typeof t&&null!==t&&Object.values(t).every(t=>o.Serialize.Primitive.isPrimitive(t)))}getQuery(t){const{method:e,body:s,query:r}=t,n=e===exports.HttpMethod.GET&&this.isValidQuery(s)?s:r;return n&&"object"==typeof n?n instanceof URLSearchParams?n.toString():o.Serialize.queryString.stringify(n,{skipNull:!0,skipEmptyString:!0}):"string"==typeof n?n:""}isFormData(t){return t instanceof FormData}addHeaders(t){this.defaultHeaders=Object.assign(Object.assign({},this.defaultHeaders),t)}on(...t){const[e,s]=t,r=this.interceptors[e];return r.includes(s)||r.push(s),this.interceptors[e]=r,this}off(...t){const[e,s]=t,r=this.interceptors[e];return this.interceptors[e]=r.filter(t=>t!==s),this}getToken(){const t=i||d.authTokenKey,e=c||d.authHeaderKey,s=p||d.authHeaderType;if(!t||!e)return{key:e,value:""};let r="";this.storage&&(r=this.storage.getItem(t)||"");const o={key:e,value:""};return r&&(o.value=r,s&&(o.value=`${s} ${r}`)),o}getHeaders(t={},e){const s=this.getToken();s.key in t&&t[s.key]||(t[s.key]=s.value),e&&"Content-Type"in t?delete t["Content-Type"]:t["Content-Type"]=t["Content-Type"]||this.defaultHeaders["Content-Type"]||"application/json";const r=Object.assign(Object.assign(Object.assign({},this.defaultHeaders),t),d.extraHeaders||{});return d.extraHeaders=null,r}getURL(t){var e;const{url:s,params:r={}}=t,n=this.getQuery(t);let a=s;if(s)if(s.match(/^https?:\/\//))a=s;else{a=`${null===(e=this.baseURL)||void 0===e?void 0:e.replace(/\/+$/,"")}/${s.replace(/^\/+/,"")}`}if(n){const t=a.includes("?")?"&":"?";a+=`${t}${n}`}return o.Serialize.interpolate(a,r)}getBody(t){const{method:e,body:s}=t;if(e!==exports.HttpMethod.GET&&e!==exports.HttpMethod.HEAD&&e!==exports.HttpMethod.OPTIONS&&e!==exports.HttpMethod.DELETE)return e!==exports.HttpMethod.POST&&e!==exports.HttpMethod.PUT&&e!==exports.HttpMethod.PATCH||!this.isFormData(s)?void 0!==s?JSON.stringify(s):void 0:s}async request(t){const{method:e=exports.HttpMethod.GET}=t;try{let s=Object.assign({},t);const r=[...d.interceptors.request,...this.interceptors.request];for(const t of r)s=await t(s);const o=this.getURL(s),n=this.getHeaders(s.headers||{});this.isFormData(s.body)&&"Content-Type"in n&&delete n["Content-Type"];const a=await fetch(o,{method:s.method||e,headers:n,body:this.getBody(s),signal:s.signal}),i=a.headers.get("Content-Type")||"";let c=null;c=i.includes("application/json")?await a.json():await a.text();const p=[...d.interceptors.transform,...this.interceptors.transform];let u=c;for(const t of p)u=await t(u);const l=[...d.interceptors.response,...this.interceptors.response];let h=a;for(const t of l)h=await t(h);const f={};h.headers.forEach((t,e)=>{f[e]=t});const y=h.ok;let T=null;if(!y){T=c.error||c;const t=[...d.interceptors.error,...this.interceptors.error];for(const e of t)T=await e(T)}return{data:u,success:y,error:y?null:T,status:h.status,statusText:h.statusText,headers:f}}catch(t){let e=t instanceof Error?t:new Error(String(t));const s=[...d.interceptors.error,...this.interceptors.error];for(const t of s)e=await t(e);return{data:null,status:0,statusText:"Error",headers:{},error:e,success:!1}}}get(t,e,s){return this.request(Object.assign(Object.assign({},s),{method:exports.HttpMethod.GET,url:t,query:e}))}post(t,e,s){return this.request(Object.assign(Object.assign({},s),{method:exports.HttpMethod.POST,url:t,body:e}))}put(t,e,s){return this.request(Object.assign(Object.assign({},s),{method:exports.HttpMethod.PUT,url:t,body:e}))}patch(t,e,s){return this.request(Object.assign(Object.assign({},s),{method:exports.HttpMethod.PATCH,url:t,body:e}))}delete(t,e){return this.request(Object.assign(Object.assign({},e),{method:exports.HttpMethod.DELETE,url:t}))}head(t,e){return this.request(Object.assign(Object.assign({},e),{method:exports.HttpMethod.HEAD,url:t}))}options(t,e){return this.request(Object.assign(Object.assign({},e),{method:exports.HttpMethod.OPTIONS,url:t}))}upload(s,o,n){const a=(null==n?void 0:n.body)?r.objectToFormData(n.body):new FormData;let i=(null==n?void 0:n.name)||"file";if(Array.isArray(o)?(i.endsWith("[]")&&(i=i.slice(0,-2)),o.forEach((t,e)=>{a.append(`${i}[${e}]`,t,t.name)})):t.isFileList(o)?(i.endsWith("[]")&&(i=i.slice(0,-2)),Array.from(o).forEach((t,e)=>{a.append(`${i}[${e}]`,t,t.name)})):a.append(i,o,o.name),null==n?void 0:n.body){const t=e.flatten(n.body);Object.entries(t).forEach(([t,e])=>{null!=e&&a.append(t,String(e))})}return(null==n?void 0:n.onProgress)&&"undefined"!=typeof XMLHttpRequest?new Promise(async t=>{try{let e=Object.assign(Object.assign({},n),{method:exports.HttpMethod.POST,url:s,body:a});const r=[...d.interceptors.request,...this.interceptors.request];for(const t of r)e=await t(e);const o=this.getURL(e),i=this.getHeaders(e.headers||{},!0),c=new XMLHttpRequest;c.upload.addEventListener("progress",t=>{var e;if(t.lengthComputable){const s=Math.round(t.loaded/t.total*100);null===(e=n.onProgress)||void 0===e||e.call(n,{loaded:t.loaded,total:t.total,percentage:s})}}),c.addEventListener("load",async()=>{try{const e=c.getResponseHeader("Content-Type")||"";let s=null;s=e.includes("application/json")?JSON.parse(c.responseText):c.responseText;const r=[...d.interceptors.transform,...this.interceptors.transform];let o=s;for(const t of r)o=await t(o);const n=[...d.interceptors.response,...this.interceptors.response];let a=new Response(c.responseText,{status:c.status,statusText:c.statusText,headers:i});for(const t of n)a=await t(a);const p={};a.headers.forEach((t,e)=>{p[e]=t});const u=a.ok;t({data:o,success:u,error:u?null:o,status:a.status,statusText:a.statusText,headers:p})}catch(e){const s=[...d.interceptors.error,...this.interceptors.error];for(const t of s)t(e);t({data:null,status:0,statusText:"Error",headers:{},error:e,success:!1})}}),c.addEventListener("error",e=>{const s=[...d.interceptors.error,...this.interceptors.error];for(const t of s)t(e);t({data:null,status:0,statusText:"Error",headers:{},error:e,success:!1})}),c.addEventListener("abort",()=>{t({data:null,status:0,statusText:"Aborted",headers:{},error:null,success:!1})}),c.open(e.method||exports.HttpMethod.POST,o,!0),Object.entries(i).forEach(([t,e])=>{"content-type"!==(null==t?void 0:t.toLowerCase())&&e&&c.setRequestHeader(t,e)}),c.send(e.body)}catch(e){const s=[...d.interceptors.error,...this.interceptors.error];for(const t of s)t(e);t({data:null,status:0,statusText:"Error",headers:{},error:e,success:!1})}}):this.request({method:exports.HttpMethod.POST,url:s,body:a,headers:null==n?void 0:n.headers,params:null==n?void 0:n.params,signal:null==n?void 0:n.signal,query:null==n?void 0:n.query})}static createFactory(t={}){const s=t.http||new d(t.baseURL);return function(r,o){const n=e.flatten("function"==typeof t.endpoint?t.endpoint():t.endpoint||{})[r];return{key:r,fn:async(...e)=>{const r="function"==typeof t.storage?await t.storage():t.storage;switch(r&&s.setStorage(r),o){case exports.HttpMethod.POST:return await s.post(n,...e);case exports.HttpMethod.PUT:return await s.put(n,...e);case exports.HttpMethod.PATCH:return await s.patch(n,...e);case exports.HttpMethod.DELETE:return await s.delete(n,...e);case exports.HttpMethod.HEAD:return await s.head(n,...e);case exports.HttpMethod.OPTIONS:return await s.options(n,...e);case"UPLOAD":return await s.upload(n,...e);default:return await s.get(n,...e)}}}}}}d.method=exports.HttpMethod,d.authTokenKey="access_token",d.authHeaderKey="Authorization",d.authHeaderType="Bearer",d.authDetectToken=["localStorage","sessionStorage","cookie"],d.extraHeaders=null,d.storage=null,d.interceptors={request:[],response:[],transform:[],error:[]},exports.API_AUTH_HEADER_KEY=c,exports.API_AUTH_HEADER_TYPE=p,exports.API_AUTH_TOKEN_KEY=i,exports.DEFAULT_BASE_URL=a,exports.Http=d;
|
package/dist/http.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./utilities/clone.mjs";import{isFileList as t}from"./utilities/filelist.mjs";import{flatten as e}from"./utilities/flatten.mjs";import{
|
|
1
|
+
import"./utilities/clone.mjs";import{isFileList as t}from"./utilities/filelist.mjs";import{flatten as e}from"./utilities/flatten.mjs";import{isLiteralObject as s}from"./utilities/object.mjs";import{objectToFormData as r}from"./utilities/object-to-formdata.mjs";import{Serialize as o}from"./serialize.mjs";function n(t,e){var r;return"undefined"!=typeof process&&s(process.env)&&null!==(r=process.env[t])&&void 0!==r?r:e}const a=n("API_URL","/"),i=n("API_AUTH_TOKEN_KEY"),c=n("API_AUTH_HEADER_KEY"),u=n("API_AUTH_HEADER_TYPE");var l;!function(t){t.GET="GET",t.POST="POST",t.PUT="PUT",t.DELETE="DELETE",t.PATCH="PATCH",t.HEAD="HEAD",t.OPTIONS="OPTIONS"}(l||(l={}));class d{constructor(t=a){this.baseURL=t,this.defaultHeaders={"Content-Type":"application/json"},this.method=d.method,this.storage=d.storage,this.interceptors={request:[],response:[],transform:[],error:[]}}static on(...t){const[e,s]=t,r=d.interceptors[e];r.includes(s)||r.push(s),d.interceptors[e]=r}static off(...t){const[e,s]=t,r=d.interceptors[e];d.interceptors[e]=r.filter(t=>t!==s)}setStorage(t){this.storage=t}isValidQuery(t){return"string"==typeof t||t instanceof URLSearchParams||(Array.isArray(t)?t.every(t=>Array.isArray(t)&&2===t.length&&"string"==typeof t[0]&&o.Primitive.isPrimitive(t[1])):"object"==typeof t&&null!==t&&Object.values(t).every(t=>o.Primitive.isPrimitive(t)))}getQuery(t){const{method:e,body:s,query:r}=t,n=e===l.GET&&this.isValidQuery(s)?s:r;return n&&"object"==typeof n?n instanceof URLSearchParams?n.toString():o.queryString.stringify(n,{skipNull:!0,skipEmptyString:!0}):"string"==typeof n?n:""}isFormData(t){return t instanceof FormData}addHeaders(t){this.defaultHeaders=Object.assign(Object.assign({},this.defaultHeaders),t)}on(...t){const[e,s]=t,r=this.interceptors[e];return r.includes(s)||r.push(s),this.interceptors[e]=r,this}off(...t){const[e,s]=t,r=this.interceptors[e];return this.interceptors[e]=r.filter(t=>t!==s),this}getToken(){const t=i||d.authTokenKey,e=c||d.authHeaderKey,s=u||d.authHeaderType;if(!t||!e)return{key:e,value:""};let r="";this.storage&&(r=this.storage.getItem(t)||"");const o={key:e,value:""};return r&&(o.value=r,s&&(o.value=`${s} ${r}`)),o}getHeaders(t={},e){const s=this.getToken();s.key in t&&t[s.key]||(t[s.key]=s.value),e&&"Content-Type"in t?delete t["Content-Type"]:t["Content-Type"]=t["Content-Type"]||this.defaultHeaders["Content-Type"]||"application/json";const r=Object.assign(Object.assign(Object.assign({},this.defaultHeaders),t),d.extraHeaders||{});return d.extraHeaders=null,r}getURL(t){var e;const{url:s,params:r={}}=t,n=this.getQuery(t);let a=s;if(s)if(s.match(/^https?:\/\//))a=s;else{a=`${null===(e=this.baseURL)||void 0===e?void 0:e.replace(/\/+$/,"")}/${s.replace(/^\/+/,"")}`}if(n){const t=a.includes("?")?"&":"?";a+=`${t}${n}`}return o.interpolate(a,r)}getBody(t){const{method:e,body:s}=t;if(e!==l.GET&&e!==l.HEAD&&e!==l.OPTIONS&&e!==l.DELETE)return e!==l.POST&&e!==l.PUT&&e!==l.PATCH||!this.isFormData(s)?void 0!==s?JSON.stringify(s):void 0:s}async request(t){const{method:e=l.GET}=t;try{let s=Object.assign({},t);const r=[...d.interceptors.request,...this.interceptors.request];for(const t of r)s=await t(s);const o=this.getURL(s),n=this.getHeaders(s.headers||{});this.isFormData(s.body)&&"Content-Type"in n&&delete n["Content-Type"];const a=await fetch(o,{method:s.method||e,headers:n,body:this.getBody(s),signal:s.signal}),i=a.headers.get("Content-Type")||"";let c=null;c=i.includes("application/json")?await a.json():await a.text();const u=[...d.interceptors.transform,...this.interceptors.transform];let l=c;for(const t of u)l=await t(l);const p=[...d.interceptors.response,...this.interceptors.response];let h=a;for(const t of p)h=await t(h);const f={};h.headers.forEach((t,e)=>{f[e]=t});const y=h.ok;let g=null;if(!y){g=c.error||c;const t=[...d.interceptors.error,...this.interceptors.error];for(const e of t)g=await e(g)}return{data:l,success:y,error:y?null:g,status:h.status,statusText:h.statusText,headers:f}}catch(t){let e=t instanceof Error?t:new Error(String(t));const s=[...d.interceptors.error,...this.interceptors.error];for(const t of s)e=await t(e);return{data:null,status:0,statusText:"Error",headers:{},error:e,success:!1}}}get(t,e,s){return this.request(Object.assign(Object.assign({},s),{method:l.GET,url:t,query:e}))}post(t,e,s){return this.request(Object.assign(Object.assign({},s),{method:l.POST,url:t,body:e}))}put(t,e,s){return this.request(Object.assign(Object.assign({},s),{method:l.PUT,url:t,body:e}))}patch(t,e,s){return this.request(Object.assign(Object.assign({},s),{method:l.PATCH,url:t,body:e}))}delete(t,e){return this.request(Object.assign(Object.assign({},e),{method:l.DELETE,url:t}))}head(t,e){return this.request(Object.assign(Object.assign({},e),{method:l.HEAD,url:t}))}options(t,e){return this.request(Object.assign(Object.assign({},e),{method:l.OPTIONS,url:t}))}upload(s,o,n){const a=(null==n?void 0:n.body)?r(n.body):new FormData;let i=(null==n?void 0:n.name)||"file";if(Array.isArray(o)?(i.endsWith("[]")&&(i=i.slice(0,-2)),o.forEach((t,e)=>{a.append(`${i}[${e}]`,t,t.name)})):t(o)?(i.endsWith("[]")&&(i=i.slice(0,-2)),Array.from(o).forEach((t,e)=>{a.append(`${i}[${e}]`,t,t.name)})):a.append(i,o,o.name),null==n?void 0:n.body){const t=e(n.body);Object.entries(t).forEach(([t,e])=>{null!=e&&a.append(t,String(e))})}return(null==n?void 0:n.onProgress)&&"undefined"!=typeof XMLHttpRequest?new Promise(async t=>{try{let e=Object.assign(Object.assign({},n),{method:l.POST,url:s,body:a});const r=[...d.interceptors.request,...this.interceptors.request];for(const t of r)e=await t(e);const o=this.getURL(e),i=this.getHeaders(e.headers||{},!0),c=new XMLHttpRequest;c.upload.addEventListener("progress",t=>{var e;if(t.lengthComputable){const s=Math.round(t.loaded/t.total*100);null===(e=n.onProgress)||void 0===e||e.call(n,{loaded:t.loaded,total:t.total,percentage:s})}}),c.addEventListener("load",async()=>{try{const e=c.getResponseHeader("Content-Type")||"";let s=null;s=e.includes("application/json")?JSON.parse(c.responseText):c.responseText;const r=[...d.interceptors.transform,...this.interceptors.transform];let o=s;for(const t of r)o=await t(o);const n=[...d.interceptors.response,...this.interceptors.response];let a=new Response(c.responseText,{status:c.status,statusText:c.statusText,headers:i});for(const t of n)a=await t(a);const u={};a.headers.forEach((t,e)=>{u[e]=t});const l=a.ok;t({data:o,success:l,error:l?null:o,status:a.status,statusText:a.statusText,headers:u})}catch(e){const s=[...d.interceptors.error,...this.interceptors.error];for(const t of s)t(e);t({data:null,status:0,statusText:"Error",headers:{},error:e,success:!1})}}),c.addEventListener("error",e=>{const s=[...d.interceptors.error,...this.interceptors.error];for(const t of s)t(e);t({data:null,status:0,statusText:"Error",headers:{},error:e,success:!1})}),c.addEventListener("abort",()=>{t({data:null,status:0,statusText:"Aborted",headers:{},error:null,success:!1})}),c.open(e.method||l.POST,o,!0),Object.entries(i).forEach(([t,e])=>{"content-type"!==(null==t?void 0:t.toLowerCase())&&e&&c.setRequestHeader(t,e)}),c.send(e.body)}catch(e){const s=[...d.interceptors.error,...this.interceptors.error];for(const t of s)t(e);t({data:null,status:0,statusText:"Error",headers:{},error:e,success:!1})}}):this.request({method:l.POST,url:s,body:a,headers:null==n?void 0:n.headers,params:null==n?void 0:n.params,signal:null==n?void 0:n.signal,query:null==n?void 0:n.query})}static createFactory(t={}){const s=t.http||new d(t.baseURL);return function(r,o){const n=e("function"==typeof t.endpoint?t.endpoint():t.endpoint||{})[r];return{key:r,fn:async(...e)=>{const r="function"==typeof t.storage?await t.storage():t.storage;switch(r&&s.setStorage(r),o){case l.POST:return await s.post(n,...e);case l.PUT:return await s.put(n,...e);case l.PATCH:return await s.patch(n,...e);case l.DELETE:return await s.delete(n,...e);case l.HEAD:return await s.head(n,...e);case l.OPTIONS:return await s.options(n,...e);case"UPLOAD":return await s.upload(n,...e);default:return await s.get(n,...e)}}}}}}d.method=l,d.authTokenKey="access_token",d.authHeaderKey="Authorization",d.authHeaderType="Bearer",d.authDetectToken=["localStorage","sessionStorage","cookie"],d.extraHeaders=null,d.storage=null,d.interceptors={request:[],response:[],transform:[],error:[]};export{c as API_AUTH_HEADER_KEY,u as API_AUTH_HEADER_TYPE,i as API_AUTH_TOKEN_KEY,a as DEFAULT_BASE_URL,d as Http,l as HttpMethod};
|
package/dist/subscriber.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Freezable, LiteralObject, PartialLiteral, ToString } from "
|
|
1
|
+
import type { Freezable, LiteralObject, PartialLiteral, ToString } from "./types";
|
|
2
2
|
export type SubscribeChannel = string;
|
|
3
3
|
export type SubcribeHandler<Payload = never> = [Payload] extends [never] ? () => void : (payload: Payload) => void;
|
|
4
4
|
export interface Shallow {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecosy/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A modular, tree-shakable collection of essential utilities, serialization primitives, and event-driven patterns for modern TypeScript applications",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|