forked from orval-labs/orval
-
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.
fix(react-query): correctly handling mutation
- Loading branch information
Showing
12 changed files
with
181 additions
and
97 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
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
51 changes: 27 additions & 24 deletions
51
...s/react-app-with-react-query/src/api/endpoints/petstoreFromFileSpecWithTransformer.msw.ts
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,37 +1,40 @@ | ||
/* | ||
* Generated by orval v2.6.0 🍺 | ||
* Generated by orval v3.0.0 🍺 | ||
* Do not edit manually. | ||
* Swagger Petstore | ||
* OpenAPI spec version: 1.0.0 | ||
*/ | ||
import { rest } from 'msw' | ||
import faker from 'faker' | ||
import { | ||
ListPetsParams, | ||
Pet, | ||
Pets, | ||
} from '../model'; | ||
import faker from 'faker'; | ||
import { rest } from 'msw'; | ||
|
||
export const getSwaggerPetstoreMSW = () => [ | ||
rest.get('*/v:version/pets', (req, res, ctx) => { | ||
rest.get('*/v:version/pets', (req, res, ctx) => { | ||
return res( | ||
ctx.delay(1000), | ||
ctx.status(200, 'Mocked status'), | ||
ctx.json([...Array(faker.random.number({min: 1, max: 10}))].map(() => ({id: (() => faker.random.number({ min: 1, max: 99999 }))(), name: (() => faker.name.lastName())(), tag: (() => faker.name.lastName())()}))), | ||
) | ||
}),rest.post('*/v:version/pets', (req, res, ctx) => { | ||
ctx.json( | ||
[...Array(faker.random.number({ min: 1, max: 10 }))].map(() => ({ | ||
id: (() => faker.random.number({ min: 1, max: 99999 }))(), | ||
name: (() => faker.name.lastName())(), | ||
tag: (() => faker.name.lastName())(), | ||
})), | ||
), | ||
); | ||
}), | ||
rest.post('*/v:version/pets', (req, res, ctx) => { | ||
return res(ctx.delay(1000), ctx.status(200, 'Mocked status')); | ||
}), | ||
rest.get('*/v:version/pets/:petId/test/:testId', (req, res, ctx) => { | ||
return res( | ||
ctx.delay(1000), | ||
ctx.status(200, 'Mocked status'), | ||
) | ||
}),rest.get('*/v:version/pets/:petId/test/:testId', (req, res, ctx) => { | ||
return res( | ||
ctx.delay(1000), | ||
ctx.status(200, 'Mocked status'), | ||
ctx.json((() => ({ | ||
id: faker.random.number({ min: 1, max: 99 }), | ||
name: faker.name.firstName(), | ||
tag: faker.helpers.randomize([faker.random.word(), undefined]), | ||
}))()), | ||
) | ||
}),] | ||
ctx.json( | ||
(() => ({ | ||
id: faker.random.number({ min: 1, max: 99 }), | ||
name: faker.name.firstName(), | ||
tag: faker.helpers.randomize([faker.random.word(), undefined]), | ||
}))(), | ||
), | ||
); | ||
}), | ||
]; |
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
2 changes: 1 addition & 1 deletion
2
samples/react-app-with-react-query/src/api/model/listPetsParams.ts
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,66 @@ | ||
import axios from 'axios'; | ||
import React, { | ||
createContext, | ||
ReactNode, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
type Dispatch = (Auth: string) => void; | ||
|
||
type AuthProviderProps = { children: ReactNode; initialState?: string | null }; | ||
|
||
const AuthContext = createContext<string | null>(null); | ||
const AuthDispatchContext = createContext<Dispatch | null>(null); | ||
|
||
const AuthProvider = ({ children, initialState = null }: AuthProviderProps) => { | ||
// it's a quick demo with useState but you can also have a more complexe state with a useReducer | ||
const [token, setToken] = useState(initialState); | ||
|
||
useEffect(() => { | ||
const interceptorId = axios.interceptors.request.use( | ||
(config) => { | ||
return { | ||
...config, | ||
baseURL: '', // use an env or your api url | ||
headers: token | ||
? { | ||
...config.headers, | ||
Authorization: `Bearer ${token}`, | ||
} | ||
: config.headers, | ||
}; | ||
}, | ||
(error) => { | ||
Promise.reject(error); | ||
}, | ||
); | ||
|
||
return () => { | ||
axios.interceptors.request.eject(interceptorId); | ||
}; | ||
}, [token]); | ||
|
||
return ( | ||
<AuthContext.Provider value={token}> | ||
<AuthDispatchContext.Provider value={setToken}> | ||
{children} | ||
</AuthDispatchContext.Provider> | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
const useAuth = (): string | null => { | ||
return useContext<string | null>(AuthContext); | ||
}; | ||
|
||
const useAuthDispatch = (): Dispatch => { | ||
const context = useContext<Dispatch | null>(AuthDispatchContext); | ||
|
||
if (context === null) { | ||
throw new Error('useAuthDispatch must be used within a AuthProvider'); | ||
} | ||
return context; | ||
}; | ||
|
||
export { AuthProvider, useAuth, useAuthDispatch }; |
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
Oops, something went wrong.