Skip to content

Commit

Permalink
feat!: Standardize baseURL to URL’s spec (#600)
Browse files Browse the repository at this point in the history
* feat!: Standardize `baseURL`

- Remove long-deprecated `baseUrl`
- Use proper `base` URL logic via `new URL(url, base)`

* refactor: Use updated `URL` types

via:
- DefinitelyTyped/DefinitelyTyped#68454
- DefinitelyTyped/DefinitelyTyped#68456

* chore: pin `karma-webpack`
  • Loading branch information
danielbankhead authored Oct 29, 2024
1 parent de28c24 commit ffff324
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 20 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ over other authentication methods, i.e., application default credentials.
```ts
interface GaxiosOptions = {
// 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: new Headers(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"karma-remap-coverage": "^0.1.5",
"karma-sourcemap-loader": "^0.4.0",
"karma-webpack": "5.0.0",
"linkinator": "^3.0.0",
"linkinator": "^4.0.0",
"mocha": "^8.0.0",
"multiparty": "^4.2.1",
"mv": "^2.1.1",
Expand Down
4 changes: 0 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ export interface GaxiosOptions extends RequestInit {
* ```
*/
headers?: Headers;
/**
* @deprecated
*/
baseUrl?: string;
baseURL?: string | URL;
/**
* The data to send in the {@link RequestInit.body} of the request. Objects will be
Expand Down
6 changes: 2 additions & 4 deletions src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,8 @@ 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) {
opts.url = new URL(opts.url, opts.baseURL);
}

// don't modify the properties of a default or provided URL
Expand Down
4 changes: 2 additions & 2 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,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 ffff324

Please sign in to comment.