open-index/open-npm huggingface dataset:
npm package: @purplet/gateway
Description: Cross-runtime Discord Gateway client
Author: Dave Caruso
Latest version: 1.0.1

# @purplet/gateway

This is a JavaScript implementation for the Discord Gateway 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 `WebSocket` standard APIs, meaning this client can run in Node.js (with polyfills), and in Bun.js.

Basic Example:

```ts
import { Gateway } from '@purplet/gateway';
import { GatewayIntentBits } from 'discord-api-types/gateway';

const client = new Gateway({
  intents: GatewayIntentBits.GuildMessages,
  token: process.env.token
});

client.on('MESSAGE_CREATE', message => {
  console.log(message);
});
```

## Installing

`@purplet/gateway` only a peer-dependency on `discord-api-types` so you we do not have to bump this package when types change slightly. We also utilize `zlib-sync` for compression and `erlpack` for encoding (these don't work on bun yet).

```sh
pnpm install @purplet/gateway discord-api-types
# or
bun add @purplet/gateway discord-api-types
```

## Node.js support

Until v18, node.js does not support `fetch`, and no version of node has `WebSocket` built-in. We provide another package, `@purplet/polyfill` which polyfills these APIs for you:

```ts
import '@purplet/polyfill';
import { Gateway } from '@purplet/gateway';
// ...
```
npm package: @purplet/polyfill
Description: Polyfills for node.js and bun.js
Author: Dave Caruso
Latest version: 1.2.1

# @purplet/polyfill

Modifies global scope to apply polyfills for Node.js and Bun for Web Standards that they do not currently support.

- `fetch` (node <=17)
  - provided by `undici`
  - includes `Request`, `Response`, `FormData`, `Headers`, `File` globals in addition to `fetch`
- `FormData` (node <=17, bun as of v2.1)
  - provided by `formdata-node`, only applied in cases where `fetch` exists but `FormData` is not.
  - `fetch` and `Blob` are patched to support passing `FormData` as a body, with code provided by `form-data-encoder`. (This might break some other uses of `Blob`).
- `WebSocket` (node as of v19)
  - provided by `ws`
- `structuredClone` (node <=17, bun as of v2.1, some browsers)
  - provided by `@ungap/structured-clone`
- parsing of `.env` files (node)
  - provided by `dotenv`
- `Blob` (node <=17)
  - provided by `node:buffer` on Node.js
- `crypto` (node <=17)
  - provided by `node:crypto` on Node.js
npm package: @purplet/rest
Description: Cross-runtime Discord Rest client
Author: Dave Caruso
Latest version: 1.2.0

# @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](https://github.com/discordjs/discord-api-types) and documented using auto-generated JSDoc comments from the [Discord API documentation](https://discordapp.com/developers/docs/resources).

Basic Example:

```ts
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](https://discordapp.com/developers/docs/resources/), 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](https://discordapp.com/developers/docs/resources/user#get-current-user).

> 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:**

```ts
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.

```sh
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:

```ts
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:

- [discord-api-docs](https://github.com/discord/discord-api-docs)
- [discord-api-types](https://github.com/discordjs/discord-api-types)

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.
npm package: @purplet/utils
Description: Utilities for Discord API driven development.
Author: Clembs
Latest version: 1.0.0

# @purplet/utils

This package contains a variety of utilities for interfacing with the Discord API, such as functions to format `@mentions`, parse snowflake IDs, builder classes to generate certain JSON structures, API constants, and more. It's designed to be useful to anyone interacting with the Discord API.

If you are using the [Purplet Framework](https://purplet.js.org), everything from this package is re-exported from the `purplet` import. Use that instead of installing this alongside your project.

## Installing

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

```sh
pnpm install @purplet/utils discord-api-types
# or
bun add @purplet/utils discord-api-types
```

## Features

Here is a subset of the utilities that are available in this package:

### Mention formatters

The following functions generate markdown mentions.

- `userMention(id or object)`
- `channelMention(id or object)`
- `roleMention(id or object)`
- `slashCommandMention(command object)`
- `emojiMention(emoji partial object)`
- `timestampMention(date, format?)`

```ts
const user = await rest.user.getCurrentUser();
console.log(`I'm ${userMention(user)}`); // I'm <@778616916092125206>
```

### Snowflakes

`snowflakeToDate` and `dateToSnowflake` allow conversion between snowflake IDs and dates.

### Builders and JSONResolvable

`JSONResolvable<T>` is a special type that allows a plain json object to contain builder classes to be placed anywhere within another interface, as `JSON.stringify` will call the `.toJSON` method on any classes that attempt to get stringified. In the example below, this is used with `APIMessage` to allow component helpers.

```ts
import { APIMessage } from 'discord-api-types/v10';
import { JSONResolvable, toJSONValue, MessageComponentBuilder, createLinkButton } from '@purplet/utils';
import { Rest } from '@purplet/rest';

const messageData: JSONResolvable<APIMessage> = {
  content: 'Here are my socials!',
  components: new MessageComponentBuilder()
    .addInline(createLinkButton('Web', 'https://purplet.js.org'))
    .addInline(createLinkButton('Discord', 'https://discord.gg/C7fpBDJDtC')),
};

messageData.components // a MessageComponentBuilder instance

// toJSONValue flattens the object without converting to a string.
// use `JSON.stringify` if you need a string value.
const rawMessage = toJSONValue(messageData);

messageData.components // an array of JSON objects.

const rest = new Rest({ token: '<token>' });
rest.channel.createMessage({
  channelId: '<channel>',
  // Most Purplet APIs accept JSONResolvable variants directly. The
  // manual step is only shown to illustrate the concept.
  body: rawMessage
});
```