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

Fix trailing ? in URL of query builder #9216

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 4 additions & 2 deletions packages/active-record/src/-private/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ export function query(

copyForwardUrlOptions(urlOptions, options);

const url = buildBaseURL(urlOptions);
const url = new URL(buildBaseURL(urlOptions));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@runspired please let me know if this is good approach?

The only downside I see - it most likely append host in it.
Also it creates new instance of URL, not sure how performant it is.

url.search = buildQueryParams(query, options.urlParamsSettings);

const headers = new Headers();
headers.append('Accept', 'application/json;charset=utf-8');

return {
url: `${url}?${buildQueryParams(query, options.urlParamsSettings)}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead we should check if there is a query / check if buildQueryParams has output and use that to conditionally join with a ?

url: url.toString(),
method: 'GET',
headers,
cacheOptions,
Expand Down
6 changes: 4 additions & 2 deletions packages/json-api/src/-private/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ export function query(

copyForwardUrlOptions(urlOptions, options);

const url = buildBaseURL(urlOptions);
const url = new URL(buildBaseURL(urlOptions));
url.search = buildQueryParams(query, options.urlParamsSettings);

const headers = new Headers();
headers.append('Accept', ACCEPT_HEADER_VALUE);

return {
url: `${url}?${buildQueryParams(query, options.urlParamsSettings)}`,
url: url.toString(),
method: 'GET',
headers,
cacheOptions,
Expand Down
6 changes: 4 additions & 2 deletions packages/rest/src/-private/builders/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ export function query(

copyForwardUrlOptions(urlOptions, options);

const url = buildBaseURL(urlOptions);
const url = new URL(buildBaseURL(urlOptions));
url.search = buildQueryParams(query, options.urlParamsSettings);

const headers = new Headers();
headers.append('Accept', 'application/json;charset=utf-8');

return {
url: `${url}?${buildQueryParams(query, options.urlParamsSettings)}`,
url: url.toString(),
method: 'GET',
headers,
cacheOptions,
Expand Down
16 changes: 16 additions & 0 deletions tests/builders/tests/unit/active-record-builder-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ module('ActiveRecord | Request Builders', function (hooks) {
assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS);
});

test('query with empty params [used to be findAll]', function (this: TestContext, assert) {
const result = query('user-setting', {}, { reload: true, backgroundReload: false });
assert.deepEqual(
result,
{
url: 'https://api.example.com/api/v1/user_settings',
method: 'GET',
headers: new Headers(ACTIVE_RECORD_HEADERS),
cacheOptions: { reload: true, backgroundReload: false },
op: 'query',
},
`query works with type and empty options, does not leave a trailing ?`
);
assert.deepEqual(headersToObject(result.headers), ACTIVE_RECORD_HEADERS);
});

test('createRecord passing store record', function (this: TestContext, assert) {
const store = this.owner.lookup('service:store') as Store;
const userSetting = store.createRecord('user-setting', {
Expand Down
16 changes: 16 additions & 0 deletions tests/builders/tests/unit/json-api-builder-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ module('JSON:API | Request Builders', function (hooks) {
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS);
});

test('query with empty params [used to be findAll]', function (this: TestContext, assert) {
const result = query('user-setting', {}, { reload: true, backgroundReload: false });
assert.deepEqual(
result,
{
url: 'https://api.example.com/api/v1/user-settings',
method: 'GET',
headers: new Headers(JSON_API_HEADERS),
cacheOptions: { reload: true, backgroundReload: false },
op: 'query',
},
`query works with type and empty options, does not leave a trailing ?`
);
assert.deepEqual(headersToObject(result.headers), JSON_API_HEADERS);
});

test('postQuery', function (this: TestContext, assert) {
const result = postQuery(
'user-setting',
Expand Down
16 changes: 16 additions & 0 deletions tests/builders/tests/unit/rest-builder-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ module('REST | Request Builders', function (hooks) {
assert.deepEqual(headersToObject(result.headers), REST_HEADERS);
});

test('query with empty params [used to be findAll]', function (this: TestContext, assert) {
const result = query('user-setting', {}, { reload: true, backgroundReload: false });
assert.deepEqual(
result,
{
url: 'https://api.example.com/api/v1/userSettings',
method: 'GET',
headers: new Headers(REST_HEADERS),
cacheOptions: { reload: true, backgroundReload: false },
op: 'query',
},
`query works with type and empty options, does not leave a trailing ?`
);
assert.deepEqual(headersToObject(result.headers), REST_HEADERS);
});

test('createRecord passing store record', function (this: TestContext, assert) {
const store = this.owner.lookup('service:store') as Store;
const userSetting = store.createRecord('user-setting', {
Expand Down