Skip to content

Commit

Permalink
feat: cloudflare event handler (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
baoshan authored Jul 16, 2021
1 parent d3a7b38 commit 0680ff0
Show file tree
Hide file tree
Showing 7 changed files with 19,647 additions and 46 deletions.
96 changes: 93 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ const app = new OAuthApp({
});

const middleware = createNodeMiddleware(app, {
pathPrefix: "/api/github/oauth/",
pathPrefix: "/api/github/oauth",
});

require("http").createServer(middleware).listen(3000);
Expand Down Expand Up @@ -949,12 +949,12 @@ Defaults to

```js
function onUnhandledRequest(request, response) {
response.writeHead(400, {
response.writeHead(404, {
"content-type": "application/json",
});
response.end(
JSON.stringify({
error: error.message,
error: `Unknown route: ${request.method} ${request.url}`,
})
);
}
Expand All @@ -964,6 +964,96 @@ function onUnhandledRequest(request, response) {
</tbody>
</table>

### `createCloudflareHandler(app, options)`

Event handler for Cloudflare workers.

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

const handleRequest = createCloudflareHandler(app, {
pathPrefix: "/api/github/oauth",
});

addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
// 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

```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>

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
Loading

0 comments on commit 0680ff0

Please sign in to comment.