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

@acusti/post

v0.5.1

Published

Super minimal fetch-inspired API wrapper around node.js’ http and https modules for making POST requests

Downloads

17

Readme

@acusti/post

latest version maintenance status downloads per month install size

post is a super minimal fetch-inspired API wrapper around node.js’ http and https modules for making POST requests. It’s a lean implementation with no dependencies that covers the most common GraphQL API use cases with a tiny fraction of the code of a spec-compliant solution like node-fetch. In addition, the ergonomics of post are optimized for making GraphQL queries.

Usage

npm install @acusti/post
# or
yarn add @acusti/post

The package exports post, a function that takes similar arguments to window.fetch (note that method defaults to POST and headers.content-type defaults to application/json; charset=UTF-8) and returns a promise. The promise is resolved with the parsed JSON version of the request’s response (i.e. return await response.json() when using the Fetch API), because that’s what you wanted anyways.

In addition, the second argument can take a query property (string) and a variables property (object), which it will JSON.stringify into a valid GraphQL request body. You can also pass in body as a string directly, but if you pass in a query, the body will be overwritten (you will get a type error in typescript if you try to pass both).

And lastly, if the response is an error (4xx or 5xx), post will throw an Error object with the response HTTP error and message as the Error object message and with the following additional properties:

  • Error.response: the node.js response IncomingMessage object
  • Error.responseJSON: if the response body can be parsed as JSON, the JSON representation returned from calling JSON.parse() on it
  • Error.responseText: the response body as text
import { post } from '@acusti/post';

type ResponseJSON = {
    data?: {
        country: {
            name: string;
            native: string;
            languages: Array<{
                code: string;
                name: string;
            }>;
        };
    };
    errors?: Array<{ message: string }>;
};

const url = 'https://countries.trevorblades.com/graphql';
const result = await post<ResponseJSON>(url, {
    query: `query Query {
        country(code: "MX") {
            name
            native
            languages {
                code
                name
            }
        }
    }`,
});

console.log(result.data.country.native); // 'México'