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

dynamic-query-fetch

v1.0.0

Published

A Fetch API extension that adds dynamic query parameters.

Downloads

1

Readme

dynamic-query-fetch

A Fetch API extension that adds dynamic query parameters.

Installation

Use NPM to install the library package.

  npm install dynamic-query-fetch --save

Usage

This library tries to reach a familiar approach to the Axios library by implementing client creation to the Fetch API. Below is a quick example of how you can setup a client and send a GET request to a ficticious API and auto-set it's used version.

require("dynamic-query-fetch");

const client = fetch.create({
    baseUrl: "https://fake-api.com",
    params: {
        version: ({ values }) => values.version || 2
    }
});

(async function() {
    const response = await client.get({
        path: "/data",
        values: { version: 3 }
    });

    const json = response.json();
    console.log(json);
})();

Documentation

Quick-jump

Requiring

As the library inserts the create method into the global fetch function, no value is actually retunerd by the require or import.

Clients

Clients are the main wrappers for holding common properties between each request, just like what Axios does. A client object provides access to Request functions for perfoming HTTP requests to the API.

properties

  • baseUrl (string or URL): A common URL for using at the beginning of every request.
  • params (object, optional): An object that contains common static and dynamic parameters.
  • defaultMethod (string, default: "GET"): A valid HTTP method in UPPERCASE.

example

const client = fetch.create({
    baseUrl: "",
    params: { },
    defaultMethod: "GET"
});

Requests

A request is a function provided by the Client which performs an HTTP request. All common HTTP methods contain a request method, such as GET (which maps to get) and POST (which maps to post). All of these methods receive an object as a parameter that holds properties for that specific request.

properties

  • path (string, default: "/"): An extra pathname to be appended to the URL.
  • method (string): A required string that contains the method to be used. HTTP method wrappers override this property.
  • params (object, optional): An object of static and dynamic parameters to add (overrides the client parameters).
  • values (object, optional): An object of values to be passed to the dynamic parameters.
  • fetchOptions (object): An object that holds Fetch API options.

methods

  • request
  • get
  • post
  • put
  • delete
  • head
  • connect
  • options
  • trace
  • patch

example

const client = fetch.create({ /* ... */ });

await client.get({
    values: { version: 23 },
    params: { customParam: 20 }
});

Dynamic Parameters and Context Values

The big goal of this library is to add dynamic parameters to the Fetch API, and it does. Dynamic parameters are query params that are re-evaluated after every request that is triggered.

Every dynamic parameter receive a props object that contains info about the current URL and a values object that provides all context values received from the request calls.

It is recommended that by using context values, you provide some sort of default value. e.g. (values.number || DEFAULT_NUM)

parameters

  • url (string): A string representation of the current URL with all the current evaluated parameters.
  • values (object): An object that contains data received from requests.

example

const client = fetch.create({
    baseUrl: BASE_URL,
    params: {
        staticParam: 3000,
        dynamicParam: ({ url, values }) => {
            return url.length * (values.number || 10);
        }
    }
});

// Generated URL:
// https://fake-api.com?staticParam=3000&dynamicParam=370

Contributing

This is a really simple project, so contributions are always welcome! Feel free to change anything.