Skip to content

Commit

Permalink
fixing up some examples, adding custom-receiver example to example TS…
Browse files Browse the repository at this point in the history
… integration CI check
  • Loading branch information
filmaj committed Sep 27, 2024
1 parent d716b7e commit 3dbf02d
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 87 deletions.
1 change: 1 addition & 0 deletions .github/workflows/samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-properties/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"license": "MIT",
"dependencies": {
"@slack/socket-mode": "^1.2.0"
"@slack/bolt": "^3"
}
}
26 changes: 13 additions & 13 deletions examples/custom-receiver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
29 changes: 12 additions & 17 deletions examples/custom-receiver/src/FastifyReceiver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Server } from 'http';
import type { Server } from 'node:http';
import {
type App,
type BufferedIncomingMessage,
Expand All @@ -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<string, any>;

export interface InstallerOptions {
stateStore?: InstallProviderOptions['stateStore']; // default ClearStateStore
stateVerification?: InstallProviderOptions['stateVerification']; // defaults true
Expand Down Expand Up @@ -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<string, any>;
customPropertiesExtractor?: CustomPropertiesExtractor;
processEventErrorHandler?: (args: ReceiverProcessEventErrorHandlerArgs) => Promise<boolean>;
// NOTE: As we use setTimeout under the hood, this cannot be async
unhandledRequestHandler?: (args: ReceiverUnhandledRequestHandlerArgs) => void;
Expand All @@ -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<string, any>;
private customPropertiesExtractor: CustomPropertiesExtractor;

private processEventErrorHandler: (args: ReceiverProcessEventErrorHandlerArgs) => Promise<boolean>;

Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down
27 changes: 11 additions & 16 deletions examples/custom-receiver/src/KoaReceiver.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';

Expand All @@ -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<string, any>;

export interface KoaReceiverOptions {
signingSecret: string | (() => PromiseLike<string>);
logger?: Logger;
Expand All @@ -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<string, any>;
customPropertiesExtractor?: CustomPropertiesExtractor;
processEventErrorHandler?: (args: ReceiverProcessEventErrorHandlerArgs) => Promise<boolean>;
// NOTE: As we use setTimeout under the hood, this cannot be async
unhandledRequestHandler?: (args: ReceiverUnhandledRequestHandlerArgs) => void;
Expand All @@ -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<string, any>;
private customPropertiesExtractor: CustomPropertiesExtractor;

private processEventErrorHandler: (args: ReceiverProcessEventErrorHandlerArgs) => Promise<boolean>;

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
14 changes: 5 additions & 9 deletions examples/custom-receiver/src/fastify-main.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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'],
Expand Down
14 changes: 5 additions & 9 deletions examples/custom-receiver/src/koa-main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/* 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';
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();
Expand All @@ -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'],
Expand Down
13 changes: 0 additions & 13 deletions examples/custom-receiver/tsconfig.eslint.json

This file was deleted.

20 changes: 12 additions & 8 deletions examples/custom-receiver/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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/**/*"
]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 3dbf02d

Please sign in to comment.