Skip to content

Commit

Permalink
added delete use case
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsalvadorpp authored and danielsan committed Oct 25, 2024
1 parent 85c2b8a commit 9617ecf
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const res = await httpClient.post(endpoint, data)


#### Write methods
If you want to use PUT or PATCH, just pass the method name on the options object, don't forget to use `.Endpoint` when creating the endpoint object
If you want to use PUT, PATCH or DELETE, just pass the method name on the options object, don't forget to use `.Endpoint` when creating the endpoint object
```js
const httpClient = require('@everymundo/http-client')

Expand Down
7 changes: 7 additions & 0 deletions classes/Endpoint.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class Endpoint extends BasicEndpoint {
return lib.promiseDataTo(url, data, options)
}

delete (data, options) { return this.constructor.delete(this, data, options) }
static delete (url, data, options = { method: null }) {
options.method = 'DELETE'

return lib.promiseDataTo(url, data, options)
}

head (options) { return this.constructor.head(this, options) }
static head (url, options = { method: null }) {
options.method = 'HEAD'
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ module.exports = {
post: Endpoint.post,
patch: Endpoint.patch,
put: Endpoint.put,
head: Endpoint.head
head: Endpoint.head,
delete: Endpoint.delete
}
2 changes: 1 addition & 1 deletion lib/promise-data-to-refactored.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const MAX_RETRY_ATTEMPTS = Math.abs(process.env.MAX_RETRY_ATTEMPTS) || 3
const RETRY_TIMEOUT_MS = process.env.RETRY_TIMEOUT_MS && Math.abs(process.env.RETRY_TIMEOUT_MS)
const REQUEST_TIMEOUT_MS = process.env.REQUEST_TIMEOUT_MS && Math.abs(process.env.REQUEST_TIMEOUT_MS)

const writeMethods = new Set(['PUT', 'PATCH', 'POST'])
const writeMethods = new Set(['PUT', 'PATCH', 'POST', 'DELETE'])

const getEndpoint = (endpoint) => {
if (!endpoint) {
Expand Down
2 changes: 1 addition & 1 deletion lib/promise-data-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { getProperDataFromInputData } = require('../lib/get-data-from-xdata.js')
const { setHeaders } = require('../lib/set-headers.js')
const config = require('../lib/config.js')

const writeMethods = new Set(['PUT', 'PATCH', 'POST'])
const writeMethods = new Set(['PUT', 'PATCH', 'POST', 'DELETE'])

const getEndpoint = (endpoint) => {
if (!endpoint) {
Expand Down
2 changes: 1 addition & 1 deletion test/classes/Endpoint.class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('classes/Endpoint', () => {
})

// write methods
;['patch', 'put', 'post'].forEach(method => {
;['patch', 'put', 'post', 'delete'].forEach(method => {
describe(`#${method}`, () => {
const upMethod = method.toUpperCase()
it(`should call promiseDataTo with method ${upMethod}`, async () => {
Expand Down
9 changes: 8 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ describe('index.js', () => {
post: Function,
patch: Function,
put: Function,
head: Function
head: Function,
delete: Function
}

it('should export the expected keys', () => {
Expand Down Expand Up @@ -69,6 +70,12 @@ describe('index.js', () => {
})
})

describe('#delete', () => {
it('should equal Endpoint.delete', () => {
expect(lib.delete).to.equal(lib.Endpoint.delete)
})
})

describe('#post', () => {
it('should equal Endpoint.post', () => {
expect(lib.post).to.equal(lib.Endpoint.post)
Expand Down

0 comments on commit 9617ecf

Please sign in to comment.