From 5f181bc8e8873f78aa7fff29a5dd66d00e0c73ab Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Tue, 22 Oct 2024 22:51:54 +1300 Subject: [PATCH] feat: Simplication of HTTPOptions across transport, batch transport and offline. (#332) * Make HTTP in SendOptions optional * To avoid an error being displayed in the express sample when the provider is offline * Building up lighter SendOptions where possible * Prettier * Linting * Linting and Prettier * prettier takes care of indentation formatting, ignore rule in tseslint config * prettier run * remove commented out line * remove extra coma --------- Co-authored-by: Miguel Beltran --- examples/express-sample/routes/index.js | 2 +- lib/raygun.transport.ts | 10 +++--- lib/raygun.ts | 47 +++++++++++++++++-------- lib/types.ts | 2 +- tseslint.config.mjs | 3 +- 5 files changed, 41 insertions(+), 23 deletions(-) diff --git a/examples/express-sample/routes/index.js b/examples/express-sample/routes/index.js index 326270a..5826089 100644 --- a/examples/express-sample/routes/index.js +++ b/examples/express-sample/routes/index.js @@ -50,7 +50,7 @@ router.get("/send", function (req, res, next) { .then((message) => { res.render("send", { title: "Sent custom error to Raygun", - body: `Raygun status code: ${message.statusCode}`, + body: `Raygun status code: ${message?.statusCode}`, }); }) .catch((error) => { diff --git a/lib/raygun.transport.ts b/lib/raygun.transport.ts index 66c8f8d..a468118 100644 --- a/lib/raygun.transport.ts +++ b/lib/raygun.transport.ts @@ -39,21 +39,21 @@ export function send( const data = Buffer.from(options.message); const httpOptions = { - host: options.http.host || API_HOST, - port: options.http.port || 443, + host: options.http?.host || API_HOST, + port: options.http?.port || 443, path: path, method: "POST", headers: { Host: API_HOST, "Content-Type": "application/json", "Content-Length": data.length, - "X-ApiKey": options.http.apiKey, + "X-ApiKey": options.http?.apiKey, }, }; // Wrap HTTP request in Promise return new Promise((resolve, reject) => { - const httpLib = options.http.useSSL ? https : http; + const httpLib = options.http?.useSSL ? https : http; const request = httpLib.request( httpOptions, (response: IncomingMessage) => { @@ -67,7 +67,7 @@ export function send( }, ); - if (options.http.timeout) { + if (options.http?.timeout) { debug(`[raygun.transport.ts] Timeout set: ${options.http.timeout}ms`); request.setTimeout(options.http.timeout, () => { console.error( diff --git a/lib/raygun.ts b/lib/raygun.ts index 2470f00..91af36d 100644 --- a/lib/raygun.ts +++ b/lib/raygun.ts @@ -38,8 +38,16 @@ import * as raygunSyncTransport from "./raygun.sync.transport"; import { v4 as uuidv4 } from "uuid"; type SendOptionsResult = - | { valid: true; message: Message; options: SendOptions; skip: boolean } - | { valid: false; message: Message }; + | { + valid: true; + message: Message; + options: SendOptions; + skip: boolean; + } + | { + valid: false; + message: Message; + }; const debug = require("debug")("raygun"); @@ -562,7 +570,10 @@ class Raygun { const apiKey = this._apiKey; if (!apiKey) { - return { valid: false, message }; + return { + valid: false, + message, + }; } return { @@ -571,13 +582,17 @@ class Raygun { skip: skip, options: { message: JSON.stringify(message), - http: { - host: this._host, - port: this._port, - useSSL: !!this._useSSL, - apiKey: apiKey, - timeout: this._timeout || DEFAULT_TIMEOUT, - }, + ...(this._batch + ? {} + : { + http: { + host: this._host, + port: this._port, + useSSL: !!this._useSSL, + apiKey: apiKey, + timeout: this._timeout || DEFAULT_TIMEOUT, + }, + }), }, }; } @@ -597,12 +612,16 @@ class Raygun { transport .send({ message, - http: httpOptions, + ...(transport instanceof RaygunBatchTransport + ? {} + : { http: httpOptions }), }) .then((response) => { - debug( - `[raygun.ts] Sent message from offline transport: ${response}`, - ); + if (!(transport instanceof RaygunBatchTransport)) { + debug( + `[raygun.ts] Sent message from offline transport: ${response?.statusCode} ${response?.statusMessage}`, + ); + } }) .catch((error) => { console.error( diff --git a/lib/types.ts b/lib/types.ts index a071013..178d8e9 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -70,7 +70,7 @@ export type Tag = string; export type SendOptions = { message: string; - http: HTTPOptions; + http?: HTTPOptions; }; export type HTTPOptions = { diff --git a/tseslint.config.mjs b/tseslint.config.mjs index 36b3fed..f41363c 100644 --- a/tseslint.config.mjs +++ b/tseslint.config.mjs @@ -50,8 +50,6 @@ export default tseslint.config( "@stylistic/semi": ["error", "always"], // Stick to double quotes "@stylistic/quotes": ["error", "double"], - // Always indent with two spaces - '@stylistic/ts/indent': ['error', 2], // Enforce curly braces spacing "@stylistic/ts/object-curly-spacing": ["error", "always"], // Enforce "one true brace style" @@ -67,6 +65,7 @@ export default tseslint.config( "@stylistic/ts/no-extra-parens": ["off", 0], "@stylistic/ts/quote-props": ["off", 0], "@stylistic/ts/space-before-function-paren": ["off", 0], + "@stylistic/ts/indent": ["off", 0], // Documentation format check "tsdoc/syntax": "warn" }