Skip to content

Commit ad39534

Browse files
committed
Erstatt manuelle post fetchers med felles funksjon
1 parent 19dbac4 commit ad39534

4 files changed

Lines changed: 13 additions & 60 deletions

File tree

apps/dolly-frontend/src/main/js/src/api/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export const multiFetcherInst = (miljoUrlListe, headers = null, path = null) =>
3535
),
3636
)
3737

38-
export const postFetcher = (url, body) =>
38+
export const postFetcher = (url, body, headers = { 'Content-Type': 'application/json' }) =>
3939
axios
40-
.post(url, body, { headers: { 'Content-Type': 'application/json' } })
40+
.post(url, body, { headers: headers })
4141
.then((res) => {
4242
if (res.status === 404) {
4343
return null

apps/dolly-frontend/src/main/js/src/utils/hooks/useKelvin.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import useSWR from 'swr'
2-
import axios from 'axios'
2+
import { postFetcher } from '@/api'
33

44
const kelvinAapBaseUrl = '/testnav-dolly-proxy/kelvin-aap/api/test/'
55

@@ -8,25 +8,7 @@ export const useKelvinAapBehandlingStatus = (ident: string, harKelvinAapBestilli
88

99
const { data, isLoading, error } = useSWR<any, Error>(
1010
shouldFetch ? [`${kelvinAapBaseUrl}behandlingStatus`, ident] : null,
11-
async ([url, ident]: [string, string]) => {
12-
try {
13-
const res = await axios.post(
14-
url,
15-
{ ident },
16-
{
17-
headers: {
18-
'Content-Type': 'application/json',
19-
},
20-
},
21-
)
22-
return res.data
23-
} catch (e: unknown) {
24-
if (axios.isAxiosError(e) && e.response?.status === 404) {
25-
return null
26-
}
27-
throw e
28-
}
29-
},
11+
([url, ident]: [string, string]) => postFetcher(url, { ident }),
3012
{ errorRetryCount: 0, revalidateOnFocus: false },
3113
)
3214

apps/dolly-frontend/src/main/js/src/utils/hooks/useNom.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
import useSWR from 'swr'
2-
import axios from 'axios'
2+
import { postFetcher } from '@/api'
33

44
const baseUrl = '/testnav-nom-proxy'
55

66
export const useNomData = (ident: string) => {
77
const { data, isLoading, error } = useSWR<any, Error>(
88
ident ? [`${baseUrl}/api/v1/dolly/hentRessurs`, ident] : null,
9-
async ([url, identValue]: [string, string]) => {
10-
try {
11-
const res = await axios.post(url, identValue, {
12-
headers: {
13-
'Content-Type': 'text/plain',
14-
},
15-
})
16-
return res.data
17-
} catch (e: any) {
18-
if (axios.isAxiosError(e) && e.response?.status === 404) {
19-
return null
20-
}
21-
throw e
22-
}
23-
},
9+
([url, identValue]: [string, string]) =>
10+
postFetcher(url, identValue, {
11+
'Content-Type': 'text/plain',
12+
}),
2413
{ errorRetryCount: 0, revalidateOnFocus: false },
2514
)
2615

apps/dolly-frontend/src/main/js/src/utils/hooks/useSkattekort.tsx

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import useSWR from 'swr'
2-
import axios from 'axios'
2+
import { postFetcher } from '@/api'
33

4-
const skattekortUrl = (miljoe: string) =>
5-
`/testnav-dolly-proxy/skattekort/${miljoe}/api/v1/person`
4+
const skattekortUrl = (miljoe: string) => `/testnav-dolly-proxy/skattekort/${miljoe}/api/v1/person`
65

76
const SKATTEKORT_MILJOER = ['q1', 'q2']
87

@@ -71,25 +70,8 @@ const SKATTEKORT_KODEVERK: Record<string, Record<string, string>> = {
7170
},
7271
}
7372

74-
const fetchSkattekortFromMiljoe = async (miljoe: string, fnr: string) => {
75-
try {
76-
const res = await axios.post(
77-
`${skattekortUrl(miljoe)}/hent-skattekort`,
78-
{ fnr },
79-
{
80-
headers: {
81-
'Content-Type': 'application/json',
82-
},
83-
},
84-
)
85-
return res.data || []
86-
} catch (e: unknown) {
87-
if (axios.isAxiosError(e) && e.response?.status === 404) {
88-
return []
89-
}
90-
throw e
91-
}
92-
}
73+
const fetchSkattekortFromMiljoe = async (miljoe: string, fnr: string) =>
74+
postFetcher(`${skattekortUrl(miljoe)}/hent-skattekort`, { fnr })
9375

9476
interface MiljoSkattekortData {
9577
miljo: string

0 commit comments

Comments
 (0)