diff --git a/dist/fetch-json.d.ts b/dist/fetch-json.d.ts index 54f03e6..a2cc65a 100644 --- a/dist/fetch-json.d.ts +++ b/dist/fetch-json.d.ts @@ -1,4 +1,4 @@ -//! fetch-json v3.2.1 ~~ https://fetch-json.js.org ~~ MIT License +//! fetch-json v3.2.2 ~~ https://fetch-json.js.org ~~ MIT License export type Json = string | number | boolean | null | undefined | JsonObject | Json[]; export type JsonObject = { diff --git a/dist/fetch-json.dev.js b/dist/fetch-json.dev.js index 5d9369d..a40f815 100644 --- a/dist/fetch-json.dev.js +++ b/dist/fetch-json.dev.js @@ -1,7 +1,7 @@ -//! fetch-json v3.2.1 ~~ https://fetch-json.js.org ~~ MIT License +//! fetch-json v3.2.2 ~~ https://fetch-json.js.org ~~ MIT License const fetchJson = { - version: '3.2.1', + version: '3.2.2', baseOptions: {}, getBaseOptions() { return this.baseOptions; diff --git a/dist/fetch-json.js b/dist/fetch-json.js index 1ef08fa..573e8e6 100644 --- a/dist/fetch-json.js +++ b/dist/fetch-json.js @@ -1,7 +1,7 @@ -//! fetch-json v3.2.1 ~~ https://fetch-json.js.org ~~ MIT License +//! fetch-json v3.2.2 ~~ https://fetch-json.js.org ~~ MIT License const fetchJson = { - version: '3.2.1', + version: '3.2.2', baseOptions: {}, getBaseOptions() { return this.baseOptions; diff --git a/dist/fetch-json.min.js b/dist/fetch-json.min.js index d625b02..c518057 100644 --- a/dist/fetch-json.min.js +++ b/dist/fetch-json.min.js @@ -1,2 +1,2 @@ -//! fetch-json v3.2.1 ~~ https://fetch-json.js.org ~~ MIT License -const fetchJson={version:"3.2.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}},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.2.2 ~~ https://fetch-json.js.org ~~ MIT License +const fetchJson={version:"3.2.2",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; diff --git a/dist/fetch-json.umd.cjs b/dist/fetch-json.umd.cjs index 2fc07c0..e469978 100644 --- a/dist/fetch-json.umd.cjs +++ b/dist/fetch-json.umd.cjs @@ -1,4 +1,4 @@ -//! fetch-json v3.2.1 ~~ https://fetch-json.js.org ~~ MIT License +//! fetch-json v3.2.2 ~~ https://fetch-json.js.org ~~ MIT License (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { @@ -13,7 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.FetchJson = exports.fetchJson = void 0; const fetchJson = { - version: '3.2.1', + version: '3.2.2', baseOptions: {}, getBaseOptions() { return this.baseOptions; diff --git a/package.json b/package.json index 83d173d..8eacaad 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "fetch-json", "description": "A wrapper around Fetch just for JSON", - "version": "3.2.1", + "version": "3.2.2", "license": "MIT", "type": "module", "module": "dist/fetch-json.js", diff --git a/task-runner.sh.command b/task-runner.sh.command index f77d68a..8f4cfbb 100755 --- a/task-runner.sh.command +++ b/task-runner.sh.command @@ -54,7 +54,7 @@ releaseInstructions() { echo "When ready to do the next release:" echo echo " === Increment version ===" - echo " Edit pacakge.json to bump $version to next version number" + echo " Edit package.json to bump $version to next version number" echo " $projectHome/package.json" } nextActionCommitTagPub() {