2.3.0 • Published 5 years ago

nocms-ajax v2.3.0

Weekly downloads
4
License
ISC
Repository
github
Last release
5 years ago

NoCMS Ajax

Ajax functions for NoCMS

semantic-release Dependency Status devDependencies

Installation

Install nocms-ajax from NPM and include it in your own React build process (using Browserify, Webpack, etc).

npm install nocms-ajax --save

Usage

import ajax from 'nocms-ajax';

Commit message format and publishing

This repository is published using semantic-release, with the default AngularJS Commit Message Conventions.

API

get, (url, options, callback)

Sends a get request and parse a JSON response. See options for other content types.

post, (url, data, options, callback)

Sends a post request with a JSON body

put, (url, data, options, callback)

Sends a put request with a JSON body

delete, (url, options, callback)

Sends a delete request with a JSON body

Response function

Response functions can be applied to every request, acting as a middleware prior to the callback functions.

applyOnResponse, (fn)

If you need to interrupt responses, e.g. if you want to reauthenticate upon 401 responses, you could apply a response function.

import ajax from 'nocms-ajax';
ajax.applyOnResponse((req, err, res, next) => {
  if (err.status === 401) {
    window.location = '/login';
    next({ interrupt: true });
  } else {
    next();
  }
});

This response functions take an object req, currently including the url of the request, err, including the error response object, res, the response object and next a function for proceeding with the next response function or callback invokation.

Pass on { interrupt: true } to prevent further handling of the response.

Pass on { replay: true } to invoke the original request once more. Callback is not called until next is called without any interruptions.

clearResponseFunctions

Removes all registered response functions.

options object

The following options are supported: accept sets the accept header of the request. Default: application/json, and thus the response is parsed. responseRequired is used for message api requests when a response on the message is required. secure is used for the message api to prevent logging of sensitive information.

2.3.0

5 years ago

2.2.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.2

7 years ago