export async function customClient(orvalParams, customParams) {
    const { url, method, data } = orvalParams;
    const { mode, cache, credentials } = customParams;
    const { headers, params } = combineParams(orvalParams, customParams);
    const baseUrl = removeTrailingSlash(customParams.hubApiUrl);
    const requestUrl = `${baseUrl}${url}?${new URLSearchParams(params)}`;
    const requestOptions = {
        headers,
        method,
        cache,
        credentials,
        mode,
    };
    if (data) {
        requestOptions.body = JSON.stringify(data);
    }
    const res = await fetch(requestUrl, requestOptions);
    const { statusText, status } = res;
    if (res.ok) {
        return res.json();
    }
    const error = await res.json();
    throw new RemoteServerError(statusText, requestUrl, status, JSON.stringify(error.message));
}
function removeTrailingSlash(hubApiUrl = "https://hub.arcgis.com") {
    return hubApiUrl.replace(/\/$/, "");
}
function combineParams(orvalParams, options) {
    const headers = new Headers(Object.assign(Object.assign({}, orvalParams.headers), options.headers));
    if (options.token) {
        headers.set("Authorization", options.token);
    }
    const params = Object.assign(Object.assign({}, orvalParams.params), options.params);
    return { headers, params };
}
class RemoteServerError extends Error {
    constructor(message, url, status, error) {
        super(message);
        this.status = status;
        this.url = url;
        this.error = error;
    }
}
//# sourceMappingURL=custom-client.js.map