Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Cordis is cut up in the form of multiple packages/libraries:
- [`@cordis/queue`](https://github.com/cordis-lib/cordis/tree/main/libs/queue) - A simple and compact sequential queue for async operations
- [`@cordis/redis-store`](https://github.com/cordis-lib/cordis/tree/main/libs/redis-store) - A Redis implementation of `@cordis/store`
- [`@cordis/rest`](https://github.com/cordis-lib/cordis/tree/main/libs/rest) - Tooling for making HTTP requests to Discord, with rate limiting handling
- [`@cordis/routers`](https://github.com/cordis-lib/cordis/tree/main/libs/routers) - Make API requests with ease using simple dynamic JavaScript property accessing
- [`@cordis/store`](https://github.com/cordis-lib/cordis/tree/main/libs/store) - A simple map-like interface for holding key-value pairs - ships with an in-memory implementation

# Contributing
Expand Down
6 changes: 3 additions & 3 deletions libs/gateway/src/websocket/Cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
WebsocketConnectionDestroyOptions
} from './WebsocketConnection';
import { stripIndent } from 'common-tags';
import { RestManager, MemoryMutex, RedisMutex } from '@cordis/rest';
import { Rest, MemoryMutex, RedisMutex } from '@cordis/rest';
import {
APIUser,
GatewayDispatchPayload,
Expand Down Expand Up @@ -117,7 +117,7 @@ export class Cluster extends EventEmitter {
/**
* REST instance
*/
public readonly rest: RestManager;
public readonly rest: Rest;

/**
* First shard ID for this cluster
Expand Down Expand Up @@ -185,7 +185,7 @@ export class Cluster extends EventEmitter {
...shardOptions
} = options;

this.rest = new RestManager(auth, { mutex: redis ? new RedisMutex(redis) : new MemoryMutex() });
this.rest = new Rest(auth, { mutex: redis ? new RedisMutex(redis) : new MemoryMutex() });
this.shardCount = shardCount;
this.startingShard = startingShard;
this.totalShardCount = totalShardCount;
Expand Down
11 changes: 8 additions & 3 deletions libs/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ Note: Props to https://github.com/spec-tacles/spectacles.js for the Mutex logic.

## Example Usage
```ts
const { RestManager } = require('@cordis/rest');
const { Rest, buildRestRouter } = require('@cordis/rest');

const main = async () => {
const rest = new RestManager('token');
const rest = new Rest('token');
const router = buildRestRouter(rest);

// using the rest manager itself
const someUser = await rest.get('/users/223703707118731264');
const someOtherUser = await rest.make({
path: '/users/198536269586890752',
method: 'get'
});

console.log(someUser, someOtherUser);
// using the router utility
const someOtherOtherUser = await router.users["223703707118731264"].get();

console.log(someUser, someOtherUser, someOtherOtherUser);
};

main();
Expand Down
4 changes: 2 additions & 2 deletions libs/rest/src/Bucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { discordFetch, DiscordFetchOptions } from './Fetch';
import { CordisRestError, HTTPError } from './Error';
import { halt } from '@cordis/common';
import type { RestManager } from './RestManager';
import type { Rest } from './Rest';

/**
* Data held to represent ratelimit state for a Bucket
Expand Down Expand Up @@ -39,7 +39,7 @@ export class Bucket {
* @param route The identifier of this bucket
*/
public constructor(
public readonly manager: RestManager,
public readonly manager: Rest,
public readonly route: string
) {}

Expand Down
2 changes: 1 addition & 1 deletion libs/routers/src/IRouter.ts → libs/rest/src/IRouter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { File, StringRecord, RequestBodyData } from '@cordis/rest';
import type { File, StringRecord, RequestBodyData } from './';

export type IRouter = {
get<T, Q = StringRecord>(options?: { query?: Q }): Promise<T>;
Expand Down
8 changes: 4 additions & 4 deletions libs/rest/src/RestManager.ts → libs/rest/src/Rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { DiscordFetchOptions, File, RequestBodyData, StringRecord } from '.
/**
* Options for constructing a rest manager
*/
export interface RestManagerOptions {
export interface RestOptions {
/**
* How many times to retry making a request before giving up
*/
Expand All @@ -25,7 +25,7 @@ export interface RestManagerOptions {
mutex?: Mutex;
}

export interface RestManager {
export interface Rest {
/**
* Fired when a request is being started (pre-ratelimit checking)
* @event
Expand Down Expand Up @@ -105,7 +105,7 @@ export interface RequestOptions<D, Q> {
* Base REST class used for making requests
* @noInheritDoc
*/
export class RestManager extends EventEmitter {
export class Rest extends EventEmitter {
/**
* Current active rate limiting Buckets
*/
Expand All @@ -121,7 +121,7 @@ export class RestManager extends EventEmitter {
*/
public constructor(
public readonly auth: string,
options: RestManagerOptions = {}
options: RestOptions = {}
) {
super();
const {
Expand Down
4 changes: 3 additions & 1 deletion libs/rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export * from './Bucket';
export * from './Constants';
export * from './Error';
export * from './Fetch';
export * from './RestManager';
export * from './Rest';
export * from './IRouter';
export * from './restRouter';
6 changes: 3 additions & 3 deletions libs/rest/src/rest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fetch, { Response, Headers } from 'node-fetch';
import Blob from 'fetch-blob';
import { Bucket } from './Bucket';
import { CordisRestError, HTTPError } from './Error';
import { RequestOptions, RestManager } from './RestManager';
import { RequestOptions, Rest } from './Rest';

jest.mock('node-fetch', () => {
const fetch: typeof import('node-fetch') = jest.requireActual('node-fetch');
Expand Down Expand Up @@ -32,10 +32,10 @@ jest.mock('@cordis/common', () => {

const mockedFetch = fetch as any as jest.Mock<Promise<Response>>;

let rest: RestManager;
let rest: Rest;

beforeEach(() => {
rest = new RestManager('token');
rest = new Rest('token');
});

describe('buckets and rate limiting', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { buildRestRouter } from './restRouter';
import { RestManager } from '@cordis/rest';
import type { Rest } from './Rest';

const manager = RestManager as any as RestManager;
const mockedMake = manager.make as any as jest.Mock;

jest.mock('@cordis/rest', () => ({
RestManager: {
make: jest.fn()
}
}));

const router = buildRestRouter(manager);
const mockedMake = jest.fn();
const rest = { make: mockedMake } as any as Rest;
const router = buildRestRouter(rest);

afterEach(() => {
mockedMake.mockClear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RestManager } from '@cordis/rest';
import type { Rest } from './Rest';
import type { IRouter } from './IRouter';

/**
Expand All @@ -16,7 +16,7 @@ import type { IRouter } from './IRouter';
* ```
* @param manager REST manager
*/
export const buildRestRouter = (manager: RestManager) => {
export const buildRestRouter = (manager: Rest) => {
const method: string[] = [''];
const handler: ProxyHandler<IRouter> = {
get(_, property) {
Expand Down
33 changes: 0 additions & 33 deletions libs/routers/README.md

This file was deleted.

40 changes: 0 additions & 40 deletions libs/routers/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions libs/routers/src/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions libs/routers/tsconfig.json

This file was deleted.

4 changes: 2 additions & 2 deletions libs/util/src/restUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* istanbul ignore file */

import { Routes } from 'discord-api-types/v8';
import type { RestManager } from '@cordis/rest';
import type { Rest } from '@cordis/rest';
// eslint-disable-next-line no-duplicate-imports
import type {
Snowflake,
Expand Down Expand Up @@ -137,7 +137,7 @@ import type {

interface webhookIdOrToken { webhookID: Snowflake; webhookToken?: string }

export const makeRestUtils = (rest: RestManager) => ({
export const makeRestUtils = (rest: Rest) => ({
createGuild: (data: RESTPostAPIGuildsJSONBody) => rest.post<RESTPostAPIGuildsResult, RESTPostAPIGuildsJSONBody>(Routes.guilds(), { data }),
fetchGuild: (...args: Parameters<typeof Routes.guild>) => rest.get<RESTGetAPIGuildResult>(Routes.guild(...args)),
fetchGuildPreview: (...args: Parameters<typeof Routes.guildPreview>) => rest.get<APIGuildPreview>(Routes.guildPreview(...args)),
Expand Down