diff --git a/package-lock.json b/package-lock.json index dc5a036..c71c4e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "react-rest-request", - "version": "0.4.0", + "version": "0.4.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index bfcb17b..0bbfa0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-rest-request", - "version": "0.4.0", + "version": "0.4.1", "description": "Minimalistic REST API client for React inspired by Apollo", "readme": "README.md", "main": "./target/index.js", diff --git a/target/client.d.ts b/target/client.d.ts index 45d62c6..53c1583 100644 --- a/target/client.d.ts +++ b/target/client.d.ts @@ -17,7 +17,7 @@ export declare type ClientResponse> = Readonly< export declare class Client { private config; constructor(config: ClientConfig); - private prepareRequest; + prepareRequest(props: PrepareRequestProps): Request; request>({ transformResponseData, ...restProps }: RequestProps): Promise>; } export {}; diff --git a/target/client.js b/target/client.js index b5c4fa5..73e7d6c 100644 --- a/target/client.js +++ b/target/client.js @@ -17,10 +17,16 @@ export class Client { this.config = config; } prepareRequest(props) { + var _a; const requestCanContainBody = [Method.POST, Method.PATCH, Method.PUT].includes(props.method); - const url = /https?:\/\//.test(props.url) ? - new URL(props.url) - : new URL(this.config.baseUrl + props.url); + const defaultBaseUrl = (_a = window) === null || _a === void 0 ? void 0 : _a.location.href; + const sourceUrl = /https?:\/\//.test(props.url) ? + props.url + : this.config.baseUrl + props.url; + if (!defaultBaseUrl && sourceUrl.startsWith('/')) { + throw new Error(`Invalid request method: ${sourceUrl}`); + } + const url = new URL(sourceUrl, defaultBaseUrl); if (!requestCanContainBody) { invariant(!(props.variables instanceof FormData), `Method ${props.method} cannot contain body`); url.search = urlSearchParamsFromObject(props.variables).toString();