npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

requestify.js

v1.1.0

Published

An add-on over fetch that allows convenient handling of caching and interceptor

Downloads

12

Readme

requestify.js

The requestify.js library is an add-on to fetch, and extends its capabilities by providing a convenient api, and the goal of requestify.js is to replace heavyweight request libraries.

Installing


Package manager

Using npm: npm install requestify.js Using yarn: yarn add requestify.js

Once the package is installed, you can import the library using import approach:

import { request } from 'requestify.js';

Example


import { request } from 'requestify.js';

request
	.get('https://api.example.com/users')
	.then(response => {
		console.log(response.data);
	})
	.catch(error => {
		console.error(error);
	});

const requestData = {
	body: {
		name: 'John Doe',
		email: '[email protected]',
	},
};

request
	.post('https://api.example.com/users', requestData)
	.then(response => {
		console.log(response.data);
	})
	.catch(error => {
		console.error(error);
	});

// Want to use async/await? Add the `async` keyword to your function/method.
async function getUsers() {
	try {
		const response = await request.get('https://api.example.com/users');
		console.log(response);
	} catch (error) {
		console.error(error);
	}
}

Requestify.js support typescript

import { request } from 'requestify.js';

interface IUsers {
	userId: number;
	name: string;
	age: number;
}

request
	.get<IUsers[]>('https://api.example.com/users')
	.then(response => {
		console.log(response.data); // response.data - IUsers[]
	})
	.catch(error => {
		console.error(error);
	});

const requestData = {
	body: {
		name: 'John Doe',
		email: '[email protected]',
	},
};

interface IRegisterData {
	name: string;
	email: string;
}

interface IRegisterResponse {
	id: number;
	email: string;
	name: string;
	age: number;
}

request
	.post<IRegisterData, IRegisterResponse>(
		'https://api.example.com/users',
		requestData
	)
	.then(response => {
		console.log(response.data); // response.data - IRegisterResponse
	})
	.catch(error => {
		console.error(error);
	});

// Want to use async/await? Add the `async` keyword to your function/method.
async function getUsers() {
	try {
		const response = await axios.get<IUser[]>('https://api.example.com/users');
		console.log(response.data); // response.data - IUser[]
	} catch (error) {
		console.error(error);
	}
}

Configuration


request has its own configuration, such as:

  • baseUrl - string
  • headers - [key: string]: string

You can set the behavior of interceptors in each request in the configuration, and you can choose whether to cache requests or not. For example:

import { request } from 'requestify.js';

request
	.post('https://api.example.com/users', {
		body: {
			name: 'test',
			email: '[email protected]',
		},
		async beforeInterceptor(request) {
			/* In this interceptor we intercept the request and perform some logic before
        sending it, we can add authorization keys to the headers and so on.
        IMPORTANT beforeInterceptor must necessarily return a request
        */
			console.log(request);
			return request;
		},
		async afterInterceptor(response) {
			// your logic

			/*
        in this interceptor we intercept the request and execute some logic before
        processing it. IMPORTANT the afterInterceptor must return some kind of response,
        how to process the data is up to you, whether it is text or blob file or json.
        */

			// default
			return {
				status: response.status,
				headers: response.headers,
				data: await response.json(),
			};
		},
	})
	.then(response => {
		console.log(response.data);
	})
	.catch(error => {
		console.error(error);
	});

Instance


For convenience and mashability, you can create separate request instances that are independent of each other

import { request } from 'requestify.js';

const myInstance = request.create();

Cache management


In order to cache requests and not to send them repeatedly, you need to pass the value "default" or "force-cache" to the cache field in the parameters to the request

The link on which the request was made and the parameters that were passed along with it are cached, if all of the above coincides, then at the value of the cache field "default" or "force-cache", the request will not be sent again

import { request } from 'requestify.js';

request
	.get('https://api.example.com/users', {
		cache: 'default',
	})
	.then(response => {
		console.log(response.data);
	})
	.catch(error => {
		console.error(error);
	});

All options requested


interface IRequest {
	baseUrl?: string;
	headers?: RequestHeaders;
	get: <T>(
		url: string,
		configs?: RequestInitial<undefined>
	) => Promise<IResponse<T>>;
	post: <T, R>(
		url: string,
		configs: RequestInitial<T>
	) => Promise<IResponse<R>>;
	put: <T, R>(url: string, configs: RequestInitial<T>) => Promise<IResponse<R>>;
	patch: <T, R>(
		url: string,
		configs: RequestInitial<T>
	) => Promise<IResponse<R>>;
	delete: <T, R>(
		url: string,
		configs: RequestInitial<T>
	) => Promise<IResponse<R>>;
	create: (config?: IConfig) => IRequest;
	_caches: Map<string, any>;
}