Skip to content

Commit 5f181bc

Browse files
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 <m@beltran.work>
1 parent 7dccce3 commit 5f181bc

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

examples/express-sample/routes/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ router.get("/send", function (req, res, next) {
5050
.then((message) => {
5151
res.render("send", {
5252
title: "Sent custom error to Raygun",
53-
body: `Raygun status code: ${message.statusCode}`,
53+
body: `Raygun status code: ${message?.statusCode}`,
5454
});
5555
})
5656
.catch((error) => {

lib/raygun.transport.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ export function send(
3939
const data = Buffer.from(options.message);
4040

4141
const httpOptions = {
42-
host: options.http.host || API_HOST,
43-
port: options.http.port || 443,
42+
host: options.http?.host || API_HOST,
43+
port: options.http?.port || 443,
4444
path: path,
4545
method: "POST",
4646
headers: {
4747
Host: API_HOST,
4848
"Content-Type": "application/json",
4949
"Content-Length": data.length,
50-
"X-ApiKey": options.http.apiKey,
50+
"X-ApiKey": options.http?.apiKey,
5151
},
5252
};
5353

5454
// Wrap HTTP request in Promise
5555
return new Promise((resolve, reject) => {
56-
const httpLib = options.http.useSSL ? https : http;
56+
const httpLib = options.http?.useSSL ? https : http;
5757
const request = httpLib.request(
5858
httpOptions,
5959
(response: IncomingMessage) => {
@@ -67,7 +67,7 @@ export function send(
6767
},
6868
);
6969

70-
if (options.http.timeout) {
70+
if (options.http?.timeout) {
7171
debug(`[raygun.transport.ts] Timeout set: ${options.http.timeout}ms`);
7272
request.setTimeout(options.http.timeout, () => {
7373
console.error(

lib/raygun.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@ import * as raygunSyncTransport from "./raygun.sync.transport";
3838
import { v4 as uuidv4 } from "uuid";
3939

4040
type SendOptionsResult =
41-
| { valid: true; message: Message; options: SendOptions; skip: boolean }
42-
| { valid: false; message: Message };
41+
| {
42+
valid: true;
43+
message: Message;
44+
options: SendOptions;
45+
skip: boolean;
46+
}
47+
| {
48+
valid: false;
49+
message: Message;
50+
};
4351

4452
const debug = require("debug")("raygun");
4553

@@ -562,7 +570,10 @@ class Raygun {
562570
const apiKey = this._apiKey;
563571

564572
if (!apiKey) {
565-
return { valid: false, message };
573+
return {
574+
valid: false,
575+
message,
576+
};
566577
}
567578

568579
return {
@@ -571,13 +582,17 @@ class Raygun {
571582
skip: skip,
572583
options: {
573584
message: JSON.stringify(message),
574-
http: {
575-
host: this._host,
576-
port: this._port,
577-
useSSL: !!this._useSSL,
578-
apiKey: apiKey,
579-
timeout: this._timeout || DEFAULT_TIMEOUT,
580-
},
585+
...(this._batch
586+
? {}
587+
: {
588+
http: {
589+
host: this._host,
590+
port: this._port,
591+
useSSL: !!this._useSSL,
592+
apiKey: apiKey,
593+
timeout: this._timeout || DEFAULT_TIMEOUT,
594+
},
595+
}),
581596
},
582597
};
583598
}
@@ -597,12 +612,16 @@ class Raygun {
597612
transport
598613
.send({
599614
message,
600-
http: httpOptions,
615+
...(transport instanceof RaygunBatchTransport
616+
? {}
617+
: { http: httpOptions }),
601618
})
602619
.then((response) => {
603-
debug(
604-
`[raygun.ts] Sent message from offline transport: ${response}`,
605-
);
620+
if (!(transport instanceof RaygunBatchTransport)) {
621+
debug(
622+
`[raygun.ts] Sent message from offline transport: ${response?.statusCode} ${response?.statusMessage}`,
623+
);
624+
}
606625
})
607626
.catch((error) => {
608627
console.error(

lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export type Tag = string;
7070

7171
export type SendOptions = {
7272
message: string;
73-
http: HTTPOptions;
73+
http?: HTTPOptions;
7474
};
7575

7676
export type HTTPOptions = {

tseslint.config.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ export default tseslint.config(
5050
"@stylistic/semi": ["error", "always"],
5151
// Stick to double quotes
5252
"@stylistic/quotes": ["error", "double"],
53-
// Always indent with two spaces
54-
'@stylistic/ts/indent': ['error', 2],
5553
// Enforce curly braces spacing
5654
"@stylistic/ts/object-curly-spacing": ["error", "always"],
5755
// Enforce "one true brace style"
@@ -67,6 +65,7 @@ export default tseslint.config(
6765
"@stylistic/ts/no-extra-parens": ["off", 0],
6866
"@stylistic/ts/quote-props": ["off", 0],
6967
"@stylistic/ts/space-before-function-paren": ["off", 0],
68+
"@stylistic/ts/indent": ["off", 0],
7069
// Documentation format check
7170
"tsdoc/syntax": "warn"
7271
}

0 commit comments

Comments
 (0)