@octokit/oauth-app
Advanced tools
Comparing version 4.0.8 to 4.1.0
@@ -15,3 +15,3 @@ 'use strict'; | ||
const VERSION = "4.0.8"; | ||
const VERSION = "4.1.0"; | ||
@@ -352,42 +352,2 @@ function addEventHandler(state, eventName, eventHandler) { | ||
function parseRequest(request) { | ||
const { | ||
method, | ||
url, | ||
headers | ||
} = request; | ||
async function text() { | ||
const text = await new Promise((resolve, reject) => { | ||
let bodyChunks = []; | ||
request.on("error", reject).on("data", chunk => bodyChunks.push(chunk)).on("end", () => resolve(Buffer.concat(bodyChunks).toString())); | ||
}); | ||
return text; | ||
} | ||
return { | ||
method, | ||
url, | ||
headers, | ||
text | ||
}; | ||
} | ||
function sendResponse(octokitResponse, response) { | ||
response.writeHead(octokitResponse.status, octokitResponse.headers); | ||
response.end(octokitResponse.text); | ||
} | ||
function onUnhandledRequestDefault(request) { | ||
return { | ||
status: 404, | ||
headers: { | ||
"content-type": "application/json" | ||
}, | ||
text: JSON.stringify({ | ||
error: `Unknown route: ${request.method} ${request.url}` | ||
}) | ||
}; | ||
} | ||
// @ts-ignore - requires esModuleInterop flag | ||
@@ -495,3 +455,3 @@ async function handleRequest(app, { | ||
}, | ||
text: `<h1>Token created successfull</h1> | ||
text: `<h1>Token created successfully</h1> | ||
@@ -683,2 +643,42 @@ <p>Your token is: <strong>${token}</strong>. Copy it now as it cannot be shown again.</p>` | ||
function parseRequest(request) { | ||
const { | ||
method, | ||
url, | ||
headers | ||
} = request; | ||
async function text() { | ||
const text = await new Promise((resolve, reject) => { | ||
let bodyChunks = []; | ||
request.on("error", reject).on("data", chunk => bodyChunks.push(chunk)).on("end", () => resolve(Buffer.concat(bodyChunks).toString())); | ||
}); | ||
return text; | ||
} | ||
return { | ||
method, | ||
url, | ||
headers, | ||
text | ||
}; | ||
} | ||
function sendResponse(octokitResponse, response) { | ||
response.writeHead(octokitResponse.status, octokitResponse.headers); | ||
response.end(octokitResponse.text); | ||
} | ||
function onUnhandledRequestDefault(request) { | ||
return { | ||
status: 404, | ||
headers: { | ||
"content-type": "application/json" | ||
}, | ||
text: JSON.stringify({ | ||
error: `Unknown route: ${request.method} ${request.url}` | ||
}) | ||
}; | ||
} | ||
function onUnhandledRequestDefaultNode(request, response) { | ||
@@ -692,4 +692,9 @@ const octokitRequest = parseRequest(request); | ||
pathPrefix, | ||
onUnhandledRequest = onUnhandledRequestDefaultNode | ||
onUnhandledRequest | ||
} = {}) { | ||
if (onUnhandledRequest) { | ||
app.octokit.log.warn("[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."); | ||
} | ||
onUnhandledRequest ?? (onUnhandledRequest = onUnhandledRequestDefaultNode); | ||
return async function (request, response, next) { | ||
@@ -737,4 +742,9 @@ const octokitRequest = parseRequest(request); | ||
pathPrefix, | ||
onUnhandledRequest = onUnhandledRequestDefaultWebWorker | ||
onUnhandledRequest | ||
} = {}) { | ||
if (onUnhandledRequest) { | ||
app.octokit.log.warn("[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."); | ||
} | ||
onUnhandledRequest ?? (onUnhandledRequest = onUnhandledRequestDefaultWebWorker); | ||
return async function (request) { | ||
@@ -793,4 +803,9 @@ const octokitRequest = parseRequest$1(request); | ||
pathPrefix, | ||
onUnhandledRequest = onUnhandledRequestDefaultAWSAPIGatewayV2 | ||
onUnhandledRequest | ||
} = {}) { | ||
if (onUnhandledRequest) { | ||
app.octokit.log.warn("[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."); | ||
} | ||
onUnhandledRequest ?? (onUnhandledRequest = onUnhandledRequestDefaultAWSAPIGatewayV2); | ||
return async function (event) { | ||
@@ -864,2 +879,3 @@ const request = parseRequest$2(event); | ||
exports.createWebWorkerHandler = createWebWorkerHandler; | ||
exports.handleRequest = handleRequest; | ||
//# sourceMappingURL=index.js.map |
@@ -14,2 +14,4 @@ import { createOAuthAppAuth } from "@octokit/auth-oauth-app"; | ||
import { deleteAuthorizationWithState, } from "./methods/delete-authorization"; | ||
// generic handlers | ||
export { handleRequest } from "./middleware/handle-request"; | ||
export { createNodeMiddleware } from "./middleware/node/index"; | ||
@@ -16,0 +18,0 @@ export { createCloudflareHandler, createWebWorkerHandler, } from "./middleware/web-worker/index"; |
@@ -10,3 +10,7 @@ import { parseRequest } from "./api-gateway-v2-parse-request"; | ||
} | ||
export function createAWSLambdaAPIGatewayV2Handler(app, { pathPrefix, onUnhandledRequest = onUnhandledRequestDefaultAWSAPIGatewayV2, } = {}) { | ||
export function createAWSLambdaAPIGatewayV2Handler(app, { pathPrefix, onUnhandledRequest, } = {}) { | ||
if (onUnhandledRequest) { | ||
app.octokit.log.warn("[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."); | ||
} | ||
onUnhandledRequest ?? (onUnhandledRequest = onUnhandledRequestDefaultAWSAPIGatewayV2); | ||
return async function (event) { | ||
@@ -13,0 +17,0 @@ const request = parseRequest(event); |
@@ -78,3 +78,3 @@ // @ts-ignore - requires esModuleInterop flag | ||
}, | ||
text: `<h1>Token created successfull</h1> | ||
text: `<h1>Token created successfully</h1> | ||
@@ -81,0 +81,0 @@ <p>Your token is: <strong>${token}</strong>. Copy it now as it cannot be shown again.</p>`, |
@@ -10,3 +10,7 @@ import { parseRequest } from "./parse-request"; | ||
} | ||
export function createNodeMiddleware(app, { pathPrefix, onUnhandledRequest = onUnhandledRequestDefaultNode, } = {}) { | ||
export function createNodeMiddleware(app, { pathPrefix, onUnhandledRequest, } = {}) { | ||
if (onUnhandledRequest) { | ||
app.octokit.log.warn("[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."); | ||
} | ||
onUnhandledRequest ?? (onUnhandledRequest = onUnhandledRequestDefaultNode); | ||
return async function (request, response, next) { | ||
@@ -13,0 +17,0 @@ const octokitRequest = parseRequest(request); |
@@ -10,3 +10,7 @@ import { parseRequest } from "./parse-request"; | ||
} | ||
export function createWebWorkerHandler(app, { pathPrefix, onUnhandledRequest = onUnhandledRequestDefaultWebWorker, } = {}) { | ||
export function createWebWorkerHandler(app, { pathPrefix, onUnhandledRequest, } = {}) { | ||
if (onUnhandledRequest) { | ||
app.octokit.log.warn("[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."); | ||
} | ||
onUnhandledRequest ?? (onUnhandledRequest = onUnhandledRequestDefaultWebWorker); | ||
return async function (request) { | ||
@@ -13,0 +17,0 @@ const octokitRequest = parseRequest(request); |
@@ -1,1 +0,1 @@ | ||
export const VERSION = "4.0.8"; | ||
export const VERSION = "4.1.0"; |
@@ -11,2 +11,4 @@ import { GetUserOctokitWithStateInterface } from "./methods/get-user-octokit"; | ||
import type { AddEventHandler, ClientType, ClientTypeFromOptions, ConstructorOptions, OctokitTypeFromOptions, Options } from "./types"; | ||
export type { HandlerOptions, OctokitRequest, OctokitResponse, } from "./middleware/types"; | ||
export { handleRequest } from "./middleware/handle-request"; | ||
export { createNodeMiddleware } from "./middleware/node/index"; | ||
@@ -13,0 +15,0 @@ export { createCloudflareHandler, createWebWorkerHandler, } from "./middleware/web-worker/index"; |
import { HandlerOptions } from "../types"; | ||
import { OAuthApp } from "../../index"; | ||
import { Options, ClientType } from "../../types"; | ||
import { ClientType, Options } from "../../types"; | ||
import type { APIGatewayProxyEventV2, APIGatewayProxyStructuredResultV2 } from "aws-lambda"; | ||
@@ -5,0 +5,0 @@ export declare function createAWSLambdaAPIGatewayV2Handler(app: OAuthApp<Options<ClientType>>, { pathPrefix, onUnhandledRequest, }?: HandlerOptions & { |
@@ -1,1 +0,1 @@ | ||
export declare const VERSION = "4.0.8"; | ||
export declare const VERSION = "4.1.0"; |
{ | ||
"name": "@octokit/oauth-app", | ||
"description": "GitHub OAuth toolset for Node.js", | ||
"version": "4.0.8", | ||
"version": "4.1.0", | ||
"license": "MIT", | ||
@@ -37,3 +37,3 @@ "files": [ | ||
"@pika/plugin-ts-standard-pkg": "^0.9.2", | ||
"@types/jest": "^28.0.0", | ||
"@types/jest": "^29.0.0", | ||
"@types/node": "^16.0.0", | ||
@@ -43,3 +43,3 @@ "@types/node-fetch": "^2.5.4", | ||
"fetch-mock": "^9.0.0", | ||
"jest": "^28.0.0", | ||
"jest": "^29.0.0", | ||
"nock": "^13.0.0", | ||
@@ -49,3 +49,3 @@ "node-fetch": "^2.6.0", | ||
"semantic-release-plugin-update-version-in-files": "^1.0.0", | ||
"ts-jest": "^28.0.0", | ||
"ts-jest": "^29.0.0", | ||
"typescript": "^4.0.2" | ||
@@ -52,0 +52,0 @@ }, |
@@ -942,3 +942,3 @@ # oauth-app.js | ||
<th> | ||
<code>options.onUnhandledRequest</code> | ||
<code>options.onUnhandledRequest</code> __deprecated__ | ||
</th> | ||
@@ -1033,3 +1033,3 @@ <th> | ||
<th> | ||
<code>options.onUnhandledRequest</code> | ||
<code>options.onUnhandledRequest</code> __deprecated__ | ||
</th> | ||
@@ -1124,3 +1124,3 @@ <th> | ||
<th> | ||
<code>options.onUnhandledRequest</code> | ||
<code>options.onUnhandledRequest</code> __deprecated__ | ||
</th> | ||
@@ -1151,2 +1151,77 @@ <th> | ||
### Build Custom Middlewares | ||
When above middlewares do not meet your needs, you can build your own | ||
using the exported `handleRequest` function. | ||
[`handleRequest`](./src/middleware/handle-request.ts) function is an abstract HTTP handler which accepts an `OctokitRequest` and returns an `OctokitResponse` if the request matches any predefined route. | ||
> Different environments (e.g., Node.js, Cloudflare Workers, Deno, etc.) exposes different APIs when processing HTTP requests (e.g., [`IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage) for Node.js, [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) for Cloudflare workers, etc.). Two HTTP-related types ([`OctokitRequest` and `OctokitResponse`](.src/middleware/types.ts)) are generalized to make an abstract HTTP handler possible. | ||
To share the behavior and capability with the existing Node.js middleware (and be compatible with [OAuth user authentication strategy in the browser](https://github.com/octokit/auth-oauth-user-client.js)), it is better to implement your HTTP handler/middleware based on `handleRequest` function. | ||
`handleRequest` function takes three parameters: | ||
<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>request</code> | ||
</th> | ||
<th> | ||
<code>OctokitRequest</code> | ||
</th> | ||
<td> | ||
Generalized HTTP request in `OctokitRequest` type. | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
Implementing an HTTP handler/middleware for a certain environment involves three steps: | ||
1. Write a function to parse the HTTP request (e.g., `IncomingMessage` in Node.js) into an `OctokitRequest` object. See [`node/parse-request.ts`](.src/middleware/node/parse-request.ts) for reference. | ||
2. Write a function to render an `OctokitResponse` object (e.g., as `ServerResponse` in Node.js). See [`node/send-response.ts`](.src/middleware/node/send-response.ts) for reference. | ||
3. Expose an HTTP handler/middleware in the dialect of the environment which performs three steps: | ||
1. Parse the HTTP request using (1). | ||
2. Process the `OctokitRequest` object using `handleRequest`. | ||
3. Render the `OctokitResponse` object using (2). | ||
## Contributing | ||
@@ -1153,0 +1228,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
162053
1849
1230