From 4b97ce1d54b683f9483821f65c346cc37121acfa Mon Sep 17 00:00:00 2001 From: Alec M Date: Fri, 21 Apr 2023 00:49:02 -0700 Subject: [PATCH] :ambulance: Fixed root entrance error Organized types, optimized their application and removed old types file as if project continues to scale. Types would be better identified in a folder basis. #584 #583 #106 - Related by setting up tests for proper editing. As before users could not test properly due to project configuration. Will come back to this one once the FATAL error above is confirmed fixed after new release. --- jest.config.ts | 18 ++++----- src/index.ts | 8 ++-- src/types.ts | 55 -------------------------- src/types/bottle-neck.ts | 8 ++++ src/types/throttle-options.ts | 0 {test => tests}/events.test.ts | 0 {test => tests}/octokit.ts | 0 {test => tests}/plugin.test.ts | 0 {test => tests}/retry.test.ts | 0 {test => tests}/smoke.test.ts | 0 {test => tests}/typescript-validate.ts | 0 11 files changed, 22 insertions(+), 67 deletions(-) delete mode 100644 src/types.ts create mode 100644 src/types/bottle-neck.ts create mode 100644 src/types/throttle-options.ts rename {test => tests}/events.test.ts (100%) rename {test => tests}/octokit.ts (100%) rename {test => tests}/plugin.test.ts (100%) rename {test => tests}/retry.test.ts (100%) rename {test => tests}/smoke.test.ts (100%) rename {test => tests}/typescript-validate.ts (100%) diff --git a/jest.config.ts b/jest.config.ts index 16d33820..80e10744 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -71,9 +71,9 @@ export default { // maxWorkers: "50%", // An array of directory names to be searched recursively up from the requiring module's location - // moduleDirectories: [ - // "node_modules" - // ], + moduleDirectories: [ + "node_modules" + ], // An array of file extensions your modules use // moduleFileExtensions: [ @@ -121,11 +121,11 @@ export default { // restoreMocks: false, // The root directory that Jest should scan for tests and modules within - // rootDir: undefined, + // rootDir: "test", // A list of paths to directories that Jest should use to search for files in // roots: [ - // "" + // "src/test/**/*.ts" // ], // Allows you to use a custom runner instead of Jest's default test runner @@ -153,10 +153,10 @@ export default { // testLocationInResults: false, // The glob patterns Jest uses to detect test files - // testMatch: [ - // "**/__tests__/**/*.[jt]s?(x)", - // "**/?(*.)+(spec|test).[tj]s?(x)" - // ], + testMatch: [ + "**/test/**/*.[jt]s?(x)", + "**/?(*.)+(spec|test).[tj]s?(x)" + ], // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped // testPathIgnorePatterns: [ diff --git a/src/index.ts b/src/index.ts index 4c0476bc..399adcdc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,12 +2,13 @@ import BottleneckLight from "bottleneck/light"; import { Octokit } from "@octokit/core"; import { OctokitOptions } from "@octokit/core/dist-types/types.d"; -import { Groups } from "./types"; +import { Groups } from "./types/bottle-neck"; import { VERSION } from "./version"; import { wrapRequest } from "./wrap-request"; import triggersNotificationPaths from "./generated/triggers-notification-paths"; import { routeMatcher } from "./route-matcher"; +import Bottleneck from "bottleneck"; // Workaround to allow tests to directly access the triggersNotification function. const regex = routeMatcher(triggersNotificationPaths); @@ -15,8 +16,7 @@ const triggersNotification = regex.test.bind(regex); const groups: Groups = {}; -// @ts-expect-error -const createGroups = function (Bottleneck, common) { +const createGroups = function (Bottleneck: Bottleneck | any, common: Object) { groups.global = new Bottleneck.Group({ id: "octokit-global", maxConcurrent: 10, @@ -192,3 +192,5 @@ export function throttling(octokit: Octokit, octokitOptions: OctokitOptions) { } throttling.VERSION = VERSION; throttling.triggersNotification = triggersNotification; + +export default throttling; diff --git a/src/types.ts b/src/types.ts deleted file mode 100644 index 1f14636d..00000000 --- a/src/types.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { Octokit } from "@octokit/core"; -import Bottleneck from "bottleneck"; - -declare module "@octokit/core/dist-types/types.d" { - interface OctokitOptions { - throttle?: ThrottlingOptions; - } -} - -type LimitHandler = ( - retryAfter: number, - options: object, - octokit: Octokit, - retryCount: number -) => void; - -export type AbuseLimitHandler = { - /** - * @deprecated "[@octokit/plugin-throttling] `onAbuseLimit()` is deprecated and will be removed in a future release of `@octokit/plugin-throttling`, please use the `onSecondaryRateLimit` handler instead" - */ - onAbuseLimit: LimitHandler; - onSecondaryRateLimit?: never; -}; - -export type SecondaryLimitHandler = { - /** - * @deprecated "[@octokit/plugin-throttling] `onAbuseLimit()` is deprecated and will be removed in a future release of `@octokit/plugin-throttling`, please use the `onSecondaryRateLimit` handler instead" - */ - onAbuseLimit?: never; - onSecondaryRateLimit: LimitHandler; -}; - -export type ThrottlingOptionsBase = { - enabled?: boolean; - Bottleneck?: typeof Bottleneck; - id?: string; - timeout?: number; - connection?: Bottleneck.RedisConnection | Bottleneck.IORedisConnection; - minimumSecondaryRateRetryAfter?: number; - retryAfterBaseValue?: number; - write?: Bottleneck.Group; - search?: Bottleneck.Group; - notifications?: Bottleneck.Group; - onRateLimit: LimitHandler; -}; - -export type ThrottlingOptions = ThrottlingOptionsBase & - (AbuseLimitHandler | SecondaryLimitHandler); - -export type Groups = { - global?: Bottleneck.Group; - write?: Bottleneck.Group; - search?: Bottleneck.Group; - notifications?: Bottleneck.Group; -}; diff --git a/src/types/bottle-neck.ts b/src/types/bottle-neck.ts new file mode 100644 index 00000000..71b5723c --- /dev/null +++ b/src/types/bottle-neck.ts @@ -0,0 +1,8 @@ +import Bottleneck from "bottleneck"; + +export interface Groups { + global?: Bottleneck.Group; + write?: Bottleneck.Group; + search?: Bottleneck.Group; + notifications?: Bottleneck.Group; +} diff --git a/src/types/throttle-options.ts b/src/types/throttle-options.ts new file mode 100644 index 00000000..e69de29b diff --git a/test/events.test.ts b/tests/events.test.ts similarity index 100% rename from test/events.test.ts rename to tests/events.test.ts diff --git a/test/octokit.ts b/tests/octokit.ts similarity index 100% rename from test/octokit.ts rename to tests/octokit.ts diff --git a/test/plugin.test.ts b/tests/plugin.test.ts similarity index 100% rename from test/plugin.test.ts rename to tests/plugin.test.ts diff --git a/test/retry.test.ts b/tests/retry.test.ts similarity index 100% rename from test/retry.test.ts rename to tests/retry.test.ts diff --git a/test/smoke.test.ts b/tests/smoke.test.ts similarity index 100% rename from test/smoke.test.ts rename to tests/smoke.test.ts diff --git a/test/typescript-validate.ts b/tests/typescript-validate.ts similarity index 100% rename from test/typescript-validate.ts rename to tests/typescript-validate.ts