Skip to content

Commit

Permalink
apiFetch: Use URL computation in apiFetch.
Browse files Browse the repository at this point in the history
For #4146, but also to fix a recent regression from 865914f.

The URL constructor already throws a special "Invalid URL" error if
it can't put together a valid URL from the inputs, so, nicely, we
get to remove the `isValidUrl` call site.

`isValidUrl` failed to alert us of a double-forward-slash issue in
this string concatenation: the realm gained a trailing slash in its
new spelling as `realm.origin` from 865914f, and we kept the
leading slash in what we were adding to the end of the realm. This
is a sign that `isValidUrl` should be retired ASAP; we'll do that in
an upcoming commit.
  • Loading branch information
chrisbobbe committed Sep 22, 2020
1 parent c917673 commit 4efb34f
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/api/apiFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Sentry from '@sentry/react-native';
import type { UrlParams } from '../utils/url';
import type { Auth } from './transportTypes';
import { getAuthHeaders } from './transport';
import { encodeParamsForUrl, isValidUrl } from '../utils/url';
import { encodeParamsForUrl } from '../utils/url';
import userAgent from '../utils/userAgent';
import { networkActivityStart, networkActivityStop } from '../utils/networkActivity';
import { makeErrorFromApi } from './apiErrors';
Expand Down Expand Up @@ -34,15 +34,7 @@ export const apiFetch = async (
auth: Auth,
route: string,
params: $Diff<$Exact<RequestOptions>, {| headers: mixed |}>,
) => {
const url = `${auth.realm.toString()}/${apiVersion}/${route}`;

if (!isValidUrl(url)) {
throw new Error(`Invalid url ${url}`);
}

return fetch(url, getFetchParams(auth, params));
};
) => fetch(new URL(`/${apiVersion}/${route}`, auth.realm).toString(), getFetchParams(auth, params));

export const apiCall = async (
auth: Auth,
Expand Down

0 comments on commit 4efb34f

Please sign in to comment.