# nocms-ajax

> Ajax functions for NoCMS

Latest version **2.3.0** (published 2018-11-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install nocms-ajax
pnpm add nocms-ajax
yarn add nocms-ajax
bun add nocms-ajax
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.3.0 |
| Published | 2018-11-06 |
| First published | 2017-05-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 19.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | nocms |
| Keywords | ajax, nocms |

## Links

- npm: https://www.npmjs.com/package/nocms-ajax
- Repository: https://github.com/miles-no/nocms-ajax
- Issues: https://github.com/miles-no/nocms-ajax/issues
- npm.io page: https://npm.io/package/nocms-ajax

## Recent versions

- 2.3.0 (latest) — 2018-11-06
- 2.2.0 — 2018-10-11
- 2.1.1 — 2018-03-22
- 2.1.0 — 2018-03-22
- 2.0.2 — 2018-03-15
- 2.0.1 — 2018-03-15
- 2.0.0 — 2018-03-15
- 1.1.1 — 2017-12-11
- 1.1.0 — 2017-12-08
- 1.0.0 — 2017-08-04
- 0.2.0 — 2017-08-04
- 0.1.2 — 2017-08-04
- 0.1.1 — 2017-08-04
- 0.1.0 — 2017-08-04
- 0.0.2 — 2017-05-12

## README

# NoCMS Ajax

Ajax functions for NoCMS

[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Dependency Status](https://david-dm.org/miles-no/nocms-ajax.svg)](https://david-dm.org/miles-no/nocms-ajax)
[![devDependencies](https://david-dm.org/miles-no/nocms-ajax/dev-status.svg)](https://david-dm.org/miles-no/nocms-ajax?type=dev)


## Installation

Install nocms-ajax from NPM and include it in your own React build process (using [Browserify](http://browserify.org), [Webpack](http://webpack.github.io/), 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](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit).

## 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.

```js
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.

---
_Source: https://npm.io/package/nocms-ajax · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
