Skip to content

Commit 16dc973

Browse files
committed
fix: #url method
1 parent 6f64efa commit 16dc973

File tree

3 files changed

+34
-43
lines changed

3 files changed

+34
-43
lines changed

.changeset/pre.json

-32
This file was deleted.

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# aspi
22

3-
### ⚠️ I'm actively iterating over the ergonomics of this library. Please refrain from using it in production until I release a stable version. Meanwhile please feel free to open issues and contribute.
4-
53
I made this project because I am not happy with any of the Rest API clients available in eco system. Sure, Axios is great but it feels so bloated and I am never going to use interceptors or any of the other features it provides. I just want to make a simple request and get the response. That's it. So, I made this project. It is a simple Rest API client that is built on top of native fetch API. It is very simple to use and has a very small bundle size. It is perfect for small projects where you don't want to bloat your project with unnecessary features.
64

75
## Why Aspi?

src/request.ts

+34-9
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,8 @@ export class Request<
414414
response.json().catch(
415415
(e) =>
416416
new CustomError('jsonParseError', {
417-
data: {
418-
message: e instanceof Error ? e.message : 'Failed to parse JSON',
419-
},
420-
}),
417+
message: e instanceof Error ? e.message : 'Failed to parse JSON',
418+
}) as JSONParseError,
421419
),
422420
);
423421
}
@@ -450,6 +448,36 @@ export class Request<
450448
return this.#makeRequest<string>((response) => response.text());
451449
}
452450

451+
#url() {
452+
// Normalize base URL by removing trailing slashes
453+
const baseUrl = this.#localRequestInit.baseUrl?.replace(/\/+$/, '') ?? '';
454+
455+
// Ensure path starts with exactly one forward slash
456+
const path = this.#path.replace(/^\/+/, '/');
457+
458+
// Safely concatenate URL parts and handle query params
459+
const queryString = this.#queryParams
460+
? `?${encodeURIComponent(this.#queryParams.toString())}`
461+
: '';
462+
463+
const url = [baseUrl, path, queryString].filter(Boolean).join('');
464+
465+
return url;
466+
}
467+
468+
/**
469+
* Returns the complete URL for the request including base URL, path, and query parameters.
470+
* @returns The complete URL string
471+
* @example
472+
* const request = new Request('/users', config);
473+
* request.setBaseUrl('https://api.example.com');
474+
* request.setQueryParams({ id: '123' });
475+
* console.log(request.url()); // 'https://api.example.com/users?id=123'
476+
*/
477+
url() {
478+
return this.#url();
479+
}
480+
453481
async #makeRequest<T>(
454482
responseParser: (response: Response) => Promise<any>,
455483
): Promise<
@@ -472,11 +500,8 @@ export class Request<
472500
// request init with certain extra properties
473501
const requestInit = request.requestInit;
474502

475-
// url
476-
const url = [
477-
new URL(this.#path, this.#localRequestInit.baseUrl).toString(),
478-
this.#queryParams ? `?${this.#queryParams.toString()}` : '',
479-
].join('');
503+
// URL
504+
const url = this.#url();
480505

481506
let attempts = 0;
482507
let response;

0 commit comments

Comments
 (0)