@nitra/vite-boot 2.1.2 → 2.1.5
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/package.json +1 -1
- package/src/apollo.js +60 -72
package/package.json
CHANGED
package/src/apollo.js
CHANGED
|
@@ -6,88 +6,76 @@ import { refreshToken } from './token.js'
|
|
|
6
6
|
import { GraphQLWsLink } from '@apollo/client/link/subscriptions'
|
|
7
7
|
import jwtDecode from '@nitra/jwt-decode'
|
|
8
8
|
|
|
9
|
+
export let apolloClient
|
|
10
|
+
|
|
9
11
|
const consola = createLogger(import.meta.url)
|
|
10
12
|
|
|
11
13
|
const token = localStorage.getItem('token')
|
|
12
14
|
|
|
13
15
|
if (!token) {
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Перевіряємо чи не старий токен знаходиться в localStorage
|
|
18
|
-
const tokenDecoded = jwtDecode(token)
|
|
19
|
-
if (tokenDecoded.exp < Math.floor(Date.now() / 1000)) {
|
|
20
|
-
refreshToken()
|
|
21
|
-
}
|
|
16
|
+
consola.debug('Skip Apollo boot')
|
|
17
|
+
} else {
|
|
18
|
+
const tokenDecoded = jwtDecode(token)
|
|
22
19
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
20
|
+
if (
|
|
21
|
+
!tokenDecoded['https://hasura.io/jwt/claims']['x-hasura-allowed-roles'].includes(import.meta.env.VITE_HASURA_ROLE)
|
|
22
|
+
) {
|
|
23
|
+
// Не дозволена роль поточного користувача в поточному прикладенні
|
|
24
|
+
window.location.replace('/logout')
|
|
25
|
+
} else if (tokenDecoded.exp < Math.floor(Date.now() / 1000)) {
|
|
26
|
+
// Перевіряємо чи не старий токен знаходиться в localStorage
|
|
27
|
+
await refreshToken()
|
|
28
|
+
} else {
|
|
29
|
+
const wsClient = createClient({
|
|
30
|
+
url: `${import.meta.env.VITE_HASURA_PROTOCOL}://${import.meta.env.VITE_HASURA_HOST}/v1/graphql`,
|
|
31
|
+
disablePong: true,
|
|
32
|
+
connectionParams: () => {
|
|
33
|
+
return {
|
|
34
|
+
headers: {
|
|
35
|
+
Authorization: `Bearer ${token}`,
|
|
36
|
+
'x-hasura-role': import.meta.env.VITE_HASURA_ROLE
|
|
37
|
+
}
|
|
38
|
+
}
|
|
35
39
|
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
})
|
|
40
|
+
})
|
|
39
41
|
|
|
40
|
-
const wsLink = new GraphQLWsLink(wsClient)
|
|
42
|
+
const wsLink = new GraphQLWsLink(wsClient)
|
|
41
43
|
|
|
42
|
-
const errorLink = onError(({ graphQLErrors, networkError, _operation, _forward }) => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
const errorLink = onError(({ graphQLErrors, networkError, _operation, _forward }) => {
|
|
45
|
+
if (graphQLErrors) {
|
|
46
|
+
for (const err of graphQLErrors) {
|
|
47
|
+
consola.debug('graphQLErrors: ', err)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
46
50
|
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
}
|
|
51
|
+
// To retry on network errors, we recommend the RetryLink
|
|
52
|
+
// instead of the onError link. This just logs the error.
|
|
53
|
+
if (networkError) {
|
|
54
|
+
if (networkError?.message?.match('JWTExpired')) {
|
|
55
|
+
// ретрай токен
|
|
56
|
+
// и якщо цей користувач ще валідний в БД
|
|
57
|
+
// то видавати йому новий токен
|
|
58
|
+
// тобто ця перевірка універсальна
|
|
59
|
+
// не в залежності який типом авторизувався користувач
|
|
60
|
+
// а в залежності від того чи він валідний в БД
|
|
61
|
+
refreshToken()
|
|
62
|
+
} else {
|
|
63
|
+
console.log(`[Network error]: ${networkError}`)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
})
|
|
64
67
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
refreshToken()
|
|
76
|
-
} else {
|
|
77
|
-
console.log(`[Network error]: ${networkError}`)
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
})
|
|
68
|
+
// Create the apollo client
|
|
69
|
+
apolloClient = new ApolloClient({
|
|
70
|
+
link: from([errorLink, wsLink]),
|
|
71
|
+
cache: new InMemoryCache(),
|
|
72
|
+
defaultOptions: {
|
|
73
|
+
watchQuery: {
|
|
74
|
+
fetchPolicy: 'cache-and-network'
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
})
|
|
81
78
|
|
|
82
|
-
|
|
83
|
-
export const apolloClient = new ApolloClient({
|
|
84
|
-
link: from([errorLink, wsLink]),
|
|
85
|
-
cache: new InMemoryCache(),
|
|
86
|
-
defaultOptions: {
|
|
87
|
-
watchQuery: {
|
|
88
|
-
fetchPolicy: 'cache-and-network'
|
|
89
|
-
}
|
|
79
|
+
consola.debug('End Apollo boot')
|
|
90
80
|
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
consola.debug('End Apollo boot')
|
|
81
|
+
}
|