Skip to content

Commit

Permalink
fix: url configuration should respect / for host and error more meani…
Browse files Browse the repository at this point in the history
…ngfully when invalid (#9164)

* fix: url configuration should respect / for host and error more meaningfully when invalid

* fix prettier
  • Loading branch information
runspired authored Dec 10, 2023
1 parent bb467fb commit d0b7878
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions packages/request-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface BuildURLConfig {
namespace: string | null;
}

let CONFIG: BuildURLConfig = {
const CONFIG: BuildURLConfig = {
host: '',
namespace: '',
};
Expand All @@ -65,7 +65,13 @@ let CONFIG: BuildURLConfig = {
* These values may still be overridden by passing
* them to buildBaseURL directly.
*
* This method may be called as many times as needed
* This method may be called as many times as needed.
* host values of `''` or `'/'` are equivalent.
*
* Except for the value of `/` as host, host should not
* end with `/`.
*
* namespace should not start or end with a `/`.
*
* ```ts
* type BuildURLConfig = {
Expand All @@ -74,6 +80,17 @@ let CONFIG: BuildURLConfig = {
* }
* ```
*
* Example:
*
* ```ts
* import { setBuildURLConfig } from '@ember-data/request-utils';
*
* setBuildURLConfig({
* host: 'https://api.example.com',
* namespace: 'api/v1'
* });
* ```
*
* @method setBuildURLConfig
* @static
* @public
Expand All @@ -82,7 +99,27 @@ let CONFIG: BuildURLConfig = {
* @return void
*/
export function setBuildURLConfig(config: BuildURLConfig) {
CONFIG = config;
assert(`setBuildURLConfig: You must pass a config object`, config);
assert(
`setBuildURLConfig: You must pass a config object with a 'host' or 'namespace' property`,
'host' in config || 'namespace' in config
);

CONFIG.host = config.host || '';
CONFIG.namespace = config.namespace || '';

assert(
`buildBaseURL: host must NOT end with '/', received '${CONFIG.host}'`,
CONFIG.host === '/' || !CONFIG.host.endsWith('/')
);
assert(
`buildBaseURL: namespace must NOT start with '/', received '${CONFIG.namespace}'`,
!CONFIG.namespace.startsWith('/')
);
assert(
`buildBaseURL: namespace must NOT end with '/', received '${CONFIG.namespace}'`,
!CONFIG.namespace.endsWith('/')
);
}

export interface FindRecordUrlOptions {
Expand Down Expand Up @@ -313,8 +350,9 @@ export function buildBaseURL(urlOptions: UrlOptions): string {
assert(`buildBaseURL: idPath must NOT start with '/', received '${idPath}'`, !idPath.startsWith('/'));
assert(`buildBaseURL: idPath must NOT end with '/', received '${idPath}'`, !idPath.endsWith('/'));

const url = [host === '/' ? '' : host, namespace, resourcePath, idPath, fieldPath].filter(Boolean).join('/');
return host ? url : `/${url}`;
const hasHost = host !== '' && host !== '/';
const url = [hasHost ? host : '', namespace, resourcePath, idPath, fieldPath].filter(Boolean).join('/');
return hasHost ? url : `/${url}`;
}

const DEFAULT_QUERY_PARAMS_SERIALIZATION_OPTIONS: QueryParamsSerializationOptions = {
Expand Down

0 comments on commit d0b7878

Please sign in to comment.