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

@pebble-solutions/api-request

v0.1.5

Published

Send and get request from resource API with a full authentication and authorization process

Downloads

8

Readme

Api Request Library

A simple way to fetch resources !

Api Request Library is a tool provided by Pebble to make authenticated requests to resources API easier.

Installation

[!WARNING]
Package is not published yet. This part of the documentation is currently not applicable

Run the following on your project

npm install @pebble-solutions/api-request

Usage

Single request with params

import {getRequest} from "@pebble-solutions/api-request";

const request = getRequest("https://my-api.tld/resource", {
    limit: 10
})
await request.send()

const content = request.content()

console.log(content)

Manage authentication with controller

Api Request Library provide a requests controller that can manage authentication mechanisms on multiple requests. Request Controller is a bridge between authentication process and HTTP requests. In a controller, all requests will share the same authentication object.

import {createRequestsController} from "@pebble-solutions/api-request";

// Auth object :
// The auth object must respect AuthorizationInterface rules
const auth = {
    getToken: () => {
        return Promise.resolve("valid.authorization.token")
    }
}

// Plug auth onto a new requests controller
const controller = createRequestsController().withAuth(auth)

// All the following requests will be authenticated.

// Send GET request through the controller
const request1 = controller.get("https://my-api.tld/resource", {
    limit: 10
})
await request1.send()

console.log(await request1.content())

// Send POST request through the controller
const request2 = controller.post("https://my-api.tld/resource", {
    name: "John",
    forename: "DOE"
})
await request2.send()

Requests bucket

A bucket is a way to send multiple requests at the same time. Bucket contains multiple requests that will be sent all together. All requests are places in a global promise that must be resolved before getting all the results.

Create a bucket from scratch


// Preparing requests
import {getRequest, createRequestsBucket} from "@pebble-solutions/api-request";

const req1 = getRequest("https://my-api.tld/resource")
const req2 = getRequest("https://my-api.tld/config")

const bucket = createRequestsBucket([req1, req2])

// All requests are sent at one time
await bucket.send()

// All content is returned back on an array with the same index order as the original one
const content = await bucket.content()

content.forEach(content => {
    console.log(content)
})

Create a bucket throughout a requests controller

import {createRequestsController, getRequest} from "@pebble-solutions/api-request";

// Auth object :
// The auth object must respect AuthorizationInterface rules
const auth = {
    getToken: () => {
        return Promise.resolve("valid.authorization.token")
    }
}

// Plug auth onto a new requests controller
const controller = createRequestsController().withAuth(auth)

const req1 = getRequest("https://my-api.tld/resource")
const req2 = getRequest("https://my-api.tld/config")

// The addRequests (with an s) method will create a bucket with all provided requests
const bucket = controller.addRequests([req1, req2])

// All requests are sent at one time
await bucket.send()

// All content is returned back on an array with the same index order as the original one
const content = await bucket.content()

content.forEach(content => {
    console.log(content)
})

Error management

All errors that might occur during the request will throw through an HTTPError instance.

import {getRequest, HTTPResponseError} from "@pebble-solutions/api-request";

const request = getRequest("https://my-api.tld/resource")

try {
    await request.send()

    const content = await request.content()

    console.log(content)
} catch (e) {
    console.log("Error occure : ", e.message)

    if (e instanceof HTTPResponseError) {
        console.log("More informations : ", e.status, e.statusText)
    }
}

Develop and build