Skip to content

Commit 8c2afa5

Browse files
MellKamfarnabaz
andauthored
types: fix strict null checks (#3265)
--------- Co-authored-by: Farnabaz <[email protected]>
1 parent 98276ff commit 8c2afa5

24 files changed

+75
-63
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test": "vitest run",
4747
"test:bun": "bun test ./test/bun.test.ts",
4848
"test:watch": "vitest watch",
49-
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit",
49+
"test:types": "vue-tsc --noEmit",
5050
"verify": "npm run dev:prepare && npm run lint && npm run test && npm run typecheck"
5151
},
5252
"dependencies": {

src/features/llms/module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ declare module 'nuxt-llms' {
3030
},
3131
write: true,
3232
}).dst
33+
nuxt.options.nitro ||= {}
34+
nuxt.options.nitro.typescript ||= {}
3335
nuxt.options.nitro.typescript.tsConfig = defu(nuxt.options.nitro.typescript.tsConfig, {
3436
include: [typeTemplate],
3537
})

src/module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export default defineNuxtModule<ModuleOptions>({
125125

126126
// Add content types to Nuxt and Nitro
127127
const typesTemplateDst = addTypeTemplate(contentTypesTemplate(manifest.collections)).dst
128+
nuxt.options.nitro.typescript ||= {}
128129
nuxt.options.nitro.typescript.tsConfig = defu(nuxt.options.nitro.typescript.tsConfig, {
129130
include: [typesTemplateDst],
130131
})
@@ -275,7 +276,7 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
275276

276277
const { fixed } = parseSourceBase(source)
277278
const cwd = source.cwd
278-
const _keys = await source.getKeys()
279+
const _keys = await source.getKeys?.() || []
279280

280281
filesCount += _keys.length
281282

@@ -293,7 +294,7 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
293294
const cache = databaseContents[keyInCollection]
294295

295296
try {
296-
const content = await source.getItem(key)
297+
const content = await source.getItem?.(key) || ''
297298
const checksum = getContentChecksum(configHash + collectionHash + content)
298299

299300
let parsedContent

src/presets/nuxthub.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default definePreset({
2424

2525
if (nitroConfig.dev === false) {
2626
// Write SQL dump to database queries when not in dev mode
27-
await mkdir(resolve(nitroConfig.rootDir, '.data/hub/database/queries'), { recursive: true })
27+
await mkdir(resolve(nitroConfig.rootDir!, '.data/hub/database/queries'), { recursive: true })
2828
let i = 1
2929
// Drop info table and prepare for new dump
3030
let dump = 'DROP TABLE IF EXISTS _content_info;\n'
@@ -45,9 +45,11 @@ export default definePreset({
4545
dumpFiles.push({ file: `content-database-${String(i).padStart(3, '0')}.sql`, content: dump.trim() })
4646
}
4747
for (const dumpFile of dumpFiles) {
48-
await writeFile(resolve(nitroConfig.rootDir, '.data/hub/database/queries', dumpFile.file), dumpFile.content)
48+
await writeFile(resolve(nitroConfig.rootDir!, '.data/hub/database/queries', dumpFile.file), dumpFile.content)
4949
}
5050
// Disable integrity check in production for performance
51+
nitroConfig.runtimeConfig ||= {}
52+
nitroConfig.runtimeConfig.content ||= {}
5153
nitroConfig.runtimeConfig.content.integrityCheck = false
5254
}
5355
},

src/runtime/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function queryCollectionSearchSections(collection: keyof Collections, opt
3131
return chainablePromise(collection, qb => generateSearchSections(qb, opts))
3232
}
3333

34-
async function executeContentQuery<T extends keyof Collections, Result = Collections[T]>(event: H3Event, collection: T, sql: string) {
34+
async function executeContentQuery<T extends keyof Collections, Result = Collections[T]>(event: H3Event | undefined, collection: T, sql: string) {
3535
if (import.meta.client) {
3636
return queryContentSqlClientWasm<T, Result>(collection, sql) as Promise<Result[]>
3737
}

src/runtime/internal/connectors/bunsqlite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const connector: typeof bunSqliteConnector = (opts) => {
88
if (!globalThis.CompressionStream) {
99
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1010
const make = (ctx: unknown, handle: any) =>
11-
Object.assign(ctx, {
11+
Object.assign(ctx as object, {
1212
writable: new WritableStream({
1313
write: (chunk: ArrayBufferView) => handle.write(chunk),
1414
close: () => handle.end(),

src/runtime/internal/database.client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ export function loadDatabaseAdapter<T>(collection: T): DatabaseAdapter {
1313
if (!db) {
1414
dbPromises._ = dbPromises._ || initializeDatabase()
1515
db = await dbPromises._
16-
dbPromises._ = undefined
16+
Reflect.deleteProperty(dbPromises, '_')
1717
}
1818
if (!loadedCollections[String(collection)]) {
1919
dbPromises[String(collection)] = dbPromises[String(collection)] || loadCollectionDatabase(collection)
2020
await dbPromises[String(collection)]
2121
loadedCollections[String(collection)] = 'loaded'
22-
dbPromises[String(collection)] = undefined
22+
Reflect.deleteProperty(dbPromises, String(collection))
2323
}
2424

2525
return db

src/runtime/internal/database.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ async function _checkAndImportDatabaseIntegrity(event: H3Event, collection: stri
123123
// in D1, there is a bug where semicolons and comments can't work together
124124
// so we need to split the SQL and remove the comment
125125
// @see https://github.com/cloudflare/workers-sdk/issues/3892
126-
const hash = dumpLinesHash[index]
126+
const hash = dumpLinesHash[index]!
127127
const statement = sql.substring(0, sql.length - hash.length - 4)
128128

129129
// If the structure has not changed,
@@ -168,10 +168,10 @@ async function waitUntilDatabaseIsReady(db: DatabaseAdapter, collection: string)
168168
let interval: NodeJS.Timer
169169
await new Promise((resolve, reject) => {
170170
interval = setInterval(async () => {
171-
const { ready } = await db.first<{ ready: boolean }>(`SELECT ready FROM ${tables.info} WHERE id = ?`, [`checksum_${collection}`])
171+
const row = await db.first<{ ready: boolean }>(`SELECT ready FROM ${tables.info} WHERE id = ?`, [`checksum_${collection}`])
172172
.catch(() => ({ ready: true }))
173173

174-
if (ready) {
174+
if (row?.ready) {
175175
clearInterval(interval)
176176
resolve(0)
177177
}

src/runtime/internal/navigation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export async function generateNavigationTree<T extends PageCollectionItemBase>(q
100100
Object.assign(existed, {
101101
page: undefined,
102102
children: [
103-
...navItem.children,
104-
...existed.children,
103+
...(navItem.children || []),
104+
...(existed.children || []),
105105
],
106106
})
107107
}
@@ -152,8 +152,8 @@ export async function generateNavigationTree<T extends PageCollectionItemBase>(q
152152
...navItem,
153153
page: undefined,
154154
children: [
155-
...navItem.children,
156-
...existed.children,
155+
...(navItem.children || []),
156+
...(existed.children || []),
157157
],
158158
})
159159
}

src/runtime/internal/preview/collection.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { getOrderedSchemaKeys } from '../schema'
77
import { parseSourceBase } from './utils'
88
import { withoutRoot } from './files'
99

10-
export const getCollectionByFilePath = (path: string, collections: Record<string, CollectionInfo>): { collection: CollectionInfo, matchedSource: ResolvedCollectionSource } => {
11-
let matchedSource: ResolvedCollectionSource
10+
export const getCollectionByFilePath = (path: string, collections: Record<string, CollectionInfo>): { collection: CollectionInfo | undefined, matchedSource: ResolvedCollectionSource | undefined } => {
11+
let matchedSource: ResolvedCollectionSource | undefined
1212
const collection = Object.values(collections).find((collection) => {
1313
if (!collection.source || collection.source.length === 0) {
1414
return
@@ -28,21 +28,21 @@ export const getCollectionByFilePath = (path: string, collections: Record<string
2828
}
2929
}
3030

31-
export const getCollectionByRoutePath = (routePath: string, collections: Record<string, CollectionInfo>): { collection: CollectionInfo, matchedSource: ResolvedCollectionSource } => {
32-
let matchedSource: ResolvedCollectionSource
31+
export const getCollectionByRoutePath = (routePath: string, collections: Record<string, CollectionInfo>): { collection: CollectionInfo | undefined, matchedSource: ResolvedCollectionSource | undefined } => {
32+
let matchedSource: ResolvedCollectionSource | undefined
3333
const collection = Object.values(collections).find((collection) => {
3434
if (!collection.source || collection.source.length === 0) {
3535
return
3636
}
3737

3838
matchedSource = collection.source.find((source) => {
39-
if (!routePath.startsWith(source.prefix)) {
39+
if (!source.prefix || !routePath.startsWith(source.prefix)) {
4040
return
4141
}
4242

4343
if (routePath === '/' || routePath === source.prefix) {
4444
const indexFiles = ['index.yml', 'index.yaml', 'index.md', 'index.json']
45-
const files = routePath === '/' ? indexFiles : indexFiles.map(file => withoutLeadingSlash(joinURL(source.prefix, file)))
45+
const files = routePath === '/' ? indexFiles : indexFiles.map(file => withoutLeadingSlash(joinURL(source.prefix!, file)))
4646
return files.some((p) => {
4747
return collection.source.find(source => minimatch(p, source.include) && !source.exclude?.some(exclude => minimatch(p, exclude)))
4848
})
@@ -98,7 +98,7 @@ export function generateRecordSelectByColumn(collection: CollectionInfo, column:
9898
function computeValuesBasedOnCollectionSchema(collection: CollectionInfo, data: Record<string, unknown>) {
9999
const fields: string[] = []
100100
const values: Array<string | number | boolean> = []
101-
const properties = (collection.schema.definitions[collection.name] as JsonSchema7ObjectType).properties
101+
const properties = (collection.schema.definitions![collection.name] as JsonSchema7ObjectType).properties
102102
const sortedKeys = getOrderedSchemaKeys(properties)
103103

104104
sortedKeys.forEach((key) => {

0 commit comments

Comments
 (0)