fetch-request-browser
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -51,2 +51,3 @@ /** | ||
}; | ||
skipStatusCodeValidation: boolean; | ||
} | ||
@@ -53,0 +54,0 @@ /** |
@@ -1,1 +0,1 @@ | ||
import{encodeError,isEncodedError}from"error-message-utils";import{ERRORS}from"../shared/errors.js";const __buildRequestInput=e=>{if(e instanceof URL)return e;try{return new URL(e)}catch(e){throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_URL))}},__buildRequestHeaders=(e,r)=>{let t;if(e&&"object"==typeof e)try{t=new Headers(e)}catch(e){throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_HEADERS))}else t=e instanceof Headers?e:"GET"===r?new Headers({Accept:"application/json"}):new Headers({Accept:"application/json","Content-Type":"application/json"});return t.has("Accept")||t.append("Accept","application/json"),t.has("Content-Type")||"GET"===r||t.append("Content-Type","application/json"),t},__buildRequestBody=e=>e?"object"==typeof e?JSON.stringify(e):e:null,__buildRequestOptions=(e={})=>{const r=e.method??"GET";return{method:r,mode:e.mode??"cors",cache:e.cache??"default",credentials:e.credentials??"same-origin",headers:__buildRequestHeaders(e.headers,r),priority:e.priority??"auto",redirect:e.redirect??"follow",referrer:e.referrer??"about:client",referrerPolicy:e.referrerPolicy??"no-referrer-when-downgrade",signal:e.signal,integrity:e.integrity||"",keepalive:e.keepalive??!1,body:"GET"===r?null:__buildRequestBody(e.body)}},buildRequest=(e,r)=>{try{return new Request(__buildRequestInput(e),__buildRequestOptions(r))}catch(e){if(isEncodedError(e))throw e;throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_OPTIONS))}},extractResponseData=async(e,r)=>{switch(r){case"arrayBuffer":return e.arrayBuffer();case"blob":return e.blob();case"formData":return e.formData();case"json":return e.json();case"text":return e.text();default:throw new Error(encodeError(`The provided response data type '${r}' is invalid.`,ERRORS.INVALID_RESPONSE_DTYPE))}},buildOptions=(e={})=>({requestOptions:e.requestOptions,responseDataType:e.responseDataType??"json",acceptableStatusCodes:e.acceptableStatusCodes,acceptableStatusCodesRange:e.acceptableStatusCodesRange??{min:200,max:299}}),delay=e=>new Promise((r=>{setTimeout(r,1e3*e)}));export{buildRequest,extractResponseData,buildOptions,delay}; | ||
import{encodeError,isEncodedError}from"error-message-utils";import{ERRORS}from"../shared/errors.js";const __buildRequestInput=e=>{if(e instanceof URL)return e;try{return new URL(e)}catch(e){throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_URL))}},__buildRequestHeaders=(e,r)=>{let t;if(e&&"object"==typeof e)try{t=new Headers(e)}catch(e){throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_HEADERS))}else t=e instanceof Headers?e:"GET"===r?new Headers({Accept:"application/json"}):new Headers({Accept:"application/json","Content-Type":"application/json"});return t.has("Accept")||t.append("Accept","application/json"),t.has("Content-Type")||"GET"===r||t.append("Content-Type","application/json"),t},__buildRequestBody=e=>e?"object"==typeof e?JSON.stringify(e):e:null,__buildRequestOptions=(e={})=>{const r=e.method??"GET";return{method:r,mode:e.mode??"cors",cache:e.cache??"default",credentials:e.credentials??"same-origin",headers:__buildRequestHeaders(e.headers,r),priority:e.priority??"auto",redirect:e.redirect??"follow",referrer:e.referrer??"about:client",referrerPolicy:e.referrerPolicy??"no-referrer-when-downgrade",signal:e.signal,integrity:e.integrity||"",keepalive:e.keepalive??!1,body:"GET"===r?null:__buildRequestBody(e.body)}},buildRequest=(e,r)=>{try{return new Request(__buildRequestInput(e),__buildRequestOptions(r))}catch(e){if(isEncodedError(e))throw e;throw new Error(encodeError(e,ERRORS.INVALID_REQUEST_OPTIONS))}},extractResponseData=async(e,r)=>{switch(r){case"arrayBuffer":return e.arrayBuffer();case"blob":return e.blob();case"formData":return e.formData();case"json":return e.json();case"text":return e.text();default:throw new Error(encodeError(`The provided response data type '${r}' is invalid.`,ERRORS.INVALID_RESPONSE_DTYPE))}},buildOptions=(e={})=>({requestOptions:e.requestOptions,responseDataType:e.responseDataType??"json",acceptableStatusCodes:e.acceptableStatusCodes,acceptableStatusCodesRange:e.acceptableStatusCodesRange??{min:200,max:299},skipStatusCodeValidation:e.skipStatusCodeValidation??!1}),delay=e=>new Promise((r=>{setTimeout(r,1e3*e)}));export{buildRequest,extractResponseData,buildOptions,delay}; |
@@ -1,1 +0,1 @@ | ||
import{encodeError}from"error-message-utils";import{ERRORS}from"../shared/errors.js";const __buildUnexpectedCodeErrorMessage=e=>encodeError(`Request Failed: received unexpected response code '${e.status}': ${e.statusText}`,ERRORS.UNEXPECTED_RESPONSE_STATUS_CODE),__validateStatusCode=(e,t)=>{if(Array.isArray(t.acceptableStatusCodes)&&t.acceptableStatusCodes.length){if(!t.acceptableStatusCodes.includes(e.status))throw new Error(__buildUnexpectedCodeErrorMessage(e))}else if(e.status<t.acceptableStatusCodesRange.min||e.status>t.acceptableStatusCodesRange.max)throw new Error(__buildUnexpectedCodeErrorMessage(e))},__validateContentType=(e,t)=>{const r=e.headers.get("Accept"),s=t.headers.get("Content-Type");if("string"!=typeof s||!s.length)throw new Error(encodeError(`The response's Content-Type Header is invalid. Received: '${s}'.`,ERRORS.INVALID_RESPONSE_CONTENT_TYPE));if(!s.includes(r))throw new Error(encodeError(`The request's Accept Header '${r}' is different to the Content-Type received in the response '${s}'.`,ERRORS.CONTENT_TYPE_MISSMATCH))},validateResponse=(e,t,r)=>{__validateStatusCode(t,r),__validateContentType(e,t)};export{validateResponse}; | ||
import{encodeError}from"error-message-utils";import{ERRORS}from"../shared/errors.js";const __buildUnexpectedCodeErrorMessage=e=>encodeError(`Request Failed: received unexpected response code '${e.status}': ${e.statusText}`,ERRORS.UNEXPECTED_RESPONSE_STATUS_CODE),__validateStatusCode=(e,t)=>{if(Array.isArray(t.acceptableStatusCodes)&&t.acceptableStatusCodes.length){if(!t.acceptableStatusCodes.includes(e.status))throw new Error(__buildUnexpectedCodeErrorMessage(e))}else if(e.status<t.acceptableStatusCodesRange.min||e.status>t.acceptableStatusCodesRange.max)throw new Error(__buildUnexpectedCodeErrorMessage(e))},__validateContentType=(e,t)=>{const r=e.headers.get("Accept"),s=t.headers.get("Content-Type");if("string"!=typeof s||!s.length)throw new Error(encodeError(`The response's Content-Type Header is invalid. Received: '${s}'.`,ERRORS.INVALID_RESPONSE_CONTENT_TYPE));if(!s.includes(r))throw new Error(encodeError(`The request's Accept Header '${r}' is different to the Content-Type received in the response '${s}'.`,ERRORS.CONTENT_TYPE_MISSMATCH))},validateResponse=(e,t,r)=>{r.skipStatusCodeValidation||__validateStatusCode(t,r),__validateContentType(e,t)};export{validateResponse}; |
{ | ||
"name": "fetch-request-browser", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "The fetch-request-browser package makes working with external APIs simple and efficient. This intuitive wrapper leverages the power of the Fetch API, providing a clean and concise interface for your API interactions.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22208
220