{"id":19050905,"url":"https://github.com/tomasbasham/ember-cli-adapter-pattern","last_synced_at":"2025-02-22T00:25:54.967Z","repository":{"id":57223083,"uuid":"50777778","full_name":"tomasbasham/ember-cli-adapter-pattern","owner":"tomasbasham","description":"An ember-cli addon to standardise a common adapter pattern","archived":false,"fork":false,"pushed_at":"2022-12-06T22:17:22.000Z","size":926,"stargazers_count":1,"open_issues_count":5,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-12T09:44:27.453Z","etag":null,"topics":["adapter-pattern","ember-addon","ember-cli"],"latest_commit_sha":null,"homepage":"","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/tomasbasham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-31T14:15:22.000Z","updated_at":"2019-02-18T12:54:37.000Z","dependencies_parsed_at":"2023-01-24T11:45:16.910Z","dependency_job_id":null,"html_url":"https://github.com/tomasbasham/ember-cli-adapter-pattern","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-adapter-pattern","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-adapter-pattern/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-adapter-pattern/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomasbasham%2Fember-cli-adapter-pattern/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomasbasham","download_url":"https://codeload.github.com/tomasbasham/ember-cli-adapter-pattern/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240107490,"owners_count":19748828,"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":["adapter-pattern","ember-addon","ember-cli"],"created_at":"2024-11-08T23:16:39.312Z","updated_at":"2025-02-22T00:25:54.906Z","avatar_url":"https://github.com/tomasbasham.png","language":"JavaScript","readme":"# ember-cli-adapter-pattern [![Build Status](https://travis-ci.com/tomasbasham/ember-cli-adapter-pattern.svg?branch=master)](https://travis-ci.com/tomasbasham/ember-cli-adapter-pattern) [![Maintainability](https://api.codeclimate.com/v1/badges/56fc7fabc8e2cb29e1d0/maintainability)](https://codeclimate.com/github/tomasbasham/ember-cli-adapter-pattern/maintainability)\n\nAn [Ember CLI](https://ember-cli.com/) addon to standardise a common adapter\npattern.\n\nThe adapter pattern helps to provide a common interface from which two\nincompatible interface may work together. For example you may wish to include\nin your applications the ability to authenticate users through a variety of\nsocial platforms (i.e. Facebook and Twitter). Each platform defines its own API\nwhich to interface with it's servers. Using the adapter pattern you can\nseparate the logic of both APIs into their own adapter objects, using a common\ninterface to work with both.\n\nThis addon implements a common adapter pattern that can be included in any\nember object allowing it to act as a proxy between the application and any\nexternal interface.\n\n## Compatibility\n\n* Ember.js v2.18 or above\n* Ember CLI v2.13 or above\n\n## Installation\n\nFrom within your Ember CLI project directory run:\n```\nember install ember-cli-adapter-pattern\n```\n\n## Usage\n\nThis addon implements a mixin that should be included in any object you wish to\nact as the interface between your application and any external platform or API.\n\n### Adaptable Mixin\n\nIn order to implement the adapter pattern, it is recommended you include the\n`Adaptable` mixin into an ember object that will act as a singleton, i.e. a\nservice.\n\n##### Adaptable Example\n\n```JavaScript\n// app/services/social.js\nimport Service from '@ember/service';\n\nimport Adaptable from 'ember-cli-adapter-pattern/mixins/adaptable';\nimport proxyToAdapter from 'ember-cli-adapter-pattern/utils/proxy-to-adapter';\n\nimport { getOwner } from '@ember/application';\nimport { A } from '@ember/array';\nimport { assert } from '@ember/debug';\nimport { get, getWithDefault } from '@ember/object';\nimport { on } from '@embber/object/evented';\nimport { dasherize } from '@ember/string';\n\nexport default Service.extend(Adaptable, {\n  login: proxyToAdapter('login'), // Provides a safe method to proxy your API to each adapter.\n\n  /**\n   * There are potentially many ways\n   * to activate adapters, but it is\n   * imperative that somewhere the\n   * `activateAdapters` method is\n   * called.\n   */\n  createAdapters: on('init', function() {\n    const adapters = getWithDefault(this, 'property.with.adapter.configurations', A());\n\n    this.set('_adapters', {});\n    this.set('context', {});\n\n    // Activate configured adapter.\n    this.activateAdapters(adapters);\n  }),\n\n  /**\n   * This is the only method that you\n   * are required to write and defines\n   * how you look up your adapters. It\n   * is not limited to using the\n   * container.\n   */\n  _lookupAdapter(adapterName) {\n    assert('Could not find adapter without a name', adapterName);\n\n    const owner = getOwner(this);\n    const dasherizedAdapterName = dasherize(adapterName);\n    const adapter = owner.lookup(`container:${dasherizedAdapterName}`);\n\n    return adapter;\n  }\n});\n```\n\nThis creates a service that will act as the common API for each of your\nadapters. Here you can see I have defined a single common API method named\n`login`.\n\n### Making API Calls\n\nTo make calls to your API you must inject the `Adaptable` object into another\nember object (i.e. a controller) and invoke it as normal.\n\n##### All Adapters Example\n\n```JavaScript\n// app/controller/application.js\nimport Controller from '@ember/controller';\n\nimport { get } from '@ember/object';\nimport { inject } from '@ember/service';\n\nexport default Controller.extend({\n  social: inject(),\n\n  actions: {\n    loginWithService() {\n      const social = get(this, 'social');\n      social.login({ username: 'Jean-Luc Picard', password: 'Enterprise-D' });\n    }\n  }\n});\n```\n\nThe action defined in the controller will call the `login` method on the\n`social` service. This will in turn forward the invocation on to each of the\nadapters. Of course in this example it would make little sense to login with\nmore than one platform at the same time. If you wish to call only one adapter\nthen you must pass in it's name to the API call.\n\n##### Single Adapter Example\n\n```JavaScript\n// app/controller/application.js\nimport Controller from '@ember/controller';\n\nimport { get } from '@ember/object';\nimport { inject } from '@ember/service';\n\nexport default Controller.extend({\n  social: inject(),\n\n  actions: {\n    loginWithService() {\n      get(this, 'social').login('Facebook', { username: 'Jean-Luc Picard', password: 'Enterprise-D' });\n    }\n  }\n});\n```\n\nThis will only make the API call to the 'Facebook' adapter.\n\nEach API call will be wrapped within a promise that resolves with the returned\nresult of the adapter mapped to the adapter name.\n\n### ProxyToAdapter Utility\n\nThe `proxyToAdapter` utility method simply forwards calls made to each of your\nAPI methods to all defined adapters, or just a single adapter if specified. It\nis recommended you use `proxyToAdapter` because it implements guard statements\nto prevent the application from throwing errors.\n\nIf however you need to extend the functionality of your API methods then\nsomewhere in its implementation it needs to call the `invoke` method defined\nwithin the `Adaptable` mixin.\n\n```JavaScript\n// app/services/social.js\nimport Service from '@ember/service';\nimport Adaptable from 'ember-cli-adapter-pattern/mixins/adaptable';\n\nexport default Service.extend(Adaptable, {\n  login(...args) {\n    // Extended operations here.\n    this.invoke('login', ...args);\n  }\n});\n```\n\nHere we are passing the name of the API call as the first argument to `invoke`\nfollowed by the arguments that were passed into the API call.\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE.md).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasbasham%2Fember-cli-adapter-pattern","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomasbasham%2Fember-cli-adapter-pattern","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomasbasham%2Fember-cli-adapter-pattern/lists"}