Skip to content

Latest commit

 

History

History
303 lines (175 loc) · 7.28 KB

api-configurator.md

File metadata and controls

303 lines (175 loc) · 7.28 KB

APIConfigurator

The APIConfigurator class provides methods to handle requests to non static resources. An instance of this configurator can be found with the Configurator.prototype.api property:

const server = new Configurator();

// *Getting the API configurator:
let api_configurator = server.api;



Properties

APIConfigurator.METHODS

Note: see Configurator.METHODS.



APIConfigurator.prototype.resources

APIResource[] - An array containing all the API resources

Note: readonly, use the add() method to add a new resource.



APIConfigurator.prototype.advanced

Starts an advanced configurator chain for the last added resource

AdvancedAPIConfigurator - The advanced configurator of the last added route

Note: Must be called only right after a route has been added.

Throws

  • Error - If it's not being called right after a route has been added

Examples

server.api
   // *Adding some GET route:
   .get(...)
      // *The following advanced chain will configure the last GET route:
      .advanced
      // *Setting some advanced configurations:
      .header(...)
      // *After the done() gets called, the chain goes back to the outer configurator again:
      .done()
   // *Continuing with the APIConfigurator:
   .put(...);

// *The following lines will throw an error,
//  as the advanced chain must be available only right after a route been added:
server.api
   .advanced;



Methods

APIConfigurator.prototype.add(methods, route, middleware)

Registers a middleware for the given route and HTTP methods

See: supported HTTP methods

Parameters

  • methods string|string[] - One or more supported HTTP methods
  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)

Throws

  • TypeError - If methods is not a string or an array of strings
  • Error - If some HTTP method is not supported

Examples

server.api
   .add(Configurator.METHODS.POST, '/some-route', someMiddlewareFunction)
   .add('POST', '/some-other-route', someOtherMiddlewareFunction);


APIConfigurator.prototype.get(route, middleware)

Registers a middleware for the given GET route

Note: same as add() with the HTTP method set to GET.

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.post(route, middleware)

Registers a middleware for the given POST route

Note: same as add() with the HTTP method set to POST.

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.put(route, middleware)

Registers a middleware for the given PUT route

Note: same as add() with the HTTP method set to PUT.

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.delete(route, middleware)

Registers a middleware for the given DELETE route

Note: same as add() with the HTTP method set to DELETE.

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.head(route, middleware)

Registers a middleware for the given HEAD route

Note: same as add() with the HTTP method set to HEAD.

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.patch(route, middleware)

Registers a middleware for the given PATCH route

Note: same as add() with the HTTP method set to PATCH.

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.options(route, middleware)

Registers a middleware for the given OPTIONS route

Note: same as add() with the HTTP method set to OPTIONS.

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.all(route, middleware)

Registers a middleware for all supported HTTP methods

Note: same as add() with all the supported HTTP methods.

Note: This method is NOT equivalent to the Expressjs's app.all().

See: supported HTTP methods

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.most(route, middleware)

Registers a middleware for GET, POST, PUT, DELETE, HEAD and PATCH routes

Note: same as add() with the HTTP method set to GET, POST, PUT, DELETE, HEAD and PATCH.

Parameters

  • route string - The server route
  • middleware function|function[] - A valid Expressjs middleware function

Returns

APIConfigurator - This same configurator (for method chaining)



APIConfigurator.prototype.done()

Retrieves the main configurator

Returns

Configurator - The main configurator (for configurator chaining)

Examples

// *Main 'Configurator' object:
server
   .port(...)
   .api
      // *Inner 'APIConfigurator' object:
      .add(...)
      .post(...)
      .put(...)
      .done()   // Returns the chain back to the main configurator
   // *Main 'Configurator' object:
   .start();