From 937c8d36a3926454fecee35cd2774944ae4804c7 Mon Sep 17 00:00:00 2001 From: "Zaid \"Nico" Date: Tue, 9 Mar 2021 10:48:29 -0500 Subject: [PATCH 1/4] merge routers into rest --- libs/gateway/src/websocket/Cluster.ts | 6 +-- libs/rest/src/Bucket.ts | 4 +- libs/{routers => rest}/src/IRouter.ts | 2 +- libs/rest/src/{RestManager.ts => Rest.ts} | 8 ++-- libs/rest/src/index.ts | 4 +- libs/rest/src/rest.test.ts | 6 +-- libs/{routers => rest}/src/restRouter.test.ts | 4 +- libs/{routers => rest}/src/restRouter.ts | 4 +- libs/routers/README.md | 33 --------------- libs/routers/package.json | 40 ------------------- libs/routers/src/index.ts | 2 - libs/routers/tsconfig.json | 8 ---- 12 files changed, 20 insertions(+), 101 deletions(-) rename libs/{routers => rest}/src/IRouter.ts (86%) rename libs/rest/src/{RestManager.ts => Rest.ts} (97%) rename libs/{routers => rest}/src/restRouter.test.ts (83%) rename libs/{routers => rest}/src/restRouter.ts (90%) delete mode 100644 libs/routers/README.md delete mode 100644 libs/routers/package.json delete mode 100644 libs/routers/src/index.ts delete mode 100644 libs/routers/tsconfig.json diff --git a/libs/gateway/src/websocket/Cluster.ts b/libs/gateway/src/websocket/Cluster.ts index ac9795a..ddecf5c 100644 --- a/libs/gateway/src/websocket/Cluster.ts +++ b/libs/gateway/src/websocket/Cluster.ts @@ -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 { Store, IStore } from '@cordis/store'; import { RedisStore } from '@cordis/redis-store'; import { @@ -125,7 +125,7 @@ export class Cluster extends EventEmitter { /** * REST instance */ - public readonly rest: RestManager; + public readonly rest: Rest; /** * First shard ID for this cluster @@ -194,7 +194,7 @@ export class Cluster extends EventEmitter { } = options; this.guilds = redis ? new RedisStore({ redis, hash: 'guilds', encode: JSON.stringify, decode: JSON.parse }) : new Store(); - 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; diff --git a/libs/rest/src/Bucket.ts b/libs/rest/src/Bucket.ts index 1ef535c..70a5830 100644 --- a/libs/rest/src/Bucket.ts +++ b/libs/rest/src/Bucket.ts @@ -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 @@ -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 ) {} diff --git a/libs/routers/src/IRouter.ts b/libs/rest/src/IRouter.ts similarity index 86% rename from libs/routers/src/IRouter.ts rename to libs/rest/src/IRouter.ts index b41b072..42f8a65 100644 --- a/libs/routers/src/IRouter.ts +++ b/libs/rest/src/IRouter.ts @@ -1,4 +1,4 @@ -import type { File, StringRecord, RequestBodyData } from '@cordis/rest'; +import type { File, StringRecord, RequestBodyData } from './'; export type IRouter = { get(options?: { query?: Q }): Promise; diff --git a/libs/rest/src/RestManager.ts b/libs/rest/src/Rest.ts similarity index 97% rename from libs/rest/src/RestManager.ts rename to libs/rest/src/Rest.ts index 55ecc9f..3c0662a 100644 --- a/libs/rest/src/RestManager.ts +++ b/libs/rest/src/Rest.ts @@ -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 */ @@ -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 @@ -105,7 +105,7 @@ export interface RequestOptions { * Base REST class used for making requests * @noInheritDoc */ -export class RestManager extends EventEmitter { +export class Rest extends EventEmitter { /** * Current active rate limiting Buckets */ @@ -121,7 +121,7 @@ export class RestManager extends EventEmitter { */ public constructor( public readonly auth: string, - options: RestManagerOptions = {} + options: RestOptions = {} ) { super(); const { diff --git a/libs/rest/src/index.ts b/libs/rest/src/index.ts index 1101360..576faf7 100644 --- a/libs/rest/src/index.ts +++ b/libs/rest/src/index.ts @@ -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'; diff --git a/libs/rest/src/rest.test.ts b/libs/rest/src/rest.test.ts index 4ffce74..dd73ab9 100644 --- a/libs/rest/src/rest.test.ts +++ b/libs/rest/src/rest.test.ts @@ -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'); @@ -32,10 +32,10 @@ jest.mock('@cordis/common', () => { const mockedFetch = fetch as any as jest.Mock>; -let rest: RestManager; +let rest: Rest; beforeEach(() => { - rest = new RestManager('token'); + rest = new Rest('token'); }); describe('buckets and rate limiting', () => { diff --git a/libs/routers/src/restRouter.test.ts b/libs/rest/src/restRouter.test.ts similarity index 83% rename from libs/routers/src/restRouter.test.ts rename to libs/rest/src/restRouter.test.ts index 2ce6285..acd4ea2 100644 --- a/libs/routers/src/restRouter.test.ts +++ b/libs/rest/src/restRouter.test.ts @@ -1,7 +1,7 @@ import { buildRestRouter } from './restRouter'; -import { RestManager } from '@cordis/rest'; +import { Rest } from './Rest'; -const manager = RestManager as any as RestManager; +const manager = Rest as any as Rest; const mockedMake = manager.make as any as jest.Mock; jest.mock('@cordis/rest', () => ({ diff --git a/libs/routers/src/restRouter.ts b/libs/rest/src/restRouter.ts similarity index 90% rename from libs/routers/src/restRouter.ts rename to libs/rest/src/restRouter.ts index 9f1dc9c..8697ba2 100644 --- a/libs/routers/src/restRouter.ts +++ b/libs/rest/src/restRouter.ts @@ -1,4 +1,4 @@ -import type { RestManager } from '@cordis/rest'; +import type { Rest } from './Rest'; import type { IRouter } from './IRouter'; /** @@ -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 = { get(_, property) { diff --git a/libs/routers/README.md b/libs/routers/README.md deleted file mode 100644 index 020d1bd..0000000 --- a/libs/routers/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# `@cordis/routers` - -[![GitHub](https://img.shields.io/badge/License-Apache%202.0-yellow.svg)](https://github.com/cordis-lib/cordis/blob/main/LICENSE) -[![npm](https://img.shields.io/npm/v/@cordis/routers?color=crimson&logo=npm)](https://www.npmjs.com/package/@cordis/routers) -[![TypeScript](https://github.com/cordis-lib/cordis/actions/workflows/quality.yml/badge.svg)](https://github.com/cordis-lib/cordis/actions/workflows/quality.yml) - -Construct API paths using simple JavaScript property accessing and execute requests with method calls. - -## Installation -- `npm install @cordis/routers` -- `pnpm install @cordis/routers` -- `yarn add @cordis/routers` - -## Example Usage -```ts -const { buildRestRouter } = require('@cordis/router'); -const { RestManager } = require('@cordis/rest'); - -const manager = new RestManager(yourToken); -const router = buildRestRouter(manager); - -const user = await router.users[someUserId].get(); -console.log(user); -``` - -## Documentation -You can find documentation for the whole project over at https://cordis.didinele.me - -## Contributing -Please see the main [README.md](https://github.com/cordis-lib/cordis) for info on how to contribute to this package or the other `@cordis` packages. - -## LICENSE -Licensed under the [Apache 2.0](https://github.com/cordis-lib/cordis/blob/main/LICENSE) license. \ No newline at end of file diff --git a/libs/routers/package.json b/libs/routers/package.json deleted file mode 100644 index a03219e..0000000 --- a/libs/routers/package.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "name": "@cordis/routers", - "description": "Construct API paths using simple JavaScript property accessing and execute requests with method calls", - "main": "./dist/index.js", - "types": "./types/index.d.ts", - "version": "0.1.7", - "scripts": { - "lint": "eslint src --ext .ts", - "build": "tsc" - }, - "files": [ - "dist", - "types", - "!dist/*.tsbuildinfo", - "!**/**.map", - "!**/*.test.*" - ], - "repository": { - "type": "git", - "url": "git+https://github.com/cordis-lib/cordis.git" - }, - "contributors": [ - "didinele (https://github.com/didinele)", - "Holo-Buckshot (https://github.com/Holo-Buckshot)", - "Nico (https://github.com/zaida04)" - ], - "author": "didinele", - "bugs": { - "url": "https://github.com/cordis-lib/cordis/issues" - }, - "homepage": "https://github.com/cordis-lib/cordis#readme", - "devDependencies": { - "@types/node": "^14.14.31", - "typescript": "^4.2.2" - }, - "dependencies": { - "@cordis/rest": "workspace:^0.1.7", - "tslib": "^2.1.0" - } -} diff --git a/libs/routers/src/index.ts b/libs/routers/src/index.ts deleted file mode 100644 index 3280d5c..0000000 --- a/libs/routers/src/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './IRouter'; -export * from './restRouter'; diff --git a/libs/routers/tsconfig.json b/libs/routers/tsconfig.json deleted file mode 100644 index 5a417fa..0000000 --- a/libs/routers/tsconfig.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "../../tsconfig.json", - "compilerOptions": { - "outDir": "./dist", - "declarationDir": "./types" - }, - "include": ["./src/**/*.ts"] -} From 2e15ceb42a7d95a17984f8dc75acff3466ec94c9 Mon Sep 17 00:00:00 2001 From: "Zaid \"Nico" Date: Tue, 9 Mar 2021 12:55:40 -0500 Subject: [PATCH 2/4] update the example usage --- libs/rest/README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libs/rest/README.md b/libs/rest/README.md index 5dad945..b759485 100644 --- a/libs/rest/README.md +++ b/libs/rest/README.md @@ -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(); From 8b6ec01122228c80a772acfc28849382b52b1dbb Mon Sep 17 00:00:00 2001 From: "Zaid \"Nico" Date: Tue, 9 Mar 2021 13:09:39 -0500 Subject: [PATCH 3/4] tests: fix restrouter test --- libs/rest/src/restRouter.test.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/libs/rest/src/restRouter.test.ts b/libs/rest/src/restRouter.test.ts index acd4ea2..dc0f767 100644 --- a/libs/rest/src/restRouter.test.ts +++ b/libs/rest/src/restRouter.test.ts @@ -1,16 +1,9 @@ import { buildRestRouter } from './restRouter'; -import { Rest } from './Rest'; +import type { Rest } from './Rest'; -const manager = Rest as any as Rest; -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(); From 12b471fe04f8f33b1aa9cd93d7befb64596efb60 Mon Sep 17 00:00:00 2001 From: Zaid Date: Wed, 10 Mar 2021 10:51:10 -0500 Subject: [PATCH 4/4] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 86c9934..8fd118e 100644 --- a/README.md +++ b/README.md @@ -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/snowflake`](https://github.com/cordis-lib/cordis/tree/main/libs/snowflake) - A simple snowflake structure for destructuring Discord IDs into relevant information - [`@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