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] Removing Axios for Native Fetch #1278

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"@sendgrid/mail": "^7.4.2",
"@types/pg": "^8.10.9",
"ajv": "^8.11.0",
"axios": "^0.27.2",
"boxen": "^5.0.0",
"chalk": "^4.1.2",
"chokidar": "^3.5.1",
Expand Down
4 changes: 2 additions & 2 deletions src/components/config/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
import fs from 'fs'
import type { MonikaFlags } from '../../flag'
import { log } from '../../utils/pino'
import { sendHttpRequest } from '../../utils/http'
import { sendHttpRequestFetch } from '../../utils/http'

export const createConfigFile = async (flags: MonikaFlags): Promise<string> => {
const filename = flags['config-filename']

try {
const url =
'https://raw.githubusercontent.com/hyperjumptech/monika/main/monika.example.yml'
await sendHttpRequest({ url }).then((resp) => {
await sendHttpRequestFetch({ url }).then((resp: any) => {
fs.writeFileSync(filename, resp.data, 'utf8')
})
log.info(
Expand Down
16 changes: 9 additions & 7 deletions src/components/config/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
* SOFTWARE. *
**********************************************************************************/

import { sendHttpRequest } from '../../utils/http'
import { sendHttpRequestFetch } from '../../utils/http'

export const fetchConfig = async (url: string): Promise<string> => {
try {
const { data } = await sendHttpRequest({ url })
return data
} catch {
throw new Error(`The configuration file in ${url} is unreachable. Please check the URL again or your internet connection.
`)
const resp = await sendHttpRequestFetch({ url })
if (!resp.ok) {
// does not throw error on 404
throw new Error(
`The configuration file in ${url} is unreachable. Please check the URL again or your internet connection.`
)
}

return resp.text()
}
19 changes: 8 additions & 11 deletions src/components/config/parse-insomnia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import type { Config } from '../../interfaces/config'
import yml from 'js-yaml'
import { compile as compileTemplate } from 'handlebars'
import type { AxiosRequestHeaders, Method } from 'axios'
import Joi from 'joi'

const envValidator = Joi.object({
Expand Down Expand Up @@ -143,20 +142,18 @@ function mapInsomniaToConfig(data: unknown): Config {
return { probes }
}

function mapInsomniaRequestToConfig(req: unknown) {
export function mapInsomniaRequestToConfig(req: unknown) {
const { value: res } = resourceValidator.validate(req, { allowUnknown: true })
// eslint-disable-next-line camelcase
const url = compileTemplate(res.url)({ base_url: baseUrl })
const authorization = getAuthorizationHeader(res)
let headers: AxiosRequestHeaders | undefined
if (authorization)
headers = {
authorization,
}

let headers: Headers | undefined
if (authorization) headers = new Headers({ Authorization: authorization })
if (res.headers) {
if (headers === undefined) headers = {}
if (headers === undefined) headers = new Headers()
for (const h of res.headers) {
headers[h.name] = h.value
headers.append(h.name, h.value)
}
}

Expand All @@ -167,7 +164,7 @@ function mapInsomniaRequestToConfig(req: unknown) {
requests: [
{
url,
method: (res?.method ?? 'GET') as Method,
method: res?.method ?? 'GET',
body: JSON.parse(res.body?.text ?? '{}'),
timeout: 10_000,
headers,
Expand All @@ -178,7 +175,7 @@ function mapInsomniaRequestToConfig(req: unknown) {
}
}

function getAuthorizationHeader(data: unknown): string | undefined {
export function getAuthorizationHeader(data: unknown): string | undefined {
const { value: res } = resourceValidator.validate(data, {
allowUnknown: true,
})
Expand Down
Loading