bt-core-app 1.3.3 → 1.3.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.
@@ -1,3 +1,4 @@
1
+ import { ComputedRef } from 'vue';
1
2
  import { BTDemo } from '../composables/demo';
2
3
  import { RemovableRef } from '@vueuse/core';
3
4
 
@@ -52,15 +53,17 @@ export interface BTAuth {
52
53
  canEditPermit: (permit: string) => boolean;
53
54
  canView: (navName?: string) => boolean;
54
55
  canViewPermit: (permit: string) => boolean;
55
- credentials: any;
56
+ credentials: RemovableRef<any>;
56
57
  doShow: (subcodes?: string[], permissions?: string[], action?: 'view' | 'edit') => boolean;
57
58
  doShowByNav: (navName?: string | AuthItem, includeChildren?: boolean) => boolean;
58
59
  getAuthorizeUrl: (redirectPath?: string) => string;
59
60
  getTimeZone: () => string;
60
61
  getToken: (code?: string, state?: string) => Promise<void>;
62
+ isLoggedIn: ComputedRef<boolean>;
61
63
  login: (redirectPath?: string) => void;
62
64
  logout: (navNameRedirect?: string) => void;
63
65
  setAuth: (jwtToken?: string) => void;
66
+ timeZone: ComputedRef<string>;
64
67
  tryLogin: () => boolean | undefined;
65
68
  }
66
69
  export declare function useAuth(): BTAuth;
@@ -1,3 +1,5 @@
1
+ import { RemovableRef } from '@vueuse/core';
2
+
1
3
  interface CosmeticData {
2
4
  dark?: BaseCosmeticTheme;
3
5
  drawer?: boolean;
@@ -22,7 +24,7 @@ export interface UseCosmeticsOptions<T extends BaseCosmeticTheme> {
22
24
  defaultTheme?: string;
23
25
  }
24
26
  export interface BTCosmetics {
25
- state: CosmeticData;
27
+ state: RemovableRef<CosmeticData>;
26
28
  initiate: () => void;
27
29
  resetCosmetics: (toDefault: boolean) => void;
28
30
  setTemporaryColor: (color: string) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bt-core-app",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "Core app tools for some basic features like navigation, authentication, server apis, and cosmetics",
5
5
  "homepage": "https://github.com/BlitzItTech/bt-core",
6
6
  "bugs": {
@@ -25,7 +25,7 @@
25
25
  inline
26
26
  :is-24="false"
27
27
  :time-picker-inline="useTime"
28
- :timezone="auth.credentials.timeZone.value"
28
+ :timezone="auth.timeZone.value"
29
29
  utc
30
30
  v-bind="$attrs"
31
31
  v-model="value" />
@@ -16,7 +16,7 @@
16
16
  :model-auto="range"
17
17
  :range="range"
18
18
  :time-picker-inline="!range && useTime"
19
- :timezone="credentials.timeZone"
19
+ :timezone="timeZone"
20
20
  utc
21
21
  v-bind="$attrs"
22
22
  v-model="date" />
@@ -50,7 +50,7 @@
50
50
 
51
51
  const emit = defineEmits(['confirm', 'cancel'])
52
52
  const date = ref()
53
- const { credentials } = useAuth()
53
+ const { timeZone } = useAuth()
54
54
 
55
55
  const canAccept = computed(() => {
56
56
  return !props.required || date.value
@@ -188,13 +188,13 @@ export function createApi(options?: UseApiOptions): BTApi {
188
188
 
189
189
  function defaultBuildHeaders(path: PathOptions): HeadersInit {
190
190
  let fetchOptions:any = { ...path.headers }
191
- const auth = options?.auth?.credentials
191
+ //const auth = options?.auth?.credentials
192
192
 
193
193
  if (path.proxyID)
194
194
  fetchOptions.ManagedCompanyAccountID ??= path.proxyID
195
195
 
196
- if (options?.useBearerToken != false && auth?.isLoggedIn == true)
197
- fetchOptions.authorization ??= `bearer ${auth.token}`
196
+ if (options?.useBearerToken != false && options?.auth?.isLoggedIn.value == true)
197
+ fetchOptions.authorization ??= `bearer ${options?.auth?.credentials.value.token}`
198
198
 
199
199
  fetchOptions['Content-Type'] ??= (path.contentType ?? options?.defaultContentType ?? 'application/json')
200
200
 
@@ -3,8 +3,9 @@ import { DateTime } from 'luxon'
3
3
  import { type RemovableRef, useStorage } from '@vueuse/core'
4
4
  import { type BTDemo } from '../composables/demo'
5
5
  import { useRouter } from 'vue-router'
6
- import { toValue } from 'vue'
6
+ import { computed, type ComputedRef, toValue } from 'vue'
7
7
  import { useAuthUrl } from './urls'
8
+ import { useApi } from './api'
8
9
 
9
10
  export interface AuthItem {
10
11
  children?: AuthItem[]
@@ -65,15 +66,17 @@ export interface BTAuth {
65
66
  canEditPermit: (permit: string) => boolean
66
67
  canView: (navName?: string) => boolean
67
68
  canViewPermit: (permit: string) => boolean
68
- credentials: any
69
+ credentials: RemovableRef<any>
69
70
  doShow: (subcodes?: string[], permissions?: string[], action?: 'view' | 'edit') => boolean
70
71
  doShowByNav: (navName?: string | AuthItem, includeChildren?: boolean) => boolean
71
72
  getAuthorizeUrl: (redirectPath?: string) => string
72
73
  getTimeZone: () => string
73
74
  getToken: (code?: string, state?: string) => Promise<void>
75
+ isLoggedIn: ComputedRef<boolean>
74
76
  login: (redirectPath?: string) => void
75
77
  logout: (navNameRedirect?: string) => void
76
78
  setAuth: (jwtToken?: string) => void
79
+ timeZone: ComputedRef<string>
77
80
  tryLogin: () => boolean | undefined
78
81
  }
79
82
 
@@ -313,11 +316,8 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
313
316
  /**defaults to find parameters from route query */
314
317
  async function getToken(code?: string, state?: string) {
315
318
  options.getToken ??= async () => {
316
- // const route = useRoute()
317
-
318
- let mCode = code // ?? route.query?.code as string
319
- let mState = state //?? route.query?.state as string
320
- // let mRedirectPath = redirect_path // ?? route.query?.redirect_path as string
319
+ let mCode = code
320
+ let mState = state
321
321
  let mGrantType = options.oauthGrantType ?? 'authorization_token'
322
322
  let mClientID = options.oauthClientID ?? ''
323
323
 
@@ -342,24 +342,22 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
342
342
  formData['redirect_uri'] = `${window.location.origin}/authentication`
343
343
  formData['client_id'] = mClientID
344
344
 
345
- const res = await fetch(url, {
346
- method: 'POST',
347
- mode: 'cors',
348
- cache: 'no-cache',
349
- body: JSON.stringify(formData)
345
+ const api = useApi()
346
+ const res = await api.post<any>({
347
+ additionalUrl: url,
348
+ data: formData
350
349
  })
350
+
351
+ // const res = await fetch(url, {
352
+ // method: 'POST',
353
+ // mode: 'cors',
354
+ // cache: 'no-cache',
355
+ // body: JSON.stringify(formData)
356
+ // })
357
+
358
+ // const data = await res.json()
351
359
 
352
- const data = await res.json()
353
-
354
- setAuth(data.value?.access_token)
355
-
356
- // if (mRedirectPath) {
357
- // window.location.href = mRedirectPath
358
- // }
359
- // else {
360
- // const router = useRouter()
361
- // router.push({ name: 'home' })
362
- // }
360
+ setAuth(res.access_token)
363
361
  }
364
362
 
365
363
  return await options.getToken(code, state)
@@ -434,15 +432,19 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
434
432
  canEditPermit,
435
433
  canView,
436
434
  canViewPermit,
437
- credentials: state.value,
435
+ credentials: state,
438
436
  doShow,
439
437
  doShowByNav,
440
438
  getAuthorizeUrl,
441
439
  getTimeZone,
442
440
  getToken,
441
+ isLoggedIn: computed(() => state.value.isLoggedIn === true),
443
442
  login,
444
443
  logout,
445
444
  setAuth,
445
+ timeZone: computed(() => {
446
+ return state.value.timeZone ?? options.defaultTimeZone ?? defaultTimeZone
447
+ }),
446
448
  tryLogin
447
449
  }
448
450
 
@@ -48,7 +48,7 @@ export interface UseCosmeticsOptions<T extends BaseCosmeticTheme> {
48
48
  }
49
49
 
50
50
  export interface BTCosmetics {
51
- state: CosmeticData
51
+ state: RemovableRef<CosmeticData>
52
52
  initiate: () => void
53
53
  resetCosmetics: (toDefault: boolean) => void
54
54
  setTemporaryColor: (color: string) => void
@@ -153,7 +153,7 @@ export function useCosmetics(): BTCosmetics {
153
153
  }
154
154
 
155
155
  return {
156
- state: state.value,
156
+ state: state,
157
157
  initiate: loadCosmetics,
158
158
  resetCosmetics,
159
159
  setTemporaryColor,
@@ -193,14 +193,14 @@ export function createSessionStoreDefinition(options: UseSessionStoreOptions): B
193
193
  // const authData = useAuthData()
194
194
 
195
195
  function getKey(dOptions: PathOptions) {
196
- return `${options.storeName}_${options.auth?.credentials.userID ?? 'no-user-id'}_${dOptions.id ?? dOptions.data?.id ?? 'no-item-id'}`
196
+ return `${options.storeName}_${options.auth?.credentials.value.userID ?? 'no-user-id'}_${dOptions.id ?? dOptions.data?.id ?? 'no-item-id'}`
197
197
  }
198
198
 
199
199
  async function getAll<T extends BaseModel>(dOptions: PathOptions): Promise<StoreGetAllReturn<T>> {
200
200
  dOptions.additionalUrl ??= '/getAll'
201
201
 
202
202
  const path = buildPath(dOptions)
203
- const key = `${options.storeName}_${options.auth?.credentials.userID ?? 'no-user-id'}_${path}`
203
+ const key = `${options.storeName}_${options.auth?.credentials.value.userID ?? 'no-user-id'}_${path}`
204
204
  const refresh = dOptions.refresh
205
205
 
206
206
  if (!refresh && searchMemory.value[key] !== undefined)
@@ -508,7 +508,7 @@ export function createWholeLastUpdateStoreDefinition(options: UseWholeLastUpdate
508
508
  const cacheLocally = options.storageMode == 'local-cache'
509
509
 
510
510
  function getKey() {
511
- return `${options.storeName}_${options.auth?.credentials.userID ?? 'no-user-id'}`
511
+ return `${options.storeName}_${options.auth?.credentials.value.userID ?? 'no-user-id'}`
512
512
  }
513
513
 
514
514
  async function trySaveToLocalCache() {
package/test/auth.test.ts CHANGED
@@ -47,7 +47,7 @@ describe('default auth', () => {
47
47
 
48
48
  test('creates successfully', () => {
49
49
  const state = auth.authState
50
- const creds = auth.credentials
50
+ const creds = auth.credentials.value
51
51
 
52
52
  expect(state).not.toBeNull()
53
53
  expect(auth.credentials).not.toBeNull()