4.0.14 • Published 6 years ago

cf-util-http v4.0.14

Weekly downloads
221
License
BSD-3-Clause
Repository
-
Last release
6 years ago

cf-util-http

Cloudflare HTTP Util

This utility library is a simple interface that hides away the client-side http implementation being used so that it can easily be swapped out for something else.

Installation

$ npm install cf-util-http

Usage

import * as http from 'cf-util-http';

http.beforeSend(opts => {
  opts.url = '/api/v4' + opts.url;
});

http.post('/posts', {
  body: {
    title: 'A New Post',
    content: 'Contents of the new post.'
  }
}, (err, res) => {
  if (err) {
    console.log(err.body); // > { errors: [{ message: 'Error!' }] }
  } else {
    console.log(res.body); // > { result: { id: 1, title: 'A New Post', content: 'Contents of the new post.' } }
  }
});

API

http.request(method, url, [opts], [callback])

Perform an HTTP request.

const abortRequest = http.request('POST', '/posts', {
  headers: {...},
  parameters: {...},
  body: {...}
}, callback);

abortRequest();

Parameters:

NameTypeDescription
methodStringRequired. The HTTP method type for the request.
urlStringRequired. The url to make the request.
optsObjectOptional. Options for the request.
opts.parametersObjectOptional. Parameters to be serialized into the url.
opts.headersObjectOptional. Headers to send with the request.
opts.bodyObjectOptional. The body of the request.
opts.skipBodyTransformBooleanOptional. Prevents opts.body from being converted to JSON.
callbackFunctionCallback to call when request is complete.

Returns:

http.request will return an abort function that you can call to stop the request from being made.

callback:

The callback will receive two arguments: err and res which will both have the following shape (if they are not null):

{
  headers: {...},
  status: 200,
  body: {...}
  text: '...'
}

http.[get/post/put/patch/del]

These are all shorthands to http.request that don't require passing a method.

http.beforeSend(callback)

Modify a request before it is sent. This can be useful for authentication or other middleware.

callback will be called with a single argument opts that you can mutate before a request will be created. opts will have the following shape:

{
  method: 'POST',
  url: '/posts',

  // Headers to be sent along with the request
  headers: {
    Accept: 'application/json'
  },

  // Parameters to be serialized as `?page=1&limit=20` and appended to the url
  parameters: {
    page: 1,
    limit: 20
  },

  // Body of the request being sent
  body: {
    title: 'A New Post',
    content: 'Contents of the new post.'
  }
}

Note that headers, parameters, and body may not exist depending on the request being made.

4.0.14

6 years ago

4.0.13

6 years ago

4.0.12

6 years ago

4.0.11

6 years ago

4.0.10

6 years ago

4.0.9

7 years ago

4.0.8

7 years ago

4.0.7

7 years ago

4.0.6

7 years ago

4.0.5

7 years ago

4.0.4

7 years ago

4.0.3

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.9.0

7 years ago

3.8.0

7 years ago

3.7.1

7 years ago

3.7.0

7 years ago

3.6.2

7 years ago

3.6.1

7 years ago

3.5.10

7 years ago

3.5.9

7 years ago

3.6.0

7 years ago

3.5.8

7 years ago

3.5.7

7 years ago

3.5.6

7 years ago

3.5.5

7 years ago

3.5.4

7 years ago

3.5.3

7 years ago

3.5.2

7 years ago

3.5.1

7 years ago

3.5.0

7 years ago

3.4.0

7 years ago

3.3.0

7 years ago

3.2.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.2.4

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago