Added api/user request prototype

This commit is contained in:
Dmitriy Shishkov 2023-07-19 23:24:58 +03:00
parent 1b4eed529a
commit 7cf83d099d
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
7 changed files with 97 additions and 0 deletions

View File

@ -20,6 +20,7 @@
"react-router-dom": "^6.14.1" "react-router-dom": "^6.14.1"
}, },
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^8.0.2",
"@types/react": "^18.2.14", "@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6", "@types/react-dom": "^18.2.6",
"@typescript-eslint/eslint-plugin": "^5.61.0", "@typescript-eslint/eslint-plugin": "^5.61.0",
@ -817,6 +818,22 @@
"node": "^12.22.0 || ^14.17.0 || >=16.0.0" "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
} }
}, },
"node_modules/@faker-js/faker": {
"version": "8.0.2",
"resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.0.2.tgz",
"integrity": "sha512-Uo3pGspElQW91PCvKSIAXoEgAUlRnH29sX2/p89kg7sP1m2PzCufHINd0FhTXQf6DYGiUlVncdSPa2F9wxed2A==",
"dev": true,
"funding": [
{
"type": "opencollective",
"url": "https://opencollective.com/fakerjs"
}
],
"engines": {
"node": "^14.17.0 || ^16.13.0 || >=18.0.0",
"npm": ">=6.14.13"
}
},
"node_modules/@humanwhocodes/config-array": { "node_modules/@humanwhocodes/config-array": {
"version": "0.11.10", "version": "0.11.10",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.10.tgz", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.10.tgz",

View File

@ -24,6 +24,7 @@
"react-router-dom": "^6.14.1" "react-router-dom": "^6.14.1"
}, },
"devDependencies": { "devDependencies": {
"@faker-js/faker": "^8.0.2",
"@types/react": "^18.2.14", "@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6", "@types/react-dom": "^18.2.6",
"@typescript-eslint/eslint-plugin": "^5.61.0", "@typescript-eslint/eslint-plugin": "^5.61.0",

View File

@ -0,0 +1,25 @@
import { API_URL } from '../../config'
import { UserResponse, User } from './types'
import { faker } from '@faker-js/faker/locale/ru'
const initialUser: User = import.meta.env.DEV ? { // Temporary, until api is realized
id: Math.random() * 100,
name: faker.person.firstName() + ' ' + faker.person.lastName(),
regDate: faker.date.anytime().getTime(),
} : {
id: -1,
name: '',
regDate: 0,
}
const composeUserURL = () => (
API_URL + '/user?'
)
const processUser = (data: UserResponse): User => {
return data
}
export { initialUser, composeUserURL, processUser }

View File

@ -0,0 +1,29 @@
import { isObject } from '../../utils/types'
type User = {
id: number,
name: string,
regDate: number,
}
const isUser = (obj: unknown): obj is User => (
isObject(obj, {
'id': 'number',
'name': 'string',
'regDate': 'number',
})
)
type UserResponse = User
// const isUserResponse = (obj: unknown): obj is UserResponse => (
// isObject(obj, {
// })
// )
const isUserResponse = isUser
export type { UserResponse, User }
export { isUserResponse, isUser }

View File

@ -4,3 +4,4 @@ export { default as useAuth } from './useAuth'
export { default as useTrashboxes } from './useTrashboxes' export { default as useTrashboxes } from './useTrashboxes'
export { default as useAddAnnouncement } from './useAddAnnouncement' export { default as useAddAnnouncement } from './useAddAnnouncement'
export { default as useOsmAddresses } from './useOsmAddress' export { default as useOsmAddresses } from './useOsmAddress'
export { default as useUser } from './useUser'

View File

@ -0,0 +1,22 @@
import { initialUser } from '../../api/user'
import { User } from '../../api/user/types'
import { UseFetchErrored, UseFetchSucced } from '../useFetch'
const useUser = (): UseFetchSucced<User> | UseFetchErrored => (
// useFetch(
// composeUserUrl(getToken()),
// 'GET',
// true,
// isUserResponse,
// processUser,
// initialUser
// )
{
data: initialUser,
loading: false,
error: null,
}
)
export default useUser

View File

@ -70,6 +70,8 @@ function useFetch<R, T>(
} }
} }
export type { UseFetchErrored, UseFetchSucced }
export default useFetch export default useFetch
export { gotError, fallbackError } export { gotError, fallbackError }