Skip to content

Commit

Permalink
chore: revert back to manually typing sanity queries
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzuodong committed May 8, 2024
1 parent e85e34e commit 59d8e08
Show file tree
Hide file tree
Showing 15 changed files with 10,140 additions and 8,173 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
"type": "module",
"private": true,
"scripts": {
"bp": "pnpm --filter app bp",
"clean": "rm -rf node_modules && pnpm -r exec rm -rf node_modules .nuxt",
"dev": "pnpm --filter app dev",
"dev:studio": "pnpm --filter studio dev",
"build": "pnpm --filter app build",
"build:studio": "pnpm --filter studio build",
"preview": "pnpm --filter app preview",
"bp": "pnpm --filter app bp",
"typecheck": "pnpm --filter app typecheck",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
"lint:fix": "eslint . --fix",
"preview": "pnpm --filter app preview",
"typecheck": "pnpm --filter app typecheck"
},
"devDependencies": {
"@sanomics/eslint-config": "latest",
"@sanomics/eslint-config": "^1.2.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"env-cmd": "^10.1.0",
Expand Down
16 changes: 15 additions & 1 deletion packages/app/components/MySanityImage.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
<script lang="ts">
import { imgProps } from '#image/components/nuxt-img'
export default defineComponent({
props: {
...imgProps,
provider: {
type: String,
default: 'mySanity',
},
},
})
</script>

<template>
<nuxt-img provider="mySanity" />
<nuxt-img v-bind="$props" />
</template>
4 changes: 2 additions & 2 deletions packages/app/server/api/galleries/[slug].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export interface Gallery {

export default defineEventHandler(async (event) => {
const { slug } = getRouterParams(event)
const query = groq`*[_type == "galleries" && slug.current == "${slug}"][0]{
const query = groq`*[_type == "gallery" && slug.current == $slug][0]{
...,
images[]{
...,
asset->
}
}`
return await useSanity().fetch<Gallery | null>(query)
return await useSanity().fetch<Gallery | null>(query, { slug })
})
6 changes: 3 additions & 3 deletions packages/app/server/api/galleries/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ResolvedSanityImage } from '@sanity/asset-utils'
import type { Value } from 'sanity-plugin-internationalized-array'

const query = groq`
*[_type == "galleries"]{
const galleriesQuery = groq`
*[_type == "gallery"]{
"id": _id,
name,
description,
Expand Down Expand Up @@ -34,7 +34,7 @@ export interface GalleryListItem {
}

export default defineCachedEventHandler(async () => {
return await useSanity().fetch<GalleryListItem[]>(query)
return await useSanity().fetch<GalleryListItem[]>(galleriesQuery)
}, {
// staleMaxAge: 60 * 60,
})
6 changes: 3 additions & 3 deletions packages/app/server/api/instant-films.get.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { ResolvedSanityImage } from '@sanity/asset-utils'

const query = groq`
*[_id == "instantFilms"].instantFilms[-5..-1]{
const instantFilmsQuery = groq`
*[_type == "instantFilm"][0].instantFilms[-5..-1]{
...,
asset->,
}
`

export default defineCachedEventHandler(async () => {
return await useSanity().fetch<ResolvedSanityImage[]>(query)
return await useSanity().fetch<ResolvedSanityImage[]>(instantFilmsQuery)
}, {
staleMaxAge: 60 * 60,
})
9 changes: 2 additions & 7 deletions packages/app/server/api/projects/[slug].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@ export interface ProjectDetail {
url: string
sourceCodeUrl: string
tags: string[]
previewImages: ResolvedSanityImage[]
coverImage: ResolvedSanityImage
content: { value?: PortableTextBlock }[]
}

export default defineEventHandler(async (event) => {
const { slug } = getRouterParams(event)
const query = groq`*[_type == "projects" && slug.current == "${slug}"][0]{
const query = groq`*[_type == "project" && slug.current == $slug][0]{
...,
previewImages[]{
...,
asset->
},
coverImage{
...,
asset->
Expand All @@ -37,5 +32,5 @@ export default defineEventHandler(async (event) => {
}
}
}`
return await useSanity().fetch(query)
return await useSanity().fetch(query, { slug })
})
6 changes: 3 additions & 3 deletions packages/app/server/api/projects/index.get.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ResolvedSanityImage } from '@sanity/asset-utils'
import type { Value } from 'sanity-plugin-internationalized-array'

const query = groq`
*[_type == "projects"]{
const projectsQuery = groq`
*[_type == "project"]{
"id": _id,
name,
description,
Expand All @@ -27,5 +27,5 @@ export interface Project {
}

export default defineEventHandler(async () => {
return await useSanity().fetch<Project[]>(query)
return await useSanity().fetch<Project[]>(projectsQuery)
})
32 changes: 13 additions & 19 deletions packages/studio/deskStructure.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import type { StructureBuilder } from 'sanity/structure'
import {
filteredDocumentListItems,
singletonDocumentListItems,
} from 'sanity-plugin-singleton-tools'

export function structure(S: StructureBuilder) {
return S.list()
.title('Content')
.items([
S.listItem()
.title('Instant Films')
.child(
S.document()
.schemaType('instantFilms')
.documentId('instantFilms')
.title('Instant Films'),
),
import type { StructureResolver } from 'sanity/structure'

...S.documentTypeListItems()
.filter(
listItem => !['instantFilms', 'media.tag'].includes(listItem.getId() as string),
),
])
}
// eslint-disable-next-line func-style
export const structure: StructureResolver = (S, context) => S.list()
.title('Sanity Love Content')
.items([
...singletonDocumentListItems({ S, context })!,
S.divider(),
...filteredDocumentListItems({ S, context }),
])
1 change: 1 addition & 0 deletions packages/studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"sanity-plugin-asset-source-unsplash": "^1.1.2",
"sanity-plugin-internationalized-array": "^2.0.0",
"sanity-plugin-media": "^2.2.5",
"sanity-plugin-singleton-tools": "^1.0.1",
"styled-components": "^6.1.8"
}
}
2 changes: 2 additions & 0 deletions packages/studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { structureTool } from 'sanity/structure'
import { unsplashImageAsset } from 'sanity-plugin-asset-source-unsplash'
import { media } from 'sanity-plugin-media'
import { internationalizedArray } from 'sanity-plugin-internationalized-array'
import { singletonTools } from 'sanity-plugin-singleton-tools'

import { schemaTypes } from './schemas'
import { structure } from './deskStructure'
Expand All @@ -24,6 +25,7 @@ const config = defineConfig({
structureTool({ structure }),
unsplashImageAsset(),
visionTool({ defaultApiVersion: '2023-06-21' }),
singletonTools(),
media(),
internationalizedArray({
languages: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineArrayMember, defineField, defineType } from 'sanity'

export default defineType({
name: 'galleries',
name: 'gallery',
type: 'document',
title: 'Galleries',
fields: [
Expand Down
12 changes: 6 additions & 6 deletions packages/studio/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import galleries from './galleries'
import instantFilms from './instantFilms'
import projects from './projects'
import gallery from './gallery'
import instantFilm from './instantFilm'
import project from './project'

export const schemaTypes = [
galleries,
instantFilms,
projects,
gallery,
instantFilm,
project,
]
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { defineArrayMember, defineField, defineType } from 'sanity'

export default defineType({
name: 'instantFilms',
name: 'instantFilm',
type: 'document',
title: 'Instant Films',
fields: [
defineField({
name: 'instantFilms',
type: 'array',
title: 'Instant Films',
title: 'All Instant Films',
description: 'Only the last 5 images will be displayed on the homepage.',
of: [defineArrayMember({
name: 'image',
title: 'Image',
Expand All @@ -24,4 +25,7 @@ export default defineType({
},
}),
],
options: {
singleton: true,
},
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineField, defineType } from 'sanity'

export default defineType({
name: 'projects',
name: 'project',
type: 'document',
title: 'Projects',
fields: [
Expand Down
Loading

0 comments on commit 59d8e08

Please sign in to comment.