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

adapters.ts

v2.0.5

Published

Quick, easy and type-safe way for declarative http endpoint definitions.

Downloads

53

Readme

adapters.ts

Quick, easy and type-safe way for declarative http endpoint definitions.

Quick Start

Simple, untyped requests.

import { adapter } from "adapters.ts";

// get request
adapter.get("/api/films", {
  baseURL: "https://swapi.dev/",
}).then(response => {
  console.log(response.data);
});

// post request
adapter.post("/api/films", {
  body: {
    title: "A New Hope",
  },
  baseURL: "https://swapi.dev/",
});

Request with types and validation

import { adapter } from "adapters.ts";

adapter.get("/api/films", {
  baseURL: "https://swapi.dev/",
  validate: (data): data is { title: string }[] => {
    return Array.isArray(data)
      && data.every(film => typeof film.title === "string");
  },
}).then(response => {
  console.log(response.data);
});

Endpoint defintions

Endpoint definitions can enforce specific response data types and request data types, as well as possible or required search and query parameters.

import { Adapter } from "adapters.ts";

const swapi = Adapter.new({ baseURL: "https://swapi.dev/" });

const film = swapi.endpoint({
  // path to endpoint with a required "id" query parameter
  url: "/api/film/{id}",
  // define an optional "fields" search parameter
  searchParams: ["?fields"],
  validate: {
    // shape of GET response
    get: (data): data is { id: string; title: string } => {
      return typeof data === "object" && typeof data.title === "string"
        && typeof data.id === "string";
    },
    // shape of PATCH response
    patch: (data): data is { ok: boolean } => {
      return typeof data === "object" && typeof data.ok === "boolean";
    },
  },
  validateRequest: {
    // shape of POST request
    patch: (data): data is { title: string } => {
      return typeof data === "object" && typeof data.title === "string";
    },
  },
});

// sends a GET request to https://swapi.dev/api/film/1?fields=id,title
film.get({ id: 1 }, { searchParams: { fields: "id,title" } }).then(response => {
  response.data; // { id: string, title: string }
});

// sends a PATCH request to https://swapi.dev/api/film/1
film.patch({ id: 1 }, { title: "A New Hope" }).then(response => {
  response.data; // { ok: boolean }
});

Note: The validate and validateRequest functions don't actually have to perform any runtime validation. A basic method returning true with appropraite type definition can be used insted to only provide type definitions. (e.x. (data): data is MyObject => true)

Url templating

See here how to create more advanced URL templates

Fetch

By default fetch is being used to make requests, but other methods can be used instead by providing an interface to the Adapter.new method.

Required interface can be foud here: XHRInterface