{"action":"created","issue":{"url":"https://api.github.com/repos/netlify/netlify-cms/issues/1171","repository_url":"https://api.github.com/repos/netlify/netlify-cms","labels_url":"https://api.github.com/repos/netlify/netlify-cms/issues/1171/labels{/name}","comments_url":"https://api.github.com/repos/netlify/netlify-cms/issues/1171/comments","events_url":"https://api.github.com/repos/netlify/netlify-cms/issues/1171/events","html_url":"https://github.com/netlify/netlify-cms/issues/1171","id":304957636,"number":1171,"title":"(WIP) Backend restructure - Integrations","user":{"login":"Benaiah","id":1425133,"avatar_url":"https://avatars1.githubusercontent.com/u/1425133?v=4","gravatar_id":"","url":"https://api.github.com/users/Benaiah","html_url":"https://github.com/Benaiah","followers_url":"https://api.github.com/users/Benaiah/followers","following_url":"https://api.github.com/users/Benaiah/following{/other_user}","gists_url":"https://api.github.com/users/Benaiah/gists{/gist_id}","starred_url":"https://api.github.com/users/Benaiah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Benaiah/subscriptions","organizations_url":"https://api.github.com/users/Benaiah/orgs","repos_url":"https://api.github.com/users/Benaiah/repos","events_url":"https://api.github.com/users/Benaiah/events{/privacy}","received_events_url":"https://api.github.com/users/Benaiah/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-03-13T22:24:16Z","updated_at":"2018-03-29T22:46:23Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"Integrations\r\n============\r\n\r\nWhat are they?\r\n--------------\r\n\r\n<details>\r\n<summary>Expand...</summary>\r\n\r\nIntegrations hook into a number of different places in the CMS to replace different calls, most (all?) of which would normally go to a backend. Currently the assets store (at least) requires additional work There are currently two integration providers:\r\n\r\n-   Algolia: wraps searching and entry retrieval for large repositories.\r\n-   Assets store: stores binary assets in a separate data store from entry data. (Currently unclear how this interacts with the media library)\r\n\r\n### Integrations vs integration providers\r\n\r\nOstensibly, the integration providers are implementations of generalized APIs for a named set of integration hooks:\r\n\r\n-   `assetStore` - global\r\n-   `listEntries` - collection-specific\r\n-   `search` - collection-specific\r\n\r\nEach of these APIs indicates that certain methods will be available on the `provider` instance. There doesn't seem to be a list of these anywhere in the code, as they're implicitly required, but I've compiled one here:\r\n\r\n-   `assetStore`\r\n    -   `upload(file, privateUpload) -> { success, url, asset }` *todo: determine the required shape of the `file` object*\r\n    -   `retrieve(query, page, privateUpload) -> fileList`\r\n    -   `delete(assetID) -> OptionPromise` (`OptionPromise` here just means that the value of the Promise doesn't appear to matter, just whether it succeeds or fails).\r\n-   `listEntries`: `listEntries(collection, page) -> entryList`\r\n-   `search`\r\n    -   `search(collections, searchTerm, page) -> entryList`\r\n    -   `searchBy(field, collection, query) -> entryList` The `field` here is expected to be a string, which doesn't seem to match how it's called in `src/actions/search.js/query`.\r\n\r\nThere are some other methods implemented by integration providers, like `getEntry` in the Algolia provider, but they don't appear to be used currently.\r\n\r\n</details>\r\n\r\nHow they work currently\r\n-----------------------\r\n\r\n<details>\r\n<summary>Expand...</summary>\r\n\r\n### Configuration\r\n\r\nThe integrations are added by creating an `integrations` list in the config. This looks like the following (all currently supported configuration providers and options are listed here):\r\n\r\n``` {.yaml}\r\nintegrations:\r\n  - provider: algolia\r\n\r\n    # provider-specific options\r\n    applicationID: example # algolia API application ID\r\n    apiKey: example # algolia API key\r\n    indexPrefix: example-cms # prefix for index names, which are in the form \"${indexPrefix}${collection}\"\r\n\r\n    # hooks to attach the algolia functions to\r\n    hooks:\r\n      - search\r\n      - listEntries\r\n\r\n    # collections to apply hooks to\r\n    collections:\r\n      - example-entries\r\n      - example-pages\r\n\r\n  - provider: assetStore\r\n\r\n    # provider-specific options\r\n    getSignedFormURL: /example-asset-store/ # Used to generate URLs to the asset store endpoint. This is a constant, not a getter. I'm not entirely sure what this represents.\r\n    shouldConfirmUpload: true # should confirm the upload by PUTing `{ state: \"uploaded\" }` to the asset URL\r\n\r\n    # The assetStore hook is not tied to a specific collection\r\n    hooks:\r\n      - assetStore\r\n```\r\n\r\n### Redux state init\r\n\r\nIn `reducers/integrations.js`, the exported reducer updates its integrations on a `CONFIG_SUCCESS` action. Each integration is an object, with several special keys:\r\n\r\n-   `provider`: integration key.\r\n-   `hooks`: a Map that connects hook names to the `provider` handling them\r\n-   `collections`: a list of collection names to apply hooks to, or the string `*` to apply to all collections (but not the global hooks table)\r\n\r\nOther keys are stored as provider data. The final state produced by this has two main keys:\r\n\r\n-   `providers`: provider data - a Map of each integration's `provider` to a Map of any unknown keys in the integration object.\r\n-   `hooks`: a Map of hooks\r\n\r\nIf `collections` is *not* present on the integration object, then each hook name in `hooks` is set as a key in the `state.hooks` Map, with the provider name as the value.\r\n\r\nIf `collections` *is* present on the integration object, then the config\r\n\r\nSince this is implemented by reducing over the configured list of integrations, the precedence of integration hooks is determined by the order of the `integrations` list in the config, so re-ordering the list in the config can cause errors if the precedence is important. This makes generating a config which includes integrations unsafe unless you strictly control the order of the generated `integrations` list.\r\n\r\n**Collection names can collide with hook names**, since they're both used as keys to the same Map. (e.g., a collection named `listEntries` would collide with the `listEntries` hook name). This includes the names in both the `collections` list in the integration object *and*, if any integration sets its `collections` to `*`, the collections listed in the main CMS config. This has two effects, depending on whether the integration config that sets the global hook comes before or after the integration config that sets the collection hooks for the colliding collection:\r\n\r\n-   If the integration configuration with the global hook comes first, the provider name set as the value of that global hook will have the collection-specific hooks set directly on its string value.\r\n\r\n-   If the integration configuring collection-specific keys for the colliding collection name comes first, the Map of collection hooks for that collection will be overwritten with the provider name of the global hook.\r\n\r\nSince the order of the integrations is dependent on the order of their listing in the `config.yml`, bugs due to this can show up in previously-working configs if that list is reordered, even without any other changes made.\r\n\r\nThere are *two* ways to create hooks that apply to the whole CMS: either list `hooks` without `collections` in an integration, or set `*` as the integration's `collections` to apply collection-specific hooks to all configured collections. It's unclear what the precise roles of these different categories of hooks are.\r\n\r\nThe source of the reducer described above is as follows:\r\n\r\n<https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/reducers/integrations.js#L4-L29>\r\n\r\n### Integration provider init\r\n\r\nThe actual integration providers themselves are initialized in `src/integrations/index.js`, in [`resolveIntegrations`](https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/integrations/index.js#L5-L18), which creates the integration providers configured when called (if the integration is included more than once ). `resolveIntegrations` is called by `getIntegrationProvider` on its first call and cached (note that `getIntegrationProvider` is declared in an IIFE, so the `integrations` declaration happens immediately when the file is loaded). This caching means that integration providers are invisible to the rest of the code, and cannot be reinitialized. The `integrations` variable here is effectively a singleton, just implemented with a closure instead of a class:\r\n\r\n<https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/integrations/index.js#L21-L32>\r\n\r\n`getIntegrationProvider` is called in a few places:\r\n\r\n-   `src/actions/entries.js` - called in [`loadEntries`](https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/actions/entries.js#L249)\r\n-   `src/actions/search.js` - called in [`searchEntries`](https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/actions/search.js#L115) and [`query`](https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/actions/search.js#L135)\r\n-   `src/valueObjects/AssetProxy.js` - called in [`createAssetProxy`](https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/valueObjects/AssetProxy.js#L48)\r\n-   `src/actions/mediaLibrary` - called in [`loadMedia`](https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/actions/mediaLibrary.js#L47) and [`deleteMedia`](https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/actions/mediaLibrary.js#L128)\r\n\r\nWhichever of these lines run first in a particular CMS setup determines when the integrations objects are set up. For most setups this probably occurs in `loadEntries`, as this is called whenever a collection is displayed, which is the default view when logging in to the CMS.\r\n\r\n### Hooks\r\n\r\nAs described above, hooks are configured and stored as lists of strings. The strings must match both the predefined hook name and a method on the integration provider object.\r\n\r\nA hook is called via the following process (this process is not wrapped by anything else, so every site where a hook may be called must implement `selectIntegrationProxy` and `getIntegrationProvider` as described below):\r\n\r\n-   `selectIntegration` is called with the following arguments:\r\n\r\n    -   `state`: the `integrations` list from the config\r\n    -   `collection`: the name of the collection (this is set to `null` for `assetStore`, the only non-collection-specific hook)\r\n    -   `hook`: the name of the hook\r\n\r\n    It then returns, from the integration state in the Redux store, either `hooks.<collection name>.<hook>` or `hooks.<hook>`, depending on whether the collection name is set. The value returned from here will either be `null` or the name of an integrations provider (see the \"Redux state init\" section above for the shape of the `state.integration.hooks` Map).\r\n\r\n-   If an integration exists, `getIntegrationProvider` is called with the following arguments:\r\n\r\n    -   `interationsConfig`: the `integrations` list from the config\r\n    -   `getToken`: an async function for retrieving a token to authenticate to the backend (used only by the assetStore integration)\r\n    -   `provider`: name of the integrations provider\r\n\r\n    It then returns the actual provider instance (initializing the provider instances with `resolveIntegrations` as necessary).\r\n\r\n-   The provider instance is called with whatever methods can be assumed to exist on the provider instance based on the API specified in the call to `selectIntegration`. The methods are listed above.\r\n\r\nAn example hook call follows:\r\n\r\n<https://github.com/netlify/netlify-cms/blob/b4b584682473924556a41cae10f64f085b0f432b/src/actions/entries.js#L241-L256>\r\n\r\n</details>\r\n\r\nComparison of potential integration designs\r\n-------------------------------------------\r\n\r\n<details>\r\n<summary>Expand...</summary>\r\n\r\n### Integrations as is\r\n\r\nIntegrations are currently a very complex API, with multiple layers of setup and configuration. Parts of the API are very general and indirected (e.g., the integration/integration provider distinction), and others are tightly coupled to specific implementations of both integration providers and backends (e.g., `getToken` or the `AssetProxy` system, both of which require explicit support from backends).\r\n\r\nThis leads to a situation where the API is simultaneously so flexible in principle that it's difficult to follow or implement, while so specific in operation that it requires indirect support across wide swaths of the codebase.\r\n\r\nIt's also inherently stateful - the list of providers instances is a singleton, and providers themselves are class instances which store information on the instance's properties.\r\n\r\nFinally, the integrations API has sole responsibility for some concerns, meaning they cannot be implemented by backends. Search, for instance, is either done locally or through an integration - there's no ability for a backend to implement server-side search. Adding this to the backend API as well would introduce further API duplication between the backend and integration API.\r\n\r\n*TODO: expand*\r\n\r\n### Integrations as backend composition\r\n\r\nOne potential approach would be to unify backends and integrations into a single API, allowing them to be combined with normal code. For instance, an Algolia integration could be defined as a function which wraps an existing backend and calls its functions, except for `search`, `searchBy`, `getEntry`, and `listEntries`.\r\n\r\nBenefits of this could include removing `getToken` from the backend API (currently only used for integrations), and unifying the media library integration and backend APIs.\r\n\r\n*TODO: expand*\r\n\r\n### Integrations as middleware\r\n\r\nRedux allows intercepting actions before they hit reducers using [middleware](https://redux.js.org/advanced/middleware). This is very powerful, but it does allow unrestricted access to our Redux actions, essentially making our current action structure the public integrations API.\r\n\r\n*TODO: expand*\r\n\r\n</details>"},"comment":{"url":"https://api.github.com/repos/netlify/netlify-cms/issues/comments/377395342","html_url":"https://github.com/netlify/netlify-cms/issues/1171#issuecomment-377395342","issue_url":"https://api.github.com/repos/netlify/netlify-cms/issues/1171","id":377395342,"user":{"login":"Benaiah","id":1425133,"avatar_url":"https://avatars1.githubusercontent.com/u/1425133?v=4","gravatar_id":"","url":"https://api.github.com/users/Benaiah","html_url":"https://github.com/Benaiah","followers_url":"https://api.github.com/users/Benaiah/followers","following_url":"https://api.github.com/users/Benaiah/following{/other_user}","gists_url":"https://api.github.com/users/Benaiah/gists{/gist_id}","starred_url":"https://api.github.com/users/Benaiah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Benaiah/subscriptions","organizations_url":"https://api.github.com/users/Benaiah/orgs","repos_url":"https://api.github.com/users/Benaiah/repos","events_url":"https://api.github.com/users/Benaiah/events{/privacy}","received_events_url":"https://api.github.com/users/Benaiah/received_events","type":"User","site_admin":false},"created_at":"2018-03-29T22:46:22Z","updated_at":"2018-03-29T22:46:22Z","author_association":"CONTRIBUTOR","body":"@erquhart good point on middleware - if we do use that kind of design, we'd definitely want an abstraction layer."}}
{"actor":{"display_login":"Benaiah"}}