Skip to content

Commit

Permalink
feat: Simplication of HTTPOptions across transport, batch transport a…
Browse files Browse the repository at this point in the history
…nd 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 <[email protected]>
  • Loading branch information
TheRealAgentK and miquelbeltran authored Oct 22, 2024
1 parent 7dccce3 commit 5f181bc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/express-sample/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
10 changes: 5 additions & 5 deletions lib/raygun.transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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(
Expand Down
47 changes: 33 additions & 14 deletions lib/raygun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -562,7 +570,10 @@ class Raygun {
const apiKey = this._apiKey;

if (!apiKey) {
return { valid: false, message };
return {
valid: false,
message,
};
}

return {
Expand All @@ -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,
},
}),
},
};
}
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type Tag = string;

export type SendOptions = {
message: string;
http: HTTPOptions;
http?: HTTPOptions;
};

export type HTTPOptions = {
Expand Down
3 changes: 1 addition & 2 deletions tseslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
Expand Down

0 comments on commit 5f181bc

Please sign in to comment.