MCPcopy Create free account
hub / github.com/contentlayerdev/contentlayer / fetchData

Function fetchData

packages/archive/source-sanity/src/fetchData.ts:10–48  ·  view source on GitHub ↗
({
  studioDirPath,
  schemaDef,
}: {
  studioDirPath: string
  schemaDef: Core.SchemaDef
})

Source from the content-addressed store, hash-verified

8import type * as Sanity from './sanity-types'
9
10export const fetchData = async ({
11 studioDirPath,
12 schemaDef,
13}: {
14 studioDirPath: string
15 schemaDef: Core.SchemaDef
16}): Promise<Core.DataCache.Cache> => {
17 const client = await getSanityClient(studioDirPath)
18
19 const imageUrlBuilder = SantityImageUrlBuilder(client)
20
21 const { _updatedAt }: { _updatedAt: string } = await client.fetch('*[] | order(_updatedAt desc) [0]{_updatedAt}')
22 // const lastUpdateInMs = new Date(_updatedAt).getTime()
23
24 // if (force || previousCache === undefined || lastUpdateInMs > previousCache.lastUpdateInMs) {
25 const entries: Sanity.DataDocument[] = await client.fetch('*[]')
26 // TODO overlay drafts -> ids: draft.__original_id__
27
28 // ;(await import('fs')).writeFileSync('entries.json', JSON.stringify(entries, null, 2))
29
30 const documents = entries
31 // Ignores documents that are not explicitly defined in the schema (e.g. assets which are accessed via URLs instead)
32 .filter((_) => _._type in schemaDef.documentTypeDefMap)
33 .map((rawDocumentData) =>
34 makeDocument({
35 rawDocumentData,
36 documentTypeDef: schemaDef.documentTypeDefMap[rawDocumentData._type]!,
37 schemaDef,
38 imageUrlBuilder,
39 }),
40 )
41
42 const cacheItemsMap = Object.fromEntries(documents.map((_) => [_.document._id, _]))
43
44 return { cacheItemsMap }
45 // }
46
47 // return { documents: previousCache.documents, lastUpdateInMs: previousCache.lastUpdateInMs }
48}
49
50const makeDocument = ({
51 rawDocumentData,

Callers 1

makeSourcePluginFunction · 0.70

Calls 4

getSanityClientFunction · 0.90
mapMethod · 0.80
filterMethod · 0.80
makeDocumentFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…