@hidrografico/request 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/api/api.d.ts +8 -0
- package/dist/api/api.d.ts.map +1 -0
- package/dist/api/api.js +35 -0
- package/dist/component/auth.d.ts +8 -0
- package/dist/component/auth.d.ts.map +1 -0
- package/dist/component/auth.js +9 -0
- package/dist/component/errorMessage.d.ts +6 -0
- package/dist/component/errorMessage.d.ts.map +1 -0
- package/dist/component/errorMessage.js +7 -0
- package/dist/component/request.d.ts +13 -0
- package/dist/component/request.d.ts.map +1 -0
- package/dist/component/request.js +12 -0
- package/dist/component/response.d.ts +13 -0
- package/dist/component/response.d.ts.map +1 -0
- package/dist/component/response.js +13 -0
- package/dist/component/token.d.ts +32 -0
- package/dist/component/token.d.ts.map +1 -0
- package/dist/component/token.js +16 -0
- package/dist/hook/useProvider.d.ts +13 -0
- package/dist/hook/useProvider.d.ts.map +1 -0
- package/dist/hook/useProvider.js +51 -0
- package/dist/hook/useRequest.d.ts +11 -0
- package/dist/hook/useRequest.d.ts.map +1 -0
- package/dist/hook/useRequest.js +38 -0
- package/dist/hook/useRequireAuth.d.ts +4 -0
- package/dist/hook/useRequireAuth.d.ts.map +1 -0
- package/dist/hook/useRequireAuth.js +21 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/service/crud.d.ts +15 -0
- package/dist/service/crud.d.ts.map +1 -0
- package/dist/service/crud.js +116 -0
- package/dist/service/fetchPage.d.ts +5 -0
- package/dist/service/fetchPage.d.ts.map +1 -0
- package/dist/service/fetchPage.js +22 -0
- package/dist/service/token.d.ts +10 -0
- package/dist/service/token.d.ts.map +1 -0
- package/dist/service/token.js +93 -0
- package/package.json +11 -6
- package/src/api/api.tsx +0 -38
- package/src/component/auth.tsx +0 -13
- package/src/component/errorMessage.tsx +0 -9
- package/src/component/request.tsx +0 -21
- package/src/component/response.tsx +0 -23
- package/src/component/token.tsx +0 -46
- package/src/hook/useProvider.tsx +0 -68
- package/src/hook/useRequest.tsx +0 -38
- package/src/hook/useRequireAuth.tsx +0 -20
- package/src/service/crud.tsx +0 -111
- package/src/service/fetchPage.tsx +0 -23
- package/src/service/token.tsx +0 -94
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getHeader = exports.decodeJwt = exports.getPayload = exports.removeToken = exports.setToken = exports.getToken = exports.isValidToken = void 0;
|
|
4
|
+
const auth_1 = require("../component/auth");
|
|
5
|
+
const token_1 = require("../component/token");
|
|
6
|
+
const isValidJSON = (json) => {
|
|
7
|
+
try {
|
|
8
|
+
JSON.parse(json);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const isValidToken = () => {
|
|
16
|
+
try {
|
|
17
|
+
const token = (0, exports.getToken)();
|
|
18
|
+
if (!(token === null || token === void 0 ? void 0 : token.accessToken))
|
|
19
|
+
return false;
|
|
20
|
+
const { exp } = (0, exports.getPayload)();
|
|
21
|
+
if (typeof exp !== 'number')
|
|
22
|
+
return false;
|
|
23
|
+
return exp * 1000 > Date.now();
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
exports.isValidToken = isValidToken;
|
|
30
|
+
const getToken = () => {
|
|
31
|
+
let token = `${localStorage.getItem(`token`)}`;
|
|
32
|
+
return isValidJSON(token) ? JSON.parse(token) : auth_1.initialAuth;
|
|
33
|
+
};
|
|
34
|
+
exports.getToken = getToken;
|
|
35
|
+
const setToken = (token) => {
|
|
36
|
+
localStorage.setItem(`token`, JSON.stringify(token));
|
|
37
|
+
};
|
|
38
|
+
exports.setToken = setToken;
|
|
39
|
+
const removeToken = () => {
|
|
40
|
+
localStorage.removeItem('token');
|
|
41
|
+
};
|
|
42
|
+
exports.removeToken = removeToken;
|
|
43
|
+
const getPayload = () => {
|
|
44
|
+
try {
|
|
45
|
+
const token = (0, exports.getToken)();
|
|
46
|
+
if (!(token === null || token === void 0 ? void 0 : token.accessToken))
|
|
47
|
+
return token_1.initialPayload;
|
|
48
|
+
const base64 = token.accessToken.split('.')[1]
|
|
49
|
+
.replace(/-/g, '+')
|
|
50
|
+
.replace(/_/g, '/');
|
|
51
|
+
const payload = decodeURIComponent(atob(base64)
|
|
52
|
+
.split('')
|
|
53
|
+
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
|
|
54
|
+
.join(''));
|
|
55
|
+
return isValidJSON(payload) ? JSON.parse(payload) : token_1.initialPayload;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return token_1.initialPayload;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
exports.getPayload = getPayload;
|
|
62
|
+
const decodeJwt = () => {
|
|
63
|
+
if ((0, exports.getToken)() !== null) {
|
|
64
|
+
var base64Url = (0, exports.getToken)().accessToken.split('.')[1];
|
|
65
|
+
var base64 = decodeURIComponent(atob(base64Url).split('').map((c) => {
|
|
66
|
+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
67
|
+
}).join(''));
|
|
68
|
+
return base64;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
exports.decodeJwt = decodeJwt;
|
|
75
|
+
const getHeader = () => {
|
|
76
|
+
try {
|
|
77
|
+
const token = (0, exports.getToken)();
|
|
78
|
+
if (!(token === null || token === void 0 ? void 0 : token.accessToken))
|
|
79
|
+
return token_1.initialHeader;
|
|
80
|
+
const base64 = token.accessToken.split('.')[0]
|
|
81
|
+
.replace(/-/g, '+')
|
|
82
|
+
.replace(/_/g, '/');
|
|
83
|
+
const header = decodeURIComponent(atob(base64)
|
|
84
|
+
.split('')
|
|
85
|
+
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
|
|
86
|
+
.join(''));
|
|
87
|
+
return isValidJSON(header) ? JSON.parse(header) : token_1.initialHeader;
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return token_1.initialHeader;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
exports.getHeader = getHeader;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hidrografico/request",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"homepage": "https://github.com/hidrografico/request#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -17,19 +17,24 @@
|
|
|
17
17
|
],
|
|
18
18
|
"author": "Marcelo Gadelha",
|
|
19
19
|
"type": "commonjs",
|
|
20
|
-
"main": "index.js",
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
21
25
|
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
22
27
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
23
28
|
},
|
|
24
29
|
"peerDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"react": "^
|
|
30
|
+
"react": "^19.2.3",
|
|
31
|
+
"react-router-dom": "^7.12.0"
|
|
27
32
|
},
|
|
28
33
|
"devDependencies": {
|
|
29
34
|
"@types/react": "^19.2.9",
|
|
30
|
-
"
|
|
35
|
+
"typescript": "^5.9.3"
|
|
31
36
|
},
|
|
32
37
|
"dependencies": {
|
|
33
|
-
"
|
|
38
|
+
"axios": "^1.13.2"
|
|
34
39
|
}
|
|
35
40
|
}
|
package/src/api/api.tsx
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance } from "axios"
|
|
2
|
-
import { getToken, removeToken } from "../service/token"
|
|
3
|
-
|
|
4
|
-
export type ApiClientOptions = {
|
|
5
|
-
baseURL: string
|
|
6
|
-
onUnauthorized?: () => void
|
|
7
|
-
onForbidden?: () => void
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const createApiClient = (options: ApiClientOptions): AxiosInstance => {
|
|
11
|
-
const api = axios.create({
|
|
12
|
-
baseURL: options.baseURL,
|
|
13
|
-
headers: { 'content-type': 'application/json' }
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
api.interceptors.request.use(async config => {
|
|
17
|
-
const token = getToken()?.accessToken
|
|
18
|
-
if (token) {
|
|
19
|
-
config.headers!.Authorization = `Bearer ${token}`
|
|
20
|
-
}
|
|
21
|
-
return config
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
api.interceptors.response.use(
|
|
25
|
-
(response) => response,
|
|
26
|
-
(error) => {
|
|
27
|
-
if (error.response?.status === 401) {
|
|
28
|
-
removeToken()
|
|
29
|
-
options.onUnauthorized?.()
|
|
30
|
-
}
|
|
31
|
-
if (error.response?.status === 403) {
|
|
32
|
-
options.onForbidden?.()
|
|
33
|
-
}
|
|
34
|
-
return Promise.reject(error)
|
|
35
|
-
}
|
|
36
|
-
)
|
|
37
|
-
return api
|
|
38
|
-
}
|
package/src/component/auth.tsx
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
interface Sort {
|
|
2
|
-
key: string,
|
|
3
|
-
order: 'ASC' | 'DESC'
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export interface Search {
|
|
7
|
-
value?: string,
|
|
8
|
-
page?: number,
|
|
9
|
-
size?: number,
|
|
10
|
-
sort?: Sort
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const initialSearch: Search = {
|
|
14
|
-
value: '',
|
|
15
|
-
page: 0,
|
|
16
|
-
size: 15,
|
|
17
|
-
sort: {
|
|
18
|
-
key: 'id',
|
|
19
|
-
order: 'ASC',
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
interface PageInfo {
|
|
2
|
-
size: number,
|
|
3
|
-
number: number,
|
|
4
|
-
totalElements: number,
|
|
5
|
-
totalPages: number
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const initialPageInfo: PageInfo = {
|
|
9
|
-
size: 0,
|
|
10
|
-
number: 0,
|
|
11
|
-
totalElements: 0,
|
|
12
|
-
totalPages: 0
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface Page<T = unknown> {
|
|
16
|
-
content: T[],
|
|
17
|
-
page: PageInfo
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const initialPage: Page = {
|
|
21
|
-
content: [],
|
|
22
|
-
page: initialPageInfo
|
|
23
|
-
}
|
package/src/component/token.tsx
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
export interface Token {
|
|
2
|
-
header: {
|
|
3
|
-
alg: string,
|
|
4
|
-
typ: string,
|
|
5
|
-
},
|
|
6
|
-
payload: {
|
|
7
|
-
jti: string, //jsonTokenIdentifier
|
|
8
|
-
iss: string, //issuer
|
|
9
|
-
iat: string, //issuedAt
|
|
10
|
-
nbf: string, //notBefore (Não Antes)
|
|
11
|
-
exp: string, //expiration
|
|
12
|
-
sub: string, //Subject
|
|
13
|
-
aud: string, //Audience
|
|
14
|
-
},
|
|
15
|
-
signature: string
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface Header {
|
|
19
|
-
alg: string,
|
|
20
|
-
typ: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface Payload {
|
|
24
|
-
jti: string, //jsonTokenIdentifier
|
|
25
|
-
iss: string, //issuer
|
|
26
|
-
iat: string, //issuedAt
|
|
27
|
-
nbf: string, //notBefore (Não Antes)
|
|
28
|
-
exp: string, //expiration
|
|
29
|
-
sub: string, //Subject
|
|
30
|
-
aud: string //Audience
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const initialHeader: Header = {
|
|
34
|
-
alg: '',
|
|
35
|
-
typ: ''
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export const initialPayload: Payload = {
|
|
39
|
-
jti: '',
|
|
40
|
-
iss: '',
|
|
41
|
-
iat: '',
|
|
42
|
-
nbf: '',
|
|
43
|
-
exp: '',
|
|
44
|
-
sub: '',
|
|
45
|
-
aud: ''
|
|
46
|
-
}
|
package/src/hook/useProvider.tsx
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { createContext, useCallback, useEffect, useState } from 'react'
|
|
2
|
-
import { getToken, isValidToken } from '../service/token'
|
|
3
|
-
import { Auth, initialAuth } from '../component/auth'
|
|
4
|
-
import { login, logout } from '../service/crud'
|
|
5
|
-
import { AxiosInstance } from 'axios'
|
|
6
|
-
|
|
7
|
-
interface AuthContextType extends Auth {
|
|
8
|
-
loginUser: (credentials: any) => Promise<any>
|
|
9
|
-
logoutUser: () => void
|
|
10
|
-
isAuthenticated: boolean
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const AuthContext = createContext<AuthContextType>(initialAuth as AuthContextType)
|
|
14
|
-
|
|
15
|
-
export const AuthProvider = ({ api, children }: {
|
|
16
|
-
api: AxiosInstance
|
|
17
|
-
children: React.ReactNode
|
|
18
|
-
}) => {
|
|
19
|
-
const [state, setState] = useState<Auth>(() =>
|
|
20
|
-
isValidToken() ? getToken() : initialAuth
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
const loginUser = useCallback(async (credentials: any) => {
|
|
24
|
-
const result = await login(api, '/auth/login', credentials)
|
|
25
|
-
|
|
26
|
-
if (!Array.isArray(result)) {
|
|
27
|
-
setState(result)
|
|
28
|
-
return { success: true, data: result }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return { success: false, errors: result }
|
|
32
|
-
}, [])
|
|
33
|
-
|
|
34
|
-
const logoutUser = useCallback(() => {
|
|
35
|
-
logout()
|
|
36
|
-
setState(initialAuth)
|
|
37
|
-
window.location.href = '/login'
|
|
38
|
-
}, [])
|
|
39
|
-
// Verifica token expirado a cada minuto
|
|
40
|
-
useEffect(() => {
|
|
41
|
-
const interval = setInterval(() => {
|
|
42
|
-
if (state.accessToken && !isValidToken()) {
|
|
43
|
-
logoutUser()
|
|
44
|
-
}
|
|
45
|
-
}, 60000)
|
|
46
|
-
return () => clearInterval(interval)
|
|
47
|
-
}, [state.accessToken, logoutUser])
|
|
48
|
-
// Escuta mudanças no localStorage (ex.: logout em outra aba)
|
|
49
|
-
useEffect(() => {
|
|
50
|
-
const handleStorageChange = (e: StorageEvent) => {
|
|
51
|
-
if (e.key === 'token') {
|
|
52
|
-
if (!e.newValue || !isValidToken()) {
|
|
53
|
-
setState(initialAuth)
|
|
54
|
-
} else {
|
|
55
|
-
setState(getToken())
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
window.addEventListener('storage', handleStorageChange)
|
|
60
|
-
return () => window.removeEventListener('storage', handleStorageChange)
|
|
61
|
-
}, [])
|
|
62
|
-
|
|
63
|
-
return (
|
|
64
|
-
<AuthContext.Provider value={{ ...state, loginUser, logoutUser, isAuthenticated: isValidToken() }}>
|
|
65
|
-
{children}
|
|
66
|
-
</AuthContext.Provider>
|
|
67
|
-
)
|
|
68
|
-
}
|
package/src/hook/useRequest.tsx
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
2
|
-
import { ErrorMessage, initialErrorMessage } from '../component/component.errorMessage'
|
|
3
|
-
import { initialPage, Page } from '../component/component.response'
|
|
4
|
-
import { Search } from '../component/component.request'
|
|
5
|
-
import { FetchPage } from '../service/fetchPage'
|
|
6
|
-
import { AxiosInstance } from 'axios'
|
|
7
|
-
|
|
8
|
-
export const useRequest = (api: AxiosInstance, endpoint: string, search?: Search) => {
|
|
9
|
-
const [response, setResponse] = useState<Page>(initialPage)
|
|
10
|
-
const [error, setError] = useState<ErrorMessage[]>([initialErrorMessage])
|
|
11
|
-
const [loading, setLoading] = useState<boolean>(false)
|
|
12
|
-
const abortControllerRef = useRef<AbortController | null>(null)
|
|
13
|
-
|
|
14
|
-
const request = useCallback(async () => {
|
|
15
|
-
abortControllerRef.current?.abort()
|
|
16
|
-
const controller = new AbortController()
|
|
17
|
-
abortControllerRef.current = controller
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
setLoading(true)
|
|
21
|
-
setError([initialErrorMessage])
|
|
22
|
-
const data = await FetchPage(api, endpoint, search, controller.signal)
|
|
23
|
-
setResponse(data)
|
|
24
|
-
} catch (requestError) {
|
|
25
|
-
setError([requestError as ErrorMessage])
|
|
26
|
-
} finally {
|
|
27
|
-
setLoading(false)
|
|
28
|
-
abortControllerRef.current = null
|
|
29
|
-
}
|
|
30
|
-
}, [endpoint, search])
|
|
31
|
-
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
request()
|
|
34
|
-
return () => abortControllerRef.current?.abort()
|
|
35
|
-
}, [request])
|
|
36
|
-
|
|
37
|
-
return { response, error, loading, request }
|
|
38
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { useContext } from 'react'
|
|
2
|
-
import { useLocation, Navigate, Outlet } from 'react-router-dom'
|
|
3
|
-
import { AuthContext } from './useProvider'
|
|
4
|
-
|
|
5
|
-
export const useAuth = () => {
|
|
6
|
-
return useContext(AuthContext)
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export const RequireAuth = ({ allowedRoles }: any) => {
|
|
10
|
-
const { role, accessToken } = useAuth()
|
|
11
|
-
const location = useLocation()
|
|
12
|
-
|
|
13
|
-
return (
|
|
14
|
-
role?.find((role: any) => allowedRoles?.includes(role))
|
|
15
|
-
? <Outlet />
|
|
16
|
-
: accessToken
|
|
17
|
-
? <Navigate to='/notAllowed' state={{ from: location }} replace />
|
|
18
|
-
: <Navigate to='/login' state={{ from: location }} replace />
|
|
19
|
-
)
|
|
20
|
-
}
|
package/src/service/crud.tsx
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { AxiosInstance } from 'axios'
|
|
2
|
-
import { ErrorMessage } from '../component/errorMessage'
|
|
3
|
-
import { Search } from '../component/request'
|
|
4
|
-
import { removeToken, setToken } from './token'
|
|
5
|
-
|
|
6
|
-
// Respostas de informação (100-199),
|
|
7
|
-
// Respostas de sucesso (200-299),
|
|
8
|
-
// Redirecionamentos (300-399)
|
|
9
|
-
// Erros do cliente (400-499)
|
|
10
|
-
// Erros do servidor (500-599).
|
|
11
|
-
|
|
12
|
-
const addError = (error: any):ErrorMessage[] => {
|
|
13
|
-
let errorMessage: ErrorMessage[] = []
|
|
14
|
-
if (error.response.data.validationErrors !== undefined){
|
|
15
|
-
error.response.data?.validationErrors?.forEach((element: ErrorMessage) => {
|
|
16
|
-
errorMessage.push({ field: element.field, message: element.message })
|
|
17
|
-
})
|
|
18
|
-
} else {
|
|
19
|
-
errorMessage.push({ field: 'Error', message: 'Internal Error' })
|
|
20
|
-
}
|
|
21
|
-
return errorMessage
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const login = async<Auth,>(api: AxiosInstance, url: string, object: Auth) => {
|
|
25
|
-
return await api.post<Auth>(url, object)
|
|
26
|
-
.then(response => {
|
|
27
|
-
setToken(response.data)
|
|
28
|
-
return response.data
|
|
29
|
-
})
|
|
30
|
-
.catch(error => { return addError(error) })
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const reset = async<Auth,>(api: AxiosInstance, url: string, object: Auth) => {
|
|
34
|
-
return await api.put<Auth>(url, object)
|
|
35
|
-
.then(response => {
|
|
36
|
-
setToken(response.data)
|
|
37
|
-
return response.data
|
|
38
|
-
})
|
|
39
|
-
.catch(error => { return addError(error) })
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export const logout = () => {
|
|
43
|
-
removeToken()
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export const changePassword = async<User,>(api: AxiosInstance, data: User) => {
|
|
47
|
-
return await api.put<User>(`/user/changePassword`, data)
|
|
48
|
-
.then(response => {
|
|
49
|
-
return response.data
|
|
50
|
-
})
|
|
51
|
-
.catch(error => {
|
|
52
|
-
return addError(error) })
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export const create = async<T,>(api: AxiosInstance, url: string, object: T) => {
|
|
56
|
-
return await api.post(`/${url}`, object)
|
|
57
|
-
.then(response => { return response.data })
|
|
58
|
-
.catch(error => { return addError(error) })
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export const createAll = async<T,>(api: AxiosInstance, url: string, object: T[]) => {
|
|
62
|
-
return await api.post<T>(`/${url}/createAll`, object)
|
|
63
|
-
.then(response => { return response.data })
|
|
64
|
-
.catch(error => { return addError(error) })
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const retrieve = async<T,>(api: AxiosInstance, url: string, search?: Search, signal?: AbortSignal ) => {
|
|
68
|
-
if(search?.page === undefined && search?.size === undefined){
|
|
69
|
-
return await api.get<T>(`/${url}`)
|
|
70
|
-
.then(response => { return response.data })
|
|
71
|
-
.catch(error => { return addError(error) })
|
|
72
|
-
} else if (search?.sort?.order === undefined) {
|
|
73
|
-
return await api.get<T>(`/${url}?value=${search?.value}`, { params: { page: search?.page, size: search?.size }, signal } )
|
|
74
|
-
.then(response => { return response.data })
|
|
75
|
-
.catch(error => { return addError(error) })
|
|
76
|
-
} else {
|
|
77
|
-
return await api.get<T>(`/${url}?value=${search?.value}`, { params: { page: search?.page, size: search?.size, sort: `${search?.sort?.key},${search?.sort?.order}` }, signal } )
|
|
78
|
-
.then(response => { return response.data })
|
|
79
|
-
.catch(error => { return addError(error) })
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export const update = async<T,>(api: AxiosInstance, url: string, object: T) => {
|
|
84
|
-
return await api.put<T>(`/${url}`, object)
|
|
85
|
-
.then(response => { return response.data })
|
|
86
|
-
.catch(error => { return addError(error) })
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export const remove = async<T,>(api: AxiosInstance, url: string, id: string) => {
|
|
90
|
-
return await api.delete<T>(`/${url}/${id}`)
|
|
91
|
-
.then(response => { return response.data })
|
|
92
|
-
.catch(error => { return addError(error) })
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export const removeComposite = async<T,>(api: AxiosInstance, url: string, object: Object, one: string, two: string, three: string, four: string) => {
|
|
96
|
-
if(three !== '' && four !== ''){
|
|
97
|
-
return await api.delete<T>(`/${url}`, object)
|
|
98
|
-
.then(response => { return response.data })
|
|
99
|
-
.catch(error => { return addError(error) })
|
|
100
|
-
} else {
|
|
101
|
-
return await api.delete<T>(`/${url}/${one}/${two}`, object)
|
|
102
|
-
.then(response => { return response.data })
|
|
103
|
-
.catch(error => { return addError(error) })
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export const removeAll = async<T,>(api: AxiosInstance, url: string) => {
|
|
108
|
-
return await api.delete<T>(`/${url}`)
|
|
109
|
-
.then(response => { return response.data })
|
|
110
|
-
.catch(error => { return addError(error) });
|
|
111
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { AxiosInstance } from 'axios'
|
|
2
|
-
import { Page } from '../component/response'
|
|
3
|
-
import { Search } from '../component/request'
|
|
4
|
-
|
|
5
|
-
export const FetchPage = async (api: AxiosInstance, endpoint: string, search?: Search, signal?: AbortSignal): Promise<Page> => {
|
|
6
|
-
const uri = search?.value?.trim()
|
|
7
|
-
? `/${endpoint}?value=${encodeURIComponent(search.value)}`
|
|
8
|
-
: `/${endpoint}`
|
|
9
|
-
const params: Record<string, unknown> = {
|
|
10
|
-
page: search?.page,
|
|
11
|
-
size: search?.size
|
|
12
|
-
}
|
|
13
|
-
if (search?.sort?.order && search?.sort?.key) {
|
|
14
|
-
params.sort = `${search.sort.key},${search.sort.order}`
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const { data } = await api.get<Page>(uri, {
|
|
18
|
-
params: Object.keys(params).length > 0 ? params : undefined,
|
|
19
|
-
signal
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
return data
|
|
23
|
-
}
|
package/src/service/token.tsx
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { Auth, initialAuth } from '../component/auth'
|
|
2
|
-
import { Header, initialHeader, initialPayload, Payload } from '../component/token'
|
|
3
|
-
|
|
4
|
-
const isValidJSON = (json: string) => {
|
|
5
|
-
try {
|
|
6
|
-
JSON.parse(json)
|
|
7
|
-
return true
|
|
8
|
-
} catch {
|
|
9
|
-
return false
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const isValidToken = (): boolean => {
|
|
14
|
-
try {
|
|
15
|
-
const token = getToken()
|
|
16
|
-
if (!token?.accessToken) return false
|
|
17
|
-
|
|
18
|
-
const { exp } = getPayload()
|
|
19
|
-
if (typeof exp !== 'number') return false
|
|
20
|
-
|
|
21
|
-
return exp * 1000 > Date.now()
|
|
22
|
-
} catch {
|
|
23
|
-
return false
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const getToken = (): Auth => {
|
|
28
|
-
let token: string = `${localStorage.getItem(`token`)}`
|
|
29
|
-
return isValidJSON(token) ? JSON.parse(token) : initialAuth
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const setToken = (token: any): void => {
|
|
33
|
-
localStorage.setItem(`token`, JSON.stringify(token))
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const removeToken = () => {
|
|
37
|
-
localStorage.removeItem('token')
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const getPayload = (): Payload => {
|
|
41
|
-
try {
|
|
42
|
-
const token = getToken()
|
|
43
|
-
if (!token?.accessToken) return initialPayload;
|
|
44
|
-
|
|
45
|
-
const base64 = token.accessToken.split('.')[1]
|
|
46
|
-
.replace(/-/g, '+')
|
|
47
|
-
.replace(/_/g, '/')
|
|
48
|
-
|
|
49
|
-
const payload = decodeURIComponent(
|
|
50
|
-
atob(base64)
|
|
51
|
-
.split('')
|
|
52
|
-
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
|
|
53
|
-
.join('')
|
|
54
|
-
)
|
|
55
|
-
|
|
56
|
-
return isValidJSON(payload) ? JSON.parse(payload) : initialPayload
|
|
57
|
-
} catch {
|
|
58
|
-
return initialPayload
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export const decodeJwt = () => {
|
|
63
|
-
if (getToken() !== null) {
|
|
64
|
-
var base64Url = getToken().accessToken.split('.')[1];
|
|
65
|
-
var base64 = decodeURIComponent(atob(base64Url).split('').map((c) => {
|
|
66
|
-
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
|
|
67
|
-
}).join(''))
|
|
68
|
-
return base64
|
|
69
|
-
} else {
|
|
70
|
-
return null
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export const getHeader = (): Header => {
|
|
75
|
-
try {
|
|
76
|
-
const token = getToken()
|
|
77
|
-
if (!token?.accessToken) return initialHeader
|
|
78
|
-
|
|
79
|
-
const base64 = token.accessToken.split('.')[0]
|
|
80
|
-
.replace(/-/g, '+')
|
|
81
|
-
.replace(/_/g, '/')
|
|
82
|
-
|
|
83
|
-
const header = decodeURIComponent(
|
|
84
|
-
atob(base64)
|
|
85
|
-
.split('')
|
|
86
|
-
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
|
|
87
|
-
.join('')
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
return isValidJSON(header) ? JSON.parse(header) : initialHeader
|
|
91
|
-
} catch {
|
|
92
|
-
return initialHeader
|
|
93
|
-
}
|
|
94
|
-
}
|