diff --git a/package-lock.json b/package-lock.json index 08a6f78..c197640 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@envoy/envoy-integrations-sdk", - "version": "2.3.6", + "version": "2.4.0-beta.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@envoy/envoy-integrations-sdk", - "version": "2.3.6", + "version": "2.4.0-beta.5", "license": "ISC", "dependencies": { "@types/dotenv": "^8.2.0", diff --git a/package.json b/package.json index 3130d13..aa4f51d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@envoy/envoy-integrations-sdk", - "version": "2.3.6", + "version": "2.4.0-beta.5", "description": "SDK for building Envoy integrations.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/sdk/middleware.ts b/src/sdk/middleware.ts index f2f31f5..878286e 100644 --- a/src/sdk/middleware.ts +++ b/src/sdk/middleware.ts @@ -9,6 +9,16 @@ import EnvoyPluginJobAttachment from './EnvoyPluginJobAttachment'; import EnvoyPluginSDK from './EnvoyPluginSDK'; import EnvoyPluginAPI from './EnvoyPluginAPI'; +/** + * Options for configuring the Envoy middleware. + */ +export interface EnvoyMiddlewareOptions extends Partial { + /** Optional custom client ID to use for API authentication instead of environment variable */ + customClientId?: string; + /** Optional custom client secret to use for API authentication instead of environment variable */ + customClientSecret?: string; +} + /** * Sets up an {@link EnvoyPluginSDK} object in the path `req.envoy`. * Modifies the `res` object to include Envoy's helpers, per {@link EnvoyResponse}. @@ -18,8 +28,8 @@ import EnvoyPluginAPI from './EnvoyPluginAPI'; * * @category Middleware */ -export function envoyMiddleware(options?: EnvoySignatureVerifierOptions): RequestHandler { - const signatureVerifier = new EnvoySignatureVerifier(options); +export function envoyMiddleware(options?: EnvoyMiddlewareOptions): RequestHandler { + const signatureVerifier = new EnvoySignatureVerifier(options as EnvoySignatureVerifierOptions); const verify = (req: VerifiedRequest, res: Response, rawBody: Buffer) => { req[VERIFIED] = signatureVerifier.verify(req, rawBody); }; @@ -35,7 +45,10 @@ export function envoyMiddleware(options?: EnvoySignatureVerifierOptions): Reques try { const now = Date.now(); if (now > threshold) { - const { access_token: rawAccessToken, expires_in: expiresIn } = await EnvoyPluginAPI.loginAsPlugin(); + const hasCustomCredentials = options?.customClientId && options?.customClientSecret; + const { access_token: rawAccessToken, expires_in: expiresIn } = hasCustomCredentials + ? await EnvoyPluginAPI.loginAsPlugin(options?.customClientId, options?.customClientSecret) + : await EnvoyPluginAPI.loginAsPlugin(); accessToken = rawAccessToken; threshold = now + expiresIn * 1000 - 1000 * 60 * 10; } @@ -101,3 +114,9 @@ export function errorMiddleware(onError: (err: Error) => void = () => {}): Error res.end(JSON.stringify({ message: err.message })); }; } + +/** + * Backward compatibility alias for envoyMiddleware + * @deprecated Use envoyMiddleware instead + */ +export const middleware = envoyMiddleware;