AbstractApiModule

AbstractApiModule

Abstract module for creating APIs

Constructor

new AbstractApiModule()

Source:

Extends

  • AbstractModule

Members

accessCheckHook :Hook

Source:
Hook invoked by DB wrapper functions to check access to individual data items
Type:
  • Hook

cache

Source:
Data cache which can be used to reduce DB calls

collectionName :String

Source:
Default DB collection to store data to (can be overridden by individual handlers)
Type:
  • String

isApiModule :Boolean

Source:
Signifies that the module instance is an API module. Can be used by other modules for quick verification checks.
Type:
  • Boolean

permissionsScope :String

Source:
The scope to be used (see AbstractApiModule#useDefaultRouteConfig)
Type:
  • String

postDeleteHook :Hook

Source:
Hook invoked after data is deleted
Type:
  • Hook

postInsertHook :Hook

Source:
Hook invoked after data is inserted into the database
Type:
  • Hook

postUpdateHook :Hook

Source:
Hook invoked after data is updated in the database
Type:
  • Hook

preDeleteHook :Hook

Source:
Hook invoked before data is deleted
Type:
  • Hook

preInsertHook :Hook

Source:
Hook invoked before data is inserted into the database
Type:
  • Hook

preUpdateHook :Hook

Source:
Hook invoked before data is updated in the database
Type:
  • Hook

requestHook :Hook

Source:
Hook invoked when a new API request is handled
Type:
  • Hook

root :String

Source:
Name of the API module
Type:
  • String

router :Router

Source:
The Router instance used for HTTP requests
Type:
  • Router

routes :Array.<ApiRoute>

Source:
Routes to be added to the API router
Type:
  • Array.<ApiRoute>

schemaName :String

Source:
Default schema to use for validation (can be overridden by individual handlers)
Type:
  • String

Methods

addRoute(config, auth)

Source:
Adds a single route definition
Parameters:
Name Type Description
config Route The route config
auth AuthModule Reference to the AuthModule instance to save await-ing

(async) addRoutes() → {Promise}

Source:
Adds any defined routes
Returns:
Type
Promise

(async) checkAccess(req, data) → {Promise}

Source:
Invokes the access check hook to allow modules to determine whether the request user has sufficient access to the requested resource(s)
Parameters:
Name Type Description
req external:ExpressRequest
data Object The data to be checked
Returns:
Rejects if access should be blocked
Type
Promise

(async) delete(query, options, mongoOptions) → {Promise}

Source:
Removes a single document from the DB
Parameters:
Name Type Description
query Object Attributes to use to filter DB documents
options DeleteOptions Function options
mongoOptions external:MongoDBDeleteOptions Options to be passed to the MongoDB function
Returns:
Resolves with DB data
Type
Promise

(async) deleteMany(query, options, mongoOptions) → {Promise}

Source:
Removes multiple documents from the DB
Parameters:
Name Type Description
query Object Attributes to use to filter DB documents
options DeleteOptions Function options
mongoOptions external:MongoDBDeleteOptions Options to be passed to the MongoDB function
Returns:
Type
Promise

(async) find(query, options, mongoOptions) → {Promise}

Source:
Retrieves documents from the DB
Parameters:
Name Type Description
query Object Attributes to use to filter DB documents
options FindOptions Function options
mongoOptions external:MongoDBFindOptions Options to be passed to the MongoDB function
Returns:
Resolves with DB data
Type
Promise

(async) getSchema(schemaName, data) → {Object}

Source:
Retrieves a schema by name
Parameters:
Name Type Description
schemaName String
data Object Can be used when determining schema type
Returns:
Type
Object

(async) getSchemaName(data) → {String}

Source:
Derives the schema name from passed apiData
Parameters:
Name Type Description
data Object Request data
Returns:
The schema name
Type
String

(async) init()

Source:

(async) insert(data, options, mongoOptions) → {Promise}

Source:
Inserts a new document into the DB
Parameters:
Name Type Description
data Object Data to be inserted into the DB
options InsertOptions Function options
mongoOptions external:MongoDBInsertOneOptions Options to be passed to the MongoDB function
Returns:
Resolves with DB data
Type
Promise

mapStatusCode(httpMethod) → {Number}

Source:
Returns the 'OK' status code to match the HTTP method
Parameters:
Name Type Description
httpMethod String
Returns:
HTTP status code
Type
Number

(async) parseQuery(schemaName, query, mongoOptions) → {Object}

Source:
Parses an incoming query for use in the DB module
Parameters:
Name Type Description
schemaName String The schema name for the data being queried
query Object The query data
mongoOptions Object Options to be passed to the MongoDB function
Returns:
The parsed query
Type
Object

(async) processRequestMiddleware(req, res, next) → {function}

Source:
Express middleware which correctly formats incoming request data and stores as req.apiData to be used by later handlers. See ApiRequestData typedef for full details.
Parameters:
Name Type Description
req external:ExpressRequest
res external:ExpressResponse
next function
Returns:
Middleware function
Type
function

queryHandler() → {function}

Source:
Express request handler for advanced API queries. Supports collation/limit/page/skip/sort and pagination. For incoming query data to be correctly parsed, it must be sent as body data using a POST request.
Returns:
Type
function

requestHandler() → {function}

Source:
Middleware to handle a generic API request. Supports POST, GET, PUT and DELETE of items in the database.
Returns:
Express middleware function
Type
function

(async) sanitise(schemaName, data, options) → {Promise}

Source:
Recursive sanitiser, see sanitiseItem
Parameters:
Name Type Description
schemaName string Name of schema to sanitise against
data object Data to sanitise
options object see sanitiseItem
Returns:
Resolves with the sanitised data
Type
Promise

(async) sanitiseRequestDataMiddleware(req, res, next) → {function}

Source:
Sanitises incoming request data
Parameters:
Name Type Description
req external:ExpressRequest
res external:ExpressResponse
next function
Returns:
Middleware function
Type
function

(async) serveSchema(req, res, next) → {function}

Source:
Express request handler for serving the schema
Parameters:
Name Type Description
req external:ExpressRequest
res external:ExpressResponse
next function
Returns:
Type
function

setDefaultOptions(options)

Source:
Takes an input options param and populates it with defaults
Parameters:
Name Type Description
options Object

(async) setUpPagination(req, res, mongoOpts)

Source:
Validates and sets the relevant pagination options (limit, skip) and HTTP headers (X-Adapt-Page, X-Adapt-PageTotal, Link).
Parameters:
Name Type Description
req external:ExpressRequest
res external:ExpressResponse
mongoOpts Object The MongoDB options

(async) setValues() → {Promise}

Source:
Sets values used to initialise the API
Returns:
Type
Promise

(async) update(query, data, options, mongoOptions) → {Promise}

Source:
Updates an existing document in the DB
Parameters:
Name Type Description
query Object Attributes to use to filter DB documents
data Object Data to be inserted into the DB
options UpdateOptions Function options
mongoOptions external:MongoDBFindOneAndUpdateOptions Options to be passed to the MongoDB function
Returns:
Resolves with DB data
Type
Promise

useDefaultRouteConfig()

Source:
Uses default configuration for API routes
Example
POST /
GET /:_id?
PUT/DELETE  /:_id

(async) validate(schemaName, data, options)

Source:
Validates data
Parameters:
Name Type Description
schemaName String Name of the schema to validate against
data Object Data to validate
options Object

validateValues()

Source:
Checks required values have been set