Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Standardize baseURL to URL’s spec #600

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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