Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: createNodeMiddleware(). Deprecates getNodeMiddleware() #217

Merged
merged 2 commits into from
Mar 24, 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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- [`app.webhooks`](#appwebhooks)
- [`app.oauth`](#appoauth)
- [Middlewares](#middlewares)
- [`getNodeMiddleware(app, options)`](#getnodemiddlewareapp-options)
- [`createNodeMiddleware(app, options)`](#createnodemiddlewareapp-options)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -46,7 +46,7 @@ Node
Install with `npm install @octokit/app`

```js
const { App, getNodeMiddleware } = require("@octokit/app");
const { App, createNodeMiddleware } = require("@octokit/app");
```

</td></tr>
Expand Down Expand Up @@ -94,7 +94,7 @@ app.oauth.on("token", async ({ token, octokit }) => {
console.log(`Token retrieved for ${data.login}`);
});

require("http").createServer(getNodeMiddleware(app)).listen(3000);
require("http").createServer(createNodeMiddleware(app)).listen(3000);
// can now receive requests at /api/github/*
```

Expand Down Expand Up @@ -302,12 +302,12 @@ By default, all middlewares expose the following routes
| `DELETE /api/github/oauth/token` | Invalidates current token, basically the equivalent of a logout. Must authenticate using token in `Authorization` header. |
| `DELETE /api/github/oauth/grant` | Revokes the user's grant, basically the equivalent of an uninstall. must authenticate using token in `Authorization` header. |

### `getNodeMiddleware(app, options)`
### `createNodeMiddleware(app, options)`

Native http server middleware for Node.js

```js
const { App, getNodeMiddleware } = require("@octokit/app");
const { App, createNodeMiddleware } = require("@octokit/app");
const app = new App({
appId: 123,
privateKey: "-----BEGIN PRIVATE KEY-----\n...",
Expand All @@ -320,7 +320,7 @@ const app = new App({
},
});

const middleware = getNodeMiddleware(app);
const middleware = createNodeMiddleware(app);

require("http").createServer(middleware).listen(3000);
// can now receive user authorization callbacks at /api/github/*
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"@octokit/oauth-app": "^2.0.1",
"@octokit/plugin-paginate-rest": "^2.6.0",
"@octokit/types": "^6.0.3",
"@octokit/webhooks": "^8.0.2"
"@octokit/webhooks": "^8.0.2",
"deprecation": "^2.3.1"
},
"devDependencies": {
"@pika/pack": "^0.5.0",
Expand Down
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
OAuthApp,
getNodeMiddleware as oauthNodeMiddleware,
} from "@octokit/oauth-app";
import { Deprecation } from "deprecation";

import {
Options,
Expand Down Expand Up @@ -101,7 +102,20 @@ export class App<O extends Options = Options> {
}
}

/**
* @deprecated use createNodeMiddleware()
*/
export function getNodeMiddleware(app: App) {
app.log.warn(
// @ts-expect-error
new Deprecation(
"[@octokit/app] getNodeMiddleware is deprecated. Use createNodeMiddleware instead"
)
);
return createNodeMiddleware(app);
}

export function createNodeMiddleware(app: App) {
return oauthNodeMiddleware(app.oauth, {
onUnhandledRequest: (request, response) => {
return app.webhooks.middleware(request, response);
Expand Down
30 changes: 30 additions & 0 deletions test/deprecations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Deprecation } from "deprecation";

import { getNodeMiddleware, App } from "../src";

describe("deprecations", () => {
test("getNodeMiddleware() - #263", async () => {
const warn = jest.fn();
const app = new App({
appId: "123",
privateKey: "privateKey",
oauth: {
clientId: "123",
clientSecret: "123secret",
},
webhooks: {
secret: "secret",
},
// @ts-expect-error
log: { warn },
});

getNodeMiddleware(app);

expect(warn).toBeCalledWith(
new Deprecation(
"[@octokit/app] getNodeMiddleware is deprecated. Use createNodeMiddleware instead"
)
);
});
});
6 changes: 3 additions & 3 deletions test/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, getNodeMiddleware } from "../src";
import { App, createNodeMiddleware } from "../src";

describe("smoke", () => {
it("App", () => {
Expand All @@ -23,7 +23,7 @@ describe("smoke", () => {
expect(App.VERSION).toEqual("0.0.0-development");
});

it("getNodeMiddleware", () => {
expect(typeof getNodeMiddleware).toBe("function");
it("createNodeMiddleware", () => {
expect(typeof createNodeMiddleware).toBe("function");
});
});