Skip to content

Commit d3a5d37

Browse files
SkoebaSteveSteef Janssen
andauthored
allow for passing on adapterOptions (#128)
Co-authored-by: Steef Janssen <steef.janssen.ext@qonto.com>
1 parent b6d1fef commit d3a5d37

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

addon/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const VALID_METHODS = [
1212

1313
export async function apiAction(
1414
record,
15-
{ requestType = 'updateRecord', method, path, data }
15+
{ requestType = 'updateRecord', method, path, data, adapterOptions }
1616
) {
1717
assert(`Missing \`method\` option`, method);
1818
assert(
@@ -29,6 +29,10 @@ export async function apiAction(
2929

3030
let snapshot = record._createSnapshot();
3131

32+
if (adapterOptions) {
33+
snapshot.adapterOptions = adapterOptions;
34+
}
35+
3236
let baseUrl = adapter.buildURL(modelName, record.id, snapshot, requestType);
3337
let url = path ? `${baseUrl}/${path}` : baseUrl;
3438

tests/unit/custom-actions-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,33 @@ module('customAction()', function (hooks) {
8888
let response = await apiAction(user, { method: 'POST', path: 'like' });
8989
assert.deepEqual(response, { custom: 'buildURL' });
9090
});
91+
92+
test('snapshot can receive adapterOptions', async function (assert) {
93+
class UserAdapter extends RESTAdapter {
94+
buildURL(modelName, id, snapshot) {
95+
if (snapshot.adapterOptions?.test === true) {
96+
return `/users/it-works`;
97+
} else {
98+
return super.buildURL(...arguments);
99+
}
100+
}
101+
}
102+
103+
this.owner.register('adapter:user', UserAdapter);
104+
105+
let { worker, rest, user } = await prepare(this);
106+
107+
worker.use(
108+
rest.post('/users/it-works/like', (req, res, ctx) => {
109+
return res(ctx.json({ adapterOptions: 'it works' }));
110+
})
111+
);
112+
113+
let response = await apiAction(user, {
114+
method: 'POST',
115+
path: 'like',
116+
adapterOptions: { test: true },
117+
});
118+
assert.deepEqual(response, { adapterOptions: 'it works' });
119+
});
91120
});

0 commit comments

Comments
 (0)