@nitra/vite-boot 1.0.0
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/LICENSE +29 -0
- package/README.md +1 -0
- package/package.json +38 -0
- package/src/apollo.js +117 -0
- package/src/i18n.js +32 -0
- package/src/pinia.js +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019, Nick Petruzzelli
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @nitra/vite-boot
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nitra/vite-boot",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vite boot",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./apollo": "src/apollo.js",
|
|
8
|
+
"./i18n": "src/i18n.js",
|
|
9
|
+
"./pinia": "src/pinia.js"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git@github.com:nitra/graphql-custom.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"nitra",
|
|
17
|
+
"vite",
|
|
18
|
+
"boot"
|
|
19
|
+
],
|
|
20
|
+
"author": "vitaliytv <vitaliytv@nitralabs.com>",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"prettier": "@nitra/prettier-config",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@nitra/prettier-config": "^1.0.0"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@apollo/client": "^3.5.6",
|
|
31
|
+
"@nitra/consola": "^1.3.1",
|
|
32
|
+
"graphql": "^16.2.0",
|
|
33
|
+
"graphql-ws": "^5.5.5",
|
|
34
|
+
"petite-vue-i18n": "^9.2.0-beta.26",
|
|
35
|
+
"pinia": "^2.0.9",
|
|
36
|
+
"pinia-plugin-persistedstate": "^1.0.3"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/apollo.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/* global CloseEvent */
|
|
2
|
+
|
|
3
|
+
import { ApolloLink, ApolloClient, Observable, InMemoryCache, from } from '@apollo/client/core'
|
|
4
|
+
import { onError } from '@apollo/client/link/error'
|
|
5
|
+
import { print } from 'graphql'
|
|
6
|
+
import { createClient } from 'graphql-ws'
|
|
7
|
+
import { createLogger } from '@nitra/consola/browser'
|
|
8
|
+
|
|
9
|
+
const consola = createLogger(import.meta.url)
|
|
10
|
+
|
|
11
|
+
class WebSocketLink extends ApolloLink {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super()
|
|
14
|
+
this.client = createClient(options)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
request(operation) {
|
|
18
|
+
return new Observable(sink => {
|
|
19
|
+
return this.client.subscribe(
|
|
20
|
+
{ ...operation, query: print(operation.query) },
|
|
21
|
+
{
|
|
22
|
+
next: sink.next.bind(sink),
|
|
23
|
+
complete: sink.complete.bind(sink),
|
|
24
|
+
error: err => {
|
|
25
|
+
if (Array.isArray(err)) {
|
|
26
|
+
// GraphQLError[]
|
|
27
|
+
return sink.error(new Error(err.map(({ message }) => message).join(', ')))
|
|
28
|
+
}
|
|
29
|
+
if (err instanceof CloseEvent) {
|
|
30
|
+
return sink.error(
|
|
31
|
+
new Error(
|
|
32
|
+
`Socket closed with event ${err.code} ${err.reason || ''}` // reason will be available on clean closes only
|
|
33
|
+
)
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
return sink.error(err)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const wsLink = new WebSocketLink({
|
|
45
|
+
url: `${import.meta.env.VITE_HASURA_PROTOCOL}://${import.meta.env.VITE_HASURA_HOST}/v1/graphql`,
|
|
46
|
+
disablePong: true,
|
|
47
|
+
connectionParams: () => {
|
|
48
|
+
// const session = getSession();
|
|
49
|
+
// if (!session) {
|
|
50
|
+
// return {};
|
|
51
|
+
// }
|
|
52
|
+
const token = localStorage.getItem('token')
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
headers: {
|
|
56
|
+
Authorization: `Bearer ${token}`,
|
|
57
|
+
'x-hasura-role': import.meta.env.VITE_HASURA_ROLE
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
const errorLink = onError(({ graphQLErrors, networkError, operation, forward }) => {
|
|
64
|
+
if (graphQLErrors) {
|
|
65
|
+
for (const err of graphQLErrors) {
|
|
66
|
+
consola.debug('graphQLErrors: ', err)
|
|
67
|
+
|
|
68
|
+
// TODO: реалізуввати ретрай токен
|
|
69
|
+
// ще один токен
|
|
70
|
+
// в якому будуть міститись закодовані дані користувача
|
|
71
|
+
// и якщо цей користувач ще валідний в БД
|
|
72
|
+
// то видавати йому новий токен
|
|
73
|
+
// тобто ця перевірка універсальна
|
|
74
|
+
// не в залежності який типом авторизувався користувач
|
|
75
|
+
// а в залежності від того чи він валідний в БД
|
|
76
|
+
// switch (err.extensions.code) {
|
|
77
|
+
// // Apollo Server sets code to UNAUTHENTICATED
|
|
78
|
+
// // when an AuthenticationError is thrown in a resolver
|
|
79
|
+
// case 'UNAUTHENTICATED':
|
|
80
|
+
// // Modify the operation context with a new token
|
|
81
|
+
// const oldHeaders = operation.getContext().headers
|
|
82
|
+
// operation.setContext({
|
|
83
|
+
// headers: {
|
|
84
|
+
// ...oldHeaders,
|
|
85
|
+
// authorization: getNewToken()
|
|
86
|
+
// }
|
|
87
|
+
// })
|
|
88
|
+
// // Retry the request, returning the new observable
|
|
89
|
+
// return forward(operation)
|
|
90
|
+
// }
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// To retry on network errors, we recommend the RetryLink
|
|
95
|
+
// instead of the onError link. This just logs the error.
|
|
96
|
+
if (networkError) {
|
|
97
|
+
if (networkError?.message.match('JWTExpired')) {
|
|
98
|
+
localStorage.removeItem('token')
|
|
99
|
+
window.location.replace('/login')
|
|
100
|
+
} else {
|
|
101
|
+
console.log(`[Network error]: ${networkError}`)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
// Create the apollo client
|
|
107
|
+
export const apolloClient = new ApolloClient({
|
|
108
|
+
link: from([errorLink, wsLink]),
|
|
109
|
+
cache: new InMemoryCache(),
|
|
110
|
+
defaultOptions: {
|
|
111
|
+
watchQuery: {
|
|
112
|
+
fetchPolicy: 'cache-and-network'
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
consola.debug('End Apollo boot')
|
package/src/i18n.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createI18n } from 'petite-vue-i18n'
|
|
2
|
+
import { createLogger } from '@nitra/consola/browser'
|
|
3
|
+
|
|
4
|
+
const consola = createLogger(import.meta.url)
|
|
5
|
+
|
|
6
|
+
export const i18n = createI18n({
|
|
7
|
+
locale: 'ru',
|
|
8
|
+
fallbackLocale: 'ru',
|
|
9
|
+
legacy: false
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Переводимо з варіанту:
|
|
14
|
+
* { "индекс": "індекс" }
|
|
15
|
+
* в
|
|
16
|
+
* {
|
|
17
|
+
* "ru": { "индекс": "индекс" },
|
|
18
|
+
* "uk": { "индекс": "індекс" }
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @param {Object} uk
|
|
22
|
+
* @returns {Object}
|
|
23
|
+
*/
|
|
24
|
+
export const mt = uk => {
|
|
25
|
+
const ukP = { uk: uk }
|
|
26
|
+
|
|
27
|
+
ukP.ru = Object.assign({}, ...Object.keys(uk).map(key => ({ [key]: key })))
|
|
28
|
+
|
|
29
|
+
return ukP
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
consola.debug('End i18n boot')
|
package/src/pinia.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { createPinia } from 'pinia'
|
|
2
|
+
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
|
|
3
|
+
import { createLogger } from '@nitra/consola/browser'
|
|
4
|
+
|
|
5
|
+
const consola = createLogger(import.meta.url)
|
|
6
|
+
|
|
7
|
+
export const pinia = createPinia()
|
|
8
|
+
pinia.use(piniaPluginPersistedstate)
|
|
9
|
+
|
|
10
|
+
consola.debug('End Pinia boot')
|