-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): Install orval, Query, Axios and Husky
- Loading branch information
1 parent
6e67ba6
commit c517683
Showing
14 changed files
with
4,775 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cd ./cms && yarn config-sync export -y | ||
cd ../client && yarn types && yarn lint --fix && yarn check-types && git add src/types/generated/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/.next | ||
/node_modules | ||
/public | ||
/public | ||
/src/types/generated/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"use client"; | ||
|
||
// Since QueryClientProvider relies on useContext under the hood, we have to put 'use client' on top | ||
import { isServer, QueryClient, QueryClientProvider } from "@tanstack/react-query"; | ||
import { PropsWithChildren } from "react"; | ||
|
||
function makeQueryClient() { | ||
return new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
// With SSR, we usually want to set some default staleTime | ||
// above 0 to avoid refetching immediately on the client | ||
staleTime: 60 * 1000, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
let browserQueryClient: QueryClient | undefined = undefined; | ||
|
||
function getQueryClient() { | ||
if (isServer) { | ||
// Server: always make a new query client | ||
return makeQueryClient(); | ||
} else { | ||
// Browser: make a new query client if we don't already have one | ||
// This is very important, so we don't re-make a new client if React | ||
// suspends during the initial render. This may not be needed if we | ||
// have a suspense boundary BELOW the creation of the query client | ||
if (!browserQueryClient) browserQueryClient = makeQueryClient(); | ||
return browserQueryClient; | ||
} | ||
} | ||
|
||
export default function ReactQueryProvider({ children }: PropsWithChildren) { | ||
// NOTE: Avoid useState when initializing the query client if you don't | ||
// have a suspense boundary between this and the code that may | ||
// suspend because React will throw away the client on the initial | ||
// render if it suspends and there is no boundary | ||
const queryClient = getQueryClient(); | ||
|
||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { defineConfig } from "orval"; | ||
|
||
export default defineConfig({ | ||
cms: { | ||
input: { | ||
target: "../../cms/src/extensions/documentation/documentation/1.0.0/full_documentation.json", | ||
filters: { | ||
tags: ["Topic", "Sub-topic", "Dataset", "Layer"], | ||
}, | ||
}, | ||
output: { | ||
target: "./types/generated/strapi.ts", | ||
mode: "tags", | ||
client: "react-query", | ||
clean: true, | ||
override: { | ||
mutator: { | ||
path: "./services/api.ts", | ||
name: "API", | ||
}, | ||
query: { | ||
useQuery: true, | ||
signal: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Axios, { AxiosError, AxiosRequestConfig } from "axios"; | ||
|
||
export const AXIOS_INSTANCE = Axios.create({ baseURL: process.env.NEXT_PUBLIC_API_URL }); | ||
|
||
export const API = <T>(config: AxiosRequestConfig, options?: AxiosRequestConfig): Promise<T> => { | ||
// eslint-disable-next-line import/no-named-as-default-member | ||
const source = Axios.CancelToken.source(); | ||
const promise = AXIOS_INSTANCE({ | ||
...config, | ||
...options, | ||
cancelToken: source.token, | ||
}).then(({ data }) => data); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
promise.cancel = () => { | ||
source.cancel("Query was cancelled"); | ||
}; | ||
return promise; | ||
}; | ||
|
||
export type BodyType<BodyData> = BodyData; | ||
|
||
export type ErrorType<Error> = AxiosError<Error>; | ||
|
||
export default API; |
Oops, something went wrong.