Skip to content

Commit

Permalink
chore(types): add typecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
larbish committed Apr 30, 2024
1 parent f62a4b7 commit 779ae84
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxt prepare playground",
"dev": "nuxt dev playground --tunnel",
"build": "nuxt-module-build build",
"typecheck": "nuxi typecheck",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"prepack": "pnpm lint && pnpm build",
Expand Down
2 changes: 1 addition & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default defineNuxtModule<ModuleOptions>({
const publicToken = process.env.NUXT_PUBLIC_STUDIO_TOKENS
const iframeMessagingAllowedOrigins = process.env.IFRAME_MESSAGING_ALLOWED_ORIGINS
const gitInfo = await _getLocalGitInfo(nuxt.options.rootDir) || _getGitEnv() || {}
nuxt.options.runtimeConfig.studio = defu(nuxt.options.runtimeConfig.studio, {
nuxt.options.runtimeConfig.studio = defu(nuxt.options.runtimeConfig.studio as Record<string, unknown>, {

Check failure on line 84 in src/module.ts

View workflow job for this annotation

GitHub Actions / nightly (ubuntu-latest, 20)

Type 'Omit<Record<string, unknown>, "version" | "publicToken" | "gitInfo"> & Omit<{ version: string; publicToken: string | undefined; gitInfo: GitInfo; }, "version" | "publicToken" | "gitInfo"> & { ...; }' is not assignable to type '{ version: string; publicToken: any; gitInfo: { name: string; owner: string; url: string; }; }'.
version,
publicToken,
gitInfo,
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/components/ContentPreviewMode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { onMounted, ref, onUnmounted } from 'vue'
import type { Socket } from 'socket.io-client'
import type { PreviewResponse } from '../types'
// @ts-expect-error import does exist
import { useCookie, useNuxtApp, useRouter } from '#app'
const props = defineProps({
Expand Down Expand Up @@ -71,6 +70,7 @@ const sync = async (data: PreviewResponse) => {
// Remove query params in url to refresh page (in case of 404 with no SPA fallback)
await router.replace({ query: {} })
// @ts-expect-error custom hook
nuxtApp.callHook('nuxt-studio:preview:ready')
if (window.parent && window.self !== window.parent) {
Expand Down
27 changes: 15 additions & 12 deletions src/runtime/composables/useStudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { AppConfig } from 'nuxt/schema'
import ContentPreviewMode from '../components/ContentPreviewMode.vue'
import { createSingleton, deepAssign, deepDelete, mergeDraft, StudioConfigFiles } from '../utils'
import type { PreviewFile, PreviewResponse, FileChangeMessagePayload } from '../types'
// @ts-expect-error import does exist
import { callWithNuxt } from '#app'
import { useAppConfig, useNuxtApp, useRuntimeConfig, useState, useContentState, queryContent, ref, toRaw, useRoute, useRouter } from '#imports'

Expand Down Expand Up @@ -35,6 +34,7 @@ export const useStudio = () => {
const storage = useState<Storage | null>('studio-client-db', () => null)

if (!storage.value) {
// @ts-expect-error custom hook
nuxtApp.hook('content:storage', (_storage: Storage) => {
storage.value = _storage
})
Expand Down Expand Up @@ -62,17 +62,17 @@ export const useStudio = () => {
)
}

const syncPreviewAppConfig = (appConfig?: AppConfig) => {
const syncPreviewAppConfig = (appConfig?: ParsedContent) => {
const _appConfig = callWithNuxt(nuxtApp, useAppConfig) as AppConfig

// Set dynamic icons for preview if user is using @nuxt/ui
if (_appConfig?.ui) {
(_appConfig.ui as AppConfig).icons = { ...(_appConfig.ui as AppConfig).icons as AppConfig, dynamic: true }
(_appConfig.ui as Record<string, unknown>).icons = { ...(_appConfig.ui as Record<string, unknown>).icons as AppConfig, dynamic: true }
}

// Using `defu` to merge with initial config
// This is important to revert to default values for missing properties
deepAssign(_appConfig, defu(appConfig as AppConfig, initialAppConfig))
deepAssign(_appConfig, defu(appConfig as ParsedContent, initialAppConfig))

// Reset app config to initial state if no appConfig is provided
// Makes sure that app config does not contain any preview data
Expand All @@ -81,27 +81,29 @@ export const useStudio = () => {
}
}

const syncPreviewTokensConfig = (tokensConfig?: AppConfig) => {
const syncPreviewTokensConfig = (tokensConfig?: ParsedContent) => {
// Tokens config (optional; depends on the presence of pinceauTheme provide)
// TODO: Improve typings
// TODO: Use `inject()` but wrong context seem to be resolved; while $pinceauTheme global property is present in `app` context
const themeSheet = nuxtApp?.vueApp?._context?.config?.globalProperties?.$pinceauTheme
const themeSheet = nuxtApp?.vueApp?._context?.config?.globalProperties?.$pinceauTheme as Record<string, unknown>

// Pinceau might be not present, or not booted yet
if (!themeSheet || !themeSheet?.updateTheme) return

// Set initial tokens config on first call
if (!initialTokensConfig) {
initialTokensConfig = JSON.parse(JSON.stringify(themeSheet?.theme.value || {}))
initialTokensConfig = JSON.parse(JSON.stringify((themeSheet?.theme as Record<string, unknown>).value || {}))
}

// Call updateTheme with new config
callWithNuxt(
nuxtApp,
themeSheet.updateTheme, [
// eslint-disable-next-line @typescript-eslint/no-explicit-any
themeSheet.updateTheme as any,
[
// Using `defu` to merge with initial tokens
// This is important to revert to default values for missing properties
defu(tokensConfig as AppConfig, initialTokensConfig),
defu(tokensConfig as ParsedContent, initialTokensConfig),
],
)
}
Expand All @@ -125,10 +127,10 @@ export const useStudio = () => {
await syncPreviewFiles(storage.value, contentFiles)

const appConfig = mergedFiles.find(item => item.path === StudioConfigFiles.appConfig)
syncPreviewAppConfig(appConfig?.parsed)
syncPreviewAppConfig(appConfig?.parsed as ParsedContent)

const tokensConfig = mergedFiles.find(item => item.path === StudioConfigFiles.tokensConfig)
syncPreviewTokensConfig(tokensConfig?.parsed)
syncPreviewTokensConfig(tokensConfig?.parsed as ParsedContent)

requestRerender()

Expand Down Expand Up @@ -209,7 +211,7 @@ export const useStudio = () => {

const requestRerender = async () => {
if (contentConfig?.documentDriven) {
// Update all cached pages
// @ts-expect-error Update all cached pages
const { pages } = callWithNuxt(nuxtApp, useContentState)

const contents = await Promise.all(Object.keys(pages.value).map(async (key) => {
Expand Down Expand Up @@ -359,6 +361,7 @@ export const useStudio = () => {
route.meta.studio_page_contentId = page?._id
})

// @ts-expect-error custom hook
nuxtApp.hook('nuxt-studio:preview:ready', () => {
window.parent.postMessage({
type: 'nuxt-studio:preview:ready',
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/plugins/preview.client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { defineNuxtPlugin, useCookie, useRoute, useRuntimeConfig, useState } from '#imports'
// @ts-expect-error import does exist
import type { NuxtApp } from '#app'

export default defineNuxtPlugin((nuxtApp: NuxtApp) => {
export default defineNuxtPlugin((nuxtApp) => {
const runtimeConfig = useRuntimeConfig().public.studio || {}
const route = useRoute()
const previewToken = useCookie('previewToken', { sameSite: 'none', secure: true })
Expand Down Expand Up @@ -35,6 +33,7 @@ export default defineNuxtPlugin((nuxtApp: NuxtApp) => {

// Listen to `content:storage` hook to get storage instance
// There is some cases that `content:storage` hook is called before initializing preview
// @ts-expect-error custom hook
nuxtApp.hook('content:storage', (_storage: Storage) => {
storage.value = _storage
})
Expand Down
15 changes: 8 additions & 7 deletions src/runtime/server/routes/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export default eventHandler(async () => {

const appConfig = useAppConfig()
const runtimeConfig = useRuntimeConfig()
const { app, contentSchema, appConfigSchema, studio, content: { sources, ignores, locales, defaultLocale, highlight, navigation, documentDriven, experimental } } = runtimeConfig
const { app, contentSchema, appConfigSchema, studio, content } = runtimeConfig
const { sources, ignores, locales, defaultLocale, highlight, navigation, documentDriven, experimental } = content as Record<string, unknown>

// Delete GitHub tokens for multiple source to avoid exposing them
const safeSources: Record<string, unknown> = {}
Object.keys(sources).forEach((name) => {
const { driver, prefix, base, repo, branch, dir } = sources[name] || {}
Object.keys(sources as Record<string, unknown>).forEach((name) => {
const { driver, prefix, base, repo, branch, dir } = (sources as Record<string, unknown>)[name] as Record<string, unknown> || {}
safeSources[name] = {
driver,
prefix,
Expand All @@ -45,7 +46,7 @@ export default eventHandler(async () => {
dir,
}
})
const hasPinceau = runtimeConfig?.pinceau?.studio
const hasPinceau = (runtimeConfig?.pinceau as Record<string, unknown>)?.studio
let tokensConfig
let tokensConfigSchema
if (hasPinceau) {
Expand All @@ -57,9 +58,9 @@ export default eventHandler(async () => {

return {
// Studio version
version: studio.version,
tokens: studio?.publicToken,
gitInfo: studio?.gitInfo || {},
version: (studio as Record<string, unknown>)?.version,
tokens: (studio as Record<string, unknown>)?.publicToken,
gitInfo: (studio as Record<string, unknown>)?.gitInfo || {},
// nuxt.schema for Nuxt Content frontmatter
contentSchema: contentSchema || {},
// app.config
Expand Down

0 comments on commit 779ae84

Please sign in to comment.