Comparing version 0.1.8 to 0.2.0
@@ -5,10 +5,21 @@ # Changelog | ||
### [0.1.8](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.1.7...v0.1.8) (2021-02-22) | ||
## [0.2.0](https://github.com/unjs/ohmyfetch/compare/v0.1.8...v0.2.0) (2021-04-06) | ||
### ⚠ BREAKING CHANGES | ||
* don't inline dependencies | ||
### Features | ||
* don't inline dependencies ([cf3578b](https://github.com/unjs/ohmyfetch/commit/cf3578baf265e1044f22c4ba42b227831c6fd183)) | ||
### [0.1.8](https://github.com/unjs/ohmyfetch/compare/v0.1.7...v0.1.8) (2021-02-22) | ||
### Bug Fixes | ||
* **pkg:** add top level node.d.ts ([dcc1358](https://github.com/nuxt-contrib/ohmyfetch/commit/dcc13582747ba8404dd26b48d3db755b7775b78b)) | ||
* **pkg:** add top level node.d.ts ([dcc1358](https://github.com/unjs/ohmyfetch/commit/dcc13582747ba8404dd26b48d3db755b7775b78b)) | ||
### [0.1.7](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.1.6...v0.1.7) (2021-02-19) | ||
### [0.1.7](https://github.com/unjs/ohmyfetch/compare/v0.1.6...v0.1.7) (2021-02-19) | ||
@@ -18,7 +29,7 @@ | ||
* support automatic json body for post requests ([#7](https://github.com/nuxt-contrib/ohmyfetch/issues/7)) ([97d0987](https://github.com/nuxt-contrib/ohmyfetch/commit/97d0987131e006e72aac6d1d4acb063f3e53953d)) | ||
* support automatic json body for post requests ([#7](https://github.com/unjs/ohmyfetch/issues/7)) ([97d0987](https://github.com/unjs/ohmyfetch/commit/97d0987131e006e72aac6d1d4acb063f3e53953d)) | ||
### [0.1.6](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.1.5...v0.1.6) (2021-01-12) | ||
### [0.1.6](https://github.com/unjs/ohmyfetch/compare/v0.1.5...v0.1.6) (2021-01-12) | ||
### [0.1.5](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.1.4...v0.1.5) (2021-01-04) | ||
### [0.1.5](https://github.com/unjs/ohmyfetch/compare/v0.1.4...v0.1.5) (2021-01-04) | ||
@@ -28,5 +39,5 @@ | ||
* **pkg:** use same export names for incompatible tools ([7fc450a](https://github.com/nuxt-contrib/ohmyfetch/commit/7fc450ac81596de1dea53380dc9ef3ae8ceb2304)) | ||
* **pkg:** use same export names for incompatible tools ([7fc450a](https://github.com/unjs/ohmyfetch/commit/7fc450ac81596de1dea53380dc9ef3ae8ceb2304)) | ||
### [0.1.4](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.1.3...v0.1.4) (2020-12-16) | ||
### [0.1.4](https://github.com/unjs/ohmyfetch/compare/v0.1.3...v0.1.4) (2020-12-16) | ||
@@ -36,5 +47,5 @@ | ||
* update ufo to 0.5 (reducing bundle size) ([837707d](https://github.com/nuxt-contrib/ohmyfetch/commit/837707d2ed03a7c6e69127849bf0c25ae182982d)) | ||
* update ufo to 0.5 (reducing bundle size) ([837707d](https://github.com/unjs/ohmyfetch/commit/837707d2ed03a7c6e69127849bf0c25ae182982d)) | ||
### [0.1.3](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.1.2...v0.1.3) (2020-12-16) | ||
### [0.1.3](https://github.com/unjs/ohmyfetch/compare/v0.1.2...v0.1.3) (2020-12-16) | ||
@@ -44,5 +55,5 @@ | ||
* ufo 0.3.1 ([e56e73e](https://github.com/nuxt-contrib/ohmyfetch/commit/e56e73e90bb6ad9be88f7c8413053744a64c702e)) | ||
* ufo 0.3.1 ([e56e73e](https://github.com/unjs/ohmyfetch/commit/e56e73e90bb6ad9be88f7c8413053744a64c702e)) | ||
### [0.1.2](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.1.1...v0.1.2) (2020-12-16) | ||
### [0.1.2](https://github.com/unjs/ohmyfetch/compare/v0.1.1...v0.1.2) (2020-12-16) | ||
@@ -52,5 +63,5 @@ | ||
* update ufo to 0.3 ([52d84e7](https://github.com/nuxt-contrib/ohmyfetch/commit/52d84e75034c3c6fd7542b2829e06f6d87f069c2)) | ||
* update ufo to 0.3 ([52d84e7](https://github.com/unjs/ohmyfetch/commit/52d84e75034c3c6fd7542b2829e06f6d87f069c2)) | ||
### [0.1.1](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.0.7...v0.1.1) (2020-12-12) | ||
### [0.1.1](https://github.com/unjs/ohmyfetch/compare/v0.0.7...v0.1.1) (2020-12-12) | ||
@@ -60,7 +71,7 @@ | ||
* preserve params when using baseURL ([c3a63e2](https://github.com/nuxt-contrib/ohmyfetch/commit/c3a63e2b337b09b082eb9faf8e23e818d866c49c)) | ||
* preserve params when using baseURL ([c3a63e2](https://github.com/unjs/ohmyfetch/commit/c3a63e2b337b09b082eb9faf8e23e818d866c49c)) | ||
### [0.0.7](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.0.6...v0.0.7) (2020-12-12) | ||
### [0.0.7](https://github.com/unjs/ohmyfetch/compare/v0.0.6...v0.0.7) (2020-12-12) | ||
### [0.0.6](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.0.5...v0.0.6) (2020-12-12) | ||
### [0.0.6](https://github.com/unjs/ohmyfetch/compare/v0.0.5...v0.0.6) (2020-12-12) | ||
@@ -70,5 +81,5 @@ | ||
* **pkg:** fix top level named exports ([0b51462](https://github.com/nuxt-contrib/ohmyfetch/commit/0b514620dcfa65d156397114b87ed5e4f28e33a1)) | ||
* **pkg:** fix top level named exports ([0b51462](https://github.com/unjs/ohmyfetch/commit/0b514620dcfa65d156397114b87ed5e4f28e33a1)) | ||
### [0.0.5](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.0.4...v0.0.5) (2020-12-12) | ||
### [0.0.5](https://github.com/unjs/ohmyfetch/compare/v0.0.4...v0.0.5) (2020-12-12) | ||
@@ -78,5 +89,5 @@ | ||
* **pkg:** fix ./node in exports ([c6b27b7](https://github.com/nuxt-contrib/ohmyfetch/commit/c6b27b7cb61d66444f3d43bfa5226057ec7a9c95)) | ||
* **pkg:** fix ./node in exports ([c6b27b7](https://github.com/unjs/ohmyfetch/commit/c6b27b7cb61d66444f3d43bfa5226057ec7a9c95)) | ||
### [0.0.4](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.0.3...v0.0.4) (2020-12-12) | ||
### [0.0.4](https://github.com/unjs/ohmyfetch/compare/v0.0.3...v0.0.4) (2020-12-12) | ||
@@ -86,5 +97,5 @@ | ||
* support params ([e6a56ff](https://github.com/nuxt-contrib/ohmyfetch/commit/e6a56ff083244fac918e29058aaf28bf87c98384)) | ||
* support params ([e6a56ff](https://github.com/unjs/ohmyfetch/commit/e6a56ff083244fac918e29058aaf28bf87c98384)) | ||
### [0.0.3](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.0.2...v0.0.3) (2020-12-12) | ||
### [0.0.3](https://github.com/unjs/ohmyfetch/compare/v0.0.2...v0.0.3) (2020-12-12) | ||
@@ -94,6 +105,6 @@ | ||
* bundle ufo and destr for easier bundler integration ([8f5ba88](https://github.com/nuxt-contrib/ohmyfetch/commit/8f5ba88f1ac0aa40ff2c99316da98a71d6dcc7e8)) | ||
* support `$fetch.raw` and improve docs ([f9f70a5](https://github.com/nuxt-contrib/ohmyfetch/commit/f9f70a59222bc0d0166cbe9a03eebf2a73682398)) | ||
* bundle ufo and destr for easier bundler integration ([8f5ba88](https://github.com/unjs/ohmyfetch/commit/8f5ba88f1ac0aa40ff2c99316da98a71d6dcc7e8)) | ||
* support `$fetch.raw` and improve docs ([f9f70a5](https://github.com/unjs/ohmyfetch/commit/f9f70a59222bc0d0166cbe9a03eebf2a73682398)) | ||
### [0.0.2](https://github.com/nuxt-contrib/ohmyfetch/compare/v0.0.1...v0.0.2) (2020-12-09) | ||
### [0.0.2](https://github.com/unjs/ohmyfetch/compare/v0.0.1...v0.0.2) (2020-12-09) | ||
@@ -103,3 +114,3 @@ | ||
* **pkg:** add top level dist ([6da17ca](https://github.com/nuxt-contrib/ohmyfetch/commit/6da17cad07e08cff9e5ea9e8b505638d560bcb47)) | ||
* **pkg:** add top level dist ([6da17ca](https://github.com/unjs/ohmyfetch/commit/6da17cad07e08cff9e5ea9e8b505638d560bcb47)) | ||
@@ -111,2 +122,2 @@ ### 0.0.1 (2020-12-09) | ||
* universal + isomorphic builds ([a873702](https://github.com/nuxt-contrib/ohmyfetch/commit/a873702c336c7ecce87c506d81c146db9f7516d0)) | ||
* universal + isomorphic builds ([a873702](https://github.com/unjs/ohmyfetch/commit/a873702c336c7ecce87c506d81c146db9f7516d0)) |
@@ -5,177 +5,9 @@ 'use strict'; | ||
const suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/; | ||
const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/; | ||
const JsonSigRx = /^["{[]|^-?[0-9][0-9.]{0,14}$/; | ||
function jsonParseTransform(key, value) { | ||
if (key === "__proto__" || key === "constructor") { | ||
return; | ||
} | ||
return value; | ||
} | ||
function destr(val) { | ||
if (typeof val !== "string") { | ||
return val; | ||
} | ||
const _lval = val.toLowerCase(); | ||
if (_lval === "true") { | ||
return true; | ||
} | ||
if (_lval === "false") { | ||
return false; | ||
} | ||
if (_lval === "null") { | ||
return null; | ||
} | ||
if (_lval === "nan") { | ||
return NaN; | ||
} | ||
if (_lval === "infinity") { | ||
return Infinity; | ||
} | ||
if (_lval === "undefined") { | ||
return void 0; | ||
} | ||
if (!JsonSigRx.test(val)) { | ||
return val; | ||
} | ||
try { | ||
if (suspectProtoRx.test(val) || suspectConstructorRx.test(val)) { | ||
return JSON.parse(val, jsonParseTransform); | ||
} | ||
return JSON.parse(val); | ||
} catch (_e) { | ||
return val; | ||
} | ||
} | ||
const destr = require('destr'); | ||
const ufo = require('ufo'); | ||
const HASH_RE = /#/g; | ||
const AMPERSAND_RE = /&/g; | ||
const EQUAL_RE = /=/g; | ||
const PLUS_RE = /\+/g; | ||
const ENC_BRACKET_OPEN_RE = /%5B/g; | ||
const ENC_BRACKET_CLOSE_RE = /%5D/g; | ||
const ENC_CARET_RE = /%5E/g; | ||
const ENC_BACKTICK_RE = /%60/g; | ||
const ENC_CURLY_OPEN_RE = /%7B/g; | ||
const ENC_PIPE_RE = /%7C/g; | ||
const ENC_CURLY_CLOSE_RE = /%7D/g; | ||
const ENC_SPACE_RE = /%20/g; | ||
function encode(text) { | ||
return encodeURI("" + text).replace(ENC_PIPE_RE, "|").replace(ENC_BRACKET_OPEN_RE, "[").replace(ENC_BRACKET_CLOSE_RE, "]"); | ||
} | ||
function encodeQueryValue(text) { | ||
return encode(text).replace(PLUS_RE, "%2B").replace(ENC_SPACE_RE, "+").replace(HASH_RE, "%23").replace(AMPERSAND_RE, "%26").replace(ENC_BACKTICK_RE, "`").replace(ENC_CURLY_OPEN_RE, "{").replace(ENC_CURLY_CLOSE_RE, "}").replace(ENC_CARET_RE, "^"); | ||
} | ||
function encodeQueryKey(text) { | ||
return encodeQueryValue(text).replace(EQUAL_RE, "%3D"); | ||
} | ||
function decode(text = "") { | ||
try { | ||
return decodeURIComponent("" + text); | ||
} catch (_err) { | ||
return "" + text; | ||
} | ||
} | ||
function decodeQueryValue(text) { | ||
return decode(text.replace(PLUS_RE, " ")); | ||
} | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
function parseQuery(paramsStr = "") { | ||
const obj = {}; | ||
if (paramsStr[0] === "?") { | ||
paramsStr = paramsStr.substr(1); | ||
} | ||
for (const param of paramsStr.split("&")) { | ||
const s = param.match(/([^=]+)=?(.*)/) || []; | ||
if (s.length < 2) { | ||
continue; | ||
} | ||
const key = decode(s[1]); | ||
const value = decodeQueryValue(s[2] || ""); | ||
if (obj[key]) { | ||
if (Array.isArray(obj[key])) { | ||
obj[key].push(value); | ||
} else { | ||
obj[key] = [obj[key], value]; | ||
} | ||
} else { | ||
obj[key] = value; | ||
} | ||
} | ||
return obj; | ||
} | ||
function encodeQueryItem(key, val) { | ||
if (!val) { | ||
return encodeQueryKey(key); | ||
} | ||
if (Array.isArray(val)) { | ||
return val.map((_val) => `${encodeQueryKey(key)}=${encodeQueryValue(_val)}`).join("&"); | ||
} | ||
return `${encodeQueryKey(key)}=${encodeQueryValue(val)}`; | ||
} | ||
function stringifyQuery(query) { | ||
return Object.keys(query).map((k) => encodeQueryItem(k, query[k])).join("&"); | ||
} | ||
const destr__default = /*#__PURE__*/_interopDefaultLegacy(destr); | ||
function hasProtocol(inputStr) { | ||
return /^\w+:\/\//.test(inputStr); | ||
} | ||
function withTrailingSlash(input = "") { | ||
return input.endsWith("/") ? input : input + "/"; | ||
} | ||
function hasLeadingSlash(input = "") { | ||
return input.startsWith("/"); | ||
} | ||
function withoutLeadingSlash(input = "") { | ||
return (hasLeadingSlash(input) ? input.substr(1) : input) || "/"; | ||
} | ||
function withQuery(input, query2) { | ||
const parsed = parseURL(input); | ||
const mergedQuery = {...parseQuery(parsed.search), ...query2}; | ||
parsed.search = stringifyQuery(mergedQuery); | ||
return stringifyParsedURL(parsed); | ||
} | ||
function joinURL(base, ...input) { | ||
let url2 = base || ""; | ||
for (const i of input) { | ||
const part = withoutLeadingSlash(i); | ||
if (part !== "/") { | ||
url2 = withTrailingSlash(url2) + part; | ||
} | ||
} | ||
return url2; | ||
} | ||
function parseURL(input = "") { | ||
if (!hasProtocol(input)) { | ||
return parsePath(input); | ||
} | ||
const [protocol, auth, hostAndPath] = (input.match(/([^:/]+:)\/\/([^/@]+@)?(.*)/) || []).splice(1); | ||
const [host = "", path = ""] = (hostAndPath.match(/([^/]*)(.*)?/) || []).splice(1); | ||
const {pathname, search, hash} = parsePath(path); | ||
return { | ||
protocol, | ||
auth: auth ? auth.substr(0, auth.length - 1) : "", | ||
host, | ||
pathname, | ||
search, | ||
hash | ||
}; | ||
} | ||
function parsePath(input = "") { | ||
const [pathname = "", search = "", hash = ""] = (input.match(/([^#?]*)(\?[^#]*)?(#.*)?/) || []).splice(1); | ||
return { | ||
pathname, | ||
search, | ||
hash | ||
}; | ||
} | ||
function stringifyParsedURL(parsed) { | ||
const fullpath = parsed.pathname + (parsed.search ? "?" + parsed.search : "") + parsed.hash; | ||
if (!parsed.protocol) { | ||
return fullpath; | ||
} | ||
return parsed.protocol + "//" + (parsed.auth ? parsed.auth + "@" : "") + parsed.host + fullpath; | ||
} | ||
class FetchError extends Error { | ||
@@ -232,6 +64,6 @@ constructor() { | ||
if (opts.baseURL) { | ||
request = joinURL(opts.baseURL, request); | ||
request = ufo.joinURL(opts.baseURL, request); | ||
} | ||
if (opts.params) { | ||
request = withQuery(request, opts.params); | ||
request = ufo.withQuery(request, opts.params); | ||
} | ||
@@ -245,3 +77,3 @@ if (opts.body && opts.body.toString() === "[object Object]" && payloadMethods.includes(((_a = opts.method) == null ? void 0 : _a.toLowerCase()) || "")) { | ||
const text = await response.text(); | ||
response.data = destr(text); | ||
response.data = destr__default['default'](text); | ||
if (!response.ok) { | ||
@@ -248,0 +80,0 @@ throw createFetchError(request, response); |
200
dist/node.js
@@ -6,182 +6,30 @@ 'use strict'; | ||
const nodeFetch = require('node-fetch'); | ||
const destr = require('destr'); | ||
const ufo = require('ufo'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
const nodeFetch__default = /*#__PURE__*/_interopDefaultLegacy(nodeFetch); | ||
const suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/; | ||
const suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/; | ||
const JsonSigRx = /^["{[]|^-?[0-9][0-9.]{0,14}$/; | ||
function jsonParseTransform(key, value) { | ||
if (key === "__proto__" || key === "constructor") { | ||
return; | ||
} | ||
return value; | ||
} | ||
function destr(val) { | ||
if (typeof val !== "string") { | ||
return val; | ||
} | ||
const _lval = val.toLowerCase(); | ||
if (_lval === "true") { | ||
return true; | ||
} | ||
if (_lval === "false") { | ||
return false; | ||
} | ||
if (_lval === "null") { | ||
return null; | ||
} | ||
if (_lval === "nan") { | ||
return NaN; | ||
} | ||
if (_lval === "infinity") { | ||
return Infinity; | ||
} | ||
if (_lval === "undefined") { | ||
return void 0; | ||
} | ||
if (!JsonSigRx.test(val)) { | ||
return val; | ||
} | ||
try { | ||
if (suspectProtoRx.test(val) || suspectConstructorRx.test(val)) { | ||
return JSON.parse(val, jsonParseTransform); | ||
} | ||
return JSON.parse(val); | ||
} catch (_e) { | ||
return val; | ||
} | ||
} | ||
const HASH_RE = /#/g; | ||
const AMPERSAND_RE = /&/g; | ||
const EQUAL_RE = /=/g; | ||
const PLUS_RE = /\+/g; | ||
const ENC_BRACKET_OPEN_RE = /%5B/g; | ||
const ENC_BRACKET_CLOSE_RE = /%5D/g; | ||
const ENC_CARET_RE = /%5E/g; | ||
const ENC_BACKTICK_RE = /%60/g; | ||
const ENC_CURLY_OPEN_RE = /%7B/g; | ||
const ENC_PIPE_RE = /%7C/g; | ||
const ENC_CURLY_CLOSE_RE = /%7D/g; | ||
const ENC_SPACE_RE = /%20/g; | ||
function encode(text) { | ||
return encodeURI("" + text).replace(ENC_PIPE_RE, "|").replace(ENC_BRACKET_OPEN_RE, "[").replace(ENC_BRACKET_CLOSE_RE, "]"); | ||
} | ||
function encodeQueryValue(text) { | ||
return encode(text).replace(PLUS_RE, "%2B").replace(ENC_SPACE_RE, "+").replace(HASH_RE, "%23").replace(AMPERSAND_RE, "%26").replace(ENC_BACKTICK_RE, "`").replace(ENC_CURLY_OPEN_RE, "{").replace(ENC_CURLY_CLOSE_RE, "}").replace(ENC_CARET_RE, "^"); | ||
} | ||
function encodeQueryKey(text) { | ||
return encodeQueryValue(text).replace(EQUAL_RE, "%3D"); | ||
} | ||
function decode(text = "") { | ||
try { | ||
return decodeURIComponent("" + text); | ||
} catch (_err) { | ||
return "" + text; | ||
} | ||
} | ||
function decodeQueryValue(text) { | ||
return decode(text.replace(PLUS_RE, " ")); | ||
} | ||
function parseQuery(paramsStr = "") { | ||
const obj = {}; | ||
if (paramsStr[0] === "?") { | ||
paramsStr = paramsStr.substr(1); | ||
} | ||
for (const param of paramsStr.split("&")) { | ||
const s = param.match(/([^=]+)=?(.*)/) || []; | ||
if (s.length < 2) { | ||
continue; | ||
} | ||
const key = decode(s[1]); | ||
const value = decodeQueryValue(s[2] || ""); | ||
if (obj[key]) { | ||
if (Array.isArray(obj[key])) { | ||
obj[key].push(value); | ||
} else { | ||
obj[key] = [obj[key], value]; | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { | ||
return e[k]; | ||
} | ||
}); | ||
} | ||
} else { | ||
obj[key] = value; | ||
} | ||
}); | ||
} | ||
return obj; | ||
n['default'] = e; | ||
return Object.freeze(n); | ||
} | ||
function encodeQueryItem(key, val) { | ||
if (!val) { | ||
return encodeQueryKey(key); | ||
} | ||
if (Array.isArray(val)) { | ||
return val.map((_val) => `${encodeQueryKey(key)}=${encodeQueryValue(_val)}`).join("&"); | ||
} | ||
return `${encodeQueryKey(key)}=${encodeQueryValue(val)}`; | ||
} | ||
function stringifyQuery(query) { | ||
return Object.keys(query).map((k) => encodeQueryItem(k, query[k])).join("&"); | ||
} | ||
function hasProtocol(inputStr) { | ||
return /^\w+:\/\//.test(inputStr); | ||
} | ||
function withTrailingSlash(input = "") { | ||
return input.endsWith("/") ? input : input + "/"; | ||
} | ||
function hasLeadingSlash(input = "") { | ||
return input.startsWith("/"); | ||
} | ||
function withoutLeadingSlash(input = "") { | ||
return (hasLeadingSlash(input) ? input.substr(1) : input) || "/"; | ||
} | ||
function withQuery(input, query2) { | ||
const parsed = parseURL(input); | ||
const mergedQuery = {...parseQuery(parsed.search), ...query2}; | ||
parsed.search = stringifyQuery(mergedQuery); | ||
return stringifyParsedURL(parsed); | ||
} | ||
function joinURL(base, ...input) { | ||
let url2 = base || ""; | ||
for (const i of input) { | ||
const part = withoutLeadingSlash(i); | ||
if (part !== "/") { | ||
url2 = withTrailingSlash(url2) + part; | ||
} | ||
} | ||
return url2; | ||
} | ||
const nodeFetch__namespace = /*#__PURE__*/_interopNamespace(nodeFetch); | ||
const destr__default = /*#__PURE__*/_interopDefaultLegacy(destr); | ||
function parseURL(input = "") { | ||
if (!hasProtocol(input)) { | ||
return parsePath(input); | ||
} | ||
const [protocol, auth, hostAndPath] = (input.match(/([^:/]+:)\/\/([^/@]+@)?(.*)/) || []).splice(1); | ||
const [host = "", path = ""] = (hostAndPath.match(/([^/]*)(.*)?/) || []).splice(1); | ||
const {pathname, search, hash} = parsePath(path); | ||
return { | ||
protocol, | ||
auth: auth ? auth.substr(0, auth.length - 1) : "", | ||
host, | ||
pathname, | ||
search, | ||
hash | ||
}; | ||
} | ||
function parsePath(input = "") { | ||
const [pathname = "", search = "", hash = ""] = (input.match(/([^#?]*)(\?[^#]*)?(#.*)?/) || []).splice(1); | ||
return { | ||
pathname, | ||
search, | ||
hash | ||
}; | ||
} | ||
function stringifyParsedURL(parsed) { | ||
const fullpath = parsed.pathname + (parsed.search ? "?" + parsed.search : "") + parsed.hash; | ||
if (!parsed.protocol) { | ||
return fullpath; | ||
} | ||
return parsed.protocol + "//" + (parsed.auth ? parsed.auth + "@" : "") + parsed.host + fullpath; | ||
} | ||
class FetchError extends Error { | ||
@@ -238,6 +86,6 @@ constructor() { | ||
if (opts.baseURL) { | ||
request = joinURL(opts.baseURL, request); | ||
request = ufo.joinURL(opts.baseURL, request); | ||
} | ||
if (opts.params) { | ||
request = withQuery(request, opts.params); | ||
request = ufo.withQuery(request, opts.params); | ||
} | ||
@@ -251,3 +99,3 @@ if (opts.body && opts.body.toString() === "[object Object]" && payloadMethods.includes(((_a = opts.method) == null ? void 0 : _a.toLowerCase()) || "")) { | ||
const text = await response.text(); | ||
response.data = destr(text); | ||
response.data = destr__default['default'](text); | ||
if (!response.ok) { | ||
@@ -266,3 +114,3 @@ throw createFetchError(request, response); | ||
const $fetch = createFetch({ | ||
fetch: nodeFetch__default['default'] | ||
fetch: nodeFetch__namespace.default | ||
}); | ||
@@ -269,0 +117,0 @@ |
{ | ||
"name": "ohmyfetch", | ||
"version": "0.1.8", | ||
"version": "0.2.0", | ||
"description": "oh-my-fetch", | ||
"repository": "nuxt-contrib/ohmyfetch", | ||
"repository": "unjs/ohmyfetch", | ||
"license": "MIT", | ||
@@ -34,3 +34,5 @@ "sideEffects": false, | ||
"dependencies": { | ||
"node-fetch": "^2.6.1" | ||
"destr": "^1.1.0", | ||
"node-fetch": "^2.6.1", | ||
"ufo": "^0.6.10" | ||
}, | ||
@@ -43,3 +45,2 @@ "devDependencies": { | ||
"@types/node-fetch": "latest", | ||
"destr": "latest", | ||
"eslint": "latest", | ||
@@ -53,5 +54,4 @@ "h3": "latest", | ||
"ts-jest": "latest", | ||
"typescript": "latest", | ||
"ufo": "latest" | ||
"typescript": "latest" | ||
} | ||
} |
@@ -40,7 +40,5 @@ | ||
## ✔️ Parsing Response | ||
`$fetch` Smartly parses JSON and native values using [destr](https://github.com/nuxt-contrib/destr) and fallback to text if cannot parse | ||
`$fetch` Smartly parses JSON and native values using [destr](https://github.com/unjs/destr) and fallback to text if cannot parse | ||
@@ -88,3 +86,3 @@ ```js | ||
By using `baseURL` option, `$fetch` prepends it with respecting to trailing/leading slashes and query params for baseURL using [ufo](https://github.com/nuxt-contrib/ufo): | ||
By using `baseURL` option, `$fetch` prepends it with respecting to trailing/leading slashes and query params for baseURL using [ufo](https://github.com/unjs/ufo): | ||
@@ -97,3 +95,3 @@ ```js | ||
By using `params` option, `$fetch` adds params to URL by preserving params in request itself using [ufo](https://github.com/nuxt-contrib/ufo): | ||
By using `params` option, `$fetch` adds params to URL by preserving params in request itself using [ufo](https://github.com/unjs/ufo): | ||
@@ -116,3 +114,2 @@ ```js | ||
## 📦 Bundler Notes | ||
@@ -122,3 +119,3 @@ | ||
- No export is transpiled for sake of Modern syntax | ||
- You probably need to transpile `ohmyfetch` package with babel for ES5 support | ||
- You probably need to transpile `ohmyfetch`, `destr` and `ufo` packages with babel for ES5 support | ||
- You need to polyfill `fetch` global for supporting legacy browsers like using [unfetch](https://github.com/developit/unfetch) | ||
@@ -155,10 +152,9 @@ | ||
[github-actions-src]: https://img.shields.io/github/workflow/status/nuxt-contrib/ohmyfetch/ci/main?style=flat-square | ||
[github-actions-href]: https://github.com/nuxt-contrib/ohmyfetch/actions?query=workflow%3Aci | ||
[github-actions-src]: https://img.shields.io/github/workflow/status/unjs/ohmyfetch/ci/main?style=flat-square | ||
[github-actions-href]: https://github.com/unjs/ohmyfetch/actions?query=workflow%3Aci | ||
[codecov-src]: https://img.shields.io/codecov/c/gh/nuxt-contrib/ohmyfetch/main?style=flat-square | ||
[codecov-href]: https://codecov.io/gh/nuxt-contrib/ohmyfetch | ||
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/ohmyfetch/main?style=flat-square | ||
[codecov-href]: https://codecov.io/gh/unjs/ohmyfetch | ||
[bundle-src]: https://img.shields.io/bundlephobia/minzip/ohmyfetch?style=flat-square | ||
[bundle-href]: https://bundlephobia.com/result?p=ohmyfetch | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
14
26316
3
459
155
+ Addeddestr@^1.1.0
+ Addedufo@^0.6.10
+ Addeddestr@1.2.2(transitive)
+ Addedufo@0.6.12(transitive)