Skip to content

Commit

Permalink
feat!: Standardize baseURL
Browse files Browse the repository at this point in the history
- Remove long-deprecated `baseUrl`
- Use proper `base` URL logic via `new URL(url, base)`
  • Loading branch information
danielbankhead committed Feb 1, 2024
1 parent ef40c61 commit 8490f24
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ over other authentication methods, i.e., application default credentials.
```js
{
// The url to which the request should be sent. Required.
url: string,
url: string | URL,

// The HTTP method to use for the request. Defaults to `GET`.
method: 'GET',

// The base Url to use for the request. Prepended to the `url` property above.
baseURL: 'https://example.com';
// The base Url to use for the request.
// Resolved as `new URL(url, baseURL)`
baseURL: 'https://example.com/v1/' | URL;

// The HTTP methods to be sent with the request.
headers: { 'some': 'header' },
Expand Down
4 changes: 0 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ export interface GaxiosOptions {
defaultAdapter: (options: GaxiosOptions) => GaxiosPromise<T>
) => GaxiosPromise<T>;
url?: string | URL;
/**
* @deprecated
*/
baseUrl?: string;
baseURL?: string | URL;
method?:
| 'GET'
Expand Down
8 changes: 4 additions & 4 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ export class Gaxios {
throw new Error('URL is required.');
}

// baseUrl has been deprecated, remove in 2.0
const baseUrl = opts.baseUrl || opts.baseURL;
if (baseUrl) {
opts.url = baseUrl.toString() + opts.url;
if (opts.baseURL) {
const base =
opts.baseURL instanceof URL ? opts.baseURL : new URL(opts.baseURL);
opts.url = new URL(opts.url.toString(), base);
}

opts.paramsSerializer = opts.paramsSerializer || this.paramsSerializer;
Expand Down
4 changes: 2 additions & 2 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ describe('🥁 configuration options', () => {

it('should allow setting a base url in the options', async () => {
const scope = nock(url).get('/v1/mango').reply(200, {});
const inst = new Gaxios({baseURL: `${url}/v1`});
const res = await inst.request({url: '/mango'});
const inst = new Gaxios({baseURL: `${url}/v1/`});
const res = await inst.request({url: 'mango'});
scope.done();
assert.deepStrictEqual(res.data, {});
});
Expand Down
10 changes: 4 additions & 6 deletions test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,12 @@ describe('🛸 retry & exponential backoff', () => {
scope.done();
});

it('should retain the baseUrl on retry', async () => {
it('should retain the baseURL on retry', async () => {
const body = {pumpkin: '🥧'};
const url = '/path';
const baseUrl = 'http://example.com';
const scope = nock(baseUrl).get(url).reply(500).get(url).reply(200, body);
const gaxios = new Gaxios({
baseUrl,
});
const baseURL = 'http://example.com';
const scope = nock(baseURL).get(url).reply(500).get(url).reply(200, body);
const gaxios = new Gaxios({baseURL});
const res = await gaxios.request({
url,
retry: true,
Expand Down

0 comments on commit 8490f24

Please sign in to comment.