{"id":13807134,"url":"https://github.com/EmberExperts/ember-custom-actions","last_synced_at":"2025-05-14T00:30:55.714Z","repository":{"id":17027137,"uuid":"80992565","full_name":"EmberExperts/ember-custom-actions","owner":"EmberExperts","description":"Custom API actions for Ember applications","archived":false,"fork":false,"pushed_at":"2023-07-19T14:43:56.000Z","size":12803,"stargazers_count":74,"open_issues_count":14,"forks_count":25,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-09T01:47:58.278Z","etag":null,"topics":["api","ember","ember-addon","javascript","jsonapi"],"latest_commit_sha":null,"homepage":"http://emberexperts.github.io/ember-custom-actions/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EmberExperts.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-02-05T12:52:36.000Z","updated_at":"2024-09-02T16:19:25.000Z","dependencies_parsed_at":"2024-05-03T13:57:29.359Z","dependency_job_id":"37fe0496-4faf-4dcb-bafb-f75e5a523c95","html_url":"https://github.com/EmberExperts/ember-custom-actions","commit_stats":null,"previous_names":["exelord/ember-custom-actions"],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmberExperts%2Fember-custom-actions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmberExperts%2Fember-custom-actions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmberExperts%2Fember-custom-actions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EmberExperts%2Fember-custom-actions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EmberExperts","download_url":"https://codeload.github.com/EmberExperts/ember-custom-actions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254046226,"owners_count":22005557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","ember","ember-addon","javascript","jsonapi"],"created_at":"2024-08-04T01:01:21.409Z","updated_at":"2025-05-14T00:30:50.695Z","avatar_url":"https://github.com/EmberExperts.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/Exelord/ember-custom-actions/master/logo.png\" alt=\"Ember Custom Actions Logo\" width=\"100%\"\u003e\n\n  \u003ca href='https://travis-ci.org/Exelord/ember-custom-actions'\u003e\n    \u003cimg src=\"https://travis-ci.org/Exelord/ember-custom-actions.svg?branch=master\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://david-dm.org/exelord/ember-custom-actions\"\u003e\n    \u003cimg src=\"https://david-dm.org/exelord/ember-custom-actions/status.svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href='https://gitter.im/Exelord/ember-custom-actions?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge'\u003e\n    \u003cimg src=\"https://badges.gitter.im/Exelord/ember-custom-actions.svg\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codeclimate.com/github/Exelord/ember-custom-actions/maintainability\"\u003e\n    \u003cimg src=\"https://api.codeclimate.com/v1/badges/86c2ffb0d59e8f2b6016/maintainability\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nEmber Custom Actions is a package for defining custom API actions, dedicated for Ember 2.16 (and higher) applications.\n\n# Getting started\n\n## Demo\nBefore you will start with documentation check our demo app: [Ember-Custom-Actions Website](https://exelord.github.io/ember-custom-actions)\n\n## Installation\n\n`ember install ember-custom-actions`\n\n## Documentation\n\n### Model actions\nTo define custom action like: `posts/1/publish` you can use\n`modelAction(path, options)` method with arguments:\n- `path` - url of the action scoped to our api (in our case it's `publish`)\n- `options` - optional parameter which will overwrite the configuration options\n\n```js\nimport Model from 'ember-data/model';\nimport { modelAction } from 'ember-custom-actions';\n\nexport default Model.extend({\n  publish: modelAction('publish', { pushToStore: false }),\n});\n\n```\n\n#### Usage\n```js\nlet user = this.get('currentUser');\nlet postToPublish = this.get('store').findRecord('post', 1);\nlet payload = { publisher: user };\n\npostToPublish.publish(payload, /*{ custom options }*/).then((status) =\u003e {\n  alert(`Post has been: ${status}`)\n}).catch((error) =\u003e {\n  console.log('Here are your serialized model errors', error.serializedErrors);\n});\n```\n\n### Resource actions\nTo a define custom action like: `posts/favorites` you can use\n`resourceAction(actionId/path, options)` method with arguments:\n- `path` - url of the action scoped to our api (in our case it's `favorites`)\n- `options` - optional parameter which will overwrite the configuration options\n\n```js\nimport Model from 'ember-data/model';\nimport { resourceAction } from 'ember-custom-actions';\n\nexport default Model.extend({\n  favorites: resourceAction('favorites', { method: 'GET' }),\n});\n\n```\n\n#### Usage\n```js\nlet user = this.get('currentUser');\nlet emptyPost = this.get('store').createRecord('post');\nlet payload = { user };\n\nemptyPost.favorites(payload, /*{ custom options }*/).then((favoritesPosts) =\u003e {\n  console.log(favoritesPosts);\n}).finally(()=\u003e{\n  emptyPost.deleteRecord();\n});\n```\n\n### Custom actions\nTo define `customAction` and customize it by using ember-data flow, adapters and serializer you can use `customAction(actionId, options)` method with arguments:\n- `actionId` - id of the action which can be handled later on in adpaters and serializers\n- `options` - optional parameter which will overwrite the configuration options\n\nIf you want to customize your request in your adapter please, implement our adapter mixin:\n```js\nimport JSONAPIAdapter from 'ember-data/adapters/json-api';\nimport { AdapterMixin } from 'ember-custom-actions';\n\nexport default JSONAPIAdapter.extend(AdapterMixin);\n```\n\nNow you can customize following methods in the adpater:\n* [urlForCustomAction](#urlForCustomAction)\n* [dataForCustomAction](#dataForCustomAction)\n* [methodForCustomAction](#methodForCustomAction)\n* [headersForCustomAction](#headersForCustomAction)\n\n\n#### urlForCustomAction\nYou can define your custom path for every `customAction` by adding a conditional:\n\n```js\nexport default JSONAPIAdapter.extend(AdapterMixin, {\n  urlForCustomAction(modelName, id, snapshot, actionId, queryParams) {\n    if (actionId === 'myPublishAction') {\n      return 'https://my-custom-api.com/publish'\n    }\n\n    return this._super(...arguments);\n  }\n});\n```\n\nIf you would like to build custom `modelAction` you can do it by:\n\n```js\nimport { AdapterMixin } from 'ember-custom-actions';\n\nexport default JSONAPIAdapter.extend(AdapterMixin, {\n  urlForCustomAction(modelName, id, snapshot, actionId, queryParams) {\n    if (requestType === 'myPublishAction') {\n      return `${this._buildURL(modelName, id)}/publish`;\n    }\n\n    return this._super(...arguments);\n  }\n});\n```\n\n#### methodForCustomAction\nYou can define your custom method for every `customAction` by adding a conditional:\n\n```js\nimport { AdapterMixin } from 'ember-custom-actions';\n\nexport default JSONAPIAdapter.extend(AdapterMixin, {\n  methodForCustomAction(params) {\n    if (params.actionId === 'myPublishAction') {\n      return 'PUT';\n    }\n\n    return this._super(...arguments);\n  }\n});\n```\n\n#### headersForCustomAction\nYou can define your custom headers for every `customAction` by adding a conditional:\n\n```js\nimport { AdapterMixin } from 'ember-custom-actions';\n\nexport default JSONAPIAdapter.extend(AdapterMixin, {\n  headersForCustomAction(params) {\n    if (params.actionId === 'myPublishAction') {\n      return {\n        'Authorization-For-Custom-Action': 'mySuperToken123'\n      };\n    }\n\n    return this._super(...arguments);\n  }\n});\n```\n\n#### dataForCustomAction\nYou can define your custom data for every `customAction` by adding a conditional:\n\n```js\nimport { AdapterMixin } from 'ember-custom-actions';\n\nexport default JSONAPIAdapter.extend(AdapterMixin, {\n  dataForCustomAction(params) {\n    if (params.actionId === 'myPublishAction') {\n      return {\n        myParam: 'send it to the server'\n      };\n    }\n\n    return this._super(...arguments);\n  }\n});\n```\n\n`params` contains following data: `data`, `actionId`, `modelId`, `model`\n\n### Configuration\n\nYou can define your custom options in your `config/environment.js` file\n\n``` js\nmodule.exports = function(environment) {\n  var ENV = {\n    'emberCustomActions': {\n      method: 'POST',\n      data: {},\n      headers: {},\n      queryParams: {},\n      ajaxOptions: {},\n      adapterOptions: {},\n      pushToStore: false,\n      responseType: null,\n      normalizeOperation: ''\n    },\n  };\n\n  return ENV;\n}\n```\n#### `method`\nDefault method of the request (GET, PUT, POST, DELETE, etc..)\n\n#### `headers`\nAn object `{}` of custom headers. Eg:\n```js\n{\n  'my-custom-auth': 'mySuperToken123'\n}\n```\n\n#### `ajaxOptions`\nYour own ajax options.\n** USE ONLY IF YOU KNOW WHAT YOU ARE DOING! **\nThose properties will be overwritten by ECU.\n\n#### `pushToStore`\nIf you want to push the received data to the store, set this option to `true`\n\n#### `normalizeOperation`\nYou can define how your outgoing data should be serialized\n\n```\n\nExemplary data:\n```js\n{\n  firstParam: 'My Name',\n  colors: { rubyRed: 1, blueFish: 3 }\n}\n```\nAfter using a `dasherize` transformer our request data will turn into:\n\n```js\n{\n  first-param: 'My Name',\n  colors: { ruby-red: 1, blue-fish: 3 }\n}\n```\nIt's great for API with request data format restrictions\n\n**Available transformers:**\n  - camelize\n  - capitalize\n  - classify\n  - dasherize\n  - decamelize\n  - underscore\n\n#### `adapterOptions`\nPass custom adapter options to handle them in `urlForCustomAction` in case of using `customAction`. Required usage of mixin: `AdpaterMixin`\n\n#### `responseType`\nYou can easily observe the returned model by changing `responseType` to `array` or `object` according to what type of data\nyour server will return.\n\nWhen `array`:\n```js\nmodel.customAction({}, { responseType: 'array' }) // returns DS.PromiseArray\n```\n\nWhen `object`:\n```js\nmodel.customAction({}, { responseType: 'object' }) // returns DS.PromiseObject\n```\n\nWhen `null` (default):\n```js\nmodel.customAction({}, { responseType: null }) // returns Promise\n```\n`null` is useful if you don't care about the response or just want to use `then` on the promise without using `binding` or display it in the template.\n\n#### `queryParams`\nYou can pass a query params for a request by passing an `{}` with properties, eg: `{ include: 'owner' }`\n** Remember: Query params are not normalized! You have to pass it in the correct format. **\n\n# Development\n\n### Installation\n\n* `git clone https://github.com/Exelord/ember-custom-actions.git`\n* `cd ember-custom-actions`\n* `npm install`\n\n### Linting\n\n* `npm run lint:hbs`\n* `npm run lint:js`\n* `npm run lint:js -- --fix`\n\n### Running tests\n\n* `ember test` – Runs the test suite on the current Ember version\n* `ember test --server` – Runs the test suite in \"watch mode\"\n* `ember try:each` – Runs the test suite against multiple Ember versions\n\n### Running the dummy application\n\n* `ember serve`\n* Visit the dummy application at [http://localhost:4200](http://localhost:4200).\n\nFor more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/).\n\n## Thanks\nBig thanks to Mike North and his [Project](https://github.com/mike-north/ember-api-actions) for the initial concept.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/exelord/ember-custom-actions. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.\n\n## License\n\nThis version of the package is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Packages"],"sub_categories":["Job queues"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEmberExperts%2Fember-custom-actions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEmberExperts%2Fember-custom-actions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEmberExperts%2Fember-custom-actions/lists"}