Skip to content

Commit

Permalink
refactor(middleware)!: remove onUnhandledRequest middleware option
Browse files Browse the repository at this point in the history
BREAKING CHANGE: onUnhandledRequest middleware option is removed
  • Loading branch information
baoshan committed Sep 12, 2022
1 parent e138652 commit 311f618
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 111 deletions.
36 changes: 9 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,18 @@ const app = new App({
});

const middleware = createNodeMiddleware(app);

require("http").createServer(middleware).listen(3000);
createServer(async (req, res) => {
// `middleware` returns `false` when `req` is unhandled (beyond `/api/github`)
if (await middleware(req, res)) return;
res.writeHead(404);
res.end();
}).listen(3000);
// can now receive user authorization callbacks at /api/github/*
```

The middleware returned from `createNodeMiddleware` can also serve as an
`Express.js` middleware directly.

<table width="100%">
<thead align=left>
<tr>
Expand Down Expand Up @@ -391,31 +398,6 @@ Used for internal logging. Defaults to [`console`](https://developer.mozilla.org

</td>
</tr>
<tr>
<th>
<code>options.onUnhandledRequest</code>
</th>
<th>
<code>function</code>
</th>
<td>

Defaults to

```js
function onUnhandledRequest(request, response) {
response.writeHead(400, {
"content-type": "application/json",
});
response.end(
JSON.stringify({
error: error.message,
})
);
}
```

</td></tr>
</tbody>
</table>

Expand Down
77 changes: 57 additions & 20 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"@octokit/auth-app": "^4.0.0",
"@octokit/auth-unauthenticated": "^3.0.0",
"@octokit/core": "^4.0.0",
"@octokit/oauth-app": "^4.0.7",
"@octokit/oauth-app": "5.0.0-beta.1",
"@octokit/plugin-paginate-rest": "^4.0.0",
"@octokit/types": "^7.0.0",
"@octokit/webhooks": "^10.0.0"
"@octokit/webhooks": "11.0.0-beta.1"
},
"devDependencies": {
"@pika/pack": "^0.3.7",
Expand Down
54 changes: 26 additions & 28 deletions src/middleware/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
type IncomingMessage = any;
type ServerResponse = any;

import { createNodeMiddleware as oauthNodeMiddleware } from "@octokit/oauth-app";
import {
createNodeMiddleware as oauthNodeMiddleware,
sendNodeResponse,
unknownRouteResponse,
} from "@octokit/oauth-app";
import { createNodeMiddleware as webhooksNodeMiddleware } from "@octokit/webhooks";

import { App } from "../../index";
import { onUnhandledRequestDefault } from "./on-unhandled-request-default";
import { Options } from "../../types";

export type MiddlewareOptions = {
pathPrefix?: string;
log?: Options["log"];
onUnhandledRequest?: (
request: IncomingMessage,
response: ServerResponse
) => void;
};

function noop() {}
Expand All @@ -37,7 +36,6 @@ export function createNodeMiddleware(
);

const optionsWithDefaults = {
onUnhandledRequest: onUnhandledRequestDefault,
pathPrefix: "/api/github",
...options,
log,
Expand All @@ -46,41 +44,41 @@ export function createNodeMiddleware(
const webhooksMiddleware = webhooksNodeMiddleware(app.webhooks, {
path: optionsWithDefaults.pathPrefix + "/webhooks",
log,
onUnhandledRequest: optionsWithDefaults.onUnhandledRequest,
});

const oauthMiddleware = oauthNodeMiddleware(app.oauth, {
pathPrefix: optionsWithDefaults.pathPrefix + "/oauth",
onUnhandledRequest: optionsWithDefaults.onUnhandledRequest,
});

return middleware.bind(null, optionsWithDefaults, {
return middleware.bind(
null,
optionsWithDefaults.pathPrefix,
webhooksMiddleware,
oauthMiddleware,
});
oauthMiddleware
);
}

export async function middleware(
options: Required<MiddlewareOptions>,
{ webhooksMiddleware, oauthMiddleware }: any,
pathPrefix: string,
webhooksMiddleware: any,
oauthMiddleware: any,
request: IncomingMessage,
response: ServerResponse,
next?: Function
) {
): Promise<boolean> {
const { pathname } = new URL(request.url as string, "http://localhost");

if (pathname === `${options.pathPrefix}/webhooks`) {
return webhooksMiddleware(request, response, next);
}
if (pathname.startsWith(`${options.pathPrefix}/oauth/`)) {
return oauthMiddleware(request, response, next);
if (pathname.startsWith(`${pathPrefix}/`)) {
if (pathname === `${pathPrefix}/webhooks`) {
webhooksMiddleware(request, response);
} else if (pathname.startsWith(`${pathPrefix}/oauth/`)) {
oauthMiddleware(request, response);
} else {
sendNodeResponse(unknownRouteResponse(request), response);
}
return true;
} else {
next?.();
return false;
}

const isExpressMiddleware = typeof next === "function";
if (isExpressMiddleware) {
// @ts-ignore `next` must be a function as we check two lines above
return next();
}

return options.onUnhandledRequest(request, response);
}
19 changes: 0 additions & 19 deletions src/middleware/node/on-unhandled-request-default.ts

This file was deleted.

Loading

0 comments on commit 311f618

Please sign in to comment.