Skip to content

Commit

Permalink
fix(utils): fixed api utils errors when using full url
Browse files Browse the repository at this point in the history
  • Loading branch information
kilip committed Oct 24, 2023
1 parent 363df42 commit 058b635
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/pkg/github/api/search.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createMocks, createRequest } from 'node-mocks-http'
import { createMocks } from 'node-mocks-http'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import search from './search'
import { GitHubSearchParams } from '../types'
import { NextRequest, NextResponse } from 'next/server'
import { NextResponse } from 'next/server'
import fetchMock from 'fetch-mock'

describe('GET /api/github/repo', () => {
Expand Down
1 change: 0 additions & 1 deletion src/pkg/github/hooks/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default function useSearchRepos() {
method: 'GET',
}
)

return response
},
})
Expand Down
1 change: 0 additions & 1 deletion src/pkg/github/views/repo/RepoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export default function RepoCard({ repo }: Props) {
queryKey: [GitHub.search.queryKey],
})
} catch (e: any) {
console.log(e.message)
error(e.message)
}
setLoading(false)
Expand Down
28 changes: 13 additions & 15 deletions src/pkg/utils/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,28 @@ import { api } from './fetch'
import fetchMock from 'fetch-mock'

describe('fetch()', () => {
it('should fetch request', async() => {
fetchMock
.get('http://localhost:3000/some/api', {
status: 200,
body: {hello: 'world'}
})
it('should fetch request', async () => {
fetchMock.get('path:/some/api', {
status: 200,
body: { hello: 'world' },
})

const data = await api('/some/api', {
method: 'GET'
method: 'GET',
})

expect(data).toEqual({hello: 'world'})
expect(data).toEqual({ hello: 'world' })

fetchMock.reset()
})

it('throws FetchError during failed response', async() => {
fetchMock
.get('http://localhost:3000/test', () => {
return new Response(JSON.stringify({hello: 'world'}), {
status: 500,
statusText: 'Server down',
})
it('throws FetchError during failed response', async () => {
fetchMock.get('path:/test', () => {
return new Response(JSON.stringify({ hello: 'world' }), {
status: 500,
statusText: 'Server down',
})
})

expect(api('/test')).rejects.toThrowError()

Expand Down
6 changes: 1 addition & 5 deletions src/pkg/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
const BASE_URL = process.env.NEXTAUTH_ORIGIN || 'http://localhost:3000'

export interface FetchError {
message: string
status: number
data: object
}

export async function api<T>(url: string, init?: RequestInit): Promise<T> {
const fullUrl = new URL(`${BASE_URL}${url}`)
const resp = await fetch(fullUrl, init)
const resp = await fetch(url, init)
const text = await resp.text()
const json = await JSON.parse(text)

if (resp.ok) {
return json as T
}
Expand Down

0 comments on commit 058b635

Please sign in to comment.