nitro-web 0.0.28 → 0.0.29

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/client/app.tsx CHANGED
@@ -49,9 +49,8 @@ export async function setupApp(config: Config, storeContainer: StoreContainer, l
49
49
  // Setup the jwt token
50
50
  updateJwt(localStorage.getItem(injectedConfig.jwtName))
51
51
 
52
- // Fetch the store/state
53
- const data = (await settings.beforeApp(config)) || {}
54
- // Make the store data available to the store
52
+ // Fetch the store data, and make it available to the store
53
+ const data = await settings.beforeApp(config)
55
54
  Object.assign(preloadedStoreData, data)
56
55
 
57
56
  const root = ReactDOM.createRoot(document.getElementById('app') as HTMLElement)
@@ -270,8 +269,8 @@ async function beforeApp(config: Config) {
270
269
  * Gets called once before React is initialised
271
270
  * @return {promise} - newStoreData which is used for sharedStore, later merged with the config.store() defaults
272
271
  */
273
- let apiAvailable
274
- let stateData
272
+ let apiAvailable = false
273
+ let storeData = {}
275
274
  try {
276
275
  // Unload prehot data
277
276
  // if (window.prehot) {
@@ -279,14 +278,14 @@ async function beforeApp(config: Config) {
279
278
  // delete window.prehot
280
279
  // }
281
280
  if (!config.isStatic) {
282
- stateData = (await axios().get('/api/state', { 'axios-retry': { retries: 3 }, timeout: 4000 } as AxiosRequestConfig)).data
281
+ storeData = (await axios().get('/api/store', { 'axios-retry': { retries: 3 }, timeout: 4000 } as AxiosRequestConfig)).data
283
282
  apiAvailable = true
284
283
  }
285
284
  } catch (err) {
286
285
  console.error('We had trouble connecting to the API, please refresh')
287
286
  console.log(err)
288
287
  }
289
- return { ...stateData, apiAvailable }
288
+ return { ...storeData, apiAvailable }
290
289
  }
291
290
 
292
291
  const defaultMiddleware = {
package/client/store.ts CHANGED
@@ -4,8 +4,8 @@ import { axios, isObject } from 'nitro-web/util'
4
4
  import { updateJwt } from 'nitro-web'
5
5
  import { Store } from 'nitro-web/types'
6
6
 
7
- export let preloadedStoreData: Store
8
- export let exposedStoreData: Store
7
+ export const preloadedStoreData: Store = {}
8
+ export let exposedStoreData: Store = preloadedStoreData
9
9
 
10
10
  export function createStore<T extends Store>(store: T) {
11
11
  const container = createContainer(() => {
@@ -14,7 +14,7 @@ const JWT_SECRET = process.env.JWT_SECRET || 'replace_this_with_secure_env_secre
14
14
  export default {
15
15
 
16
16
  routes: {
17
- 'get /api/state': ['state'],
17
+ 'get /api/store': ['store'],
18
18
  'get /api/signout': ['signout'],
19
19
  'post /api/signin': ['signin'],
20
20
  'post /api/signup': ['signup'],
@@ -87,8 +87,8 @@ export default {
87
87
  })
88
88
  },
89
89
 
90
- state: async function (req, res) {
91
- res.json(await this._getState(req.user))
90
+ store: async function (req, res) {
91
+ res.json(await this._getStore(req.user))
92
92
  },
93
93
 
94
94
  signup: async function (req, res) {
@@ -208,10 +208,10 @@ export default {
208
208
 
209
209
  /* ---- Private fns ---------------- */
210
210
 
211
- _getState: async function (user) {
212
- // Initial state
211
+ _getStore: async function (user) {
212
+ // Initial store
213
213
  return {
214
- user: user || null,
214
+ user: user || undefined,
215
215
  }
216
216
  },
217
217
 
@@ -220,8 +220,8 @@ export default {
220
220
  user.desktop = isDesktop
221
221
 
222
222
  const jwt = jsonwebtoken.sign({ _id: user._id }, JWT_SECRET, { expiresIn: '30d' })
223
- const state = await this._getState(user)
224
- return { ...state, jwt }
223
+ const store = await this._getStore(user)
224
+ return { ...store, jwt }
225
225
  },
226
226
 
227
227
  _tokenCreate: function (id) {
@@ -10,7 +10,7 @@ export function ResetInstructions() {
10
10
  async function onSubmit (event: React.FormEvent<HTMLFormElement>) {
11
11
  try {
12
12
  await util.request('post /api/reset-instructions', state, event, isLoading)
13
- setStore(s => ({ ...s, message: 'Done! Please check your email.' }))
13
+ setStore((s) => ({ ...s, message: 'Done! Please check your email.' }))
14
14
  navigate('/signin')
15
15
  } catch (e) {
16
16
  return setState({ ...state, errors: e as Errors })
@@ -53,7 +53,7 @@ export function ResetPassword() {
53
53
  async function onSubmit (event: React.FormEvent<HTMLFormElement>) {
54
54
  try {
55
55
  const data = await util.request('post /api/reset-password', state, event, isLoading)
56
- setStore(() => data)
56
+ setStore((s) => ({ ...s, ...data }))
57
57
  navigate('/')
58
58
  } catch (e) {
59
59
  return setState({ ...state, errors: e as Errors })
@@ -7,6 +7,7 @@ export function Signin() {
7
7
  const isSignout = location.pathname == '/signout'
8
8
  const isLoading = useState(isSignout)
9
9
  const [, setStore] = useTracked()
10
+
10
11
  const [state, setState] = useState({
11
12
  email: injectedConfig.env == 'development' ? (injectedConfig.placeholderEmail || '') : '',
12
13
  password: injectedConfig.env == 'development' ? '1234' : '',
@@ -21,7 +22,7 @@ export function Signin() {
21
22
 
22
23
  useEffect(() => {
23
24
  if (isSignout) {
24
- setStore(() => ({ user: null }))
25
+ setStore((s) => ({ ...s, user: undefined }))
25
26
  // util.axios().get('/api/signout')
26
27
  Promise.resolve()
27
28
  .then(() => isLoading[1](false))
@@ -35,7 +36,7 @@ export function Signin() {
35
36
  try {
36
37
  const data = await util.request('post /api/signin', state, e, isLoading)
37
38
  isLoading[1](true)
38
- setStore(() => data)
39
+ setStore((s) => ({ ...s, ...data }))
39
40
  setTimeout(() => { // wait for setStore
40
41
  if (location.search.includes('redirect')) navigate(location.search.replace('?redirect=', ''))
41
42
  else navigate('/')
@@ -17,7 +17,7 @@ export function Signup() {
17
17
  try {
18
18
  const data = await util.request('post /api/signup', state, e, isLoading)
19
19
  isLoading[1](true)
20
- setStore(() => data)
20
+ setStore((s) => ({ ...s, ...data }))
21
21
  setTimeout(() => navigate('/'), 0) // wait for setStore
22
22
  } catch (e) {
23
23
  return setState({ ...state, errors: e as Errors })
@@ -74,7 +74,7 @@ export function Message() {
74
74
 
75
75
  function hide() {
76
76
  setVisible(false)
77
- setTimeout(() => setStore(s => ({ ...s, message: null })), 250)
77
+ setTimeout(() => setStore(s => ({ ...s, message: undefined })), 250)
78
78
  }
79
79
 
80
80
  return (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nitro-web",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "repository": "github:boycce/nitro-web",
5
5
  "homepage": "https://boycce.github.io/nitro-web/",
6
6
  "description": "Nitro is a battle-tested, modular base project to turbocharge your projects, styled using Tailwind 🚀",
package/tsconfig.json CHANGED
@@ -31,6 +31,7 @@
31
31
  "client",
32
32
  "components/**/*.tsx",
33
33
  "components/**/*.ts",
34
- "types"
34
+ "types",
35
+ "./types/core-only-globals.d.ts"
35
36
  ]
36
37
  }
@@ -0,0 +1,9 @@
1
+ import { Store } from 'nitro-web/types'
2
+ import { Dispatch, SetStateAction } from 'react'
3
+
4
+ // Core-only global, this global will be defined globally in the project (e.g. in ./client/index.ts)
5
+ declare global {
6
+ const useTracked: () => [Store, Dispatch<SetStateAction<Store>>]
7
+ }
8
+
9
+ export {}
package/types.ts CHANGED
@@ -46,10 +46,10 @@ export type MessageObject = {
46
46
  }
47
47
 
48
48
  export type Store = {
49
- message?: MessageObject | string | null
50
- user?: User | null,
51
49
  apiAvailable?: boolean
52
50
  jwt?: string
51
+ message?: MessageObject | string
52
+ user?: User,
53
53
  }
54
54
 
55
55
  export type Svg = React.FC<React.SVGProps<SVGElement>>