Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

@meetup/fetch-request

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@meetup/fetch-request

HTTP Request Helpers

latest
npmnpm
Version
1.0.9
Version published
Maintainers
1
Created
Source

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

FAQs

Package last updated on 08 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts