Skip to content

Commit 7a887e7

Browse files
authored
feat: add hook for exported app in octokit package (#37)
Signed-off-by: re-Tick <jain.ritik.1001@gmail.com> Signed-off-by: re-Tick <jain.ritik.1001@gmail.com>
1 parent 99e9638 commit 7a887e7

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,42 @@ const { Octokit, App } = require("octokit");
140140
describe('routes', function () {
141141
var server, octokit;
142142
beforeEach(function () {
143-
NewContext({Mode: "record", Name: "your demo app name"}) // Set your keploy mode and name here.
144-
octokit = new Octokit({ auth: "your authentication token"});
143+
NewContext({Mode: "<record_OR_test_OR_off>", Name: "your demo app name"}) // Set your keploy mode and name here.
144+
octokit = new Octokit({ auth: "<your_authentication_token>"});
145145

146146
});
147147
// Test to make sure URLs respond correctly.
148148
it("url/", async function () {
149149
return new Promise(function(resolve){
150-
octokit.rest.users.getAuthenticated({}).then((result) => {
151-
assert.equal(result.data.login, "your github username")
152-
resolve()
150+
const app = new App({
151+
appId: "<APP_ID>",
152+
privateKey: `<PEM_FILE>`,
153+
})
154+
const { data: slug } = app.octokit.rest.apps.getAuthenticated().then((result) => {
155+
156+
app.getInstallationOctokit(<InstallationId>).then((octokit) => {
157+
octokit.rest.issues.create({
158+
owner: "LOREM",
159+
repo: "IPSUM",
160+
title: "Hello " + "World",
161+
}).then((res) => {
162+
assert.equal(true, true)
163+
resolve()
164+
});
165+
});
153166
});
154167
})
155168
});
156169
});
157170
```
171+
Above example test is written in commonjs module.
172+
173+
**Note**: Since, this package uses require-in-the-middle for adding hook. Therefore, it is supported for commonjs module currently. Also, you can use require statements in esmodule by:
174+
```js
175+
// Define "require"
176+
import { createRequire } from "module";
177+
const require = createRequire(import.meta.url);
178+
```
158179
159180
### Integration with Mocha testing framework
160181
You just need to do some imports and call a built-in assert function in your code in your unit test file and that's it!!🔥🔥🔥

integrations/octokit/register.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import Hook from "import-in-the-middle";
66
import mixin from "merge-descriptors";
77
import { wrappedNodeFetch } from "./require";
8+
import fetch from "node-fetch";
89

910
// @ts-ignore
1011
Hook(["octokit"], (exported, name: any, baseDir: any) => {
1112
const octokitDefault = exported.default;
1213
exported.Octokit = class WrappedOctokit {
1314
constructor(props: { request: { fetch: any; } | undefined; }) {
14-
const wrappedFetch = wrappedNodeFetch();
15+
const wrappedFetch = wrappedNodeFetch(fetch);
1516

1617
if (props.request != undefined) {
1718
props.request.fetch = wrappedFetch;

integrations/octokit/require.ts

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,58 @@ import { MockIds } from "../../mock/mock";
1717
Hook(["octokit"], function (exported) {
1818
const octokitDefault = exported;
1919

20-
class WrappedOctokit {
20+
octokitDefault.Octokit = octokitDefault.Octokit.defaults({
21+
request: {
22+
fetch: wrappedNodeFetch(fetch),
23+
},
24+
});
25+
26+
class WrappedOctokit extends octokitDefault.Octokit {
2127
constructor(props: any) {
22-
const wrappedFetch = wrappedNodeFetch();
28+
if (
29+
props.request !== undefined &&
30+
props.request.fetch !== undefined &&
31+
typeof props.request.fetch === typeof wrappedNodeFetch
32+
) {
33+
super(props);
34+
return;
35+
}
36+
const request = {
37+
fetch: wrappedNodeFetch(
38+
props.request === undefined || props.request.fetch === undefined
39+
? fetch
40+
: props.request.fetch
41+
),
42+
};
43+
if (props.request !== undefined) {
44+
mixin(request, props.request, false);
45+
}
46+
props.request = request;
47+
super(props);
48+
}
49+
}
2350

24-
if (props.request != undefined) {
25-
props.request.fetch = wrappedFetch;
51+
class WrappedApp extends octokitDefault.App {
52+
constructor(props: any) {
53+
if (props.Octokit !== undefined) {
54+
props.Octokit = props.Octokit.defaults({
55+
request: {
56+
fetch: wrappedNodeFetch(fetch),
57+
},
58+
});
2659
} else {
27-
props.request = {
28-
fetch: wrappedFetch,
29-
};
60+
props.Octokit = octokitDefault.Octokit.defaults({
61+
request: {
62+
fetch: wrappedNodeFetch(fetch),
63+
},
64+
});
3065
}
31-
const octo = new octokitDefault.Octokit(props);
32-
mixin(this, octo, false);
66+
super(props);
3367
}
3468
}
3569
const wrappedExports = {
3670
Octokit: WrappedOctokit,
71+
App: WrappedApp,
3772
};
3873

3974
exported = mixin(wrappedExports, octokitDefault, false);
@@ -50,8 +85,9 @@ function getHeadersInit(headers: { [k: string]: string[] }): {
5085
return result;
5186
}
5287

53-
export function wrappedNodeFetch() {
54-
const fetchFunc = fetch;
88+
// eslint-disable-next-line @typescript-eslint/ban-types
89+
export function wrappedNodeFetch(fetchFunc: Function) {
90+
// const fetchFunc = fetch;
5591
async function wrappedFetch(
5692
this: { fetch: (url: any, options: any) => any },
5793
url: any,

0 commit comments

Comments
 (0)