Skip to content

Commit

Permalink
refactor: deprecate onUnhandledRequest middleware option
Browse files Browse the repository at this point in the history
  • Loading branch information
baoshan committed Sep 7, 2022
1 parent 0e0853e commit e6baaf1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/
</tr>
<tr>
<th>
<code>options.onUnhandledRequest</code>
<code>options.onUnhandledRequest</code> __deprecated__
</th>
<th>
<code>function</code>
Expand Down Expand Up @@ -1029,7 +1029,7 @@ All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/
</tr>
<tr>
<th>
<code>options.onUnhandledRequest</code>
<code>options.onUnhandledRequest</code> __deprecated__
</th>
<th>
<code>function</code>
Expand Down Expand Up @@ -1119,7 +1119,7 @@ All exposed paths will be prefixed with the provided prefix. Defaults to `"/api/
</tr>
<tr>
<th>
<code>options.onUnhandledRequest</code>
<code>options.onUnhandledRequest</code> __deprecated__
</th>
<th>
<code>function</code>
Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ 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,
Expand Down
12 changes: 9 additions & 3 deletions src/middleware/aws-lambda/api-gateway-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { handleRequest } from "../handle-request";
import { onUnhandledRequestDefault } from "../on-unhandled-request-default";
import { HandlerOptions } from "../types";
import { OAuthApp } from "../../index";
import { Options, ClientType } from "../../types";
import { ClientType, Options } from "../../types";
import type {
APIGatewayProxyEventV2,
APIGatewayProxyStructuredResultV2,
Expand All @@ -22,16 +22,22 @@ export function createAWSLambdaAPIGatewayV2Handler(
app: OAuthApp<Options<ClientType>>,
{
pathPrefix,
onUnhandledRequest = onUnhandledRequestDefaultAWSAPIGatewayV2,
onUnhandledRequest,
}: HandlerOptions & {
onUnhandledRequest?: (
event: APIGatewayProxyEventV2
) => Promise<APIGatewayProxyStructuredResultV2>;
} = {}
) {
if (onUnhandledRequest) {
app.octokit.log.warn(
"[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."
);
}
onUnhandledRequest ??= onUnhandledRequestDefaultAWSAPIGatewayV2;
return async function (event: APIGatewayProxyEventV2) {
const request = parseRequest(event);
const response = await handleRequest(app, { pathPrefix }, request);
return response ? sendResponse(response) : onUnhandledRequest(event);
return response ? sendResponse(response) : onUnhandledRequest!(event);
};
}
10 changes: 8 additions & 2 deletions src/middleware/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ export function createNodeMiddleware(
app: OAuthApp<Options<ClientType>>,
{
pathPrefix,
onUnhandledRequest = onUnhandledRequestDefaultNode,
onUnhandledRequest,
}: HandlerOptions & {
onUnhandledRequest?: (
request: IncomingMessage,
response: ServerResponse
) => void;
} = {}
) {
if (onUnhandledRequest) {
app.octokit.log.warn(
"[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."
);
}
onUnhandledRequest ??= onUnhandledRequestDefaultNode;
return async function (
request: IncomingMessage,
response: ServerResponse,
Expand All @@ -50,7 +56,7 @@ export function createNodeMiddleware(
} else if (typeof next === "function") {
next();
} else {
onUnhandledRequest(request, response);
onUnhandledRequest!(request, response);
}
};
}
10 changes: 8 additions & 2 deletions src/middleware/web-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ export function createWebWorkerHandler<T extends Options<ClientType>>(
app: OAuthApp<T>,
{
pathPrefix,
onUnhandledRequest = onUnhandledRequestDefaultWebWorker,
onUnhandledRequest,
}: HandlerOptions & {
onUnhandledRequest?: (request: Request) => Response | Promise<Response>;
} = {}
) {
if (onUnhandledRequest) {
app.octokit.log.warn(
"[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."
);
}
onUnhandledRequest ??= onUnhandledRequestDefaultWebWorker;
return async function (request: Request): Promise<Response> {
const octokitRequest = parseRequest(request);
const octokitResponse = await handleRequest(
Expand All @@ -32,7 +38,7 @@ export function createWebWorkerHandler<T extends Options<ClientType>>(
);
return octokitResponse
? sendResponse(octokitResponse)
: await onUnhandledRequest(request);
: await onUnhandledRequest!(request);
};
}

Expand Down
41 changes: 40 additions & 1 deletion test/deprecations.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { URL } from "url";
import * as nodeFetch from "node-fetch";
import fromEntries from "fromentries";
import { createCloudflareHandler, OAuthApp } from "../src";
import {
createAWSLambdaAPIGatewayV2Handler,
createCloudflareHandler,
createNodeMiddleware,
createWebWorkerHandler,
OAuthApp,
} from "../src";
import { Octokit } from "@octokit/core";

describe("deprecations", () => {
Expand Down Expand Up @@ -52,4 +58,37 @@ describe("deprecations", () => {
expect(url.searchParams.get("state")).toMatch(/^\w+$/);
expect(url.searchParams.get("scope")).toEqual(null);
});

it("`onUnhandledRequest` is deprecated and will be removed from the next major version", async () => {
const warn = jest.fn().mockResolvedValue(undefined);
const handleRequest = createAWSLambdaAPIGatewayV2Handler(
new OAuthApp({
clientType: "github-app",
clientId: "client_id_123",
clientSecret: "client_secret_456",
Octokit: Octokit.defaults({
log: {
debug: () => undefined,
info: () => undefined,
warn,
error: () => undefined,
},
}),
}),
{
onUnhandledRequest: async (request) => {
return {
statusCode: 404,
headers: {},
body: "",
};
},
}
);

expect(warn.mock.calls.length).toEqual(1);
expect(warn.mock.calls[0][0]).toEqual(
"[@octokit/oauth-app] `onUnhandledRequest` is deprecated and will be removed from the next major version."
);
});
});
6 changes: 5 additions & 1 deletion test/smoke.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OAuthApp } from "../src";
import { handleRequest, OAuthApp } from "../src";

describe("Smoke test", () => {
it("OAuthApp is a function", () => {
Expand All @@ -12,4 +12,8 @@ describe("Smoke test", () => {
it("OAuthApp.VERSION is set", () => {
expect(OAuthApp.VERSION).toEqual("0.0.0-development");
});

it("handleRequest is a function", () => {
expect(typeof handleRequest).toEqual("function");
});
});

0 comments on commit e6baaf1

Please sign in to comment.