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

Add Decorator Support #428

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ export default DS.Model.extend({

*Warning* this implemention only works for JSON API, but it should be easy to write your own `after` hook to handle your use case. Have a look at the [implementation of `serializeAndPush`](https://github.com/mike-north/ember-api-actions/blob/master/addon/utils/serialize-and-push.ts) for an example.

### ES Class Support

Decorators are also provided for `memberAction` and `collectionAction` for usage with ES classes rather than the classic Ember classes. The same configuration options are supported:

```javascript
import Model, { attr } from '@ember-data/model';
import { memberAction, collectionAction } from 'ember-api-actions/decorators';

export default class FruidModel extends Model {
@attr('string') name;
// /fruits/123/ripen
@memberAction({ path: 'ripen' }) ripen;
// /fruits/citrus
@collectionAction({
path: 'citrus',
type: 'post', // HTTP POST request
urlType: 'findRecord' // Base of the URL that's generated for the action
}) getAllCitrus;
}
```

## Customization

ember-api-actions generates URLs and ajax configuration via ember-data adapters. It will identify the appropriate adapter, and call the `buildURL` and `ajaxOptions` methods to send a JSON request similar to way conventional ember-data usage works.
Expand Down
19 changes: 19 additions & 0 deletions addon/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import collectionOp, { CollectionOperationOptions } from './utils/collection-action';
import instanceOp, { InstanceOperationOptions } from './utils/member-action';

export function collectionAction<IN = any, OUT = any>(options: CollectionOperationOptions<IN, OUT>) {
return function createCollectionActionDescriptor(target: any, propertyName: string): any {
return {
value: collectionOp(options)
};
}
}

export function memberAction<IN = any, OUT = any>(options: InstanceOperationOptions<IN, OUT>) {
return function createMemberActionDescriptor(target: any, propertyName: string): any {
return {
value: instanceOp(options)
};
}
}

51 changes: 51 additions & 0 deletions tests/unit/utils/decorators/collection-action-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { collectionAction } from 'ember-api-actions/decorators';
import DS from 'ember-data';
import { setupTest } from 'ember-qunit';
import { TestContext } from 'ember-test-helpers';
import { module, test } from 'qunit';
import setupPretender from '../../../helpers/setup-pretender';

const { Model, attr } = DS;

class Fruit extends Model {
@attr('string')
public name?: string;

@collectionAction({ path: 'ripenEverything' })
public ripenAll: any;
}

module('Unit | Utility | decorators/collection-action', (hooks) => {
setupTest(hooks);
setupPretender(hooks);

let fruit: Fruit;

hooks.beforeEach(function(this: TestContext) {
this.owner.unregister('model:fruit');
this.owner.register('model:fruit', Fruit);

this.store = this.owner.lookup('service:store');

fruit = this.store.createRecord('fruit', {
id: 1,
name: 'apple'
});
});

test('it adds a method through a decorator', async function(assert) {
assert.expect(3);

this.server.put('/fruits/ripenEverything', (request) => {
const data = JSON.parse(request.requestBody);
assert.deepEqual(data, { test: 'ok' }, 'collection action - request payload is correct');
return [200, {}, '{"status": "ok"}'];
});

assert.equal(typeof fruit.ripenAll, 'function', 'Assigns a method on the type');

const result = await fruit.ripenAll({ test: 'ok' });

assert.deepEqual(result, { status: 'ok' }, 'Passes through the API response');
});
});
53 changes: 53 additions & 0 deletions tests/unit/utils/decorators/member-action-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { memberAction } from 'ember-api-actions/decorators';
import DS from 'ember-data';
import { setupTest } from 'ember-qunit';
import { TestContext } from 'ember-test-helpers';
import { module, test } from 'qunit';
import setupPretender from '../../../helpers/setup-pretender';

const { Model, attr } = DS;

class Fruit extends Model {
@attr('string')
public name?: string;

@memberAction({ path: 'doRipen' })
public ripen: any;
}

module('Unit | Utility | decorators/member-action', (hooks) => {
setupTest(hooks);
setupPretender(hooks);

let fruit: Fruit;

hooks.beforeEach(function(this: TestContext) {
this.owner.unregister('model:fruit');
this.owner.register('model:fruit', Fruit);

this.store = this.owner.lookup('service:store');

fruit = this.store.createRecord('fruit', {
id: 1,
name: 'apple'
});
});

test('it adds a method through a decorator', async function(assert) {
assert.expect(4);

this.server.put('/fruits/:id/doRipen', (request) => {
const data = JSON.parse(request.requestBody);
assert.deepEqual(data, { id: '1', name: 'apple' }, 'member action - request payload is correct');
assert.equal(request.params.id, '1', 'request was made to the right ID');
return [200, {}, '{"status": "ok"}'];
});

assert.equal(typeof fruit.ripen, 'function', 'Assigns a method on the type');

const { id, name } = fruit;
const result = await fruit.ripen({ id, name });

assert.deepEqual(result, { status: 'ok' }, 'Passes through the API response');
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"allowJs": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"noEmitOnError": false,
"noImplicitAny": true,
"noImplicitThis": true,
Expand Down