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

Refactor code base and tests to pave the way to migrate it to esm #7

Merged
merged 7 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion classes/Endpoint.class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const BasicEndpoint = require('./BasicEndpoint.class.js')
const lib = require('../lib/promise-data-to')
const lib = require('../lib/promise-data-to.js')

class Endpoint extends BasicEndpoint {
post (data, options) { return this.constructor.post(this, data, options) }
Expand Down
2 changes: 1 addition & 1 deletion classes/Headers.class.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require('../lib/object-polyfill')(Object)
require('../lib/object-polyfill.js')(Object)

class Headers extends Map {
constructor (entries) {
Expand Down
2 changes: 1 addition & 1 deletion classes/Response.class.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { decompressBuffer } = require('../lib/decompress-buffer')
const { decompressBuffer } = require('../lib/decompress-buffer.js')

class Response {
code = undefined
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Endpoint, GetEndpoint, PostEndpoint } = require('./classes/Endpoint.clas
const Headers = require('./classes/Headers.class.js')
const Response = require('./classes/Response.class.js')
const { parseEndpoints } = require('./lib/parse-endpoints.js')
const { promiseDataTo } = require('./lib/promise-data-to.js')

const urlToEndpoint = (url, headers, agent) => new Endpoint(url, headers || null, agent)

Expand All @@ -13,7 +14,7 @@ module.exports = {
Response,
parseEndpoints,
urlToEndpoint,
promiseDataTo: Endpoint.post,
promiseDataTo,
promiseGet: Endpoint.get,
promisePost: Endpoint.post,
fetch: Endpoint.fetch,
Expand Down
4 changes: 2 additions & 2 deletions lib/fetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Endpoint = require('../classes/Endpoint.class')
const httpClient = require('../')
const Endpoint = require('../classes/Endpoint.class.js')
const httpClient = require('../index.js')

const fetch = (url, options = {}) => {
const {
Expand Down
2 changes: 1 addition & 1 deletion lib/parse-endpoints.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { Endpoint } = require('../classes/Endpoint.class')
const { Endpoint } = require('../classes/Endpoint.class.js')

const validateEndpointProtocol = endpointKey => (urlString) => {
if (/^https?:/.test(urlString)) return true
Expand Down
36 changes: 1 addition & 35 deletions lib/parse-url.js
Original file line number Diff line number Diff line change
@@ -1,35 +1 @@
/*
const parseUrlOld = (urlString) => {
const u = new url.URL(urlString)
const result = {
href: u.href,
origin: u.origin,
protocol: u.protocol,
username: u.username,
password: u.password,
host: u.host,
hostname: u.hostname,
port: u.port,
pathname: u.pathname,
search: u.search,
searchParams: u.searchParams,
hash: u.hash,
slashes: true,

get auth () {
if (this.username) {
if (this.password) return `${this.username}:${this.password}`

return `${this.username}`
}

return `${this.password}`
},
get query () { return this.search.slice(1) },
get path () { return `${this.pathname}${this.search}` }
}

return result
} */

module.exports = require('../classes/EMUrl.class')
module.exports = require('../classes/EMUrl.class.js')
20 changes: 10 additions & 10 deletions lib/promise-data-to-refactored.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict'

const Endpoint = require('../classes/Endpoint.class')
const Headers = require('../classes/Headers.class')
const RequestError = require('../classes/Headers.class')
const logr = require('../lib/logr.js')
// const { addQueryToPath } = require('../lib/add-query-to-path')
const { simulatedResponse } = require('../lib/simulate-response')
// const { setResTxt } = require('../lib/set-response-text')
const { decompressBuffer } = require('../lib/decompress-buffer')
const { getDataFromXData } = require('../lib/get-data-from-xdata')
const { setHeaders } = require('../lib/set-headers')
const Endpoint = require('../classes/Endpoint.class.js')
const Headers = require('../classes/Headers.class.js')
const RequestError = require('../classes/Headers.class.js')
const logr = require('../lib/logr.js.js')
// const { addQueryToPath } = require('../lib/add-query-to-path.js')
const { simulatedResponse } = require('../lib/simulate-response.js')
// const { setResTxt } = require('../lib/set-response-text.js')
const { decompressBuffer } = require('../lib/decompress-buffer.js')
const { getDataFromXData } = require('../lib/get-data-from-xdata.js')
const { setHeaders } = require('../lib/set-headers.js')

const SIMULATE = +process.env.SIMULATE
const MAX_RETRY_ATTEMPTS = Math.abs(process.env.MAX_RETRY_ATTEMPTS) || 3
Expand Down
2 changes: 1 addition & 1 deletion lib/promise-get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const httpClient = require('../lib/promise-data-to')
const httpClient = require('../lib/promise-data-to.js')

const promiseGet = async (_endpoint) => {
const endpoint = httpClient.getEndpoint(_endpoint)
Expand Down
93 changes: 31 additions & 62 deletions package-lock.json

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

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@everymundo/http-client",
"version": "4.5.1",
"version": "4.6.0",
"description": "This is a helper to perform POST requests using promises and no external dependencies",
"main": "index.js",
"directories": {
Expand All @@ -19,6 +19,11 @@
"pre-push": "npm run cover && npm run check-coverage"
}
},
"standard": {
"env": {
"mocha": true
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/EveryMundo/http-client.git"
Expand All @@ -38,7 +43,7 @@
"homepage": "https://github.com/EveryMundo/http-client#readme",
"devDependencies": {
"c8": "^10.1.2",
"chai": "^4.5.0",
"chai": "^5.1.2",
"flatstr": "^1.0.12",
"ghooks": "^2.0.4",
"mocha": "^10.7.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'require strict'

/* eslint-env mocha */
/* eslint-disable import/no-unresolved, no-unused-expressions */
import sinon from 'sinon'
import { expect } from 'chai'
import http from 'http'

const sinon = require('sinon')
const { expect } = require('chai')
const http = require('http')
import BasicEndpoint from '../../classes/BasicEndpoint.class.js'
import EMUrl from '../../classes/EMUrl.class.js'

describe('classes/Endpoint', () => {
const BasicEndpoint = require('../../classes/BasicEndpoint.class')
const EMUrl = require('../../classes/EMUrl.class')

// eslint-disable-next-line one-var-declaration-per-line
let box
beforeEach(() => { box = sinon.createSandbox() })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
'require strict'

/* eslint-env mocha */
/* eslint-disable import/no-unresolved, no-unused-expressions */
import { createSandbox } from 'sinon'
import { expect } from 'chai'
import { Endpoint } from '../../classes/Endpoint.class.js'
import EMUrl from '../../classes/EMUrl.class.js'
import httpClient from '../../lib/promise-data-to.js'

describe('classes/Endpoint', () => {
const sinon = require('sinon')
const { expect } = require('chai')
const { Endpoint } = require('../../classes/Endpoint.class')
const EMUrl = require('../../classes/EMUrl.class')
const httpClient = require('../../lib/promise-data-to')

// eslint-disable-next-line one-var-declaration-per-line
let box
beforeEach(() => { box = sinon.createSandbox() })

beforeEach(() => { box = createSandbox() })
afterEach(() => { box.restore() })

describe('class Endpoint', () => {
Expand All @@ -32,7 +30,7 @@ describe('classes/Endpoint', () => {
const inputUrlString = 'https://head-url'
const expectedUrlString = 'https://head-url/'

context('instance httpMethods', () => {
describe('instance httpMethods', () => {
beforeEach(() => { box.stub(httpClient, 'promiseDataTo') })
// read methods
;['get', 'head'].forEach((method) => {
Expand Down Expand Up @@ -124,7 +122,7 @@ describe('classes/Endpoint', () => {
})
})

context.skip('static httpMethods', () => {
describe.skip('static httpMethods', () => {
beforeEach(() => { box.stub(httpClient, 'promiseDataTo') })

describe('#head', () => {
Expand Down
Loading
Loading