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

@ronin-public/request

v1.1.0

Published

## XMRequestPromise

Downloads

27

Readme

XMRequest APIs

XMRequestPromise

abstract class XMRequestPromise<T> extends Promise<T> {
  public abstract cancel(): XMRequestPromise<T>;
  public abstract interceptor(hanlder: () => void): XMRequestPromise<T>;
}

get<T>(url: string): XMRequestPromise<T>

exmaple

// index.ts
import {get}, XMRequest from '@ronin-public/request'

get<string>('/name?id=123456').then(res => console.log(res))

// or
XMRequest.get<string>('/name?id=123456').then(res => console.log(res))

post<T, P = Record<string, any>>(url: string, data: P): XMRequestPromise<T>

example

// index.ts
import {post}, XMRequest from '@ronin-public/request'

type userT = {
    id: number
}

// default
post<string>('/name', {id: 123456}).then(res => console.log(res))


// or you can customize defaine parameter's type
post<string, userT>('/name', {id: 123456}).then(res => console.log(res))

// or you also can use like that
XMRequest.post<string>('/name', {id: 123456}).then(res => console.log(res))

request<T>(url: string, data?: RequestInit): XMRequestPromise<T>

example

// index.ts
import {request}, XMRequest from '@ronin-public/request'

request<string>('/name', {
	method: 'POST',
	body: JSON.stringify({id: 123456})
	headers: {
		'Content-Type': 'application/text'
	}
}).then(res => console.log(res))

// or
XMRequest.request<string>('/name', {
	method: 'POST',
	body: JSON.stringify({id: 123456})
	headers: {
		'Content-Type': 'application/text'
	}
}).then(res => console.log(res))

View MDN to learn how to set data

Cancel request

request().cancel()

example

import { get } from "XMRequest";

const promise = get("/abc");
promise.then((res) => console.log(res));
setTimeout(() => {
  promise.cancel();
}, 2000);

Interceptor before request

beforeRequest(handler: (url: string, param?: RequestInit) => boolean)

example

// index.ts
import {beforeRequest}, XMRequest from '@ronin-public/request'
beforeRequest(url => url.endsWith('/user'))

// or
XMRequest.beforeRequest(url => url.endsWith('/user'))

// test
XMrequest.get('/abc')
.interceptor(() => {
	console.log('request /abc has been intercepted') // output: request /abc has been intercepted
})
.catch(e => {
	console.error(e) // output: This request is intercepted before request
})

Interceptor callback

Global interceptor

onInterceptor(handler: (url: string, param?: RequestInit) => void)

example

// index.ts
import {onInterceptor}, XMRequest from '@ronin-public/request'

onInterceptor(url => {
	console.error(`request ${url} has been intercepted`)
})

// or
XMRequest.onInterceptor(url => {
	console.error(`request ${url} has been intercepted`)
})

Single interceptor

request().interceptor(handler: () => void)

If you want to fire the interceptor handler, you need to use beforeRequest first.

example

// intercept.ts
XMRequest.beforeRequest((url) => url.endsWith("/user"));

// index.ts
import XMRequest from "XMRequest";

// catch
// it also can be catched
XMRequest.get("/abc").catch((e) => {
  console.error(e); // output: This request is intercepted before request
});

// get interceptor
XMRequest.get("/abc").interceptor(() => {
  console.log("request has been intercepted"); // output: request has been intercepted
});

// post interceptor
XMRequest.post("/abc").interceptor(() => {
  console.log("request has been intercepted"); // output: request has been intercepted
});

// request interceptor

XMRequest.request("/abc").interceptor(() => {
  console.log("request has been intercepted"); // output: request has been intercepted
});

Unified error handler: onError(handler: (url: string) => void)

The handler have only one, pre one will be covered by next one. example

import {onError}, XMRequest from '@ronin-public/request'
onError(url => {
	console.error(`An error occurred  when request {url}`)
})

// or
XMRequest.onError(url => {
	console.error(`An error occurred  when request {url}`)
})

setUnifiedRequestInit(handler: (url: string) => RequestInit)

The handler will be called before a request, you can customize the unified params according to the url, if you set the same field when a request called, the unified config will be covered. example

import {setUnifiedRequestInit}, XMRequest from '@ronin-public/request'

setUnifiedRequestInit(() => {
	return {
		headers: {
			'Content-Type': 'application/text'
		}
	}
})

// the value of headers's 'Content-Type' is 'application/text'
XMRequest.get('/abc')

// the 'Content-Type' will be used 'application/json' as its value
XMRequest.request('/abc', { headers: { 'Content-Type': 'application/json' } })