UNPKG

aws-delivlib

Version:

A fabulous library for defining continuous pipelines for building, testing and releasing code libraries.

55 lines (54 loc) 1.78 kB
import { createNodeMiddleware as oauthNodeMiddleware } from "@octokit/oauth-app"; import { createNodeMiddleware as webhooksNodeMiddleware } from "@octokit/webhooks"; import { App } from "../../index"; import { onUnhandledRequestDefault } from "./on-unhandled-request-default"; function noop() { } function createNodeMiddleware(app, options = {}) { const log = Object.assign( { debug: noop, info: noop, warn: console.warn.bind(console), error: console.error.bind(console) }, options.log ); const optionsWithDefaults = { onUnhandledRequest: onUnhandledRequestDefault, pathPrefix: "/api/github", ...options, log }; const webhooksMiddleware = webhooksNodeMiddleware(app.webhooks, { path: optionsWithDefaults.pathPrefix + "/webhooks", log, onUnhandledRequest: optionsWithDefaults.onUnhandledRequest }); const oauthMiddleware = oauthNodeMiddleware(app.oauth, { pathPrefix: optionsWithDefaults.pathPrefix + "/oauth", onUnhandledRequest: optionsWithDefaults.onUnhandledRequest }); return middleware.bind(null, optionsWithDefaults, { webhooksMiddleware, oauthMiddleware }); } async function middleware(options, { webhooksMiddleware, oauthMiddleware }, request, response, next) { const { pathname } = new URL(request.url, "http://localhost"); if (pathname === `${options.pathPrefix}/webhooks`) { return webhooksMiddleware(request, response, next); } if (pathname.startsWith(`${options.pathPrefix}/oauth/`)) { return oauthMiddleware(request, response, next); } const isExpressMiddleware = typeof next === "function"; if (isExpressMiddleware) { return next(); } return options.onUnhandledRequest(request, response); } export { createNodeMiddleware, middleware };