Skip to content

Commit

Permalink
feat: deprecate http module in favor of fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
afuh committed Dec 3, 2022
1 parent 015cc3f commit 94fb80a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 61 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,9 @@
## 2.0.0

### Breaking Changes
- Only Node 18 is supported.
- Use `fetch` API ([experimental](https://nodejs.org/en/blog/announcements/v18-release-announce/#fetch-experimental) in Node 18) instead of the `https` module. This makes it easier to use in applications running Webpack 5.

## 1.0.0

### New Features
Expand Down
16 changes: 5 additions & 11 deletions src/character/character.test.ts
Expand Up @@ -13,13 +13,13 @@ describe('getCharacters', () => {
test('get all', async () => {
const res = await getCharacters()

expect(res.data.results.length).toBe(20)
expect(res.data.results?.length).toBe(20)
})

test('get by filter', async () => {
const res = await getCharacters({ name: 'Rick', status: 'Alive' })

res.data.results.forEach((item) => {
res.data.results?.forEach((item) => {
expect(item.name.includes('Rick')).toBe(true)
expect(item.status).toBe('Alive')
})
Expand All @@ -28,7 +28,7 @@ describe('getCharacters', () => {
test('pagination', async () => {
const res = await getCharacters({ page: 2 })

res.data.info.prev.includes('page=1')
res.data.info?.prev?.includes('page=1')
})
})

Expand All @@ -41,13 +41,7 @@ describe('getCharacter', () => {

test('get by IDs', async () => {
const res = await getCharacter([1, 2])

expect(res.data.map(({ id }: { id: number }) => id)).toStrictEqual([1, 2])
})

test('empty array', async () => {
const res = await getCharacter([])

expect(res.data).toStrictEqual([])
expect(res.data[0].id).toBe(1)
expect(res.data[1].id).toBe(2)
})
})
16 changes: 5 additions & 11 deletions src/episode/episode.test.ts
Expand Up @@ -13,21 +13,21 @@ describe('getEpisodes', () => {
test('get all', async () => {
const res = await getEpisodes()

expect(res.data.results.length).toBe(20)
expect(res.data.results?.length).toBe(20)
})

test('get by filter', async () => {
const res = await getEpisodes({ episode: 'S01E01' })

res.data.results.forEach((item) => {
res.data.results?.forEach((item) => {
expect(item.episode.includes('S01E01')).toBe(true)
})
})

test('pagination', async () => {
const res = await getEpisodes({ page: 2 })

expect(res.data.info.prev.includes('page=1')).toBe(true)
expect(res.data.info?.prev?.includes('page=1')).toBe(true)
})
})

Expand All @@ -40,13 +40,7 @@ describe('getEpisode', () => {

test('get by IDs', async () => {
const res = await getEpisode([1, 2])

expect(res.data.map(({ id }: { id: number }) => id)).toStrictEqual([1, 2])
})

test('empty array', async () => {
const res = await getEpisode([])

expect(res.data).toStrictEqual([])
expect(res.data[0].id).toBe(1)
expect(res.data[1].id).toBe(2)
})
})
16 changes: 5 additions & 11 deletions src/location/location.test.ts
Expand Up @@ -13,21 +13,21 @@ describe('getLocations', () => {
test('get all', async () => {
const res = await getLocations()

expect(res.data.results.length).toBe(20)
expect(res.data.results?.length).toBe(20)
})

test('get by filter', async () => {
const res = await getLocations({ dimension: 'C-137' })

res.data.results.forEach((item) => {
res.data.results?.forEach((item) => {
expect(item.dimension.includes('C-137')).toBe(true)
})
})

test('pagination', async () => {
const res = await getLocations({ page: 2 })

expect(res.data.info.prev.includes('page=1')).toBe(true)
expect(res.data.info?.prev?.includes('page=1')).toBe(true)
})
})

Expand All @@ -40,13 +40,7 @@ describe('getLocation', () => {

test('get by IDs', async () => {
const res = await getLocation([1, 2])

expect(res.data.map(({ id }: { id: number }) => id)).toStrictEqual([1, 2])
})

test('empty array', async () => {
const res = await getLocation([])

expect(res.data).toStrictEqual([])
expect(res.data[0].id).toBe(1)
expect(res.data[1].id).toBe(2)
})
})
47 changes: 19 additions & 28 deletions src/utils/get.ts
@@ -1,31 +1,22 @@
import https from 'https'

const BASE_URL = 'https://rickandmortyapi.com/api'

const get = (endpoint: string): Promise<unknown> => {
return new Promise((resolve, reject) => {
const url = `${BASE_URL}/${endpoint}`

https.get(url, (response) => {
response.setEncoding('utf8')
let body = ''

response.on('data', (data) => (body += data))

response.on('error', (error) => reject(error))

response.on('end', () => {
const isStatusOk = response.statusCode && response.statusCode >= 200 && response.statusCode < 300
const data = JSON.parse(body)

resolve({
data: isStatusOk ? data : {},
status: response.statusCode as number,
statusMessage: !isStatusOk && data.error ? data.error : response.statusMessage,
})
})
})
})
const get = async (endpoint: string): Promise<{ data: unknown; status: number; statusMessage: string }> => {
const res = await fetch(`https://rickandmortyapi.com/api/${endpoint}`)

// response.status >= 200 && response.status < 300
if (res.ok) {
const data = await res.json()

return {
data,
status: res.status,
statusMessage: res.statusText,
}
}

return {
data: {},
status: res.status,
statusMessage: res.statusText,
}
}

export default get

0 comments on commit 94fb80a

Please sign in to comment.