Skip to content

Latest commit

 

History

History
130 lines (83 loc) · 2.49 KB

respond.md

File metadata and controls

130 lines (83 loc) · 2.49 KB

Respond

Respond is used to create responses with a fluent API

import RESPOND_PROVIDERS, {Respond} from 'respond-ng';

const injector = Injector.resolveAndCreate([
  RESPOND_PROVIDERS
]);

const respond = injector.get(Respond);

respond.withBody({ error: 'Not Found' }).withStatus(404);

withHeader(name, value)

Adds a header to the response object

Parameters

  • name string Name of the header to set
  • value string | string[] Value of the header to set

Returns this

respond.withHeader('Accept', 'text/json')

withBody(body)

Sets the body for the response object

Parameters

  • body any Body string or object. If it is an object it will be serialized to JSON

Returns this

respond.withBody({ auth: true })

withStatus(status)

Sets the status code of the response object. If the code is less than 200 or greater than 299 then the response will be treated like a failure.

Parameters

  • status number The status code to set

Returns this

respond.withStatus(404)

withUrl(url)

Sets the URL of the response object

Parameters

  • url string The response object's URL

Returns this

respond.withUrl('/api')

with(status, body, headers)

Shortcut to set the status, body, and headers in one call

Parameters

  • status number Status code of the response
  • body optional any Body of the response
  • headers optional Headers Headers object to use

Returns this

const headers = new Headers();
headers.set('Accept', 'text/json');

respond.with(500, { error: 'Internal Server Error' }, headers)

ok(body, headers)

Shortcut to set the body and headers with a 200 status code

Parameters

  • body optional any Body of the response
  • headers optional Headers Headers object to use

Returns this

const headers = new Headers();
headers.set('Accept', 'text/json');

respond.ok({ authenticated: true }, headers)

error(body, headers)

Shortcut to set the body and headers with a 500 status code

Parameters

  • body optional any Body of the response
  • headers optional Headers Headers object to use

Returns this

const headers = new Headers();
headers.set('Accept', 'text/json');

respond.error({ authenticated: false }, headers)

when

Serializes the response, then creates and registers a new RequestMatcher for the response

Returns RequestMatcher

respond.ok().when.get('/api');