Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Simplication of HTTPOptions across transport, batch transport and offline. #332

Merged
merged 10 commits into from
Oct 22, 2024
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
15 changes: 10 additions & 5 deletions lib/raygun.batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export class RaygunBatchTransport {

private batchId: number = 0;

private batchState: BatchState = { messages: [], messageSizeInBytes: 0 };
private batchState: BatchState = { messages: [],
messageSizeInBytes: 0 };

constructor(options: { interval: number; httpOptions: HTTPOptions }) {
constructor(options: { interval: number;
httpOptions: HTTPOptions; }) {
this.interval = options.interval;
this.httpOptions = options.httpOptions;
}
Expand Down Expand Up @@ -104,8 +106,10 @@ export class RaygunBatchTransport {
}

const promise = new Promise<IncomingMessage>((resolve, reject) => {
const promise = { resolve, reject };
this.batchState.messages.push({ serializedMessage, promise });
const promise = { resolve,
reject };
this.batchState.messages.push({ serializedMessage,
promise });
});

const batchIsFull = this.batchState.messages.length === 100;
Expand All @@ -130,7 +134,8 @@ export class RaygunBatchTransport {

this.sendBatch(batch);

this.batchState = { messages: [], messageSizeInBytes: 0 };
this.batchState = { messages: [],
messageSizeInBytes: 0 };

this.stopProcessing();
}
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
6 changes: 3 additions & 3 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,8 +65,10 @@ 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"
"tsdoc/syntax": "warn",
//"@stylistic/indent": "off",
}
}
);
Loading