maka:http

v1.1.9Published 4 months ago

maka:http

Github Link to: Source code of released version


Installation

    $ meteor add maka:http

If you're using maka-cli:

    $ maka add maka:http

Docs

This is just an iteration of the Meteor HTTP package. It generally adheres to the original HTTP contracts, but the calls can either return a Callback OR a Promise.

You can still use the old callback way (as in http/meteor):

    import { HTTP } from 'meteor/maka:http';

    HTTP.get('url', (error, response) => {
        if (error) {
            console.error(error);
        }

        console.log(response);
    });

Or migrate to:

    import { HTTP } from 'meteor/maka:http';

    const { data, statusCode } = await HTTP.get('url');

    if (statusCode > 400) {
        console.error(data);
    } else {
        console.log(data);
    }

Changes

  1. The response object no longer contains both a 'data' AND a 'content'. It only contains the data object. This object will be in a format based on the content type. (e.g., 'application/json')

  2. All call, get, post, put, del, and options will return a Promise only if a callback isn't defined. (see previous section)

  3. If there is an error (400 or over), this will no longer return an Error object. It will simply return the response from the server as it does with all other status codes.

  4. The tests are broken (13 out of 15 are passing)! I plan on fixing them.

  5. There is no longer an 'auth', param, query, or content option, you'll need to make your own param string or declare the options.data (rather than options.content. To declare 'auth' use the headers section.

  6. 'npmRequestOptions' and 'beforeSend' have been removed, not sure it actually did anything in the end 🤔. Instead of before send, use the new HTTP.addRequestInterceptor() method (see #7). If you need raw repsonse data, use rawResponse which will return an ArrayBuffer

1    const { data } = await HTTP.get(url, { rawResponse: true }); // data contains an ArrayBuffer
  1. There are now interceptors that will allow you to catch either the request before it goes out, or the response just before it's handled. They can be added on the server or client, with the same signature.
1    HTTP.addRequestInterceptor(async (method, url, options) => {
2      if (method === 'GET') {
3        console.log('Intercepted GET request to URL:', url);
4      }
5      return { method, url, options };
6    });

NOTE: If you are using callbacks, then you should include callback in the return:

1    HTTP.addRequestInterceptor(async (method, url, options, callback) => {
2      if (method === 'GET') {
3        console.log('Intercepted GET request to URL:', url);
4      }
5      return { method, url, options, callback };
6    });
  1. There are now two options for retries: maxRetries and retryDelay. Default is maxRetry: 0 (no retries) and retryDelay: 1000 (1 second). Retry delays are in miliseconds, and they are exponential. Usable on both Server and Client with the same interface.
1    const { data, statusCode } = await HTTP.get(url, {
2      retryDelay: 2000,
3      maxRetries: 3,
4    });
  1. The timeout option is defaulted now to 30 seconds (30000 ms) instead of no default.

See the HTTP section in the Meteor docs for more details... but with a grain of salt, based on the changes above, and that Meteor has deprecated those documents 😉.

Acknowledgments

Thank you to Meteor and the Meteor community who kept this original package running well!