├── .circleci └── config.yml ├── .editorconfig ├── .eslintignore ├── .eslintrc.js ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── babel.config.js ├── commitlint.config.js ├── docs ├── .vuepress │ └── config.js ├── README.md ├── api │ └── README.md └── usage │ └── README.md ├── example ├── nuxt.config.js └── pages │ └── index.vue ├── husky.config.js ├── jest.config.js ├── lib ├── module.js └── plugin.js ├── package.json ├── renovate.json ├── test └── module.test.js └── yarn.lock /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: circleci/node 6 | steps: 7 | # Checkout repository 8 | - checkout 9 | 10 | # Restore cache 11 | - restore_cache: 12 | key: yarn-cache-{{ checksum "yarn.lock" }} 13 | 14 | # Install dependencies 15 | - run: 16 | name: Install Dependencies 17 | command: NODE_ENV=dev yarn 18 | 19 | # Keep cache 20 | - save_cache: 21 | key: yarn-cache-{{ checksum "yarn.lock" }} 22 | paths: 23 | - "node_modules" 24 | 25 | # Lint 26 | - run: 27 | name: Lint 28 | command: yarn lint 29 | 30 | # Tests 31 | - run: 32 | name: Tests 33 | command: yarn jest 34 | 35 | # Coverage 36 | - run: 37 | name: Coverage 38 | command: yarn codecov 39 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = space 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # Common 2 | node_modules 3 | dist 4 | .nuxt 5 | coverage 6 | 7 | # Plugin 8 | lib/plugin.js 9 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parserOptions: { 4 | parser: 'babel-eslint', 5 | sourceType: 'module' 6 | }, 7 | extends: [ 8 | '@nuxtjs' 9 | ] 10 | } 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.iml 3 | .idea 4 | *.log* 5 | .nuxt 6 | .vscode 7 | .DS_Store 8 | coverage 9 | dist 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ### [0.0.7](https://github.com/vicbergquist/nuxt-sanity/compare/v0.0.4...v0.0.7) (2019-09-30) 6 | 7 | 8 | 9 | ### [0.0.5](https://github.com/vicbergquist/nuxt-sanity/compare/v0.0.4...v0.0.5) (2019-09-30) 10 | 11 | 12 | 13 | ### 0.0.2 (2019-06-04) 14 | 15 | 16 | 17 | # Changelog 18 | 19 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) vicbergquist 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [deprecated] nuxt-sanity 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![Circle CI][circle-ci-src]][circle-ci-href] 6 | [![Codecov][codecov-src]][codecov-href] 7 | [![Dependencies][david-dm-src]][david-dm-href] 8 | [![Standard JS][standard-js-src]][standard-js-href] 9 | 10 | > 11 | 12 | This package is now deprecated and replaced by [@nuxtjs/sanity](https://github.com/nuxt-community/sanity-module#readme) 13 | 14 | [📖 **Read documentation**](https://nuxt-sanity.netlify.com/). 15 | 16 | ## Setup 17 | 18 | 1. Add the `nuxt-sanity` dependency with `yarn` or `npm` to your project 19 | 20 | ```js 21 | yarn add nuxt-sanity // or npm install nuxt-sanity 22 | ``` 23 | 24 | 2. Add `nuxt-sanity` to the `modules` section of `nuxt.config.js` 25 | 26 | ```js 27 | { 28 | modules: [ 29 | 'nuxt-sanity' 30 | ] 31 | } 32 | ``` 33 | 34 | 3. Configure the module: 35 | 36 | ```js 37 | { 38 | modules: [ 39 | // Simple usage 40 | 'nuxt-sanity', 41 | 42 | // With inline options 43 | ['nuxt-sanity', { /* module options */ }] 44 | ], 45 | // Or a Sanity object 46 | sanity: { 47 | projectId: '', // string, required 48 | dataset: '', // string, required 49 | token: '', // string, optional 50 | useCdn: false // boolean, optional, default is false 51 | } 52 | } 53 | ``` 54 | 55 | ## Development 56 | 57 | 1. Clone this repository 58 | 2. Install dependencies using `yarn install` or `npm install` 59 | 3. Start development server using `npm run dev` 60 | 61 | ## License 62 | 63 | [MIT License](./LICENSE) 64 | 65 | 66 | [npm-version-src]: https://img.shields.io/npm/dt/nuxt-sanity.svg?style=flat-square 67 | [npm-version-href]: https://npmjs.com/package/nuxt-sanity 68 | 69 | [npm-downloads-src]: https://img.shields.io/npm/v/nuxt-sanity/latest.svg?style=flat-square 70 | [npm-downloads-href]: https://npmjs.com/package/nuxt-sanity 71 | 72 | [circle-ci-src]: https://img.shields.io/circleci/project/github/vicbergquist/nuxt-sanity.svg?style=flat-square 73 | [circle-ci-href]: https://circleci.com/gh/vicbergquist/nuxt-sanity 74 | 75 | [codecov-src]: https://img.shields.io/codecov/c/github/vicbergquist/nuxt-sanity.svg?style=flat-square 76 | [codecov-href]: https://codecov.io/gh/vicbergquist/nuxt-sanity 77 | 78 | [david-dm-src]: https://david-dm.org/vicbergquist/nuxt-sanity/status.svg?style=flat-square 79 | [david-dm-href]: https://david-dm.org/vicbergquist/nuxt-sanity 80 | 81 | [standard-js-src]: https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square 82 | [standard-js-href]: https://standardjs.com 83 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | [ 4 | '@babel/preset-env', { 5 | targets: { 6 | esmodules: true 7 | } 8 | } 9 | ] 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /commitlint.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: [ 3 | '@commitlint/config-conventional' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /docs/.vuepress/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | title: 'Nuxt ️️+ Sanity', 3 | description: 'Easily integrate Sanity in your Nuxt.js project.', 4 | themeConfig: { 5 | repo: 'vicbergquist/nuxt-sanity', 6 | repoLabel: 'Contribute!', 7 | lastUpdated: 'Last Updated', 8 | editLinkText: 'Help us improve this page!', 9 | editLinks: true, 10 | nav: [ 11 | { 12 | text: 'Sanity', 13 | link: 'https://www.sanity.io/' 14 | }, 15 | { 16 | text: 'Official Docs', 17 | link: 'https://www.sanity.io/docs' 18 | } 19 | ], 20 | displayAllHeaders: true, 21 | sidebar: [ 22 | { 23 | title: 'Guide', 24 | path: '/', 25 | collapsable: false 26 | }, 27 | { 28 | title: 'Usage', 29 | path: '/usage/', 30 | collapsable: false 31 | }, 32 | { 33 | title: 'API Reference', 34 | path: '/api/', 35 | collapsable: false, 36 | sidebarDepth: 4 37 | } 38 | ] 39 | } 40 | } -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # Get started 2 | 3 | ## What is Sanity? 4 | 5 | Sanity is a fully customizable headless CMS that treats your content as structured data and makes it available through a simple and powerful API. 6 | 7 | This module provides an easy way to integrate Sanity in your Nuxt.js project to fetch and display your data. 8 | 9 | ## Installation 10 | 11 | Install the npm package with `yarn` or `npm`: 12 | 13 | ```js 14 | yarn add nuxt-sanity // or npm install nuxt-sanity 15 | ``` 16 | 17 | Add `nuxt-sanity` to the `modules` section of `nuxt.config.js`: 18 | 19 | ```js 20 | // nuxt.config.js 21 | { 22 | modules: [ 23 | 'nuxt-sanity' 24 | ] 25 | } 26 | ``` 27 | 28 | ## Configuration 29 | 30 | Configure the module with `projectId` and `dataset` in the options for it to work. You can also pass other [options](/api) to override the defaults. 31 | 32 | ```js 33 | // nuxt.config.js 34 | { 35 | sanity: { 36 | projectId: 'xxx', // required 37 | dataset: 'xxx' // required 38 | } 39 | } 40 | ``` 41 | 42 | To read more about Nuxt.js modules, see the official documentation: [How to configure modules in Nuxt.js](https://nuxtjs.org/api/configuration-modules). 43 | 44 | To read more about the JavaScript Sanity client, please see the [official documentation](https://www.sanity.io/docs/client-libraries/js-client). -------------------------------------------------------------------------------- /docs/api/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | sidebarDepth: 3 3 | --- 4 | 5 | # API Reference 6 | 7 | ## Options 8 | 9 | You can pass options using module options or by adding a `sanity` object to `nuxt.config.js`. 10 | 11 | ```js 12 | modules: [ 13 | // With inline options 14 | ['nuxt-sanity', { /* module options */ }] 15 | ], 16 | // OR 17 | sanity: { 18 | /* module options */ 19 | } 20 | ``` 21 | 22 | ### `projectId` 23 | 24 | - Type: `string` 25 | - Default: `''` 26 | - Required: true 27 | 28 | The ID of your project. You can find this ID in the [management console](https://www.sanity.io/manage) when looking at your project. 29 | 30 | ### `dataset` 31 | 32 | - Type: `string` 33 | - Default: `''` 34 | - Required: true 35 | 36 | The dataset you want to connect the Sanity client to. Datasets can be created and deleted in your project's [management console](https://www.sanity.io/manage), under the "Datasets" tab. 37 | 38 | Read more about [datasets](https://www.sanity.io/docs/data-store/datasets). 39 | 40 | ### `token` 41 | 42 | - Type: `string` 43 | - Default: `''` 44 | - Required: false 45 | 46 | Sanity uses tokens for authentication. You can create these in the [management console](https://www.sanity.io/manage). By default, unauthenticated users have read access to published documents, but with [with certain exceptions](https://www.sanity.io/docs/data-store/access-control). 47 | 48 | Read more about Sanity [authentication](https://www.sanity.io/docs/http-auth). 49 | 50 | ### `useCdn` 51 | 52 | - Type: `boolean` 53 | - Default: `false` 54 | - Required: false 55 | 56 | Sanity provides a CDN-distributed, cached API, as an opt-in feature that will give you very fast responses to requests that have been cached. Set this option to `true` to use this CDN. 57 | 58 | ### `withCredentials` 59 | 60 | - Type: `boolean` 61 | - Default: `false` 62 | - Required: false 63 | 64 | Use this option to show draft documents. If you do all the rendering client side, you don't have to worry about tokens, but you still need to make sure the browser makes authenticated API requests when fetching the draft document. 65 | 66 | Read more about [previewing content](https://www.sanity.io/docs/preview-content-on-site). 67 | 68 | ## Methods 69 | 70 | For a list of all the available methods with the Sanity client, please see the [official documentation](https://www.sanity.io/docs/client-libraries/js-client). -------------------------------------------------------------------------------- /docs/usage/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | sidebarDepth: 4 3 | --- 4 | 5 | # Usage 6 | 7 | How to use the Sanity JavaScript client in Nuxt.js. See the [official documentation](/api) for more information. Sanity uses the GRaph Oriented Query language (GROQ). For an introduction to GROQ, please see the [how to guide](https://www.sanity.io/docs/data-store/how-queries-work). You can also read this [technical reference](https://www.sanity.io/docs/reference/groq) for even more information. 8 | 9 | After installing the nuxt-sanity module and [configuring](/#configuration) it in `nuxt.config.js`, the Sanity client will be available globally under `$sanity`. Here is a [cheat sheet](https://www.sanity.io/docs/data-store/query-cheat-sheet) of typical queries put together for you by the Sanity team to get you started. 10 | 11 | For a list of all the available methods with the Sanity client, please see the [official documentation](https://www.sanity.io/docs/client-libraries/js-client). 12 | 13 | ## In `asyncData` 14 | 15 | To fetch data in `asyncData`, access the Sanity client with `$sanity` like so: 16 | 17 | ```js 18 | export default { 19 | asyncData({ $sanity }) { 20 | const query = '{ "persons": *[_type == "person"] }' 21 | return $sanity.fetch(query) 22 | } 23 | } 24 | ``` 25 | 26 | ## In Component Methods 27 | 28 | Access the Sanity client in a component's methods with `this.$sanity`: 29 | 30 | ```js 31 | export default { 32 | data() { 33 | return { 34 | persons: [] 35 | } 36 | }, 37 | methods: { 38 | fetchPersons(query) { 39 | return this.$sanity.fetch(query) 40 | } 41 | }, 42 | async mounted() { 43 | const query = '*[_type == "person]' 44 | const persons = await this.fetchPersons(query) 45 | this.persons = persons 46 | } 47 | } 48 | ``` 49 | 50 | ## In mounted() 51 | 52 | Access the Sanity client in mounted() with `this.$sanity`: 53 | 54 | ```js 55 | export default { 56 | data() { 57 | return { 58 | persons: [] 59 | } 60 | }, 61 | mounted() { 62 | const query = '*[_type == "event"]' 63 | this.$sanity.fetch(query).then(result => { 64 | this.persons = result 65 | }) 66 | } 67 | } 68 | ``` -------------------------------------------------------------------------------- /example/nuxt.config.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = { 4 | rootDir: resolve(__dirname, '..'), 5 | buildDir: resolve(__dirname, '.nuxt'), 6 | srcDir: __dirname, 7 | render: { 8 | resourceHints: false 9 | }, 10 | modules: [ 11 | { handler: require('../') } 12 | ], 13 | sanity: { 14 | projectId: 'projectId', // required 15 | dataset: 'production' // required 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /example/pages/index.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /husky.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | hooks: { 3 | 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS', 4 | 'pre-commit': 'yarn lint', 5 | 'pre-push': 'yarn lint' 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | testEnvironment: 'node', 3 | collectCoverage: true, 4 | collectCoverageFrom: [ 5 | 'lib/**/*.js', 6 | '!lib/plugin.js' 7 | ], 8 | moduleNameMapper: { 9 | '^~/(.*)$': '/lib/$1', 10 | '^~~$': '', 11 | '^@@$': '', 12 | '^@/(.*)$': '/lib/$1' 13 | }, 14 | transform: { 15 | '^.+\\.js$': 'babel-jest' 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /lib/module.js: -------------------------------------------------------------------------------- 1 | const { resolve } = require('path') 2 | 3 | module.exports = function (moduleOptions) { 4 | // Get all Sanity config options 5 | // including top level options 6 | const options = { 7 | ...this.options.sanity, 8 | ...moduleOptions 9 | } 10 | // Throw error if project id is not defined in the config 11 | if (!options.projectId) throw new Error('[Sanity module] No project ID was found in the Sanity configuration') 12 | // Throw error if a dataset is not defined in the config 13 | if (!options.dataset) throw new Error('[Sanity module] No dataset was configured in the Sanity configuration') 14 | 15 | // Set default for token 16 | options.token = options.token || '' 17 | // Set default for useCdn 18 | options.useCdn = options.useCdn || false 19 | // Set default for withCredentials 20 | options.withCredentials = options.withCredentials || false 21 | 22 | this.addPlugin({ 23 | src: resolve(__dirname, 'plugin.js'), 24 | fileName: 'sanity.js', 25 | options 26 | }) 27 | } 28 | module.exports.meta = require('../package.json') 29 | -------------------------------------------------------------------------------- /lib/plugin.js: -------------------------------------------------------------------------------- 1 | import sanityClient from '@sanity/client' 2 | 3 | // Config data for Sanity Client 4 | const sanity = sanityClient({ 5 | projectId: <%= JSON.stringify(options.projectId) %>, 6 | dataset: <%= JSON.stringify(options.dataset) %>, 7 | token: <%= JSON.stringify(options.token) %>, 8 | useCdn: <%= options.useCdn %>, 9 | withCredentials: <%= options.withCredentials %> 10 | }) 11 | 12 | export default (context, inject) => { 13 | // Inject sanity client to the instance and context as $sanity 14 | context.$sanity = sanity 15 | inject('sanity', sanity) 16 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nuxt-sanity", 3 | "version": "0.0.7", 4 | "description": "", 5 | "license": "MIT", 6 | "contributors": [ 7 | { 8 | "name": "vicbergquist " 9 | } 10 | ], 11 | "main": "lib/module.js", 12 | "repository": "https://github.com/vicbergquist/nuxt-sanity", 13 | "publishConfig": { 14 | "access": "public" 15 | }, 16 | "scripts": { 17 | "dev": "nuxt example", 18 | "lint": "eslint --ext .js,.vue example lib test", 19 | "test": "yarn lint && jest", 20 | "release": "yarn test && standard-version && git push --follow-tags && npm publish", 21 | "dev:docs": "vuepress dev docs", 22 | "build:docs": "vuepress build docs" 23 | }, 24 | "files": [ 25 | "lib" 26 | ], 27 | "dependencies": { 28 | "@sanity/client": "^0.142.6" 29 | }, 30 | "devDependencies": { 31 | "@babel/core": "latest", 32 | "@babel/preset-env": "latest", 33 | "@commitlint/cli": "8.0.0", 34 | "@commitlint/config-conventional": "8.0.0", 35 | "@nuxtjs/eslint-config": "latest", 36 | "babel-eslint": "latest", 37 | "babel-jest": "latest", 38 | "codecov": "latest", 39 | "eslint": "latest", 40 | "eslint-config-standard": "latest", 41 | "eslint-plugin-import": "latest", 42 | "eslint-plugin-jest": "latest", 43 | "eslint-plugin-node": "latest", 44 | "eslint-plugin-promise": "latest", 45 | "eslint-plugin-standard": "latest", 46 | "eslint-plugin-vue": "latest", 47 | "get-port": "latest", 48 | "husky": "2.4.0", 49 | "jest": "latest", 50 | "nuxt-edge": "2.9.0-25999211.5c6c35c4", 51 | "request": "latest", 52 | "request-promise-native": "latest", 53 | "standard-version": "latest", 54 | "vuepress": "^1.0.3" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "@nuxtjs" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /test/module.test.js: -------------------------------------------------------------------------------- 1 | jest.setTimeout(60000) 2 | 3 | const { Nuxt, Builder } = require('nuxt-edge') 4 | const request = require('request-promise-native') 5 | const getPort = require('get-port') 6 | 7 | const config = require('../example/nuxt.config') 8 | config.dev = false 9 | 10 | let nuxt, port 11 | 12 | const url = path => `http://localhost:${port}${path}` 13 | const get = path => request(url(path)) 14 | 15 | describe('basic', () => { 16 | beforeAll(async () => { 17 | nuxt = new Nuxt(config) 18 | await nuxt.ready() 19 | await new Builder(nuxt).build() 20 | port = await getPort() 21 | await nuxt.listen(port) 22 | }) 23 | 24 | afterAll(async () => { 25 | await nuxt.close() 26 | }) 27 | 28 | test('render', async () => { 29 | const html = await get('/') 30 | expect(html).toContain('Works!') 31 | }) 32 | 33 | // Test sanity config 34 | const sanity = config.sanity 35 | 36 | test('sanity config is an object', () => { 37 | expect(typeof sanity).toBe('object') 38 | }) 39 | // Test required config options 40 | test('required sanity config options exist', () => { 41 | expect(sanity).toHaveProperty('projectId') 42 | expect(sanity).toHaveProperty('dataset') 43 | }) 44 | 45 | test('required config options, project ID and dataset, are strings', () => { 46 | expect(typeof sanity.projectId).toBe('string') 47 | expect(typeof sanity.dataset).toBe('string') 48 | }) 49 | }) 50 | --------------------------------------------------------------------------------