Skip to content

Commit

Permalink
Move myFetch to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
leung018 committed Aug 15, 2024
1 parent 7b051ea commit 4e06b9c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
18 changes: 1 addition & 17 deletions frontend/src/services/duty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Duty } from '../models/duty'
import { myFetch } from './fetch'

export interface DutyRemoteService {
createDuty: (name: string) => Promise<Duty>
Expand All @@ -7,23 +8,6 @@ export interface DutyRemoteService {
completeDuty: (dutyId: string) => Promise<void>
}

function myFetch(url: string, init?: RequestInit) {
return fetch(url, init)
.then((res) => {
if (!res.ok) {
// TODO: Currently frontend already guards the case for bad request. If future display error message is needed, we can add more specific error handling here.
throw new Error('Unexpected error')
}
const contentType = res.headers.get('Content-Type')
if (contentType && contentType.includes('application/json')) {
return res.json()
}
})
.catch(() => {
throw new Error('Unable to interact with server')
})
}

export class DutyRemoteServiceImpl implements DutyRemoteService {
private readonly apiEndpoint: string

Expand Down
16 changes: 16 additions & 0 deletions frontend/src/services/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function myFetch(url: string, init?: RequestInit) {
return fetch(url, init)
.then((res) => {
if (!res.ok) {
// TODO: Currently frontend already guards the case for bad request. If future display error message is needed, we can add more specific error handling here.
throw new Error('Unexpected error')
}
const contentType = res.headers.get('Content-Type')
if (contentType && contentType.includes('application/json')) {
return res.json()
}
})
.catch(() => {
throw new Error('Unable to interact with server')
})
}

0 comments on commit 4e06b9c

Please sign in to comment.