From ca4444e1c933ab7d0a3218d1ea8dfd5a042bce21 Mon Sep 17 00:00:00 2001 From: dpilafian Date: Fri, 28 Jun 2024 22:14:56 -0700 Subject: [PATCH] Release v3.3.1 --- dist/fetch-json.d.ts | 16 ++++++---------- dist/fetch-json.dev.js | 7 ++----- dist/fetch-json.js | 7 ++----- dist/fetch-json.min.js | 4 ++-- docs/index.html | 6 +++--- fetch-json.ts | 5 ----- package.json | 18 +++++++++--------- 7 files changed, 24 insertions(+), 39 deletions(-) diff --git a/dist/fetch-json.d.ts b/dist/fetch-json.d.ts index 389f6c6..a2a8b04 100644 --- a/dist/fetch-json.d.ts +++ b/dist/fetch-json.d.ts @@ -1,4 +1,4 @@ -//! fetch-json v3.3.0 ~~ https://fetch-json.js.org ~~ MIT License +//! fetch-json v3.3.1 ~~ https://fetch-json.js.org ~~ MIT License export type Json = string | number | boolean | null | undefined | JsonObject | Json[]; export type JsonObject = { @@ -14,7 +14,6 @@ export type FetchJsonParams = { [field: string]: string | number | boolean | null | undefined; }; export type FetchJsonParsedResponse = Json | any; -export type FetchJsonTextResponse = FetchJsonAltResponse; export type FetchJsonAltResponse = { ok: boolean; error: boolean; @@ -31,21 +30,18 @@ declare const fetchJson: { baseOptions: FetchJsonOptions; getBaseOptions(): FetchJsonOptions; setBaseOptions(options: FetchJsonOptions): FetchJsonOptions; - request(method: FetchJsonMethod, url: string, data?: Json | FetchJsonParams | T, options?: FetchJsonOptions): Promise; + request(method: FetchJsonMethod, url: string, data?: FetchJsonParams | Json | T, options?: FetchJsonOptions): Promise; get(url: string, params?: FetchJsonParams, options?: FetchJsonOptions): Promise; - post(url: string, resource?: Json | T_1, options?: FetchJsonOptions): Promise; - put(url: string, resource?: Json | T_2, options?: FetchJsonOptions): Promise; - patch(url: string, resource?: Json | T_3, options?: FetchJsonOptions): Promise; - delete(url: string, resource?: Json | T_4, options?: FetchJsonOptions): Promise; + post(url: string, resource?: Json | T, options?: FetchJsonOptions): Promise; + put(url: string, resource?: Json | T, options?: FetchJsonOptions): Promise; + patch(url: string, resource?: Json | T, options?: FetchJsonOptions): Promise; + delete(url: string, resource?: Json | T, options?: FetchJsonOptions): Promise; head(url: string, params?: FetchJsonParams, options?: FetchJsonOptions): Promise; logger: FetchJsonLogger | null; getLogHeaders(): string[]; getLogHeaderIndexMap(): { [header: string]: number; }; - getLogHeaderIndex(): { - [header: string]: number; - }; enableLogger(customLogger?: FetchJsonLogger): FetchJsonLogger; disableLogger(): void; }; diff --git a/dist/fetch-json.dev.js b/dist/fetch-json.dev.js index d361d55..c736b41 100644 --- a/dist/fetch-json.dev.js +++ b/dist/fetch-json.dev.js @@ -1,7 +1,7 @@ -//! fetch-json v3.3.0 ~~ https://fetch-json.js.org ~~ MIT License +//! fetch-json v3.3.1 ~~ https://fetch-json.js.org ~~ MIT License const fetchJson = { - version: '3.3.0', + version: '3.3.1', baseOptions: {}, getBaseOptions() { return this.baseOptions; @@ -99,9 +99,6 @@ const fetchJson = { getLogHeaderIndexMap() { return { timestamp: 0, http: 1, method: 2, domain: 3, url: 4, ok: 5, status: 6, text: 7, type: 8 }; }, - getLogHeaderIndex() { - return { timestamp: 0, http: 1, method: 2, domain: 3, url: 4, ok: 5, status: 6, text: 7, type: 8 }; - }, enableLogger(customLogger) { this.logger = customLogger ?? console.log; return this.logger; diff --git a/dist/fetch-json.js b/dist/fetch-json.js index 6a5f026..c9123bc 100644 --- a/dist/fetch-json.js +++ b/dist/fetch-json.js @@ -1,7 +1,7 @@ -//! fetch-json v3.3.0 ~~ https://fetch-json.js.org ~~ MIT License +//! fetch-json v3.3.1 ~~ https://fetch-json.js.org ~~ MIT License const fetchJson = { - version: '3.3.0', + version: '3.3.1', baseOptions: {}, getBaseOptions() { return this.baseOptions; @@ -99,9 +99,6 @@ const fetchJson = { getLogHeaderIndexMap() { return { timestamp: 0, http: 1, method: 2, domain: 3, url: 4, ok: 5, status: 6, text: 7, type: 8 }; }, - getLogHeaderIndex() { - return { timestamp: 0, http: 1, method: 2, domain: 3, url: 4, ok: 5, status: 6, text: 7, type: 8 }; - }, enableLogger(customLogger) { this.logger = customLogger ?? console.log; return this.logger; diff --git a/dist/fetch-json.min.js b/dist/fetch-json.min.js index 9766ce2..6bc7781 100644 --- a/dist/fetch-json.min.js +++ b/dist/fetch-json.min.js @@ -1,2 +1,2 @@ -//! fetch-json v3.3.0 ~~ https://fetch-json.js.org ~~ MIT License -const fetchJson={version:"3.3.0",baseOptions:{},getBaseOptions(){return this.baseOptions},setBaseOptions(options){this.baseOptions=options;return this.baseOptions},request(method,url,data,options){const defaults={method:method,credentials:"same-origin",strictErrors:false};const settings={...defaults,...this.baseOptions,...options};if(!settings.method||typeof settings.method!=="string")throw Error("[fetch-json] HTTP method missing or invalid.");if(typeof url!=="string")throw Error("[fetch-json] URL must be a string.");const httpMethod=settings.method.trim().toUpperCase();const isGetRequest=httpMethod==="GET";const jsonHeaders={Accept:"application/json"};if(!isGetRequest&&data)jsonHeaders["Content-Type"]="application/json";settings.headers={...jsonHeaders,...settings.headers};const paramKeys=isGetRequest&&data?Object.keys(data):[];const getValue=key=>data?data[key]:"";const toPair=key=>key+"="+encodeURIComponent(getValue(key));const params=()=>paramKeys.map(toPair).join("&");const requestUrl=!paramKeys.length?url:url+(url.includes("?")?"&":"?")+params();settings.body=!isGetRequest&&data?JSON.stringify(data):null;const log=(type,...items)=>{const logUrl=url.replace(/[?].*/,"");const domain=logUrl.replace(/.*:[/][/]/,"").replace(/[:/].*/,"");if(this.logger)this.logger((new Date).toISOString(),type,httpMethod,domain,logUrl,...items)};const toJson=value=>{const response=value;const contentType=response.headers.get("content-type");const isHead=httpMethod==="HEAD";const isJson=!!contentType&&/json|javascript/.test(contentType);const headersObj=()=>Object.fromEntries(response.headers.entries());const textToObj=(httpBody,data)=>({ok:response.ok,error:!response.ok,status:response.status,contentType:contentType,bodyText:httpBody,data:data??null,response:response});const jsonToObj=data=>response.ok?data:textToObj(JSON.stringify(data),data);const errToObj=error=>({ok:false,error:true,status:500,contentType:contentType,bodyText:"Invalid JSON ["+error.toString()+"]",data:null,response:response});log("response",response.ok,response.status,response.statusText,contentType);if(settings.strictErrors&&!response.ok)throw Error('[fetch-json] HTTP response status ("strictErrors" mode enabled): '+response.status);return isHead?response.text().then(headersObj):isJson?response.json().then(jsonToObj).catch(errToObj):response.text().then(textToObj)};log("request");const settingsRequestInit=JSON.parse(JSON.stringify(settings));return fetch(requestUrl,settingsRequestInit).then(toJson)},get(url,params,options){return this.request("GET",url,params,options)},post(url,resource,options){return this.request("POST",url,resource,options)},put(url,resource,options){return this.request("PUT",url,resource,options)},patch(url,resource,options){return this.request("PATCH",url,resource,options)},delete(url,resource,options){return this.request("DELETE",url,resource,options)},head(url,params,options){return this.request("HEAD",url,params,options)},logger:null,getLogHeaders(){return["Timestamp","HTTP","Method","Domain","URL","Ok","Status","Text","Type"]},getLogHeaderIndexMap(){return{timestamp:0,http:1,method:2,domain:3,url:4,ok:5,status:6,text:7,type:8}},getLogHeaderIndex(){return{timestamp:0,http:1,method:2,domain:3,url:4,ok:5,status:6,text:7,type:8}},enableLogger(customLogger){this.logger=customLogger??console.log;return this.logger},disableLogger(){this.logger=null}};class FetchJson{constructor(options){this.fetchJson={...fetchJson};this.fetchJson.setBaseOptions(options||{})}}globalThis.fetchJson=fetchJson;globalThis.FetchJson=FetchJson; +//! fetch-json v3.3.1 ~~ https://fetch-json.js.org ~~ MIT License +const fetchJson={version:"3.3.1",baseOptions:{},getBaseOptions(){return this.baseOptions},setBaseOptions(options){this.baseOptions=options;return this.baseOptions},request(method,url,data,options){const defaults={method:method,credentials:"same-origin",strictErrors:false};const settings={...defaults,...this.baseOptions,...options};if(!settings.method||typeof settings.method!=="string")throw Error("[fetch-json] HTTP method missing or invalid.");if(typeof url!=="string")throw Error("[fetch-json] URL must be a string.");const httpMethod=settings.method.trim().toUpperCase();const isGetRequest=httpMethod==="GET";const jsonHeaders={Accept:"application/json"};if(!isGetRequest&&data)jsonHeaders["Content-Type"]="application/json";settings.headers={...jsonHeaders,...settings.headers};const paramKeys=isGetRequest&&data?Object.keys(data):[];const getValue=key=>data?data[key]:"";const toPair=key=>key+"="+encodeURIComponent(getValue(key));const params=()=>paramKeys.map(toPair).join("&");const requestUrl=!paramKeys.length?url:url+(url.includes("?")?"&":"?")+params();settings.body=!isGetRequest&&data?JSON.stringify(data):null;const log=(type,...items)=>{const logUrl=url.replace(/[?].*/,"");const domain=logUrl.replace(/.*:[/][/]/,"").replace(/[:/].*/,"");if(this.logger)this.logger((new Date).toISOString(),type,httpMethod,domain,logUrl,...items)};const toJson=value=>{const response=value;const contentType=response.headers.get("content-type");const isHead=httpMethod==="HEAD";const isJson=!!contentType&&/json|javascript/.test(contentType);const headersObj=()=>Object.fromEntries(response.headers.entries());const textToObj=(httpBody,data)=>({ok:response.ok,error:!response.ok,status:response.status,contentType:contentType,bodyText:httpBody,data:data??null,response:response});const jsonToObj=data=>response.ok?data:textToObj(JSON.stringify(data),data);const errToObj=error=>({ok:false,error:true,status:500,contentType:contentType,bodyText:"Invalid JSON ["+error.toString()+"]",data:null,response:response});log("response",response.ok,response.status,response.statusText,contentType);if(settings.strictErrors&&!response.ok)throw Error('[fetch-json] HTTP response status ("strictErrors" mode enabled): '+response.status);return isHead?response.text().then(headersObj):isJson?response.json().then(jsonToObj).catch(errToObj):response.text().then(textToObj)};log("request");const settingsRequestInit=JSON.parse(JSON.stringify(settings));return fetch(requestUrl,settingsRequestInit).then(toJson)},get(url,params,options){return this.request("GET",url,params,options)},post(url,resource,options){return this.request("POST",url,resource,options)},put(url,resource,options){return this.request("PUT",url,resource,options)},patch(url,resource,options){return this.request("PATCH",url,resource,options)},delete(url,resource,options){return this.request("DELETE",url,resource,options)},head(url,params,options){return this.request("HEAD",url,params,options)},logger:null,getLogHeaders(){return["Timestamp","HTTP","Method","Domain","URL","Ok","Status","Text","Type"]},getLogHeaderIndexMap(){return{timestamp:0,http:1,method:2,domain:3,url:4,ok:5,status:6,text:7,type:8}},enableLogger(customLogger){this.logger=customLogger??console.log;return this.logger},disableLogger(){this.logger=null}};class FetchJson{constructor(options){this.fetchJson={...fetchJson};this.fetchJson.setBaseOptions(options||{})}}globalThis.fetchJson=fetchJson;globalThis.FetchJson=FetchJson; diff --git a/docs/index.html b/docs/index.html index 9f8b455..d3351d3 100644 --- a/docs/index.html +++ b/docs/index.html @@ -22,8 +22,8 @@ - - + + - +