From 3dbf02dc36afc765799b9ca00d3853f29f84ca2a Mon Sep 17 00:00:00 2001 From: Filip Maj Date: Fri, 27 Sep 2024 12:25:27 -0400 Subject: [PATCH] fixing up some examples, adding custom-receiver example to example TS integration CI check --- .github/workflows/samples.yml | 1 + examples/custom-properties/package.json | 2 +- examples/custom-receiver/package.json | 26 ++++++++--------- .../custom-receiver/src/FastifyReceiver.ts | 29 ++++++++----------- examples/custom-receiver/src/KoaReceiver.ts | 27 +++++++---------- examples/custom-receiver/src/fastify-main.ts | 14 ++++----- examples/custom-receiver/src/koa-main.ts | 14 ++++----- examples/custom-receiver/tsconfig.eslint.json | 13 --------- examples/custom-receiver/tsconfig.json | 20 ++++++++----- package.json | 2 +- 10 files changed, 61 insertions(+), 87 deletions(-) delete mode 100644 examples/custom-receiver/tsconfig.eslint.json diff --git a/.github/workflows/samples.yml b/.github/workflows/samples.yml index 024108ef3..9a6d34f0c 100644 --- a/.github/workflows/samples.yml +++ b/.github/workflows/samples.yml @@ -14,6 +14,7 @@ jobs: node-version: [18.x, 20.x, 22.x] example: - examples/getting-started-typescript + - examples/custom-receiver steps: - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v4 diff --git a/examples/custom-properties/package.json b/examples/custom-properties/package.json index ec2af1efa..f38030765 100644 --- a/examples/custom-properties/package.json +++ b/examples/custom-properties/package.json @@ -10,6 +10,6 @@ }, "license": "MIT", "dependencies": { - "@slack/socket-mode": "^1.2.0" + "@slack/bolt": "^3" } } diff --git a/examples/custom-receiver/package.json b/examples/custom-receiver/package.json index 6507886c7..f087771f5 100644 --- a/examples/custom-receiver/package.json +++ b/examples/custom-receiver/package.json @@ -4,25 +4,25 @@ "description": "Example app using OAuth", "main": "app.js", "scripts": { - "lint": "eslint --fix --ext .ts src", - "build": "npm run lint && tsc -p .", - "build:watch": "npm run lint && tsc -w -p .", + "build": "tsc -p .", + "build:watch": "tsc -w -p .", "koa": "npm run build && node dist/koa-main.js", "fastify": "npm run build && node dist/fastify-main.js" }, "license": "MIT", "dependencies": { - "@koa/router": "^10.1.1", - "@slack/logger": "^3.0.0", - "@slack/oauth": "^2.5.0", - "dotenv": "^8.2.0", - "fastify": "^3.27.4", - "koa": "^2.13.4" + "@koa/router": "^13", + "@slack/bolt": "^3", + "@slack/logger": "^4.0.0", + "@slack/oauth": "^3", + "dotenv": "^16", + "fastify": "^5", + "koa": "^2" }, "devDependencies": { - "@types/koa__router": "^8.0.11", - "@types/node": "^14.14.35", - "ts-node": "^9.1.1", - "typescript": "^4.2.3" + "@types/koa__router": "^12", + "@types/node": "^18", + "ts-node": "^10", + "typescript": "5.3.3" } } diff --git a/examples/custom-receiver/src/FastifyReceiver.ts b/examples/custom-receiver/src/FastifyReceiver.ts index 6df3ebabc..4ae2e647e 100644 --- a/examples/custom-receiver/src/FastifyReceiver.ts +++ b/examples/custom-receiver/src/FastifyReceiver.ts @@ -1,4 +1,4 @@ -import type { Server } from 'http'; +import type { Server } from 'node:http'; import { type App, type BufferedIncomingMessage, @@ -14,11 +14,14 @@ import { HTTPModuleFunctions as httpFunc, } from '@slack/bolt'; import { ConsoleLogger, type LogLevel, type Logger } from '@slack/logger'; -/* eslint-disable node/no-extraneous-import */ -/* eslint-disable import/no-extraneous-dependencies */ import { type CallbackOptions, type InstallPathOptions, InstallProvider } from '@slack/oauth'; import Fastify, { type FastifyInstance } from 'fastify'; +type CustomPropertiesExtractor = ( + request: BufferedIncomingMessage, + // biome-ignore lint/suspicious/noExplicitAny: custom properties can be anything +) => Record; + export interface InstallerOptions { stateStore?: InstallProviderOptions['stateStore']; // default ClearStateStore stateVerification?: InstallProviderOptions['stateVerification']; // defaults true @@ -50,10 +53,7 @@ export interface FastifyReceiverOptions { scopes?: InstallURLOptions['scopes']; installerOptions?: InstallerOptions; fastify?: FastifyInstance; - customPropertiesExtractor?: ( - request: BufferedIncomingMessage, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ) => Record; + customPropertiesExtractor?: CustomPropertiesExtractor; processEventErrorHandler?: (args: ReceiverProcessEventErrorHandlerArgs) => Promise; // NOTE: As we use setTimeout under the hood, this cannot be async unhandledRequestHandler?: (args: ReceiverUnhandledRequestHandlerArgs) => void; @@ -75,10 +75,7 @@ export default class FastifyReceiver implements Receiver { private unhandledRequestTimeoutMillis: number; - private customPropertiesExtractor: ( - request: BufferedIncomingMessage, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ) => Record; + private customPropertiesExtractor: CustomPropertiesExtractor; private processEventErrorHandler: (args: ReceiverProcessEventErrorHandlerArgs) => Promise; @@ -184,7 +181,7 @@ export default class FastifyReceiver implements Receiver { const req = request.raw; const res = response.raw; - // eslint-disable-next-line @typescript-eslint/no-explicit-any + // biome-ignore lint/suspicious/noExplicitAny: request bodies can be anything (req as any).rawBody = Buffer.from(request.body as string); // Verify authenticity let bufferedReq: BufferedIncomingMessage; @@ -198,8 +195,7 @@ export default class FastifyReceiver implements Receiver { req, ); } catch (err) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const e = err as any; + const e = err as Error; if (this.signatureVerification) { this.logger.warn(`Failed to parse and verify the request data: ${e.message}`); } else { @@ -210,13 +206,12 @@ export default class FastifyReceiver implements Receiver { } // Parse request body - // eslint-disable-next-line @typescript-eslint/no-explicit-any + // biome-ignore lint/suspicious/noExplicitAny: request bodies can be anything let body: any; try { body = httpFunc.parseHTTPRequestBody(bufferedReq); } catch (err) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const e = err as any; + const e = err as Error; this.logger.warn(`Malformed request body: ${e.message}`); httpFunc.buildNoBodyResponse(res, 400); return; diff --git a/examples/custom-receiver/src/KoaReceiver.ts b/examples/custom-receiver/src/KoaReceiver.ts index f20d66cc4..5dfeee8ef 100644 --- a/examples/custom-receiver/src/KoaReceiver.ts +++ b/examples/custom-receiver/src/KoaReceiver.ts @@ -1,4 +1,4 @@ -import { type Server, createServer } from 'http'; +import { type Server, createServer } from 'node:http'; import Router from '@koa/router'; import { type App, @@ -15,8 +15,6 @@ import { HTTPModuleFunctions as httpFunc, } from '@slack/bolt'; import { ConsoleLogger, type LogLevel, type Logger } from '@slack/logger'; -/* eslint-disable node/no-extraneous-import */ -/* eslint-disable import/no-extraneous-dependencies */ import { type CallbackOptions, type InstallPathOptions, InstallProvider } from '@slack/oauth'; import Koa from 'koa'; @@ -36,6 +34,11 @@ export interface InstallerOptions { authorizationUrl?: InstallProviderOptions['authorizationUrl']; } +type CustomPropertiesExtractor = ( + request: BufferedIncomingMessage, + // biome-ignore lint/suspicious/noExplicitAny: custom app properties can be anything +) => Record; + export interface KoaReceiverOptions { signingSecret: string | (() => PromiseLike); logger?: Logger; @@ -52,10 +55,7 @@ export interface KoaReceiverOptions { installerOptions?: InstallerOptions; koa?: Koa; router?: Router; - customPropertiesExtractor?: ( - request: BufferedIncomingMessage, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ) => Record; + customPropertiesExtractor?: CustomPropertiesExtractor; processEventErrorHandler?: (args: ReceiverProcessEventErrorHandlerArgs) => Promise; // NOTE: As we use setTimeout under the hood, this cannot be async unhandledRequestHandler?: (args: ReceiverUnhandledRequestHandlerArgs) => void; @@ -77,10 +77,7 @@ export default class KoaReceiver implements Receiver { private unhandledRequestTimeoutMillis: number; - private customPropertiesExtractor: ( - request: BufferedIncomingMessage, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - ) => Record; + private customPropertiesExtractor: CustomPropertiesExtractor; private processEventErrorHandler: (args: ReceiverProcessEventErrorHandlerArgs) => Promise; @@ -189,8 +186,7 @@ export default class KoaReceiver implements Receiver { req, ); } catch (err) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const e = err as any; + const e = err as Error; if (this.signatureVerification) { this.logger.warn(`Failed to parse and verify the request data: ${e.message}`); } else { @@ -201,13 +197,12 @@ export default class KoaReceiver implements Receiver { } // Parse request body - // eslint-disable-next-line @typescript-eslint/no-explicit-any + // biome-ignore lint/suspicious/noExplicitAny: request bodies can be anything let body: any; try { body = httpFunc.parseHTTPRequestBody(bufferedReq); } catch (err) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const e = err as any; + const e = err as Error; this.logger.warn(`Malformed request body: ${e.message}`); httpFunc.buildNoBodyResponse(res, 400); return; diff --git a/examples/custom-receiver/src/fastify-main.ts b/examples/custom-receiver/src/fastify-main.ts index b9785a65e..f4a6ccbfa 100644 --- a/examples/custom-receiver/src/fastify-main.ts +++ b/examples/custom-receiver/src/fastify-main.ts @@ -1,16 +1,13 @@ -/* eslint-disable no-param-reassign */ -/* eslint-disable import/no-internal-modules */ -/* eslint-disable import/no-extraneous-dependencies */ -/* eslint-disable import/extensions */ -/* eslint-disable node/no-extraneous-import */ -/* eslint-disable import/no-extraneous-dependencies */ - import { App, FileInstallationStore } from '@slack/bolt'; import { ConsoleLogger, LogLevel } from '@slack/logger'; import { FileStateStore } from '@slack/oauth'; import Fastify from 'fastify'; import FastifyReceiver from './FastifyReceiver'; +if (!process.env.SLACK_SIGNING_SECRET) { + throw new Error('SLACK_SIGNING_SECRET environment variable not found!'); +} + const logger = new ConsoleLogger(); logger.setLevel(LogLevel.DEBUG); @@ -21,8 +18,7 @@ fastify.get('/', async (_, res) => { }); const receiver = new FastifyReceiver({ - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - signingSecret: process.env.SLACK_SIGNING_SECRET!, + signingSecret: process.env.SLACK_SIGNING_SECRET, clientId: process.env.SLACK_CLIENT_ID, clientSecret: process.env.SLACK_CLIENT_SECRET, scopes: ['commands', 'chat:write', 'app_mentions:read'], diff --git a/examples/custom-receiver/src/koa-main.ts b/examples/custom-receiver/src/koa-main.ts index 55a5871b3..8855b8703 100644 --- a/examples/custom-receiver/src/koa-main.ts +++ b/examples/custom-receiver/src/koa-main.ts @@ -1,10 +1,3 @@ -/* eslint-disable no-param-reassign */ -/* eslint-disable import/no-internal-modules */ -/* eslint-disable import/no-extraneous-dependencies */ -/* eslint-disable import/extensions */ -/* eslint-disable node/no-extraneous-import */ -/* eslint-disable import/no-extraneous-dependencies */ - import Router from '@koa/router'; import { App, FileInstallationStore } from '@slack/bolt'; import { ConsoleLogger, LogLevel } from '@slack/logger'; @@ -12,6 +5,10 @@ import { FileStateStore } from '@slack/oauth'; import Koa from 'koa'; import KoaReceiver from './KoaReceiver'; +if (!process.env.SLACK_SIGNING_SECRET) { + throw new Error('SLACK_SIGNING_SECRET environment variable not found!'); +} + const logger = new ConsoleLogger(); logger.setLevel(LogLevel.DEBUG); const koa = new Koa(); @@ -22,8 +19,7 @@ router.get('/', async (ctx) => { }); const receiver = new KoaReceiver({ - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - signingSecret: process.env.SLACK_SIGNING_SECRET!, + signingSecret: process.env.SLACK_SIGNING_SECRET, clientId: process.env.SLACK_CLIENT_ID, clientSecret: process.env.SLACK_CLIENT_SECRET, scopes: ['commands', 'chat:write', 'app_mentions:read'], diff --git a/examples/custom-receiver/tsconfig.eslint.json b/examples/custom-receiver/tsconfig.eslint.json deleted file mode 100644 index d19325c71..000000000 --- a/examples/custom-receiver/tsconfig.eslint.json +++ /dev/null @@ -1,13 +0,0 @@ -// This config is only used to allow ESLint to use a different include / exclude setting than the actual build -{ - // extend the build config to share compilerOptions - "extends": "./tsconfig.json", - "compilerOptions": { - // Setting "noEmit" prevents misuses of this config such as using it to produce a build - "noEmit": true - }, - "include": [ - // Since extending a config overwrites the entire value for "include", those value are copied here - "src/**/*", - ] -} diff --git a/examples/custom-receiver/tsconfig.json b/examples/custom-receiver/tsconfig.json index dd5eaaa80..80e839c68 100644 --- a/examples/custom-receiver/tsconfig.json +++ b/examples/custom-receiver/tsconfig.json @@ -1,16 +1,20 @@ { + "$schema": "https://json.schemastore.org/tsconfig", "compilerOptions": { - "target": "es6", - "module": "commonjs", - "moduleResolution": "node", + "allowJs": true, + "allowSyntheticDefaultImports": true, "esModuleInterop": true, + "module": "CommonJS", + "moduleResolution": "node", "resolveJsonModule": true, - "allowSyntheticDefaultImports": true, - "strict": true, - "allowJs": true, - "sourceMap": true, "rootDir": "src", + "skipLibCheck": true, + "sourceMap": true, + "strict": true, + "target": "es6", "outDir": "dist" }, - "include": ["src/**/*"] + "include": [ + "src/**/*" + ] } diff --git a/package.json b/package.json index c7ce39a81..5e8dbb097 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "prepare": "npm run build", "build": "npm run build:clean && tsc", "build:clean": "shx rm -rf ./dist ./coverage", - "lint": "npx @biomejs/biome check --write docs src test", + "lint": "npx @biomejs/biome check --write docs src test examples", "test": "npm run build && npm run lint && npm run test:types && npm run test:coverage", "test:unit": "TS_NODE_PROJECT=tsconfig.json mocha --config test/unit/.mocharc.json", "test:coverage": "c8 npm run test:unit",