Skip to content

Commit

Permalink
refactor(types): adapt syntax to type restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
oscard0m committed Sep 24, 2024
1 parent 45a2804 commit 8e18da7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
47 changes: 30 additions & 17 deletions src/middleware/handle-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ClientType, Options } from "../types.js";
export async function handleRequest(
app: OAuthApp<Options<ClientType>>,
{ pathPrefix = "/api/github/oauth" }: HandlerOptions,
request: OctokitRequest,
request: OctokitRequest
): Promise<OctokitResponse | undefined> {
// request.url may include ?query parameters which we don't want for `route`
// hence the workaround using new URL()
Expand Down Expand Up @@ -82,22 +82,35 @@ export async function handleRequest(

try {
if (route === routes.getLogin) {
const { url } = app.getWebFlowAuthorizationUrl({
state: query.state,
scopes: query.scopes ? query.scopes.split(",") : undefined,
allowSignup: query.allowSignup
? query.allowSignup === "true"
: undefined,
redirectUrl: query.redirectUrl,
});
const authOptions = {};

if (query.state) {
Object.assign(authOptions, { state: query.state });
}

if (query.scopes) {
Object.assign(authOptions, { scopes: query.scopes.split(",") });
}

if (query.allowSignup) {
Object.assign(authOptions, {
allowSignup: query.allowSignup === "true",
});
}

if (query.redirectUrl) {
Object.assign(authOptions, { redirectUrl: query.redirectUrl });
}

const { url } = app.getWebFlowAuthorizationUrl(authOptions);

return { status: 302, headers: { location: url } };
}

if (route === routes.getCallback) {
if (query.error) {
throw new Error(
`[@octokit/oauth-app] ${query.error} ${query.error_description}`,
`[@octokit/oauth-app] ${query.error} ${query.error_description}`
);
}
if (!query.code) {
Expand Down Expand Up @@ -151,7 +164,7 @@ export async function handleRequest(

if (!token) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required',
'[@octokit/oauth-app] "Authorization" header is required'
);
}

Expand All @@ -177,7 +190,7 @@ export async function handleRequest(

if (!token) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required',
'[@octokit/oauth-app] "Authorization" header is required'
);
}

Expand All @@ -201,15 +214,15 @@ export async function handleRequest(

if (!token) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required',
'[@octokit/oauth-app] "Authorization" header is required'
);
}

const { refreshToken } = json;

if (!refreshToken) {
throw new Error(
"[@octokit/oauth-app] refreshToken must be sent in request body",
"[@octokit/oauth-app] refreshToken must be sent in request body"
);
}

Expand All @@ -233,7 +246,7 @@ export async function handleRequest(

if (!token) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required',
'[@octokit/oauth-app] "Authorization" header is required'
);
}

Expand All @@ -260,7 +273,7 @@ export async function handleRequest(

if (!token) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required',
'[@octokit/oauth-app] "Authorization" header is required'
);
}

Expand All @@ -279,7 +292,7 @@ export async function handleRequest(

if (!token) {
throw new Error(
'[@octokit/oauth-app] "Authorization" header is required',
'[@octokit/oauth-app] "Authorization" header is required'
);
}

Expand Down
11 changes: 8 additions & 3 deletions src/middleware/web-worker/send-response.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { OctokitResponse } from "../types.js";

export function sendResponse(octokitResponse: OctokitResponse): Response {
return new Response(octokitResponse.text, {
const responseOptions = {
status: octokitResponse.status,
headers: octokitResponse.headers,
});
};

if (octokitResponse.headers) {
Object.assign(responseOptions, { headers: octokitResponse.headers });
}

return new Response(octokitResponse.text, responseOptions);
}
12 changes: 6 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ export type State = {
clientId: ClientId;
clientSecret: ClientSecret;
defaultScopes: Scope[];
allowSignup?: boolean;
baseUrl?: string;
redirectUrl?: string;
log?: typeof console;
allowSignup?: boolean | undefined;
baseUrl?: string | undefined;
redirectUrl?: string | undefined;
log?: typeof console | undefined;
Octokit: OAuthAppOctokitClassType;
octokit: OctokitInstance;
eventHandlers: {
Expand Down Expand Up @@ -116,9 +116,9 @@ export type EventHandlerContext<TOptions extends Options<ClientType>> =
authentication?: GithubAppUserAuthenticationWithOptionalExpiration;
};
export type EventHandler<TOptions extends Options<ClientType>> = (
context: EventHandlerContext<TOptions>,
context: EventHandlerContext<TOptions>
) => void;
export type AddEventHandler<TOptions extends Options<ClientType>> = (
eventName: EventAndActionName | EventAndActionName[],
eventHandler: EventHandler<TOptions>,
eventHandler: EventHandler<TOptions>
) => void;

0 comments on commit 8e18da7

Please sign in to comment.