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

fetch-resolver

v1.12.0

Published

Generate custom fetch resolver functions for REST and GraphQL using a JSON definition

Downloads

28

Readme

fetch-resolver

Generate custom fetch resolver functions for REST and GraphQL using a JSON definition

import createFetchResolver from 'fetch-resolver'

// Example 01
// ----------
// create a resolver for a simple GET to a public REST API
const resolver01 = createFetchResolver({
    type: 'rest',
    url: 'https://jsonplaceholder.typicode.com/users',
})

const res01 = await resolver01()
// -> [ full response from typicode ]


// Example 02
// ----------
// create a resolver that can take in parameters
// re-shape the API data in order to match custom needs
const resolver02 = createFetchResolver({
    type: 'rest',
    url: 'https://jsonplaceholder.typicode.com/users/{{ id }}',
    shape: {
        id: 'id',
        name: 'name',
        address: '{{ address.street }}, {{ address.city }}',
    }
})

const res02 = await resolver02({ id: 1 })
// -> { id: 1, name: 'xxx', address: 'xxx' }


// Example 03
// ----------
// add custom headers to the request (with parameters)
// grab just a piece of the returned data and reshape it
const resolver03 = createFetchResolver({
    type: 'rest',
    url: 'https://jsonplaceholder.typicode.com/users/{{ id }}',
    headers: {
        'X-Origin': 'fetch-resolver',
        'X-RequestID': '{{ id }}',
    },
    grab: 'address',
    shape: {
        value: '{{ street }}, {{ city }}',
        loc: [ 'geo.lat', 'geo.lng' ],
    }
})

const res03 = await resolver03({ id: 1 })
// -> { value: 'xxx', loc: [ lat, lng ]}


// Example 04
// ----------
// compose a GraphQL request
const resolve04 = createFetchResolver({
    type: 'graphql',
    url: 'https://countries.trevorblades.com/',
    query: 'query foo ($code: String!) { country (code: $code) { code name phone currency }}',
    variables: { code: '{{ the.code }}' },
    grab: 'data.country.code',
})

const res04 = await resolve04({ the: { code: 'US' }})
// -> "US"
// note that we build a custom "variable" description that is capable of using the actual set
// of variables that are passed down to the resolver handler.
// this way we can use a nested JSON structure as data origin and re-shape the GraphQL's variables
// in a decent and declarative way.

Take a look at the tests to see other usecases with POST and GraphQL requests.