1.0.9 • Published 5 years ago

@meetup/fetch-request v1.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Version

fetch-request

fetch-request is a library that provides a simple way to perform HTTPS requests in your lambda.

Wait, why another HTTP library? Doesn't this exist?

This library comes with AWS XRay integration and tracing out of the box (our very own lambda-xray package!) and follows an async/await pattern that we've been using in all our lambdas. We found ourselves copy-pasting a lot of our code between repos, so we decided to centralize our best practice patterns.

Install

npm install @meetup/fetch-request

Use

import httpsHelper from '@meetup/fetch-request';

const callMyApi = async (): Promise<string> => {
    const requestOpts = {
        host: 'fun-api.com',
        path: '/my-favorite-food?myname=juanbi',
        method: 'GET',
    };

    const response: string = await httpsHelper.fetchRequest<string>(requestOpts);
    return response;
};

You can also specify a payload and your own response/error handlers!

import httpsHelper from '@meetup/fetch-request';

const httpResponseHandler = <T>(
    response: import('http').IncomingMessage,
    buffer: string,
    payload: string,
    resolve: (data: T) => void,
    reject: (error: RequestError) => void,
): void => {
    if (response.statusCode && response.statusCode === 200) {
        // Do something with this response!
        const parsedBuffer = JSON.parse(buffer);
        resolve(parsedBuffer);
    } else {
        // Do something else with error!
        console.error('Error!');
        reject({ message: 'Serious error..', error: "I'm not kidding", statusCode: 500 });
    }
};

const callMyApi = async (): Promise<string> => {
    const requestOpts = {
        host: 'fun-api.com',
        path: '/my-favorite-food?myname=juanbi',
        method: 'GET',
    };

    const response: string = await httpsHelper.fetchRequest(requestOpts, 'IMPORTANT-PAYLOAD', httpResponseHandler);
    return response;
};

Note

We use the Node https library behind the scenes, so check their documentation out for information on what types of options and requests you can make: https://nodejs.org/api/https.html

Enjoy!

Made with :heart: by Comms Tools squad

1.0.9

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago