Fix streaming methods to support baseURL resolution

- Extract URL resolution logic into resolveUrl() helper method
- Fix stream() and streamJSON() to use baseURL for relative URLs
- Prevents 'Failed to parse URL' errors when using relative paths
- Bump version to 0.1.3
This commit is contained in:
Casey Collier 2025-07-20 16:15:36 -04:00
parent 4affa51978
commit 0459622c43
3 changed files with 11 additions and 7 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@catalystlabs/typedfetch",
"version": "0.1.2",
"version": "0.1.3",
"description": "Type-safe HTTP client that doesn't suck - Fetch for humans who have stuff to build",
"type": "module",
"main": "./dist/index.js",

View file

@ -151,19 +151,22 @@ export class RevolutionaryTypedFetch {
return this.request<T>('DELETE', url, options)
}
private async request<T>(method: string, url: string, options: RequestInit = {}): Promise<{ data: T; response: Response }> {
private resolveUrl(url: string): string {
// Use baseURL from config or instance
const baseURL = this.config.request.baseURL || this.baseURL
// Construct full URL
let fullUrl: string
if (url.startsWith('http')) {
fullUrl = url
return url
} else if (baseURL) {
fullUrl = new URL(url, baseURL).toString()
return new URL(url, baseURL).toString()
} else {
throw new Error(`Relative URL "${url}" requires a baseURL to be set`)
}
}
private async request<T>(method: string, url: string, options: RequestInit = {}): Promise<{ data: T; response: Response }> {
const fullUrl = this.resolveUrl(url)
const cacheKey = `${method}:${fullUrl}`
const startTime = performance.now()
let cached = false
@ -378,7 +381,8 @@ export class RevolutionaryTypedFetch {
// Streaming support
async stream(url: string): Promise<ReadableStream> {
const response = await fetch(url)
const fullUrl = this.resolveUrl(url)
const response = await fetch(fullUrl)
if (!response.body) throw new Error('No response body')
return response.body
}

View file

@ -9,7 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"@catalystlabs/typedfetch": "^0.1.1",
"@catalystlabs/typedfetch": "^0.1.2",
"@mantine/code-highlight": "^8.0.0",
"@mantine/core": "^8.0.0",
"@mantine/hooks": "^8.0.0",