Skip to content

Commit

Permalink
Add requestType option (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 authored Nov 2, 2022
1 parent 29b1b0f commit 542a8e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const VALID_METHODS = [
'PATCH',
];

export async function apiAction(record, { method, path, data }) {
export async function apiAction(
record,
{ requestType = 'updateRecord', method, path, data }
) {
assert(`Missing \`method\` option`, method);
assert(
[
Expand All @@ -24,7 +27,6 @@ export async function apiAction(record, { method, path, data }) {
let modelName = modelClass.modelName;
let adapter = record.store.adapterFor(modelName);

let requestType = 'updateRecord';
let baseUrl = adapter.buildURL(modelName, record.id, null, requestType);
let url = path ? `${baseUrl}/${path}` : baseUrl;

Expand Down
16 changes: 16 additions & 0 deletions tests/unit/custom-actions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ module('customAction()', function (hooks) {
assert.deepEqual(response, { success: true });
});

test('requestType option changes the base URL', async function (assert) {
let { worker, rest, user } = await prepare(this);

worker.use(
rest.post('/users', (req, res, ctx) => {
return res(ctx.json({ requestType: 'works' }));
})
);

let response = await apiAction(user, {
method: 'POST',
requestType: 'createRecord',
});
assert.deepEqual(response, { requestType: 'works' });
});

test('it fails as expected', async function (assert) {
let { worker, rest, user } = await prepare(this);

Expand Down

0 comments on commit 542a8e4

Please sign in to comment.