AbstractApiModule

AbstractApiModule

Abstract module for creating APIs

Constructor

new AbstractApiModule()

Source:

Extends

  • AbstractModule

Members

accessCheckHook :Hook

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

cache

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

collectionName :String

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

isApiModule :Boolean

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

permissionsScope :String

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

postDeleteHook :Hook

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

postInsertHook :Hook

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

postUpdateHook :Hook

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

preDeleteHook :Hook

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

preInsertHook :Hook

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

preUpdateHook :Hook

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

requestHook :Hook

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

root :String

Description:
  • Name of the API module
Source:
Name of the API module
Type:
  • String

router :Router

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

routes :Array.<ApiRoute>

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

schemaName :String

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

Methods

addRoute(config, auth)

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

(async) addRoutes() → {Promise}

Description:
  • Adds any defined routes
Source:
Returns:
Type
Promise

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

Description:
  • Invokes the access check hook to allow modules to determine whether the request user has sufficient access to the requested resource(s)
Source:
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}

Description:
  • Removes a single document from the DB
Source:
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}

Description:
  • Removes multiple documents from the DB
Source:
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}

Description:
  • Retrieves documents from the DB
Source:
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}

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

(async) getSchemaName(data) → {String}

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

(async) init()

Source:

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

Description:
  • Inserts a new document into the DB
Source:
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}

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

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

Description:
  • Parses an incoming query for use in the DB module
Source:
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}

Description:
  • 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.
Source:
Parameters:
Name Type Description
req external:ExpressRequest
res external:ExpressResponse
next function
Returns:
Middleware function
Type
function

queryHandler() → {function}

Description:
  • 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.
Source:
Returns:
Type
function

requestHandler() → {function}

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

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

Description:
  • Recursive sanitiser, see sanitiseItem
Source:
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}

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

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

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

setDefaultOptions(options)

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

(async) setUpPagination(req, res, mongoOpts)

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

(async) setValues() → {Promise}

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

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

Description:
  • Updates an existing document in the DB
Source:
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()

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

(async) validate(schemaName, data, options)

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

validateValues()

Description:
  • Checks required values have been set
Source: