Skip to content

Commit

Permalink
Merge pull request #11 from AthennaIO/develop
Browse files Browse the repository at this point in the history
chore(helpers): little improvements
  • Loading branch information
jlenon7 authored Jan 14, 2023
2 parents b6d9da1 + 75273ad commit a4310a3
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 22 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "3.0.0",
"version": "3.0.1",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -141,6 +141,9 @@
"es2021": true,
"node": true
},
"globals": {
"Path": true
},
"plugins": [
"prettier"
],
Expand Down
56 changes: 47 additions & 9 deletions src/Helpers/Exception.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-extend-native */
/**
* @athenna/common
*
Expand Down Expand Up @@ -114,19 +115,56 @@ export class Exception extends Error {
displayMainFrameOnly: false,
})

const message = `${chalk.yellow.bold('MESSAGE')}\n ${this.message}`
const help = ` ${chalk.green.bold('HELP')}\n ${this.help}`

this.name = this.code
const helpKey = chalk.green.bold('HELP')
const messageKey = chalk.yellow.bold('MESSAGE')

if (this.message && this.message !== '') {
this.message = `${messageKey}\n ${this.message}`
}

if (this.help) {
this.message = `${message}\n\n${help}`
} else {
this.message = `${message}`
if (this.help && this.help !== '') {
this.message = `${this.message}\n\n ${helpKey}\n ${this.help}`
}

const jsonResponse = await new Youch(this, {}).toJSON()
const pretty = await new Youch(this, {}).toJSON()

return YouchTerminal(jsonResponse, options).concat('\n')
if (!pretty.error.frames.find(frame => frame.isApp)) {
pretty.error.frames = pretty.error.frames.map(frame => {
frame.isApp = true
return frame
})
}

return YouchTerminal(pretty, options).concat('\n')
}
}

/**
* Transform your error to an instance of
* the Athenna exception.
*
* @param options {{
* code?: string,
* status?: number,
* content?: string,
* help?: string,
* stack?: any
* }}
* @return {Exception}
*/
Error.prototype.toAthennaException = function (options) {
const { content, status, code, help, stack } = Options.create(options, {
content: this.message,
status: 0,
code: this.name,
help: undefined,
stack: this.stack,
})

const exception = new Exception(content, status, code, help)

exception.stack = stack

return exception
}
18 changes: 18 additions & 0 deletions src/Helpers/FakeApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import { Debug } from '#src/Helpers/Debug'
import { Folder } from '#src/Helpers/Folder'

export class FakeApi {
/**
* Set if the FakeApi server is running.
*
* @type {boolean}
*/
static #isRunning = false

/**
* Create the fastify server with plugins.
*
Expand Down Expand Up @@ -49,6 +56,15 @@ export class FakeApi {
return app.printRoutes()
}

/**
* List the routes registered in the fake server.
*
* @return {boolean}
*/
static isRunning() {
return this.#isRunning
}

/**
* Register all routes inside folder path
* and start the fake api server at port 8989.
Expand All @@ -63,6 +79,7 @@ export class FakeApi {
}

await app.listen({ port })
this.#isRunning = true
}

/**
Expand All @@ -74,6 +91,7 @@ export class FakeApi {
await app.close()

app = FakeApi.recreate()
this.#isRunning = false
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/Helpers/HttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,15 +634,13 @@ export class HttpClientBuilder {
}

/**
* Alias for the searchParameters method.
* Alias for the searchParams method.
*
* @param value { string | import('got').SearchParameters | URLSearchParams }
* @return {HttpClientBuilder}
*/
searchParameters(value) {
this.#options.searchParameters = value

return this
queryParams(value) {
return this.searchParams(value)
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/Helpers/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ export class Path {
*/
static defaultBeforePath = ''

/**
* Resolve the environment where the application
* is running by verifying the import.meta.url.
*
* This method will auto set the IS_TS env and the
* defaultBeforePath if IS_TS is true.
*
* The beforePath is always set as '/build' by default.
*
* @param metaUrl {string}
* @param beforePath {string}
* @return {typeof Path}
*/
static resolveEnvironment(metaUrl, beforePath = '/build') {
const isTs = metaUrl.endsWith('.ts') ? 'true' : 'false'

process.env.IS_TS = isTs
this.defaultBeforePath = isTs === 'true' ? beforePath : ''

return this
}

/**
* Return js or ts extension depending on IS_TS.
*
Expand Down
Loading

0 comments on commit a4310a3

Please sign in to comment.