0.0.23 • Published 5 years ago

ajax2 v0.0.23

Weekly downloads
40
License
Unlicense
Repository
github
Last release
5 years ago

ajax2

A simple ajax library with optional case converter

Installation

Using npm:

npm i --save ajax2

Basic usage

To make a request, use the following structure:

import ajax2 from 'ajax2'

ajax2.get('/path/to/resource')
    .then(function(response){
        // response will be an object if json was returned or text.
    })
    .catch(function(err){
        console.log({
            statusCode: err.statusCode, // e.g. 404
            statusText: err.statusText, // e.g. Not found
            response: err.response // Whatever the body was. Json will be parsed and an object returned
        });
    });

# Adding data to the request
ajax2.post('/whatever', {hereGoesTheData: ''})

Supported methods are get, post, put, patch, delete.

All calls will return a Promise, which you can use with await.

Case conversion

Optionally, ajax2 will convert request data from camelCase to snake_case, for usage with Python or Ruby. Conversely responses can be converted from snake_case to camelCase.

Note that only the keys will be converted. Example:

ajax2.post('/whatever', {exampleKey: 'exampleValue'})

will create a request with the following json:

{"example_key": "exampleValue"}

This is intended to preserve user input.

To enable this, call ajax2._configure...

Changing configuration

To change configuration, use ajax2._configure.

ajax2._configure({
    convertRequest: null, // 'snakeCase', 'camelCase', or null
    convertResponse: null, // 'snakeCase', 'camelCase', or null
    headers: {}, // A dictionary with headers to send with all requests
    addRequestedWith: true, // Adds 'X-Requested-With' to headers
    credentials: 'same-origin', // 'same-origin', 'include', or null
    baseUrl: null // Whatever url to prepend all requests urls with
});

You can override configuration for a particular request by passing configuration to the request method.

ajax2.post('/whatever', {hereGoesTheData: ''}, {credentials: null})

NB: It is not necessary to specify a Content-Type header as this will be set to application/json if there is data (and it is not a GET request).

0.0.21

5 years ago

0.0.22

5 years ago

0.0.23

5 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

7 years ago

0.0.17

8 years ago

0.0.16

8 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.1

10 years ago