Skip to content

Commit 8314cd7

Browse files
committed
feat: replace jquery-params with URL API and authorize undefined action name
1 parent 64ddf4d commit 8314cd7

File tree

9 files changed

+55
-16
lines changed

9 files changed

+55
-16
lines changed

addon/.gitkeep

Whitespace-only changes.

addon/utils/url-builder.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
import param from 'jquery-param';
1+
import URL from 'url';
2+
import { isEmpty } from '@ember/utils';
23

34
export default function(url, path, queryParams) {
4-
let query = param(queryParams);
5-
let pathUrl = url.charAt(url.length - 1) === '/' ? `${url}${path}` : `${url}/${path}`;
5+
let pathUrl = url;
6+
if (path && !isEmpty(path)) {
7+
let trailingSlash = url.endsWith('/') ? '' : '/'
8+
pathUrl += `${trailingSlash}${path}`;
9+
}
10+
11+
// Use native URL API to generate query
12+
let queryObj = new URL('https://exelord.com/ember-custom-actions/').searchParams;
13+
if (queryParams) {
14+
Object.keys(queryParams).forEach(key =>
15+
queryObj.set(key, queryParams[key])
16+
);
17+
}
18+
let query = queryObj.toString();
619

720
return query ? `${pathUrl}?${query}` : pathUrl;
821
}

index.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,5 @@ module.exports = {
1313
{ transformation: 'cjs', as: 'lodash.merge' }
1414
]
1515
});
16-
17-
this.import('node_modules/jquery-param/jquery-param.js', {
18-
using: [
19-
{ transformation: 'amd', as: 'jquery-param' }
20-
]
21-
});
2216
},
2317
};

package-lock.json

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"dependencies": {
3232
"ember-cli-babel": "6.16.0",
3333
"ember-cli-cjs-transform": "2.0.0",
34-
"jquery-param": "1.0.1",
34+
"ember-url": "0.6.0",
3535
"lodash.merge": "4.6.2"
3636
},
3737
"devDependencies": {

tests/dummy/app/models/.gitkeep

Whitespace-only changes.

tests/dummy/app/models/bike.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ import { modelAction } from 'ember-custom-actions';
44

55
export default Model.extend({
66
name: attr(),
7-
ride: modelAction('ride', { method: 'PUT', data: { defaultParam: 'ok' } })
7+
ride: modelAction('ride', { method: 'PUT', data: { defaultParam: 'ok' } }),
8+
clean: modelAction(undefined, {
9+
method: 'PATCH',
10+
data: { defaultParam: 'ok' }
11+
})
812
});

tests/unit/.gitkeep

Whitespace-only changes.

tests/unit/models/bike-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,28 @@ test('model action set serialized errors in error object', function(assert) {
113113
done();
114114
});
115115
});
116+
117+
test('model action without actionId', function(assert) {
118+
assert.expect(4);
119+
120+
this.server.patch('/bikes/:id', (request) => {
121+
let data = JSON.parse(request.requestBody);
122+
123+
assert.deepEqual(data, { myParam: 'My first param', defaultParam: 'ok' });
124+
assert.deepEqual(request.queryParams, { soap: 'true', include: 'sponge' });
125+
assert.equal(request.url, '/bikes/1?include=sponge&soap=true');
126+
127+
return [200, { }, 'true'];
128+
});
129+
130+
let done = assert.async();
131+
let payload = { myParam: 'My first param' };
132+
133+
let model = this.subject();
134+
model.set('id', 1);
135+
136+
model.clean(payload, { queryParams: { soap: true, include: 'sponge' } }).then((response) => {
137+
assert.ok(response, true);
138+
done();
139+
});
140+
});

0 commit comments

Comments
 (0)