@nitra/cap 1.0.3 → 1.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nitra/cap",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Nitra capacitor components",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -15,30 +15,38 @@ saveToken() => Promise<void>
15
15
  ```
16
16
 
17
17
  ## usage with Vite
18
- ### Set vapid key : ``` import.meta.env.VITE_VAPID_KEY ```
19
- ### Set host url of the notify project: ``` import.meta.env.VITE_NOTIFY_URL ```
20
- The ```notify project``` is a server-side component designed to handle API requests for saving user push tokens. It uses the Fastify framework and JWT-based security for authentication.
21
- ### Or set for local dev: ```import.meta.env.VITE_LOCALHOST```
22
18
 
23
- ### Set the name of your app: ```import.meta.env.VITE_APP```
19
+ ### Set vapid key : `import.meta.env.VITE_VAPID_KEY`
20
+
21
+ ### Set host url of the notify project: `import.meta.env.VITE_NOTIFY_URL`
22
+
23
+ The `notify project` is a server-side component designed to handle API requests for saving user push tokens. It uses the Fastify framework and JWT-based security for authentication.
24
+
25
+ ### Or set for local dev: `import.meta.env.VITE_LOCALHOST`
26
+
27
+ ### Set the name of your app: `import.meta.env.VITE_APP`
24
28
 
25
29
  ## usage the library
30
+
26
31
  ### To use this package, you should add this code to your project where you want to provide push notifications
27
32
 
28
33
  ```typescript
29
34
  initializeApp({
30
- apiKey: import.meta.env.VITE_API_KEY,
31
- authDomain: import.meta.env.VITE_AUTH_DOMAIN,
32
- projectId: import.meta.env.VITE_PROJECT_ID,
33
- storageBucket: import.meta.env.VITE_STORAGE_BUCKET,
34
- messagingSenderId: import.meta.env.VITE_MESSAGING_SENDER_ID,
35
- appId: import.meta.env.VITE_APP_ID
35
+ apiKey: import.meta.env.VITE_API_KEY,
36
+ authDomain: import.meta.env.VITE_AUTH_DOMAIN,
37
+ projectId: import.meta.env.VITE_PROJECT_ID,
38
+ storageBucket: import.meta.env.VITE_STORAGE_BUCKET,
39
+ messagingSenderId: import.meta.env.VITE_MESSAGING_SENDER_ID,
40
+ appId: import.meta.env.VITE_APP_ID
36
41
  })
37
42
  ```
43
+
38
44
  ### This code will initialize firebase
39
45
 
40
46
  ## example in node
47
+
41
48
  ### Using function after login
49
+
42
50
  ```jsx
43
51
  <template>
44
52
  <n-code @token-checked="saveToken" />
@@ -59,26 +67,60 @@ initializeApp({
59
67
  ```
60
68
 
61
69
  ## Environment Variable Validation with Vite
62
- ### To ensure that required environment variables are set, add the following to your vite.config.js
70
+
71
+ ### To ensure that the required environment variables are set and the sw.js file is added to the project, add the following to your vite.config.js
72
+
63
73
  ```typescript
64
74
  import { requireEnvVar } from '@nitra/vite-check-env'
75
+ import { defineConfig, loadEnv } from 'vite'
76
+ import { build } from 'esbuild'
65
77
 
66
78
  export default {
67
79
  plugins: [
80
+ {
81
+ name: 'nitra-service-worker-transform',
82
+ apply: 'build',
83
+ transformIndexHtml() {
84
+ build({
85
+ plugins: [env(loadEnv(mode, '.'))],
86
+ format: 'esm',
87
+ entryPoints: [path.join(process.cwd(), '../node_modules/@nitra/cap/save-token/sw.js')],
88
+ outfile: path.join(process.cwd(), 'dist', 'sw.js')
89
+ })
90
+ }
91
+ },
68
92
  requireEnvVar([
93
+ 'VITE_VAPID_KEY',
94
+ 'VITE_APP',
69
95
  'VITE_API_KEY',
70
96
  'VITE_AUTH_DOMAIN',
71
- 'VITE_PROJECT_ID'
97
+ 'VITE_PROJECT_ID',
72
98
  'VITE_STORAGE_BUCKET',
73
99
  'VITE_MESSAGING_SENDER_ID',
74
- 'VITE_APP_ID',
75
- 'VITE_VAPID_KEY',
76
- 'VITE_NOTIFY_URL',
77
- 'VITE_APP'])
100
+ 'VITE_APP_ID'
101
+ ])
78
102
  ]
79
103
  }
104
+
105
+ function env(props) {
106
+ return {
107
+ name: 'env',
108
+ setup: build => {
109
+ const options = build.initialOptions
110
+ const define = options.define ?? {}
111
+ for (const k in props) {
112
+ define[`import.meta.env.${k}`] = JSON.stringify(props[k])
113
+ }
114
+ options.define = {
115
+ ...define
116
+ }
117
+ }
118
+ }
119
+ }
80
120
  ```
121
+
81
122
  ### Install the plugin in the project directory
123
+
82
124
  ```typescript
83
125
  yarn add -D @nitra/vite-check-env
84
126
  ```
@@ -1,7 +1,7 @@
1
1
  import { Capacitor } from '@capacitor/core'
2
2
  import { FirebaseMessaging } from '@capacitor-firebase/messaging'
3
3
  import { getToken } from '@nitra/vite-boot/token'
4
- import { initializeApp } from "firebase/app"
4
+ import { initializeApp } from 'firebase/app'
5
5
 
6
6
  /**
7
7
  * Save the Firebase push token for a given app and platform.
@@ -65,7 +65,7 @@ export async function saveToken() {
65
65
  * @returns {Promise<object>} Web Push token
66
66
  */
67
67
  async function getWebPushToken() {
68
- const serviceWorkerRegistration = await navigator.serviceWorker.register('src/firebase-messaging-sw.js', {
68
+ const serviceWorkerRegistration = await navigator.serviceWorker.register('/sw.js', {
69
69
  type: 'module'
70
70
  })
71
71
 
File without changes