项目作者: danielroe

项目描述 :
Sanity integration for Vue Composition API
高级语言: TypeScript
项目地址: git://github.com/danielroe/vue-sanity.git
创建时间: 2020-03-26T10:28:53Z
项目社区:https://github.com/danielroe/vue-sanity

开源协议:MIT License

下载


🟢 vue-sanity

Sanity integration for Vue




















Composition API methods to incorporate Sanity into a Vue project.

Features

  • 🗄 Caching: Query results are cached.
  • 💪 TypeScript: Written in TypeScript.
  • 📡 Real-time: Supports previews using Sanity listening mode.
  • 🖇 Composition API: Vue3/Vue2 support using vue-demi.
  • 📝 SSR support: Compatible with server-side rendering with Nuxt and vanilla Vue.

Quick Start

First install vue-sanity:

  1. yarn add vue-sanity
  2. # or npm
  3. npm install vue-sanity --save

Now configure Sanity in your root component:

  1. import { useSanityClient } from 'vue-sanity'
  2. export default {
  3. name: 'App',
  4. setup() {
  5. useSanityClient({
  6. projectId: 'myprojectid',
  7. dataset: 'production',
  8. useCdn: process.env.NODE_ENV === 'production',
  9. })
  10. },
  11. }

Then you can use useSanityFetcher in any child component:

  1. <script>
  2. import { useSanityFetcher } from 'vue-sanity'
  3. export default {
  4. setup() {
  5. const { data: title } = useSanityFetcher('*[_type == "article"][0].title')
  6. // OR use a factory function
  7. const { data: title } = useSanityFetcher(
  8. () => `*[slug.current == ${slug.value}][0].title`
  9. )
  10. return { title }
  11. },
  12. }
  13. </script>
  14. <template>
  15. <div>
  16. <h1>
  17. {{ title }}
  18. </h1>
  19. </div>
  20. </template>

API

useSanityClient

  • config

    These are the options required by @sanity/client. For more details, see the Sanity docs.

  • supportPreview

    In addition to the config you would normally pass to @sanity/client, you can pass a boolean as a second parameter for whether to create a preview client. (Used currently only when listening to real-time data updating.)

  • defaultOptions

    You may also pass an object of options that will be passed to any queries you make using useSanityFetcher, although of course they will be overridden by any specific options you pass to useSanityFetcher.

Example

  1. import { useSanityClient } from 'vue-sanity'
  2. export default {
  3. setup() {
  4. useSanityClient(
  5. {
  6. projectId: 'myprojectid',
  7. dataset: 'production',
  8. useCdn: process.env.NODE_ENV === 'production',
  9. },
  10. true // will now create a preview client for use elsewhere
  11. )
  12. },
  13. }

useSanityFetcher

  • query

    A function that retuns a query string. If the return value changes, a new Sanity query will be run and the return value automatically updated.

  • initialValue
    You can provide an initial value for the query result (which will be returned before query completes).

  • mapper

    You can provide a function to transform the query result.

  • options

    You can also provide an object of additional options.

    • listen: true, false or an object of options to pass to client.listen (defaults to false)
    • strategy: strategy for fetching. Defaults to both.
      • :server: will not refetch if the cache has been populated on SSR
      • :client: will disable SSR fetching entirely
      • :both: will fetch on server and refetch when page is loaded
    • deduplicate: Whether to de-duplicate identical fetches. If set to true, additional fetches will not run unless made after the previous request errors or succeeds. If set to a number, additional fetches will run, but only after this many milliseconds after the previous fetch began.

useSanityQuery

If you are using sanity-typed-queries to define your schema, this is a helper function to reduce boilerplate and explicit typing.

  1. import { useSanityQuery } from 'vue-sanity'
  2. import { builder } from './cms/schemas/author.js'
  3. export default {
  4. setup() {
  5. // title will be typed as Ref<string | null>, with null as a default value
  6. const { data: title } = useSanityQuery(builder.pick('name').first())
  7. // authors will be typed as Ref<string[]>, with an empty array as a default value
  8. const { data: authors } = useSanityQuery(builder.pick('name'))
  9. return { title, authors }
  10. },
  11. }

Example

  1. import { useSanityClient } from 'vue-sanity'
  2. export default {
  3. setup() {
  4. const { data: title } = useSanityFetcher(
  5. // query
  6. () => `*[_type == "article"][0].title`,
  7. // initial value
  8. 'Title - Default',
  9. // mapper
  10. result => `Title - ${result}`,
  11. // options
  12. {
  13. listen: true,
  14. clientOnly: true,
  15. }
  16. )
  17. return { title }
  18. },
  19. }

Usage with TypeScript

You can type the return value of useSanityFetcher in several ways.

  1. // data will be typed as Ref<string | null>
  2. const { data } = useSanityFetcher<string>(
  3. () => `*[_type == "article"][0].title`
  4. )
  1. // data will be typed as Ref<string | number> as a number has been provided as a default value
  2. const { data } = useSanityFetcher<string, number>(
  3. () => `*[_type == "article"][0].title`,
  4. 3
  5. )
  1. // data will be typed as Ref<boolean | { value: string }> as it can infer the type
  2. const { data } = useSanityFetcher(
  3. () => `*[_type == "article"][0].title`,
  4. true,
  5. (result: string) => ({ value: result })
  6. )

Inspirations

Projects I’ve found helpful are:

Contributors

This has been developed to suit my needs but additional use cases and contributions are very welcome.

License

MIT License - Copyright © Daniel Roe