@octokit/app
Advanced tools
Comparing version 13.1.3 to 13.1.4
@@ -1,24 +0,45 @@ | ||
'use strict'; | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
// pkg/dist-src/index.js | ||
var dist_src_exports = {}; | ||
__export(dist_src_exports, { | ||
App: () => App, | ||
createNodeMiddleware: () => createNodeMiddleware | ||
}); | ||
module.exports = __toCommonJS(dist_src_exports); | ||
var import_core = require("@octokit/core"); | ||
var import_auth_app3 = require("@octokit/auth-app"); | ||
var import_oauth_app2 = require("@octokit/oauth-app"); | ||
var core = require('@octokit/core'); | ||
var authApp = require('@octokit/auth-app'); | ||
var oauthApp = require('@octokit/oauth-app'); | ||
var authUnauthenticated = require('@octokit/auth-unauthenticated'); | ||
var webhooks$1 = require('@octokit/webhooks'); | ||
var pluginPaginateRest = require('@octokit/plugin-paginate-rest'); | ||
// pkg/dist-src/version.js | ||
var VERSION = "13.1.4"; | ||
const VERSION = "13.1.3"; | ||
function webhooks(appOctokit, options | ||
// Explict return type for better debugability and performance, | ||
// see https://github.com/octokit/app.js/pull/201 | ||
) { | ||
return new webhooks$1.Webhooks({ | ||
// pkg/dist-src/webhooks.js | ||
var import_auth_app = require("@octokit/auth-app"); | ||
var import_auth_unauthenticated = require("@octokit/auth-unauthenticated"); | ||
var import_webhooks = require("@octokit/webhooks"); | ||
function webhooks(appOctokit, options) { | ||
return new import_webhooks.Webhooks({ | ||
secret: options.secret, | ||
transform: async event => { | ||
transform: async (event) => { | ||
if (!("installation" in event.payload) || typeof event.payload.installation !== "object") { | ||
const octokit = new appOctokit.constructor({ | ||
authStrategy: authUnauthenticated.createUnauthenticatedAuth, | ||
const octokit2 = new appOctokit.constructor({ | ||
authStrategy: import_auth_unauthenticated.createUnauthenticatedAuth, | ||
auth: { | ||
@@ -30,3 +51,3 @@ reason: `"installation" key missing in webhook event payload` | ||
...event, | ||
octokit | ||
octokit: octokit2 | ||
}; | ||
@@ -41,3 +62,3 @@ } | ||
...auth.octokitOptions, | ||
authStrategy: authApp.createAppAuth, | ||
authStrategy: import_auth_app.createAppAuth, | ||
...{ | ||
@@ -52,10 +73,4 @@ auth: { | ||
}); | ||
// set `x-github-delivery` header on all requests sent in response to the current | ||
// event. This allows GitHub Support to correlate the request with the event. | ||
// This is not documented and not considered public API, the header may change. | ||
// Once we document this as best practice on https://docs.github.com/en/rest/guides/best-practices-for-integrators | ||
// we will make it official | ||
/* istanbul ignore next */ | ||
octokit.hook.before("request", options => { | ||
options.headers["x-github-delivery"] = event.id; | ||
octokit.hook.before("request", (options2) => { | ||
options2.headers["x-github-delivery"] = event.id; | ||
}); | ||
@@ -70,16 +85,16 @@ return { | ||
// pkg/dist-src/each-installation.js | ||
var import_plugin_paginate_rest = require("@octokit/plugin-paginate-rest"); | ||
// pkg/dist-src/get-installation-octokit.js | ||
var import_auth_app2 = require("@octokit/auth-app"); | ||
async function getInstallationOctokit(app, installationId) { | ||
return app.octokit.auth({ | ||
type: "installation", | ||
installationId: installationId, | ||
installationId, | ||
factory(auth) { | ||
const options = { | ||
...auth.octokitOptions, | ||
authStrategy: authApp.createAppAuth, | ||
...{ | ||
auth: { | ||
...auth, | ||
installationId: installationId | ||
} | ||
} | ||
authStrategy: import_auth_app2.createAppAuth, | ||
...{ auth: { ...auth, installationId } } | ||
}; | ||
@@ -91,2 +106,3 @@ return new auth.octokit.constructor(options); | ||
// pkg/dist-src/each-installation.js | ||
function eachInstallationFactory(app) { | ||
@@ -108,12 +124,13 @@ return Object.assign(eachInstallation.bind(null, app), { | ||
async *[Symbol.asyncIterator]() { | ||
const iterator = pluginPaginateRest.composePaginateRest.iterator(app.octokit, "GET /app/installations"); | ||
for await (const { | ||
data: installations | ||
} of iterator) { | ||
const iterator = import_plugin_paginate_rest.composePaginateRest.iterator( | ||
app.octokit, | ||
"GET /app/installations" | ||
); | ||
for await (const { data: installations } of iterator) { | ||
for (const installation of installations) { | ||
const installationOctokit = await getInstallationOctokit(app, installation.id); | ||
yield { | ||
octokit: installationOctokit, | ||
installation | ||
}; | ||
const installationOctokit = await getInstallationOctokit( | ||
app, | ||
installation.id | ||
); | ||
yield { octokit: installationOctokit, installation }; | ||
} | ||
@@ -125,2 +142,4 @@ } | ||
// pkg/dist-src/each-repository.js | ||
var import_plugin_paginate_rest2 = require("@octokit/plugin-paginate-rest"); | ||
function eachRepositoryFactory(app) { | ||
@@ -132,3 +151,6 @@ return Object.assign(eachRepository.bind(null, app), { | ||
async function eachRepository(app, queryOrCallback, callback) { | ||
const i = eachRepositoryIterator(app, callback ? queryOrCallback : undefined)[Symbol.asyncIterator](); | ||
const i = eachRepositoryIterator( | ||
app, | ||
callback ? queryOrCallback : void 0 | ||
)[Symbol.asyncIterator](); | ||
let result = await i.next(); | ||
@@ -157,14 +179,10 @@ while (!result.done) { | ||
const iterator = query ? singleInstallationIterator(app, query.installationId) : app.eachInstallation.iterator(); | ||
for await (const { | ||
octokit | ||
} of iterator) { | ||
const repositoriesIterator = pluginPaginateRest.composePaginateRest.iterator(octokit, "GET /installation/repositories"); | ||
for await (const { | ||
data: repositories | ||
} of repositoriesIterator) { | ||
for await (const { octokit } of iterator) { | ||
const repositoriesIterator = import_plugin_paginate_rest2.composePaginateRest.iterator( | ||
octokit, | ||
"GET /installation/repositories" | ||
); | ||
for await (const { data: repositories } of repositoriesIterator) { | ||
for (const repository of repositories) { | ||
yield { | ||
octokit: octokit, | ||
repository | ||
}; | ||
yield { octokit, repository }; | ||
} | ||
@@ -177,2 +195,7 @@ } | ||
// pkg/dist-src/middleware/node/index.js | ||
var import_oauth_app = require("@octokit/oauth-app"); | ||
var import_webhooks2 = require("@octokit/webhooks"); | ||
// pkg/dist-src/middleware/node/on-unhandled-request-default.js | ||
function onUnhandledRequestDefault(request, response) { | ||
@@ -182,15 +205,22 @@ response.writeHead(404, { | ||
}); | ||
response.end(JSON.stringify({ | ||
error: `Unknown route: ${request.method} ${request.url}` | ||
})); | ||
response.end( | ||
JSON.stringify({ | ||
error: `Unknown route: ${request.method} ${request.url}` | ||
}) | ||
); | ||
} | ||
function noop() {} | ||
// pkg/dist-src/middleware/node/index.js | ||
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 log = Object.assign( | ||
{ | ||
debug: noop, | ||
info: noop, | ||
warn: console.warn.bind(console), | ||
error: console.error.bind(console) | ||
}, | ||
options.log | ||
); | ||
const optionsWithDefaults = { | ||
@@ -202,3 +232,3 @@ onUnhandledRequest: onUnhandledRequestDefault, | ||
}; | ||
const webhooksMiddleware = webhooks$1.createNodeMiddleware(app.webhooks, { | ||
const webhooksMiddleware = (0, import_webhooks2.createNodeMiddleware)(app.webhooks, { | ||
path: optionsWithDefaults.pathPrefix + "/webhooks", | ||
@@ -208,3 +238,3 @@ log, | ||
}); | ||
const oauthMiddleware = oauthApp.createNodeMiddleware(app.oauth, { | ||
const oauthMiddleware = (0, import_oauth_app.createNodeMiddleware)(app.oauth, { | ||
pathPrefix: optionsWithDefaults.pathPrefix + "/oauth", | ||
@@ -218,9 +248,4 @@ onUnhandledRequest: optionsWithDefaults.onUnhandledRequest | ||
} | ||
async function middleware(options, { | ||
webhooksMiddleware, | ||
oauthMiddleware | ||
}, request, response, next) { | ||
const { | ||
pathname | ||
} = new URL(request.url, "http://localhost"); | ||
async function middleware(options, { webhooksMiddleware, oauthMiddleware }, request, response, next) { | ||
const { pathname } = new URL(request.url, "http://localhost"); | ||
if (pathname === `${options.pathPrefix}/webhooks`) { | ||
@@ -234,3 +259,2 @@ return webhooksMiddleware(request, response, next); | ||
if (isExpressMiddleware) { | ||
// @ts-ignore `next` must be a function as we check two lines above | ||
return next(); | ||
@@ -241,3 +265,4 @@ } | ||
class App { | ||
// pkg/dist-src/index.js | ||
var App = class { | ||
static defaults(defaults) { | ||
@@ -255,24 +280,30 @@ const AppWithDefaults = class extends this { | ||
constructor(options) { | ||
const Octokit = options.Octokit || core.Octokit; | ||
const authOptions = Object.assign({ | ||
appId: options.appId, | ||
privateKey: options.privateKey | ||
}, options.oauth ? { | ||
clientId: options.oauth.clientId, | ||
clientSecret: options.oauth.clientSecret | ||
} : {}); | ||
const Octokit = options.Octokit || import_core.Octokit; | ||
const authOptions = Object.assign( | ||
{ | ||
appId: options.appId, | ||
privateKey: options.privateKey | ||
}, | ||
options.oauth ? { | ||
clientId: options.oauth.clientId, | ||
clientSecret: options.oauth.clientSecret | ||
} : {} | ||
); | ||
this.octokit = new Octokit({ | ||
authStrategy: authApp.createAppAuth, | ||
authStrategy: import_auth_app3.createAppAuth, | ||
auth: authOptions, | ||
log: options.log | ||
}); | ||
this.log = Object.assign({ | ||
debug: () => {}, | ||
info: () => {}, | ||
warn: console.warn.bind(console), | ||
error: console.error.bind(console) | ||
}, options.log); | ||
// set app.webhooks depending on whether "webhooks" option has been passed | ||
this.log = Object.assign( | ||
{ | ||
debug: () => { | ||
}, | ||
info: () => { | ||
}, | ||
warn: console.warn.bind(console), | ||
error: console.error.bind(console) | ||
}, | ||
options.log | ||
); | ||
if (options.webhooks) { | ||
// @ts-expect-error TODO: figure this out | ||
this.webhooks = webhooks(this.octokit, options.webhooks); | ||
@@ -286,5 +317,4 @@ } else { | ||
} | ||
// set app.oauth depending on whether "oauth" option has been passed | ||
if (options.oauth) { | ||
this.oauth = new oauthApp.OAuthApp({ | ||
this.oauth = new import_oauth_app2.OAuthApp({ | ||
...options.oauth, | ||
@@ -297,15 +327,25 @@ clientType: "github-app", | ||
get() { | ||
throw new Error("[@octokit/app] oauth.clientId / oauth.clientSecret options are not set"); | ||
throw new Error( | ||
"[@octokit/app] oauth.clientId / oauth.clientSecret options are not set" | ||
); | ||
} | ||
}); | ||
} | ||
this.getInstallationOctokit = getInstallationOctokit.bind(null, this); | ||
this.eachInstallation = eachInstallationFactory(this); | ||
this.eachRepository = eachRepositoryFactory(this); | ||
this.getInstallationOctokit = getInstallationOctokit.bind( | ||
null, | ||
this | ||
); | ||
this.eachInstallation = eachInstallationFactory( | ||
this | ||
); | ||
this.eachRepository = eachRepositoryFactory( | ||
this | ||
); | ||
} | ||
} | ||
}; | ||
App.VERSION = VERSION; | ||
exports.App = App; | ||
exports.createNodeMiddleware = createNodeMiddleware; | ||
//# sourceMappingURL=index.js.map | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
App, | ||
createNodeMiddleware | ||
}); |
import { composePaginateRest } from "@octokit/plugin-paginate-rest"; | ||
import { getInstallationOctokit } from "./get-installation-octokit"; | ||
export function eachInstallationFactory(app) { | ||
return Object.assign(eachInstallation.bind(null, app), { | ||
iterator: eachInstallationIterator.bind(null, app), | ||
}); | ||
function eachInstallationFactory(app) { | ||
return Object.assign(eachInstallation.bind(null, app), { | ||
iterator: eachInstallationIterator.bind(null, app) | ||
}); | ||
} | ||
export async function eachInstallation(app, callback) { | ||
const i = eachInstallationIterator(app)[Symbol.asyncIterator](); | ||
let result = await i.next(); | ||
while (!result.done) { | ||
await callback(result.value); | ||
result = await i.next(); | ||
async function eachInstallation(app, callback) { | ||
const i = eachInstallationIterator(app)[Symbol.asyncIterator](); | ||
let result = await i.next(); | ||
while (!result.done) { | ||
await callback(result.value); | ||
result = await i.next(); | ||
} | ||
} | ||
function eachInstallationIterator(app) { | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
const iterator = composePaginateRest.iterator( | ||
app.octokit, | ||
"GET /app/installations" | ||
); | ||
for await (const { data: installations } of iterator) { | ||
for (const installation of installations) { | ||
const installationOctokit = await getInstallationOctokit( | ||
app, | ||
installation.id | ||
); | ||
yield { octokit: installationOctokit, installation }; | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
export function eachInstallationIterator(app) { | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
const iterator = composePaginateRest.iterator(app.octokit, "GET /app/installations"); | ||
for await (const { data: installations } of iterator) { | ||
for (const installation of installations) { | ||
const installationOctokit = await getInstallationOctokit(app, installation.id); | ||
yield { octokit: installationOctokit, installation }; | ||
} | ||
} | ||
}, | ||
}; | ||
} | ||
export { | ||
eachInstallation, | ||
eachInstallationFactory, | ||
eachInstallationIterator | ||
}; |
import { composePaginateRest } from "@octokit/plugin-paginate-rest"; | ||
export function eachRepositoryFactory(app) { | ||
return Object.assign(eachRepository.bind(null, app), { | ||
iterator: eachRepositoryIterator.bind(null, app), | ||
}); | ||
function eachRepositoryFactory(app) { | ||
return Object.assign(eachRepository.bind(null, app), { | ||
iterator: eachRepositoryIterator.bind(null, app) | ||
}); | ||
} | ||
export async function eachRepository(app, queryOrCallback, callback) { | ||
const i = eachRepositoryIterator(app, callback ? queryOrCallback : undefined)[Symbol.asyncIterator](); | ||
let result = await i.next(); | ||
while (!result.done) { | ||
if (callback) { | ||
await callback(result.value); | ||
} | ||
else { | ||
await queryOrCallback(result.value); | ||
} | ||
result = await i.next(); | ||
async function eachRepository(app, queryOrCallback, callback) { | ||
const i = eachRepositoryIterator( | ||
app, | ||
callback ? queryOrCallback : void 0 | ||
)[Symbol.asyncIterator](); | ||
let result = await i.next(); | ||
while (!result.done) { | ||
if (callback) { | ||
await callback(result.value); | ||
} else { | ||
await queryOrCallback(result.value); | ||
} | ||
result = await i.next(); | ||
} | ||
} | ||
function singleInstallationIterator(app, installationId) { | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
yield { | ||
octokit: await app.getInstallationOctokit(installationId), | ||
}; | ||
}, | ||
}; | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
yield { | ||
octokit: await app.getInstallationOctokit(installationId) | ||
}; | ||
} | ||
}; | ||
} | ||
export function eachRepositoryIterator(app, query) { | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
const iterator = query | ||
? singleInstallationIterator(app, query.installationId) | ||
: app.eachInstallation.iterator(); | ||
for await (const { octokit } of iterator) { | ||
const repositoriesIterator = composePaginateRest.iterator(octokit, "GET /installation/repositories"); | ||
for await (const { data: repositories } of repositoriesIterator) { | ||
for (const repository of repositories) { | ||
yield { octokit: octokit, repository }; | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
function eachRepositoryIterator(app, query) { | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
const iterator = query ? singleInstallationIterator(app, query.installationId) : app.eachInstallation.iterator(); | ||
for await (const { octokit } of iterator) { | ||
const repositoriesIterator = composePaginateRest.iterator( | ||
octokit, | ||
"GET /installation/repositories" | ||
); | ||
for await (const { data: repositories } of repositoriesIterator) { | ||
for (const repository of repositories) { | ||
yield { octokit, repository }; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
export { | ||
eachRepository, | ||
eachRepositoryFactory, | ||
eachRepositoryIterator | ||
}; |
import { createAppAuth } from "@octokit/auth-app"; | ||
export async function getInstallationOctokit(app, installationId) { | ||
return app.octokit.auth({ | ||
type: "installation", | ||
installationId: installationId, | ||
factory(auth) { | ||
const options = { | ||
...auth.octokitOptions, | ||
authStrategy: createAppAuth, | ||
...{ auth: { ...auth, installationId: installationId } }, | ||
}; | ||
return new auth.octokit.constructor(options); | ||
}, | ||
}); | ||
async function getInstallationOctokit(app, installationId) { | ||
return app.octokit.auth({ | ||
type: "installation", | ||
installationId, | ||
factory(auth) { | ||
const options = { | ||
...auth.octokitOptions, | ||
authStrategy: createAppAuth, | ||
...{ auth: { ...auth, installationId } } | ||
}; | ||
return new auth.octokit.constructor(options); | ||
} | ||
}); | ||
} | ||
export { | ||
getInstallationOctokit | ||
}; |
@@ -10,70 +10,82 @@ import { Octokit as OctokitCore } from "@octokit/core"; | ||
class App { | ||
static defaults(defaults) { | ||
const AppWithDefaults = class extends this { | ||
constructor(...args) { | ||
super({ | ||
...defaults, | ||
...args[0], | ||
}); | ||
} | ||
}; | ||
return AppWithDefaults; | ||
} | ||
constructor(options) { | ||
const Octokit = (options.Octokit || | ||
OctokitCore); | ||
const authOptions = Object.assign({ | ||
appId: options.appId, | ||
privateKey: options.privateKey, | ||
}, options.oauth | ||
? { | ||
clientId: options.oauth.clientId, | ||
clientSecret: options.oauth.clientSecret, | ||
} | ||
: {}); | ||
this.octokit = new Octokit({ | ||
authStrategy: createAppAuth, | ||
auth: authOptions, | ||
log: options.log, | ||
static defaults(defaults) { | ||
const AppWithDefaults = class extends this { | ||
constructor(...args) { | ||
super({ | ||
...defaults, | ||
...args[0] | ||
}); | ||
this.log = Object.assign({ | ||
debug: () => { }, | ||
info: () => { }, | ||
warn: console.warn.bind(console), | ||
error: console.error.bind(console), | ||
}, options.log); | ||
// set app.webhooks depending on whether "webhooks" option has been passed | ||
if (options.webhooks) { | ||
// @ts-expect-error TODO: figure this out | ||
this.webhooks = webhooks(this.octokit, options.webhooks); | ||
} | ||
}; | ||
return AppWithDefaults; | ||
} | ||
constructor(options) { | ||
const Octokit = options.Octokit || OctokitCore; | ||
const authOptions = Object.assign( | ||
{ | ||
appId: options.appId, | ||
privateKey: options.privateKey | ||
}, | ||
options.oauth ? { | ||
clientId: options.oauth.clientId, | ||
clientSecret: options.oauth.clientSecret | ||
} : {} | ||
); | ||
this.octokit = new Octokit({ | ||
authStrategy: createAppAuth, | ||
auth: authOptions, | ||
log: options.log | ||
}); | ||
this.log = Object.assign( | ||
{ | ||
debug: () => { | ||
}, | ||
info: () => { | ||
}, | ||
warn: console.warn.bind(console), | ||
error: console.error.bind(console) | ||
}, | ||
options.log | ||
); | ||
if (options.webhooks) { | ||
this.webhooks = webhooks(this.octokit, options.webhooks); | ||
} else { | ||
Object.defineProperty(this, "webhooks", { | ||
get() { | ||
throw new Error("[@octokit/app] webhooks option not set"); | ||
} | ||
else { | ||
Object.defineProperty(this, "webhooks", { | ||
get() { | ||
throw new Error("[@octokit/app] webhooks option not set"); | ||
}, | ||
}); | ||
}); | ||
} | ||
if (options.oauth) { | ||
this.oauth = new OAuthApp({ | ||
...options.oauth, | ||
clientType: "github-app", | ||
Octokit | ||
}); | ||
} else { | ||
Object.defineProperty(this, "oauth", { | ||
get() { | ||
throw new Error( | ||
"[@octokit/app] oauth.clientId / oauth.clientSecret options are not set" | ||
); | ||
} | ||
// set app.oauth depending on whether "oauth" option has been passed | ||
if (options.oauth) { | ||
this.oauth = new OAuthApp({ | ||
...options.oauth, | ||
clientType: "github-app", | ||
Octokit, | ||
}); | ||
} | ||
else { | ||
Object.defineProperty(this, "oauth", { | ||
get() { | ||
throw new Error("[@octokit/app] oauth.clientId / oauth.clientSecret options are not set"); | ||
}, | ||
}); | ||
} | ||
this.getInstallationOctokit = getInstallationOctokit.bind(null, this); | ||
this.eachInstallation = eachInstallationFactory(this); | ||
this.eachRepository = eachRepositoryFactory(this); | ||
}); | ||
} | ||
this.getInstallationOctokit = getInstallationOctokit.bind( | ||
null, | ||
this | ||
); | ||
this.eachInstallation = eachInstallationFactory( | ||
this | ||
); | ||
this.eachRepository = eachRepositoryFactory( | ||
this | ||
); | ||
} | ||
} | ||
App.VERSION = VERSION; | ||
export { App }; | ||
export { createNodeMiddleware } from "./middleware/node/index"; | ||
import { createNodeMiddleware } from "./middleware/node/index"; | ||
export { | ||
App, | ||
createNodeMiddleware | ||
}; |
import { createNodeMiddleware as oauthNodeMiddleware } from "@octokit/oauth-app"; | ||
import { createNodeMiddleware as webhooksNodeMiddleware } from "@octokit/webhooks"; | ||
import { onUnhandledRequestDefault } from "./on-unhandled-request-default"; | ||
function noop() { } | ||
export 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, | ||
}); | ||
function noop() { | ||
} | ||
export 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) { | ||
// @ts-ignore `next` must be a function as we check two lines above | ||
return next(); | ||
} | ||
return options.onUnhandledRequest(request, response); | ||
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 | ||
}; |
@@ -1,8 +0,13 @@ | ||
export function onUnhandledRequestDefault(request, response) { | ||
response.writeHead(404, { | ||
"content-type": "application/json", | ||
}); | ||
response.end(JSON.stringify({ | ||
error: `Unknown route: ${request.method} ${request.url}`, | ||
})); | ||
function onUnhandledRequestDefault(request, response) { | ||
response.writeHead(404, { | ||
"content-type": "application/json" | ||
}); | ||
response.end( | ||
JSON.stringify({ | ||
error: `Unknown route: ${request.method} ${request.url}` | ||
}) | ||
); | ||
} | ||
export { | ||
onUnhandledRequestDefault | ||
}; |
@@ -1,1 +0,4 @@ | ||
export const VERSION = "13.1.3"; | ||
const VERSION = "13.1.4"; | ||
export { | ||
VERSION | ||
}; |
import { createAppAuth } from "@octokit/auth-app"; | ||
import { createUnauthenticatedAuth } from "@octokit/auth-unauthenticated"; | ||
import { Webhooks } from "@octokit/webhooks"; | ||
export function webhooks(appOctokit, options | ||
// Explict return type for better debugability and performance, | ||
// see https://github.com/octokit/app.js/pull/201 | ||
) { | ||
return new Webhooks({ | ||
secret: options.secret, | ||
transform: async (event) => { | ||
if (!("installation" in event.payload) || | ||
typeof event.payload.installation !== "object") { | ||
const octokit = new appOctokit.constructor({ | ||
authStrategy: createUnauthenticatedAuth, | ||
auth: { | ||
reason: `"installation" key missing in webhook event payload`, | ||
}, | ||
}); | ||
return { | ||
...event, | ||
octokit, | ||
}; | ||
function webhooks(appOctokit, options) { | ||
return new Webhooks({ | ||
secret: options.secret, | ||
transform: async (event) => { | ||
if (!("installation" in event.payload) || typeof event.payload.installation !== "object") { | ||
const octokit2 = new appOctokit.constructor({ | ||
authStrategy: createUnauthenticatedAuth, | ||
auth: { | ||
reason: `"installation" key missing in webhook event payload` | ||
} | ||
}); | ||
return { | ||
...event, | ||
octokit: octokit2 | ||
}; | ||
} | ||
const installationId = event.payload.installation.id; | ||
const octokit = await appOctokit.auth({ | ||
type: "installation", | ||
installationId, | ||
factory(auth) { | ||
return new auth.octokit.constructor({ | ||
...auth.octokitOptions, | ||
authStrategy: createAppAuth, | ||
...{ | ||
auth: { | ||
...auth, | ||
installationId | ||
} | ||
} | ||
const installationId = event.payload.installation.id; | ||
const octokit = (await appOctokit.auth({ | ||
type: "installation", | ||
installationId, | ||
factory(auth) { | ||
return new auth.octokit.constructor({ | ||
...auth.octokitOptions, | ||
authStrategy: createAppAuth, | ||
...{ | ||
auth: { | ||
...auth, | ||
installationId, | ||
}, | ||
}, | ||
}); | ||
}, | ||
})); | ||
// set `x-github-delivery` header on all requests sent in response to the current | ||
// event. This allows GitHub Support to correlate the request with the event. | ||
// This is not documented and not considered public API, the header may change. | ||
// Once we document this as best practice on https://docs.github.com/en/rest/guides/best-practices-for-integrators | ||
// we will make it official | ||
/* istanbul ignore next */ | ||
octokit.hook.before("request", (options) => { | ||
options.headers["x-github-delivery"] = event.id; | ||
}); | ||
return { | ||
...event, | ||
octokit, | ||
}; | ||
}, | ||
}); | ||
}); | ||
} | ||
}); | ||
octokit.hook.before("request", (options2) => { | ||
options2.headers["x-github-delivery"] = event.id; | ||
}); | ||
return { | ||
...event, | ||
octokit | ||
}; | ||
} | ||
}); | ||
} | ||
export { | ||
webhooks | ||
}; |
@@ -1,1 +0,1 @@ | ||
export declare const VERSION = "13.1.3"; | ||
export declare const VERSION = "13.1.4"; |
{ | ||
"name": "@octokit/app", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"version": "13.1.4", | ||
"description": "GitHub Apps toolset for Node.js", | ||
"version": "13.1.3", | ||
"license": "MIT", | ||
"files": [ | ||
"dist-*/**", | ||
"bin/**" | ||
], | ||
"source": "dist-src/index.js", | ||
"types": "dist-types/index.d.ts", | ||
"main": "dist-node/index.js", | ||
"pika": true, | ||
"sideEffects": false, | ||
"repository": "github:octokit/app.js", | ||
"author": "Gregor Martynus (https://github.com/gr2m)", | ||
"license": "MIT", | ||
"dependencies": { | ||
@@ -26,10 +22,10 @@ "@octokit/auth-app": "^4.0.8", | ||
"devDependencies": { | ||
"@pika/pack": "^0.3.7", | ||
"@pika/plugin-build-node": "^0.9.1", | ||
"@pika/plugin-ts-standard-pkg": "^0.9.1", | ||
"@octokit/tsconfig": "^1.0.2", | ||
"@types/jest": "^29.0.0", | ||
"@types/node": "^18.0.0", | ||
"@types/node-fetch": "^2.5.8", | ||
"esbuild": "^0.17.16", | ||
"express": "^4.17.1", | ||
"fetch-mock": "^9.10.7", | ||
"glob": "^10.0.0", | ||
"jest": "^29.0.0", | ||
@@ -46,5 +42,10 @@ "mockdate": "^3.0.2", | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
"files": [ | ||
"dist-*/**", | ||
"bin/**" | ||
], | ||
"module": "dist-web/index.js", | ||
"types": "dist-types/index.d.ts", | ||
"source": "dist-src/index.js", | ||
"sideEffects": false | ||
} |
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
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
96609
24
1467
1