Skip to content

Commit

Permalink
Merge dependabot/npm_and_yarn/vite-5.3.5 into combine-prs-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Aug 1, 2024
2 parents 2da93c4 + c818361 commit 4fca367
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 28 deletions.
48 changes: 27 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"preview": "vite preview"
},
"dependencies": {
"@amsterdam/design-system-react": "^0.10.0",
"@amsterdam/design-system-react": "^0.11.0",
"@types/lodash.get": "^4.4.9",
"@types/qs": "^6.9.15",
"@types/react": "^18.3.3",
Expand All @@ -36,7 +36,7 @@
"styled-components": "^6.1.11",
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"vite": "^5.3.1"
"vite": "^5.3.5"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^7.13.1",
Expand Down
3 changes: 2 additions & 1 deletion src/app/state/rest/cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const useCases = (options?: Options) => {
...options,
url: `${ makeApiUrl("cases") }`,
groupName: "cases",
handleError
handleError,
isProtected: true
})
}
6 changes: 3 additions & 3 deletions src/app/state/rest/hooks/useApiRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useEffect, useContext } from "react"

import { ApiContext } from "../provider/ApiProvider"
import { ApiGroup } from "../index"
import useRequest, { RequestError } from "./useRequest"
import useRequestWrapper, { RequestError } from "./useRequestWrapper"

type GetOptions = {
method: "get"
Expand Down Expand Up @@ -33,7 +33,7 @@ type Config = {
handleError?: (error: RequestError) => void
}

const useApiRequest = <Schema, Payload = Partial<Schema>>({ url, groupName, handleError, lazy, keepUsingInvalidCache }: Config) => {
const useApiRequest = <Schema, Payload = Partial<Schema>>({ url, groupName, isProtected, handleError, lazy, keepUsingInvalidCache }: Config) => {
const {
getCacheItem,
setCacheItem,
Expand All @@ -44,7 +44,7 @@ const useApiRequest = <Schema, Payload = Partial<Schema>>({ url, groupName, hand
isRequestPendingInQueue
} = useContext(ApiContext)[groupName]

const request = useRequest()
const request = useRequestWrapper(isProtected)

/**
* Executes an API request
Expand Down
39 changes: 39 additions & 0 deletions src/app/state/rest/hooks/useProtectedRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useCallback } from "react"
import { useAuth } from "react-oidc-context"
import useRequest, { Method } from "./useRequest"


const useProtectedRequest = () => {
const request = useRequest()
const auth = useAuth()
const token = auth.user?.access_token

return useCallback(
async <Schema>(method: Method, url: string, data?: unknown, additionalHeaders = {}) => {
try {
// TODO: What if token expires?
const headers = {
Authorization: `Bearer ${ token }`,
...additionalHeaders
}
const response = await request<Schema>(
method,
url,
data,
headers
)
return response
} catch (error) {
console.log("Error protected request:", error)
// switch ((error as RequestError)?.response?.status) {
// case 401: keycloak.logout(); break
// case 403: navigateTo("/auth"); break
// }
if (error !== undefined) throw error
}
},
[token, request]
)
}

export default useProtectedRequest
4 changes: 3 additions & 1 deletion src/app/state/rest/hooks/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios, { Method, AxiosError } from "axios"
export type { Method } from "axios"
export type RequestError = AxiosError

export default () => useCallback(
export const useRequest = () => useCallback(
async <Schema>(method: Method, url: string, data?: unknown, headers = {}) =>
await axios.request<Schema>({
method,
Expand All @@ -14,3 +14,5 @@ export default () => useCallback(
}),
[]
)

export default useRequest
11 changes: 11 additions & 0 deletions src/app/state/rest/hooks/useRequestWrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import useRequest from "./useRequest"
import useProtectedRequest from "./useProtectedRequest"
export type { RequestError, Method } from "./useRequest"

export default (isProtected?: boolean) => {
const request = useRequest()
const protectedRequest = useProtectedRequest()

return isProtected ? protectedRequest : request
}

0 comments on commit 4fca367

Please sign in to comment.