({
studioDirPath,
schemaDef,
}: {
studioDirPath: string
schemaDef: Core.SchemaDef
})
| 8 | import type * as Sanity from './sanity-types' |
| 9 | |
| 10 | export 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 | |
| 50 | const makeDocument = ({ |
| 51 | rawDocumentData, |
no test coverage detected
searching dependent graphs…