Skip to content

Commit

Permalink
refactor(middleware)!: remove aws-lambda handler
Browse files Browse the repository at this point in the history
  • Loading branch information
baoshan committed Aug 31, 2022
1 parent 0e0853e commit 1942460
Show file tree
Hide file tree
Showing 20 changed files with 719 additions and 1,932 deletions.
141 changes: 0 additions & 141 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -935,32 +935,6 @@ require("http").createServer(middleware).listen(3000);

All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/github/oauth"`

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

Defaults to

```js
function onUnhandledRequest(request, response) {
response.writeHead(404, {
"content-type": "application/json",
});
response.end(
JSON.stringify({
error: `Unknown route: ${request.method} ${request.url}`,
})
);
}
```

</td></tr>
</tbody>
</table>
Expand Down Expand Up @@ -1025,121 +999,6 @@ addEventListener("fetch", (event) => {

All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/github/oauth"`

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

```js
function onUnhandledRequest(request) {
return new Response(
JSON.stringify({
error: `Unknown route: ${request.method} ${request.url}`,
}),
{
status: 404,
headers: { "content-type": "application/json" },
}
);
}
```

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

### `createAWSLambdaAPIGatewayV2Handler(app, options)`

Event handler for AWS Lambda using API Gateway V2 HTTP integration.

```js
// worker.js
import {
OAuthApp,
createAWSLambdaAPIGatewayV2Handler,
} from "@octokit/oauth-app";
const app = new OAuthApp({
clientType: "oauth-app",
clientId: "1234567890abcdef1234",
clientSecret: "1234567890abcdef1234567890abcdef12345678",
});

export const handler = createAWSLambdaAPIGatewayV2Handler(app, {
pathPrefix: "/api/github/oauth",
});

// can now receive user authorization callbacks at /api/github/oauth/callback
```

<table width="100%">
<thead align=left>
<tr>
<th width=150>
name
</th>
<th width=70>
type
</th>
<th>
description
</th>
</tr>
</thead>
<tbody align=left valign=top>
<tr>
<th>
<code>app</code>
</th>
<th>
<code>OAuthApp instance</code>
</th>
<td>
<strong>Required</strong>.
</td>
</tr>
<tr>
<th>
<code>options.pathPrefix</code>
</th>
<th>
<code>string</code>
</th>
<td>

All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/github/oauth"`

</td>
</tr>
<tr>
<th>
<code>options.onUnhandledRequest</code>
</th>
<th>
<code>function</code>
</th>
<td>Defaults to returns:

```js
function onUnhandledRequest(request) {
return
{
status: 404,
headers: { "content-type": "application/json" },
body: JSON.stringify({
error: `Unknown route: [METHOD] [URL]`,
})
}
);
}
```

</td>
</tr>
</tbody>
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
"@octokit/core": "^4.0.0",
"@octokit/oauth-authorization-url": "^5.0.0",
"@octokit/oauth-methods": "^2.0.0",
"@types/aws-lambda": "^8.10.83",
"fromentries": "^1.3.1",
"universal-user-agent": "^6.0.0"
},
"devDependencies": {
Expand Down
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ import type {
Options,
State,
} from "./types";

// types required by external handlers (aws-lambda, etc)
export type {
HandlerOptions,
OctokitRequest,
OctokitResponse,
} from "./middleware/types";

// generic handlers
export { handleRequest } from "./middleware/handle-request";

export { createNodeMiddleware } from "./middleware/node/index";
export {
createCloudflareHandler,
createWebWorkerHandler,
} from "./middleware/web-worker/index";
export { createAWSLambdaAPIGatewayV2Handler } from "./middleware/aws-lambda/api-gateway-v2";
export { createWebWorkerHandler } from "./middleware/web-worker/index";

type Constructor<T> = new (...args: any[]) => T;

Expand Down
1 change: 0 additions & 1 deletion src/middleware/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ middleware
├── types.ts
├── node/
├── web-worker/ (Cloudflare Workers & Deno)
└── deno/ (to be implemented)
```

## Generic HTTP Handler
Expand Down
13 changes: 0 additions & 13 deletions src/middleware/aws-lambda/api-gateway-v2-parse-request.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/middleware/aws-lambda/api-gateway-v2-send-response.ts

This file was deleted.

37 changes: 0 additions & 37 deletions src/middleware/aws-lambda/api-gateway-v2.ts

This file was deleted.

18 changes: 11 additions & 7 deletions src/middleware/handle-request.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { OAuthApp } from "../index";
import { HandlerOptions, OctokitRequest, OctokitResponse } from "./types";
import { ClientType, Options } from "../types";
// @ts-ignore - requires esModuleInterop flag
import fromEntries from "fromentries";

export async function handleRequest(
app: OAuthApp<Options<ClientType>>,
{ pathPrefix = "/api/github/oauth" }: HandlerOptions,
request: OctokitRequest
): Promise<OctokitResponse | null> {
): Promise<OctokitResponse> {
if (request.method === "OPTIONS") {
return {
status: 200,
Expand Down Expand Up @@ -39,12 +37,18 @@ export async function handleRequest(

// handle unknown routes
if (!Object.values(routes).includes(route)) {
return null;
return {
status: 404,
headers: { "content-type": "application/json" },
text: JSON.stringify({
error: `Unknown route: ${request.method} ${request.url}`,
}),
};
}

let json: any;
try {
const text = await request.text();
const text = request.text;
json = text ? JSON.parse(text) : {};
} catch (error) {
return {
Expand All @@ -59,7 +63,7 @@ export async function handleRequest(
};
}
const { searchParams } = new URL(request.url as string, "http://localhost");
const query = fromEntries(searchParams) as {
const query = Object.fromEntries(searchParams) as {
state?: string;
scopes?: string;
code?: string;
Expand All @@ -69,7 +73,7 @@ export async function handleRequest(
error_description?: string;
error_url?: string;
};
const headers = request.headers as { authorization?: string };
const headers = (request.headers || {}) as { authorization?: string };

try {
if (route === routes.getLogin) {
Expand Down
38 changes: 5 additions & 33 deletions src/middleware/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,23 @@ type ServerResponse = any;

import { parseRequest } from "./parse-request";
import { sendResponse } from "./send-response";
import { onUnhandledRequestDefault } from "../on-unhandled-request-default";
import { handleRequest } from "../handle-request";
import { OAuthApp } from "../../index";
import { HandlerOptions } from "../types";
import { ClientType, Options } from "../../types";

function onUnhandledRequestDefaultNode(
request: IncomingMessage,
response: ServerResponse
) {
const octokitRequest = parseRequest(request);
const octokitResponse = onUnhandledRequestDefault(octokitRequest);
sendResponse(octokitResponse, response);
}

export function createNodeMiddleware(
app: OAuthApp<Options<ClientType>>,
{
pathPrefix,
onUnhandledRequest = onUnhandledRequestDefaultNode,
}: HandlerOptions & {
onUnhandledRequest?: (
request: IncomingMessage,
response: ServerResponse
) => void;
} = {}
options: HandlerOptions = {}
) {
return async function (
request: IncomingMessage,
response: ServerResponse,
next?: Function
) {
const octokitRequest = parseRequest(request);
const octokitResponse = await handleRequest(
app,
{ pathPrefix },
octokitRequest
);

if (octokitResponse) {
sendResponse(octokitResponse, response);
} else if (typeof next === "function") {
next();
} else {
onUnhandledRequest(request, response);
}
const octokitRequest = await parseRequest(request);
const octokitResponse = await handleRequest(app, options, octokitRequest);
if (octokitResponse.status === 404 && next) return next();
sendResponse(octokitResponse, response);
};
}
Loading

0 comments on commit 1942460

Please sign in to comment.