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

@purplet/rest

v1.2.0

Published

Cross-runtime Discord Rest client

Downloads

8

Readme

@purplet/rest

This is a JavaScript implementation for the Discord REST API. It is very light, and doesn't do more than is needed. We do not depend on any runtime-specific apis, but rather the fetch and FormData standard APIs, meaning this client can run in Node.js and in Bun (with polyfills). Every route is fully typed via discord-api-types and documented using auto-generated JSDoc comments from the Discord API documentation.

Basic Example:

import { Rest } from '@purplet/rest';

const rest = new Rest({ token: process.env.token });

const me = await rest.user.getCurrentUser();

All routes are based off of the Discord API Docs, in the notation of .resource.actionName, where resource is the name of a page on the sidebar, and actionName is the name of the header above each endpoint. This means the endpoint used above is "Get Current User" on the "User" page.

note: "Receiving and Responding" is exposed as interactionResponse, as it is more concise

For requests with url params, JSON bodies, queries, file uploads, and the X-Audit-Log-Reason header; a consistent object is passed, though each route has a custom type generated to only allow the fields are allowed/required.

Sending a Message:

const result = await rest.channel.createMessage({
  // Url params are on the base object
  channelId: '995650617282412594',
  // JSON Body is `body`
  body: {
    content: 'Hello, world!',
    attachments: [
      {
        id: '0',
      },
    ],
  },
  // Attached files are `files`, you can pass a string, Uint8Array, or even `Bun.file(...)`
  files: [
    {
      name: 'cat.png',
      data: Bun.file('./cat.png'),
    },
  ],
});

Installing

@purplet/rest has a peer-dependency on discord-api-types so we do not have to bump this package when types change slightly.

pnpm install @purplet/rest discord-api-types
# or
bun add @purplet/rest discord-api-types

Polyfills

Until v18, node.js does not support fetch and FormData, and bun has yet to implement FormData. We provide another package, @purplet/polyfill which polyfills these APIs for you:

import '@purplet/polyfill';
import { Rest } from '@purplet/rest';
// ...

Missing/Incorrect Types

We generate most of our library's route metadata off of these GitHub repositories:

We do apply some patches of our own, but missing types should be contributed to the above repositories instead of as extra overrides to us.