Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed Nov 15, 2022
1 parent 17d108e commit 630d501
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
18 changes: 12 additions & 6 deletions src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useProviderStore } from '~/stores/provider'
import { useFeatureFlagStore } from '~/stores/feature-flag'
import { useUiStore } from '~/stores/ui'

import type { Middleware } from '@nuxt/types'
import type { Context, Middleware } from '@nuxt/types'

/**
* In embedded mode, the app sends its url
Expand All @@ -20,7 +20,13 @@ import type { Middleware } from '@nuxt/types'
* Currently, one event type is used:
* - `urlChange` sends the relative path of the URL on every URL change.
*/
const middleware: Middleware = async ({ app, query, route, $pinia }) => {
const middleware: Middleware = async ({
$cookies,
$ua,
query,
route,
$pinia,
}: Context) => {
/* Nav store */

const navigationStore = useNavigationStore($pinia)
Expand All @@ -46,14 +52,14 @@ const middleware: Middleware = async ({ app, query, route, $pinia }) => {
/* Feature flag store */

const featureFlagStore = useFeatureFlagStore($pinia)
featureFlagStore.initFromCookies(app.$cookies.get('features') ?? {})
featureFlagStore.initFromCookies($cookies.get('features') ?? {})
featureFlagStore.initFromQuery(query)

/* UI store */

const uiStore = useUiStore($pinia)
const isMobileUa = app.$ua ? app.$ua.isMobile : false
app.$cookies.set('uiIsMobileUa', isMobileUa)
uiStore.initFromCookies(app.$cookies.getAll() ?? {})
const isMobileUa = $ua ? $ua.isMobile : false
$cookies.set('uiIsMobileUa', isMobileUa)
uiStore.initFromCookies($cookies.getAll() ?? {})
}
export default middleware
4 changes: 2 additions & 2 deletions src/pages/preferences.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default defineComponent({
VCheckbox,
},
setup() {
const { app } = useContext()
const { $cookies } = useContext()
const featureFlagStore = useFeatureFlagStore()
const flags = computed(() => featureFlagStore.flags)
Expand All @@ -106,7 +106,7 @@ export default defineComponent({
checked: boolean
}) => {
featureFlagStore.toggleFeature(name, checked ? ON : OFF)
app.$cookies.set('features', featureFlagStore.flagStateMap)
$cookies.set('features', featureFlagStore.flagStateMap)
}
return {
Expand Down
4 changes: 2 additions & 2 deletions src/stores/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const useUiStore = defineStore('ui', {
updateBreakpoint(isDesktopLayout: boolean) {
if (this.isDesktopLayout !== isDesktopLayout) {
this.isDesktopLayout = isDesktopLayout
this.$nuxt.app.$cookies.set(
this.$nuxt.$cookies.set(
'uiIsDesktopLayout',
this.isDesktopLayout,
cookieOptions
Expand All @@ -125,7 +125,7 @@ export const useUiStore = defineStore('ui', {
this.innerFilterVisible = visible
if (this.isDesktopLayout) {
this.isFilterDismissed = !visible
this.$nuxt.app.$cookies.set(
this.$nuxt.$cookies.set(
'uiIsFilterDismissed',
this.isFilterDismissed,
cookieOptions
Expand Down
8 changes: 3 additions & 5 deletions test/unit/test-utils/pinia.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ export const createPinia = () =>
captureException: jest.fn(),
captureEvent: jest.fn(),
},
app: {
$cookies: {
set: jest.fn(),
get: jest.fn(),
},
$cookies: {
set: jest.fn(),
get: jest.fn(),
},
},
}))
Expand Down
31 changes: 31 additions & 0 deletions typings/nuxt__types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
import '@nuxt/types'
import '@nuxtjs/i18n'
import { CookieSerializeOptions } from 'cookie'

import type { OpenverseCookieState } from '~/types/cookies'

import type { Details as UADetails } from 'express-useragent'

export interface SetParams<Key extends keyof OpenverseCookieState> {
name: Key
value: OpenverseCookieState[Key]
opts?: CookieSerializeOptions
}

export interface NuxtCookies {
set: <Key extends keyof OpenverseCookieState>(
name: Key,
value: OpenverseCookieState[Key],
opts?: CookieSerializeOptions
) => void
setAll: (cookieArray: SetParams[]) => void
get: <Key extends keyof OpenverseCookieState>(
name: Key,
opts?: GetOptions
) => OpenverseCookieState[Key]
getAll: (opts?: GetOptions) => OpenverseCookieState
remove: <Key extends keyof OpenverseCookieState>(
name: Key,
opts?: CookieSerializeOptions
) => void
removeAll: () => void
}

declare module '@nuxt/types' {
export interface Context {
$ua: UADetails | null
$cookies: NuxtCookies
}
export interface NuxtAppOptions {
$ua: UADetails | null
$cookies: NuxtCookies
}
}

Expand Down

0 comments on commit 630d501

Please sign in to comment.