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

retrofit-request

v1.0.2

Published

Make http request as simple as possible

Downloads

3

Readme

Introduction

retrofit-request is a Retrofit clone. Its a wrapper of request using TypeScript's decorator feature for making cleaner HTTP request.

import {HTTP,GET,POST,Body, Ajax, Query,Header,Path} from "retrofit-request"

@HTTP("http://some.api.com")
class MyAPI {
    @GET("/get/something", {timeout: 1000})
    public static async getSomething(@Query("id")id: string){return null;}
}

try {
    //making a GET request to http://some.api.com/get/something?id=123
    let result = await MyAPI.getSomething("123");
} catch(e){}

API Declaration

Annotations on the class methods and its parameters indicate how a request will be handled.

REQUEST METHOD

Every method must have an HTTP annotation that provides the request method and relative URL. There are two built-in annotations: GET, POST. The relative URL of the resource is specified in the annotation.

@GET("users/list")

You can also specify query parameters in the URL.

@GET("users/list?sort=desc")

URL MANIPULATION

A request URL can be updated dynamically using replacement blocks and parameters on the method. A replacement block is an alphanumeric string surrounded by { and }. A corresponding parameter must be annotated with @Path using the same string.

@GET("group/{id}/users")
public async groupList(@Path("id")groupId: string): Promise<any> {return null;}

Query parameters can also be added.

@GET("group/{id}/users")
public async groupList(@Path("id")groupId: string, @Query("sort")sort: string): Promise<any>{return null;}

REQUEST BODY

An object can be specified for use as an HTTP request body with the @Body annotation.

@POST("users/new")
public async createUser(@Body user:User);

The objects` method will be discarded

FORM ENCODED AND MULTIPART

Methods can also be declared to send form-encoded and multipart data.

Form-encoded data is sent when @FormUrlEncoded is present on the method. Each key-value pair is annotated with @Field containing the name and the object providing the value.

@POST("user/edit")
@FormUrlEncoded
public async updateUser(@Field("first_name")first: string, @Field("last_name") last: string): Promise<User>{return null;}

Multipart requests are used when @Multipart is present on the method. Parts are declared using the @Part annotation.

@POST("user/photo")
@Multipart
public async updateUser(@Part("photo")photo: ReadStream, @Part("description")description: string): Promise<User>{return null;}

HEADER MANIPULATION

A request Header can be updated dynamically using the @Header annotation. A corresponding parameter must be provided to the @Header. If the value is null, the header will be omitted.

@GET("user")
public async getUser(@Header("Authorization")authorization: string): Promise<User> {return null;}

An evaluator can be specified with @Header as well as other parameter annotations, when evaluator is given the parameter will be applied to the evaluator

@GET("user")
public async getUser(@Header("Authorization", (x)=>`auth_header_${x}_spetial`)authorization: string): Promise<User> {return null;}

The header would be {"Authorization": "auth_header_123_spetial"}