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(typescript): types for octokit.hook.{before,after,error,wrap}("request", () => {}) methods #301

Merged
merged 4 commits into from
Mar 9, 2021
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
"@octokit/auth-token": "^2.4.4",
"@octokit/graphql": "^4.5.8",
"@octokit/request": "^5.4.12",
"@octokit/request-error": "^2.0.5",
"@octokit/types": "^6.0.3",
"before-after-hook": "^2.1.0",
"before-after-hook": "^2.2.0",
"universal-user-agent": "^6.0.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createTokenAuth } from "@octokit/auth-token";

import {
Constructor,
Hooks,
OctokitOptions,
OctokitPlugin,
RequestParameters,
Expand Down Expand Up @@ -70,11 +71,12 @@ export class Octokit {
}

constructor(options: OctokitOptions = {}) {
const hook = new Collection();
const hook = new Collection<Hooks>();
const requestDefaults: Required<RequestParameters> = {
baseUrl: request.endpoint.DEFAULTS.baseUrl,
headers: {},
request: Object.assign({}, options.request, {
// @ts-ignore internal usage only, no need to type
hook: hook.bind(null, "request"),
}),
mediaType: {
Expand Down Expand Up @@ -175,7 +177,7 @@ export class Octokit {
error: (message: string, additionalInfo?: object) => any;
[key: string]: any;
};
hook: HookCollection;
hook: HookCollection<Hooks>;

// TODO: type `octokit.auth` based on passed options.authStrategy
auth: (...args: unknown[]) => Promise<unknown>;
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as OctokitTypes from "@octokit/types";
import { RequestError } from "@octokit/request-error";

import { Octokit } from ".";

Expand Down Expand Up @@ -49,3 +50,16 @@ export type OctokitPlugin = (
octokit: Octokit,
options: OctokitOptions
) => { [key: string]: any } | void;

export type Hooks = {
request: {
Options: OctokitTypes.EndpointOptions;
Result: OctokitTypes.OctokitResponse<any>;
Error: RequestError | Error;
};
[key: string]: {
Options: unknown;
Result: unknown;
Error: unknown;
};
};
10 changes: 8 additions & 2 deletions test/constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,21 @@ describe("Smoke test", () => {
});

octokit.hook.wrap("request", (request, options) => {
// @ts-ignore
expect(options.request.foo).toEqual("bar");
return "ok";
return {
data: { ok: true },
headers: {},
status: 200,
url: "https://example.com",
};
});

return octokit
.request("/")

.then((response) => {
expect(response).toEqual("ok");
expect(response.data.ok).toEqual(true);
});
});
});
4 changes: 3 additions & 1 deletion test/hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe("octokit.hook", () => {
qux: "quux",
request: {
fetch: mock,
// @ts-ignore
hook: options.request.hook,
},
});
Expand Down Expand Up @@ -180,11 +181,12 @@ describe("octokit.hook", () => {
format: "",
},
request: {
// @ts-ignore
hook: options.request.hook,
},
});

return { data: { ok: true } };
return { data: { ok: true }, headers: {}, status: 200, url: "" };
});

const { data } = await octokit.request("/");
Expand Down