-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(env): loaded new environment variables * chore(bump): updated libs * docs: updated readme with sentry * test(validations): added few cases for joi and sentry * feat(improvements): added sentry
- Loading branch information
1 parent
635ffc3
commit eadb9cb
Showing
13 changed files
with
189 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
TURSO_URL='123' | ||
TURSO_DB_TOKEN='123' | ||
TURSO_DB_TOKEN='123' | ||
|
||
SENTRY_DSN='123' | ||
SENTRY_ENVIRONMENT='production' | ||
SENTRY_TRACES_SAMPLE_RATE='1.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,14 @@ | ||
import { SampleApp } from "src"; | ||
import { describe, it, expect, beforeAll } from 'bun:test'; | ||
import { sampleApp } from "src"; | ||
import { describe, it, expect } from 'bun:test'; | ||
|
||
describe("SampleApp Class", () => { | ||
describe("sampleApp Function", () => { | ||
|
||
/** | ||
* @type {SampleApp} | ||
*/ | ||
let sampleApp; | ||
|
||
beforeAll(() => { | ||
sampleApp = new SampleApp(); | ||
}); | ||
|
||
it("should be an instance of SampleApp", () => { | ||
expect(sampleApp).toBeInstanceOf(SampleApp); | ||
it("should be a function", () => { | ||
expect(sampleApp).toBeInstanceOf(Function); | ||
}); | ||
|
||
it("should have a method called 'sampleMethod'", () => { | ||
expect(sampleApp.sampleMethod).toBeInstanceOf(Function); | ||
expect(sampleApp.sampleMethod()).toBe("Hello World!"); | ||
expect(sampleApp).toBeInstanceOf(Function); | ||
expect(sampleApp()).toBe("Hello World!"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Joi from "joi"; | ||
import { validateSchema } from "../src/utils/validateSchema"; | ||
import { describe, it, expect } from 'bun:test'; | ||
|
||
describe("validateSchema", () => { | ||
it("should validate the configuration data against the schema", () => { | ||
const schema = Joi.object({ | ||
name: Joi.string().required(), | ||
age: Joi.number().integer().min(18).required(), | ||
email: Joi.string().email().required(), | ||
}); | ||
|
||
const data = { | ||
name: "John Doe", | ||
age: 25, | ||
email: "johndoe@example.com", | ||
}; | ||
|
||
const { error, value } = schema.validate(data); | ||
|
||
expect(error).toBeUndefined(); | ||
expect(value).toEqual(data); | ||
}); | ||
|
||
it("should throw an error if the configuration data is invalid", () => { | ||
const schema = Joi.object({ | ||
name: Joi.string().required(), | ||
age: Joi.number().integer().min(18).required().messages({ | ||
"number.base": "Invalid configuration: \"age\" must be a number", | ||
}), | ||
email: Joi.string().email().required(), | ||
}); | ||
|
||
const data = { | ||
name: "John Doe", | ||
age: {}, | ||
email: "johndoe@example.com", | ||
}; | ||
|
||
expect(() => { | ||
validateSchema(schema, data); | ||
}).toThrow("Invalid configuration: \"age\" must be a number"); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as Sentry from "@sentry/bun"; | ||
import { describe, it, expect, mock, jest } from 'bun:test'; | ||
import { sentryInit } from "src/config/sentry"; | ||
|
||
describe("sentryInit", () => { | ||
it("should initialize the Sentry SDK with the correct configuration", () => { | ||
|
||
mock.module("@sentry/bun", () => { | ||
return { | ||
init: jest.fn(), | ||
setTag: jest.fn(), | ||
setUser: jest.fn(), | ||
bunServerIntegration: jest.fn(), | ||
} | ||
}); | ||
|
||
const config = { | ||
sentry: { | ||
env: "production", | ||
dsn: "123", | ||
tracesSampleRate: 0.5, | ||
}, | ||
app: { | ||
name: "your-app-name", | ||
version: "1.0.0", | ||
}, | ||
}; | ||
|
||
sentryInit({ | ||
sentry: config.sentry, | ||
app: config.app | ||
}); | ||
|
||
expect(Sentry.init).toHaveBeenCalledWith({ | ||
dsn: config.sentry.dsn, | ||
environment: config.sentry.env, | ||
tracesSampleRate: config.sentry.tracesSampleRate, | ||
integrations: [Sentry.bunServerIntegration()], | ||
release: `${config.app.name}@${config.app.version}`, | ||
}); | ||
|
||
expect(Sentry.setTag).toHaveBeenCalledWith("app", config.app.name); | ||
expect(Sentry.setTag).toHaveBeenCalledWith("version", config.app.version); | ||
expect(Sentry.setUser).toHaveBeenCalledWith({ id: "1", username: "sample_user" }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
/** | ||
* Configuration file for the application. | ||
*/ | ||
export const config = { | ||
app: { | ||
name: process.env.npm_package_name, | ||
version: process.env.npm_package_version, | ||
}, | ||
db: { | ||
url: process.env.TURSO_URL, | ||
authToken: process.env.TURSO_DB_TOKEN, | ||
}, | ||
sentry: { | ||
dsn: process.env.SENTRY_DSN, | ||
tracesSampleRate: Number(process.env.SENTRY_TRACES_SAMPLE_RATE) || 1.0, | ||
env: process.env.SENTRY_ENVIRONMENT, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { config } from "#config"; | ||
import * as Sentry from "@sentry/bun"; | ||
|
||
/** | ||
* Initialize the Sentry SDK. | ||
* @param {Object} configuration - The configuration object. | ||
* @param {Object} configuration.sentry - The Sentry configuration object. | ||
* @param {string} configuration.sentry.env - The environment in which the app is running. | ||
* @param {string} configuration.sentry.dsn - The DSN for the Sentry project. | ||
* @param {number} configuration.sentry.tracesSampleRate - The traces sample rate. | ||
* @param {Object} configuration.app - The app configuration object. | ||
* @param {string | undefined} configuration.app.name - The name of the app. | ||
* @param {string | undefined} configuration.app.version - The version of the app. | ||
*/ | ||
export function sentryInit(configuration) { | ||
|
||
const { sentry, app } = configuration | ||
|
||
const isProduction = sentry.env === "production"; | ||
|
||
Sentry.init({ | ||
dsn: sentry.dsn, | ||
environment: sentry.env || "development", | ||
tracesSampleRate: sentry.tracesSampleRate, // Tracing | ||
integrations: [ | ||
Sentry.bunServerIntegration(), // Bun.Serve integration | ||
], | ||
release: isProduction ? `${app.name}@${app.version}` : undefined, | ||
}); | ||
|
||
Sentry.setTag("app", app.name); | ||
Sentry.setTag("version", app.version); | ||
Sentry.setUser({ id: "1", username: "sample_user" }); // Gonna be replaced with actual user data | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Joi from "joi"; | ||
import * as Sentry from "@sentry/bun"; | ||
/** | ||
* Validate the configuration against the schema. | ||
* @param {Joi.Schema} schema - The schema to validate against. | ||
* @param {Object} data - The configuration data to validate. | ||
* @returns {Object} The validated configuration data. | ||
*/ | ||
export function validateSchema(schema, data) { | ||
const { error, value } = schema.validate(data); | ||
if (error) { | ||
Sentry.captureException({ | ||
message: "Invalid configuration", | ||
error, | ||
}); | ||
throw new Error(`Invalid configuration: ${error.message}`); | ||
} | ||
return value; | ||
}; |