Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add types for the plugin #492

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 13 additions & 4 deletions src/error-request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// @ts-ignore
import type { RequestError } from "@octokit/request-error";
import type { State, Octokit } from "./types";
import type { EndpointDefaults } from "@octokit/types";

export async function errorRequest(state, octokit, error, options) {
export async function errorRequest(
state: State,
octokit: Octokit,
error: RequestError,
options: Required<EndpointDefaults>,
) {
if (!error.request || !error.request.request) {
// address https://github.com/octokit/plugin-retry.js/issues/8
throw error;
Expand All @@ -9,8 +16,10 @@ export async function errorRequest(state, octokit, error, options) {
// retry all >= 400 && not doNotRetry
if (error.status >= 400 && !state.doNotRetry.includes(error.status)) {
const retries =
options.request.retries != null ? options.request.retries : state.retries;
const retryAfter = Math.pow((options.request.retryCount || 0) + 1, 2);
options.request?.retries != null
? options.request.retries
: state.retries;
const retryAfter = Math.pow((options.request?.retryCount || 0) + 1, 2);
throw octokit.retry.retryRequest(error, retries, retryAfter);
}

Expand Down
27 changes: 22 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Octokit } from "@octokit/core";
import type { Octokit as OctokitCore } from "@octokit/core";
import type { RequestError } from "@octokit/request-error";

import type { OctokitOptions } from "@octokit/core/dist-types/types.d";
import { errorRequest } from "./error-request";
import { wrapRequest } from "./wrap-request";
import type { RetryOptions, State } from "./types";
import type { WrapHook } from "before-after-hook";

import { VERSION } from "./version.js";
import type { EndpointDefaults, OctokitResponse } from "@octokit/types";

export const VERSION = "0.0.0-development";
export { VERSION };

export function retry(octokit: Octokit, octokitOptions: any) {
const state = Object.assign(
export function retry(octokit: OctokitCore, octokitOptions: OctokitOptions) {
const state: State = Object.assign(
{
enabled: true,
retryAfterBaseValue: 1000,
Expand All @@ -18,7 +23,13 @@ export function retry(octokit: Octokit, octokitOptions: any) {
);

if (state.enabled) {
// @ts-expect-error
octokit.hook.error("request", errorRequest.bind(null, state, octokit));
// The types for `before-after-hook` do not let us only pass through a Promise return value
// the types expect that the function can return either a Promise of the response, or diectly return the response.
// This is due to the fact that `@octokit/request` uses aysnc functions
// Also, since we add the custom `retryCount` property to the request argument, the types are not compatible.
// @ts-expect-error
octokit.hook.wrap("request", wrapRequest.bind(null, state, octokit));
}

Expand All @@ -40,3 +51,9 @@ export function retry(octokit: Octokit, octokitOptions: any) {
};
}
retry.VERSION = VERSION;

declare module "@octokit/core/dist-types/types.d" {
interface OctokitOptions {
retry?: RetryOptions;
}
}
20 changes: 20 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Octokit as OctokitCore } from "@octokit/core";
import type { RequestError } from "@octokit/request-error";

export type RetryOptions = {
enabled?: boolean;
doNotRetry?: number[];
retries?: number;
retryAfterBaseValue?: number;
};

export type State = Required<RetryOptions>;
export type Octokit = OctokitCore & {
retry: {
retryRequest: (
error: RequestError,
retries: number,
retryAfter: number,
) => RequestError;
};
};
32 changes: 25 additions & 7 deletions src/wrap-request.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
// @ts-nocheck
// @ts-expect-error
import Bottleneck from "bottleneck/light";
import type TBottleneck from "bottleneck";
import { RequestError } from "@octokit/request-error";
import { errorRequest } from "./error-request";
import type { Octokit, State } from "./types";
import type { EndpointDefaults, OctokitResponse } from "@octokit/types";

export async function wrapRequest(state, octokit, request, options) {
const limiter = new Bottleneck();
type Request = (
request: Request,
options: Required<EndpointDefaults>,
) => Promise<OctokitResponse<any>>;
export async function wrapRequest(
state: State,
octokit: Octokit,
request: (
options: Required<EndpointDefaults>,
) => Promise<OctokitResponse<any>>,
options: Required<EndpointDefaults>,
) {
const limiter: TBottleneck = new Bottleneck();

limiter.on("failed", function (error, info) {
const maxRetries = ~~error.request.request.retries;
Expand All @@ -19,16 +33,20 @@ export async function wrapRequest(state, octokit, request, options) {
});

return limiter.schedule(
// @ts-expect-error
requestWithGraphqlErrorHandling.bind(null, state, octokit, request),
options,
);
}

async function requestWithGraphqlErrorHandling(
state,
octokit,
request,
options,
state: State,
octokit: Octokit,
request: (
request: Request,
options: Required<EndpointDefaults>,
) => Promise<OctokitResponse<any>>,
options: Required<EndpointDefaults>,
) {
const response = await request(request, options);

Expand Down
2 changes: 2 additions & 0 deletions test/retry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ describe("errorRequest", function () {
delete error.request;

try {
// @ts-expect-error
await errorRequest(state, octokit, error, errorOptions);
expect(1).not.toBe(1);
} catch (e: any) {
Expand Down Expand Up @@ -454,6 +455,7 @@ describe("errorRequest", function () {
const error = new RequestError("Internal server error", 500, errorOptions);

try {
// @ts-expect-error
await errorRequest(state, octokit, error, errorOptions);
expect(1).not.toBe(1);
} catch (e: any) {
Expand Down
Loading