@exceptionless/fetchclient
Advanced tools
Comparing version 0.39.0 to 0.40.0
@@ -296,3 +296,3 @@ import { Counter } from "./Counter.js"; | ||
async fetchInternal(url, options, init) { | ||
url = this.buildUrl(url, options); | ||
const { builtUrl, absoluteUrl } = this.buildUrl(url, options); | ||
const accessToken = this.options.accessTokenFunc?.() ?? null; | ||
@@ -347,15 +347,7 @@ if (accessToken !== null) { | ||
try { | ||
request = new Request(url, init); | ||
request = new Request(builtUrl, init); | ||
} | ||
catch { | ||
// try converting to absolute URL | ||
const origin = globalThis.location?.origin ?? "http://localhost"; | ||
if (!url.startsWith("http")) { | ||
if (url.startsWith("/")) { | ||
request = new Request(origin + url, init); | ||
} | ||
else { | ||
request = new Request(origin + url, init); | ||
} | ||
} | ||
// try using absolute URL | ||
request = new Request(absoluteUrl, init); | ||
} | ||
@@ -471,9 +463,23 @@ const context = { | ||
const isAbsoluteUrl = builtUrl.startsWith("http"); | ||
const localhostOrigin = builtUrl.startsWith("/") | ||
? "http://localhost" | ||
: "http://localhost/"; | ||
const origin = isAbsoluteUrl | ||
? undefined | ||
: globalThis.location?.origin ?? localhostOrigin; | ||
const parsed = origin ? new URL(builtUrl, origin) : new URL(builtUrl); | ||
let parsed = undefined; | ||
if (isAbsoluteUrl) { | ||
parsed = new URL(builtUrl); | ||
} | ||
else if (globalThis.location?.origin && | ||
globalThis.location?.origin.startsWith("http")) { | ||
if (builtUrl.startsWith("/")) { | ||
parsed = new URL(builtUrl, globalThis.location.origin); | ||
} | ||
else { | ||
parsed = new URL(builtUrl, globalThis.location.origin + "/"); | ||
} | ||
} | ||
else { | ||
if (builtUrl.startsWith("/")) { | ||
parsed = new URL(builtUrl, "http://localhost"); | ||
} | ||
else { | ||
parsed = new URL(builtUrl, "http://localhost/"); | ||
} | ||
} | ||
if (options?.params) { | ||
@@ -485,8 +491,9 @@ for (const [key, value] of Object.entries(options?.params)) { | ||
} | ||
builtUrl = parsed.toString(); | ||
} | ||
builtUrl = parsed.toString(); | ||
const result = isAbsoluteUrl | ||
? builtUrl | ||
: `${parsed.pathname}${parsed.search}`; | ||
return result; | ||
console.log(builtUrl, result); | ||
return { builtUrl: result, absoluteUrl: builtUrl }; | ||
} | ||
@@ -493,0 +500,0 @@ validateResponse(response, options) { |
{ | ||
"name": "@exceptionless/fetchclient", | ||
"version": "0.39.0", | ||
"version": "0.40.0", | ||
"description": "A simple fetch client with middleware support for Deno and the browser.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -299,3 +299,3 @@ "use strict"; | ||
async fetchInternal(url, options, init) { | ||
url = this.buildUrl(url, options); | ||
const { builtUrl, absoluteUrl } = this.buildUrl(url, options); | ||
const accessToken = this.options.accessTokenFunc?.() ?? null; | ||
@@ -350,15 +350,7 @@ if (accessToken !== null) { | ||
try { | ||
request = new Request(url, init); | ||
request = new Request(builtUrl, init); | ||
} | ||
catch { | ||
// try converting to absolute URL | ||
const origin = globalThis.location?.origin ?? "http://localhost"; | ||
if (!url.startsWith("http")) { | ||
if (url.startsWith("/")) { | ||
request = new Request(origin + url, init); | ||
} | ||
else { | ||
request = new Request(origin + url, init); | ||
} | ||
} | ||
// try using absolute URL | ||
request = new Request(absoluteUrl, init); | ||
} | ||
@@ -474,9 +466,23 @@ const context = { | ||
const isAbsoluteUrl = builtUrl.startsWith("http"); | ||
const localhostOrigin = builtUrl.startsWith("/") | ||
? "http://localhost" | ||
: "http://localhost/"; | ||
const origin = isAbsoluteUrl | ||
? undefined | ||
: globalThis.location?.origin ?? localhostOrigin; | ||
const parsed = origin ? new URL(builtUrl, origin) : new URL(builtUrl); | ||
let parsed = undefined; | ||
if (isAbsoluteUrl) { | ||
parsed = new URL(builtUrl); | ||
} | ||
else if (globalThis.location?.origin && | ||
globalThis.location?.origin.startsWith("http")) { | ||
if (builtUrl.startsWith("/")) { | ||
parsed = new URL(builtUrl, globalThis.location.origin); | ||
} | ||
else { | ||
parsed = new URL(builtUrl, globalThis.location.origin + "/"); | ||
} | ||
} | ||
else { | ||
if (builtUrl.startsWith("/")) { | ||
parsed = new URL(builtUrl, "http://localhost"); | ||
} | ||
else { | ||
parsed = new URL(builtUrl, "http://localhost/"); | ||
} | ||
} | ||
if (options?.params) { | ||
@@ -488,8 +494,9 @@ for (const [key, value] of Object.entries(options?.params)) { | ||
} | ||
builtUrl = parsed.toString(); | ||
} | ||
builtUrl = parsed.toString(); | ||
const result = isAbsoluteUrl | ||
? builtUrl | ||
: `${parsed.pathname}${parsed.search}`; | ||
return result; | ||
console.log(builtUrl, result); | ||
return { builtUrl: result, absoluteUrl: builtUrl }; | ||
} | ||
@@ -496,0 +503,0 @@ validateResponse(response, options) { |
Sorry, the diff of this file is not supported yet
135339
2853