Mock server

Axios is used in UKO, you can use any library you want. Fetch is another viable option.

Note

  • src/utils/axios(Axios instance) is intercepted by the MockAdapter.
  • Any requests made with it will be rejected by the adapter.

How to remove mock server

  • Remove the src/__fakeApi__/ folder and uninstall axios-mock-adapter dependency.
  • Then remove the following import from src/index.tsx
import "./__fakeApi__";

Example

const login = async (email password) => {
const response: any = await axios.post('/api/auth/login', {
email,
password,
})
console.log(response)
}

Fake API

Mock.onPost('/api/auth/login').reply(async (config) => {
try {
await new Promise((resolve) => setTimeout(resolve, 1000))
const { email } = JSON.parse(config.data)
const user = userList.find((u) => u.email === email)
if (!user) {
return [400, { message: 'Invalid email or password' }]
}
const accessToken = jwt.sign({ userId: user.id }, JWT_SECRET, {
expiresIn: JWT_VALIDITY,
})
return [
200,
{
accessToken,
user: {
id: user.id,
avatar: user.avatar,
email: user.email,
name: user.name,
role: user.role,
},
},
]
} catch (err) {
console.error(err)
return [500, { message: 'Internal server error' }]
}
})