docs: type is required.

This commit is contained in:
Hayden Young 2023-06-18 03:22:18 +01:00
parent 4c3a338205
commit 7fafda8416

View File

@ -1,21 +1,30 @@
/**
* @module AccessControllers
* @description
* Provides a platform for managing access controllers. Supported access
* Provides a system for managing access controllers. Supported access
* controllers can be added and removed from the access controller list, and
* can load the associated module if they are supported.
*
* An AccessController module needs to only expose one function,
* canAppend(entry) which returns true if the entry can be appended to the
* oplog, or false otherwise:
* ```javascript
* const CustomAccessController = ({ write } = {}) => async => {
* An AccessController module needs to only expose one constant, type, and one function, canAppend(entry), which returns true if the entry can be appended to
* the oplog, or false otherwise:
* ```js
* type = 'custom'
*
* const CustomAccessController = ({ write } = {}) => async ({ orbitdb, identities, address }) => {
* const canAppend = async (entry) => {
* // Use entry.identity to determine whether the entry can be appended.
* // Return true if entry can be appended to OpLog.
* // Or return false otherwise.
* }
*
* return {
* canAppend,
* type
* }
* }
*
* export default CustomAccessController
* ```
*/
import IPFSAccessController from './ipfs.js'
import OrbitDBAccessController from './orbitdb.js'
@ -37,7 +46,7 @@ const accessControllers = {
* @return {AccessController} The access controller module.
*/
const getAccessController = (type) => {
if (!Object.keys(accessControllers).includes(type)) {
if (!accessControllers[type]) {
throw new Error(`AccessController type '${type}' is not supported`)
}
return accessControllers[type]
@ -53,12 +62,12 @@ const getAccessController = (type) => {
* controller module does not implement a type property.
*/
const addAccessController = (accessController) => {
if (accessControllers[accessController.type]) {
throw new Error(`Access controller '${accessController.type}' already added.`)
if (!accessController.type) {
throw new Error('Access controller does not contain required field \'type\'')
}
if (!accessController.type) {
throw new Error('Given AccessController class needs to implement: type.')
if (accessControllers[accessController.type]) {
throw new Error(`Access controller '${accessController.type}' already added.`)
}
accessControllers[accessController.type] = accessController