0.0.23 • Published 3 years ago

ajax2 v0.0.23

Weekly downloads
40
License
Unlicense
Repository
github
Last release
3 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

3 years ago

0.0.22

3 years ago

0.0.23

3 years ago

0.0.20

4 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

6 years ago

0.0.16

7 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.1

8 years ago