Skip to content

Commit

Permalink
fix: typecheck
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Nov 25, 2024
1 parent acfbe96 commit 4890997
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 474 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- run: pnpm dev:prepare
- run: pnpm lint
- run: pnpm prepack
- run: pnpm typecheck
- run: pnpm test
- name: Publish
run: pnpx pkg-pr-new publish --compact --no-template --pnpm
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
],
"scripts": {
"prepack": "nuxt-module-build build",
"typecheck": "nuxi typecheck",
"example": "run () { nuxi dev examples/$*; }; run",
"docs": "nuxi dev docs",
"docs:build": "nuxi build docs",
Expand Down Expand Up @@ -105,6 +106,7 @@
"@release-it/conventional-changelog": "^9.0.3",
"@types/better-sqlite3": "^7.6.12",
"@types/micromatch": "^4.0.9",
"@types/minimatch": "^5.1.2",
"@types/node": "^22.9.1",
"@types/pg": "^8.11.10",
"@types/ws": "^8.5.13",
Expand Down
448 changes: 29 additions & 419 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/runtime/adapters/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export default createDatabaseAdapter<{ filename: string }>((opts) => {
if (!db) {
const filename = !opts || isAbsolute(opts?.filename || '')
? opts?.filename
// @ts-expect-error - `_importMeta_` is defined in nitro runtime
: new URL(opts.filename, globalThis._importMeta_.url).pathname
: new URL(opts.filename, (globalThis as unknown as { _importMeta_: { url: string } })._importMeta_.url).pathname
db = new Database(filename)
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/internal/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function generateNavigationTree<T extends PageCollectionItemBase>(q
stem: content.stem,
children: [],
...pickNavigationFields(content),
})
} as ContentNavigationItem)

// Create navigation item from content
const navItem: ContentNavigationItem = getNavItem(content)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/internal/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const collectionQureyBuilder = <T extends keyof Collections>(collection:
async count(field: keyof Collections[T] | '*' = '*', distinct: boolean = false) {
return fetch(collection, buildQuery({
count: { field: String(field), distinct },
})).then(m => m[0].count)
})).then(m => (m[0] as { count: number }).count)
},
}

Expand Down
9 changes: 5 additions & 4 deletions src/runtime/internal/studio/collection.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { minimatch } from 'minimatch'
import type { CollectionInfo } from '@nuxt/content'
// import { joinURL, withoutLeadingSlash } from 'ufo'
import type { JsonSchema7ObjectType } from 'zod-to-json-schema'
import { getOrderedSchemaKeys } from '../schema'
// import { parseSourceBase } from './utils'
import { withoutRoot } from './files'

export const getCollectionByPath = (path: string, collections: Record<string, CollectionInfo>): CollectionInfo => {
return Object.values(collections).find((collection) => {
if (!collection.source) {
if (!collection.source || collection.source.length === 0) {
return
}

Expand All @@ -26,7 +27,7 @@ export const getCollectionByPath = (path: string, collections: Record<string, Co

const paths = pathWithoutRoot === '/' ? ['index.yml', 'index.yaml', 'index.md', 'index.json'] : [pathWithoutRoot]
return paths.some((p) => {
return minimatch(p, collection.source.include)
return collection.source.some(source => minimatch(p, source.include))
})
})
}
Expand Down Expand Up @@ -59,13 +60,13 @@ export function generateRecordSelectByColumn(collection: CollectionInfo, column:
function computeValuesBasedOnCollectionSchema(collection: CollectionInfo, data: Record<string, unknown>) {
const fields: string[] = []
const values: Array<string | number | boolean> = []
const properties = collection.schema.definitions[collection.name].properties
const properties = (collection.schema.definitions[collection.name] as JsonSchema7ObjectType).properties
const sortedKeys = getOrderedSchemaKeys(properties)

sortedKeys.forEach((key) => {
const value = (properties)[key]
// const underlyingType = getUnderlyingType(value as ZodType<unknown, ZodOptionalDef>)
const underlyingType = value.type
const underlyingType = (value as JsonSchema7ObjectType).type

const defaultValue = value.default ? value.default : 'NULL'
const valueToInsert = typeof data[key] !== 'undefined' ? data[key] : defaultValue
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/internal/studio/compatibility.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CollectionInfo, DraftSyncFile } from '@nuxt/content'
import type { JsonSchema7ObjectType } from 'zod-to-json-schema'

export const v2ToV3ParsedFile = (file: DraftSyncFile, collection: CollectionInfo) => {
const mappedFile: Record<string, unknown> = {
Expand All @@ -9,7 +10,7 @@ export const v2ToV3ParsedFile = (file: DraftSyncFile, collection: CollectionInfo
path: file.parsed._path,
}

const properties = collection.schema.definitions[collection.name].properties
const properties = (collection.schema.definitions[collection.name] as JsonSchema7ObjectType).properties

// Map parsed content to collection schema
Object.keys(file.parsed).forEach((key) => {
Expand Down
9 changes: 5 additions & 4 deletions src/runtime/internal/studio/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApp } from 'vue'
import type { AppConfig } from 'nuxt/schema'
import type { CollectionInfo, FileChangeMessagePayload, FileMessageData, FileSelectMessagePayload, DraftSyncData, PreviewFile, DraftSyncFile } from '@nuxt/content'
import type { PublicRuntimeConfig, CollectionInfo, FileChangeMessagePayload, FileMessageData, FileSelectMessagePayload, DraftSyncData, PreviewFile, DraftSyncFile } from '@nuxt/content'
import { withLeadingSlash } from 'ufo'
import StudioPreviewMode from '../../components/StudioPreviewMode.vue'
import { loadDatabaseAdapter } from '../database.client'
Expand Down Expand Up @@ -70,12 +70,13 @@ const syncDraftAppConfig = (appConfig?: Record<string, unknown>) => {
// Reset app config to initial state if no appConfig is provided
// Makes sure that app config does not contain any preview data
if (!appConfig) {
// @ts-expect-error -- TODO fix type
deepDelete(_appConfig, initialAppConfig)
}
}

export function mountPreviewUI() {
const { studio } = useRuntimeConfig().public
const studio: PublicRuntimeConfig['studio'] = useRuntimeConfig().public.studio || {}

const previewToken = window.sessionStorage.getItem('previewToken')
// Show loading
Expand All @@ -91,7 +92,7 @@ export function mountPreviewUI() {

export function initIframeCommunication() {
const nuxtApp = useNuxtApp()
const { studio } = useRuntimeConfig().public
const studio: PublicRuntimeConfig['studio'] = useRuntimeConfig().public.studio

// Not in an iframe
if (!window.parent || window.self === window.parent) {
Expand All @@ -116,7 +117,7 @@ export function initIframeCommunication() {
}
})

window.addEventListener('message', async (e: { data: FileMessageData }) => {
window.addEventListener('message', async (e: { origin: string, data: FileMessageData }) => {
if (!dbReady.value) {
return
}
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/plugins/studio/preview.client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { PublicRuntimeConfig } from '@nuxt/content'
import { defineNuxtPlugin, useCookie, useRoute, useRuntimeConfig } from '#imports'

export default defineNuxtPlugin(async (nuxtApp) => {
const studioConfig = useRuntimeConfig().public.studio || {}
const studioConfig: PublicRuntimeConfig['studio'] = useRuntimeConfig().public.studio || {}
const route = useRoute()
const previewToken = useCookie('previewToken', { sameSite: 'none', secure: true })

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/plugins/websocket.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { defineNuxtPlugin } from 'nuxt/app'
import { useRuntimeConfig } from '#imports'

export default defineNuxtPlugin(() => {
const publicConfig = useRuntimeConfig().public as { content: { wsUrl: string } }
const publicConfig = useRuntimeConfig().public.content as { wsUrl: string }

if (import.meta.client && publicConfig.content.wsUrl) {
if (import.meta.client && publicConfig.wsUrl) {
// Connect to websocket
import('../internal/websocket').then(({ useContentWebSocket }) => useContentWebSocket())
}
Expand Down
41 changes: 41 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
interface Window {
openFileInStudio: (path: string) => void
}

declare module '#content/manifest' {
const manifest: Record<string, unknown>
const checksums: Record<string, string>
const tables: Record<string, string>

export { manifest as default, checksums, tables }
}

declare module '#content/components' {
export const globalComponents: string[]
export const localComponents: string[]
}

declare module '#content/dump' {
export const dump: string
}

declare module '#content/adapter' {
import type { DatabaseAdapter } from './database'

const adapter: DatabaseAdapter

export { adapter as default }
}

declare module '#content/collections' {
export const collections: Record<string, unknown>
}

declare module '#content/studio' {
import type { CollectionInfo } from './collection'

export const collections: Record<string, CollectionInfo>
export const gitInfo: GitInfo
export const appConfigSchema: Record<string, unknown>
}

declare module '#content/dump' {
export const dump: string
}
35 changes: 0 additions & 35 deletions src/types/shims.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const studioTemplate = (collections: ResolvedCollection[], gitInfo: GitIn
pascalName: pascalCase(collection.name),
tableName: collection.tableName,
// Remove source from collection meta if it's a remote collection
source: collection.source?.repository ? undefined : collection.source,
source: collection.source?.filter(source => source.repository ? undefined : collection.source),
type: collection.type,
jsonFields: collection.jsonFields,
schema: zodToJsonSchema(collection.extendedSchema, collection.name),
Expand Down
12 changes: 9 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
"exclude": [
"dist",
"node_modules",
"docs"
"docs",
"playground",
"examples"
],
"include": [
"src",
"src/types/shim.d.ts"
],
"compilerOptions": {
"strictNullChecks": false
}
"strictNullChecks": false,
},
}

0 comments on commit 4890997

Please sign in to comment.