UNPKG

5.31 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Application = void 0;
7const express_1 = require("express");
8const lru_cache_1 = __importDefault(require("lru-cache"));
9const deprecation_1 = require("deprecation");
10const probot_octokit_1 = require("./octokit/probot-octokit");
11const get_log_1 = require("./helpers/get-log");
12const get_probot_octokit_with_defaults_1 = require("./octokit/get-probot-octokit-with-defaults");
13const webhook_event_check_1 = require("./helpers/webhook-event-check");
14const alias_log_1 = require("./helpers/alias-log");
15const get_webhooks_1 = require("./octokit/get-webhooks");
16const load_1 = require("./load");
17const get_router_1 = require("./get-router");
18const auth_1 = require("./auth");
19/**
20 * The `app` parameter available to `ApplicationFunction`s
21 *
22 * @property {logger} log - A logger
23 */
24class Application {
25 constructor(options) {
26 this.log = alias_log_1.aliasLog(options.log || get_log_1.getLog({ level: process.env.LOG_LEVEL }));
27 this.log.warn(`[probot] "import { Application } from 'probot'" is deprecated. Use "import { Probot } from 'probot'" instead, the APIs are the same.`);
28 const cache = options.cache ||
29 new lru_cache_1.default({
30 // cache max. 15000 tokens, that will use less than 10mb memory
31 max: 15000,
32 // Cache for 1 minute less than GitHub expiry
33 maxAge: 1000 * 60 * 59,
34 });
35 const Octokit = get_probot_octokit_with_defaults_1.getProbotOctokitWithDefaults({
36 githubToken: options.githubToken,
37 Octokit: options.Octokit || probot_octokit_1.ProbotOctokit,
38 appId: options.id,
39 privateKey: options.privateKey,
40 cache,
41 log: this.log,
42 redisConfig: options.redisConfig,
43 throttleOptions: options.throttleOptions,
44 });
45 if (options.throttleOptions) {
46 this.log.warn(`[probot] "new Application({ throttleOptions })" is deprecated. Use "new Application({Octokit: ProbotOctokit.defaults({ throttle }) })" instead`);
47 }
48 this.state = {
49 cache,
50 githubToken: options.githubToken,
51 log: this.log,
52 Octokit,
53 octokit: options.octokit || new Octokit(),
54 webhooks: {
55 path: options.webhookPath,
56 secret: options.secret,
57 },
58 };
59 this.webhooks = options.webhooks || get_webhooks_1.getWebhooks(this.state);
60 this.on = (eventNameOrNames, callback) => {
61 // when an app subscribes to an event using `app.on(event, callback)`, Probot sends a request to `GET /app` and
62 // verifies if the app is subscribed to the event and logs a warning if it is not.
63 //
64 // This feature will be moved out of Probot core as it has side effects and does not work in a stateless environment.
65 webhook_event_check_1.webhookEventCheck(this.state, eventNameOrNames);
66 if (eventNameOrNames === "*") {
67 // @ts-ignore this workaround is only to surpress a warning. The `.on()` method will be deprecated soon anyway.
68 return this.webhooks.onAny(callback);
69 }
70 return this.webhooks.on(eventNameOrNames, callback);
71 };
72 this.receive = this.webhooks.receive;
73 const router = options.router || express_1.Router();
74 // @ts-ignore
75 this.load = load_1.load.bind(null, this, router);
76 this.auth = auth_1.auth.bind(null, this.state);
77 this.internalRouter = router;
78 }
79 /**
80 * @deprecated "app.router" is deprecated, use "getRouter()" from the app function instead: "({ app, getRouter }) => { ... }"
81 */
82 get router() {
83 this.log.warn(new deprecation_1.Deprecation(`[probot] "app.router" is deprecated, use "getRouter()" from the app function instead: "({ app, getRouter }) => { ... }"`));
84 return this.internalRouter;
85 }
86 /**
87 * Get an {@link http://expressjs.com|express} router that can be used to
88 * expose HTTP endpoints
89 *
90 * ```
91 * module.exports = ({ app, getRouter }) => {
92 * // Get an express router to expose new HTTP endpoints
93 * const router = getRouter('/my-app');
94 *
95 * // Use any middleware
96 * router.use(require('express').static(__dirname + '/public'));
97 *
98 * // Add a new route
99 * router.get('/hello-world', (req, res) => {
100 * res.end('Hello World');
101 * });
102 * };
103 * ```
104 *
105 * @param path - the prefix for the routes* @param path
106 *
107 * @deprecated "app.route()" is deprecated, use the "getRouter()" argument from the app function instead: "({ app, getRouter }) => { ... }"
108 */
109 route(path) {
110 this.log.warn(new deprecation_1.Deprecation(`[probot] "app.route()" is deprecated, use the "getRouter()" argument from the app function instead: "({ app, getRouter }) => { ... }"`));
111 return get_router_1.getRouter(this.internalRouter, path);
112 }
113}
114exports.Application = Application;
115//# sourceMappingURL=application.js.map
\No newline at end of file