Skip to content

Commit

Permalink
types: update module types
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Oct 31, 2024
1 parent b922f5e commit c6571cb
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 495 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@types/micromatch": "^4.0.9",
"@types/node": "^22.8.4",
"@types/pako": "^2.0.3",
"@types/pg": "^8.11.10",
"@types/ws": "^8.5.12",
"csvtojson": "^2.0.10",
"eslint": "^9.13.0",
Expand Down
567 changes: 91 additions & 476 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
addComponentsDir,
} from '@nuxt/kit'
import type { Nuxt } from '@nuxt/schema'
import type { ModuleOptions as MDCModuleOptions } from '@nuxtjs/mdc'
import { hash } from 'ohash'
import { join, dirname, isAbsolute } from 'pathe'
import fastGlob from 'fast-glob'
Expand Down Expand Up @@ -212,7 +213,7 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
const databaseContents = db.fetchDevelopmentCache()

const configHash = hash({
mdcHighlight: nuxt.options.mdc?.highlight,
mdcHighlight: (nuxt.options as unknown as { mdc: MDCModuleOptions }).mdc?.highlight,
contentBuild: options.build?.markdown,
})

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/adapters/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createDatabaseAdapter } from '../internal/database-adapter'
export type PostgreSqlOptions = { url: string } | pg.ClientConfig

let _client: undefined | pg.Client | Promise<pg.Client>
export default createDatabaseAdapter<PostgreSqlOptions>((opts = {}) => {
export default createDatabaseAdapter<PostgreSqlOptions>((opts) => {
function getClient() {
if (_client) {
return _client
Expand Down Expand Up @@ -33,7 +33,7 @@ export default createDatabaseAdapter<PostgreSqlOptions>((opts = {}) => {
async first<T>(sql: string, params?: Array<number | string | boolean>) {
const db = await getClient()
const { rows } = params ? await db.query<T[], T>(sql, params as QueryConfigValues<T>) : await db.query<T[], T>(sql)
return rows[0]
return rows[0] as T
},
async exec(sql: string): Promise<void> {
const db = await getClient()
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/api/query.post.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { eventHandler, getRouterParam, readValidatedBody } from 'h3'
import { z } from 'zod'
import type { RuntimeConfig } from '@nuxt/content'
import loadDatabaseAdapter, { checkAndImportDatabaseIntegrity } from '../internal/database.server'
import { useRuntimeConfig } from '#imports'

export default eventHandler(async (event) => {
const { sql } = await readValidatedBody(event, z.object({ sql: z.string() }).parse)
const collection = getRouterParam(event, 'collection')!

const conf = useRuntimeConfig().content
const conf = useRuntimeConfig().content as RuntimeConfig
await checkAndImportDatabaseIntegrity(event, collection, conf)

return loadDatabaseAdapter(conf).all(sql)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export async function queryCollectionSearchSections(collection: keyof Collection

async function executeContentQuery<T extends keyof Collections, Result = Collections[T]>(collection: T, sql: string) {
if (import.meta.client) {
return await queryContentSqlClientWasm<T, Result>(collection, sql)
return queryContentSqlClientWasm<T, Result>(collection, sql) as Promise<Result[]>
}
else {
return await fetchQuery(tryUseNuxtApp()?.ssrContext?.event, String(collection), sql)
return fetchQuery(tryUseNuxtApp()?.ssrContext?.event, String(collection), sql) as Promise<Result[]>
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/internal/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { H3Event } from 'h3'
import { checksums } from '#content/manifest'

export async function fetchDatabase(event: H3Event | undefined, collection: string) {
export async function fetchDatabase(event: H3Event | undefined, collection: string): Promise<string> {
return await $fetch(`/api/content/${collection}/database.sql`, {
context: event ? { clouflare: event.context.cloudflare } : {},
responseType: 'text',
Expand All @@ -10,7 +10,7 @@ export async function fetchDatabase(event: H3Event | undefined, collection: stri
})
}

export async function fetchQuery(event: H3Event | undefined, collection: string, sql: string) {
export async function fetchQuery<Item>(event: H3Event | undefined, collection: string, sql: string): Promise<Item[]> {
return await $fetch(`/api/content/${collection}/query`, {
context: event ? { clouflare: event.context.cloudflare } : {},
headers: { 'content-type': 'application/json' },
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/internal/database-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DatabaseAdapter } from '@nuxt/content'
import { parseJsonFields } from './collection'

export function createDatabaseAdapter<Options = unknown>(factory: (otps?: Options) => DatabaseAdapter) {
export function createDatabaseAdapter<Options = unknown>(factory: (otps: Options) => DatabaseAdapter) {
return (opts: Options) => {
const adapter = factory(opts)

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 @@ -108,7 +108,7 @@ export async function generateNavigationTree<T extends PageCollectionItemBase>(q

// Create dummy parent if not found
if (!parent) {
const navigationConfig = conf ? pickConfigNavigationFields(conf) : {} as ContentNavigationItem
const navigationConfig = (conf ? pickConfigNavigationFields(conf) : {}) as ContentNavigationItem
parent = {
...navigationConfig,
title: navigationConfig.title || generateTitle(part),
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { generateSearchSections } from './internal/search'
import { fetchQuery } from './internal/api'

export const queryCollectionWithEvent = <T extends keyof Collections>(event: H3Event, collection: T): CollectionQueryBuilder<Collections[T]> => {
return collectionQureyBuilder<T>(collection, (collection, sql) => fetchQuery(event, collection, sql))
return collectionQureyBuilder<T>(collection, (collection, sql) => fetchQuery<T>(event, collection, sql))
}

export async function queryCollectionNavigationWithEvent<T extends keyof PageCollections>(event: H3Event, collection: T, fields?: Array<keyof PageCollections[T]>) {
Expand Down
14 changes: 9 additions & 5 deletions src/types/shims.d.ts → src/shims.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { DatabaseAdapter } from './database'
import type { DatabaseAdapter } from './types/database'

declare module '#content/manifest' {
export default Record<string, unknown>
export const checksums: Record<string, string>
export const tables: Record<string, string>
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' {
Expand All @@ -16,7 +18,9 @@ declare module '#content/dump' {
}

declare module '#content/adapter' {
export default DatabaseAdapter
const adapter: DatabaseAdapter

export { adapter as default }
}

declare module '#content/collections' {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/mdc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ModuleOptions as MDCModuleOptions } from '@nuxtjs/mdc'
import type { ModuleOptions as MDCModuleOptions, MdcConfig } from '@nuxtjs/mdc'
import { defu } from 'defu'
import type { Nuxt } from '@nuxt/schema'
import { extendViteConfig, installModule } from '@nuxt/kit'
Expand Down Expand Up @@ -26,7 +26,7 @@ export async function installMDCModule(contentOptions: ModuleOptions, nuxt: Nuxt
await nuxt.hook('mdc:configSources', async (mdcConfigs) => {
if (mdcConfigs.length) {
const jiti = createJiti(nuxt.options.rootDir)
const configs = await Promise.all(mdcConfigs.map(path => jiti.import(path).then(m => m.default || m)))
const configs = await Promise.all(mdcConfigs.map(path => jiti.import(path).then(m => (m as { default: MdcConfig }).default || m)))

setParserOptions({
mdcConfigs: configs,
Expand Down
2 changes: 1 addition & 1 deletion test/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('empty', async () => {
})

test('is downloadable', async () => {
const response: string = await $fetch<string>('/api/content/content/database.sql', { responseType: 'text' })
const response: string = await $fetch('/api/content/content/database.sql', { responseType: 'text' })
expect(response).toBeDefined()

const parsedDump = await decompressSQLDump(response as string)
Expand Down

0 comments on commit c6571cb

Please sign in to comment.