Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add modifiedAt field #1928

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/runtime/server/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prefixStorage } from 'unstorage'
import { StorageMeta, prefixStorage } from 'unstorage'
import { joinURL, withLeadingSlash, withoutTrailingSlash } from 'ufo'
import { hash as ohash } from 'ohash'
import type { H3Event } from 'h3'
Expand Down Expand Up @@ -152,7 +152,7 @@ export const getContent = async (event: H3Event, id: string): Promise<ParsedCont
return { _id: contentId, body: null }
}

const parsed = await parseContent(contentId, body as string) as ParsedContent
const parsed = await parseContent(contentId, body as string, meta) as ParsedContent

await cacheParsedStorage.setItem(id, { parsed, hash }).catch(() => {})

Expand All @@ -162,7 +162,7 @@ export const getContent = async (event: H3Event, id: string): Promise<ParsedCont
/**
* Parse content file using registered plugins
*/
export async function parseContent (id: string, content: string, opts: ParseContentOptions = {}) {
export async function parseContent (id: string, content: string, meta: StorageMeta, opts: ParseContentOptions = {}) {
const nitroApp = useNitroApp()
const options = defu(
opts,
Expand All @@ -183,7 +183,7 @@ export async function parseContent (id: string, content: string, opts: ParseCont
const file = { _id: id, body: content }
await nitroApp.hooks.callHook('content:file:beforeParse', file)

const result = await transformContent(id, file.body, options)
const result = await transformContent(id, file.body, meta, options)

// Call hook after parsing the file
await nitroApp.hooks.callHook('content:file:afterParse', result)
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/transformers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { extname } from 'pathe'
import { camelCase } from 'scule'
import { StorageMeta } from 'unstorage'
import type { ContentTransformer, TransformContentOptions } from '../types'
import csv from './csv'
import markdown from './markdown'
Expand Down Expand Up @@ -36,7 +37,7 @@ function getTransformers (ext: string, additionalTransformers: ContentTransforme
/**
* Parse content file using registered plugins
*/
export async function transformContent (id: string, content: string, options: TransformContentOptions = {}) {
export async function transformContent (id: string, content: string, meta: StorageMeta = {}, options: TransformContentOptions = {}) {
const { transformers = [] } = options
// Call hook before parsing the file
const file = { _id: id, body: content }
Expand Down Expand Up @@ -66,6 +67,11 @@ export async function transformContent (id: string, content: string, options: Tr
return cur.transform!(next, transformOptions || {})
}, Promise.resolve(parsed))

// Add meta
if (meta && meta.mtime) {
result._updatedAt = meta.mtime.toISOString()
}

return result
}

Expand Down
4 changes: 4 additions & 0 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export interface ParsedContentInternalMeta {
* Extension of the file
*/
_extension?: string
/**
* Last update date
*/
_updatedAt?: string
}

export interface ParsedContentMeta extends ParsedContentInternalMeta {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic/server/api/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default eventHandler(async (event) => {
const { id, content, options } = await readBody(event)

// @ts-ignore
const parsedContent = await parseContent(id, content, options)
const parsedContent = await parseContent(id, content, {}, options)

return parsedContent
})