New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sanity-generator

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sanity-generator - npm Package Compare versions

Comparing version 0.0.2 to 0.1.0

build/cli/main.d.ts

29

package.json
{
"name": "sanity-generator",
"version": "0.0.2",
"main": "./build/cli.js",
"typings": "./build/cli.d.ts",
"version": "0.1.0",
"main": "build/index.js",
"typings": "build/index.d.ts",
"files": [
"build"
],
"bin": {
"sanity-generator": "build/cli.js"
"sanity-generator": "build/cli/main.js",
"sg": "build/cli/main.js"
},
"scripts": {
"dev": "npx nodemon --exec ts-node -- src/cli --config ./playground/sanity-generator.config.ts",
"dev": "npx nodemon --ignore playground/sanity-generator/ --exec ts-node -- src/cli/main generate --config ./playground/sanity-generator.config.ts",
"dev:sanity": "cd ./playground/sanity && sanity dev",
"build": "rimraf ./build && tsc",
"preview": "node build/cli.js --config ./playground/sanity-generator.config.ts"
"build": "rimraf ./build && tsc && cp -r src/static build/static",
"preview": "node build/cli/main.js generate --config ./playground/sanity-generator.config.ts"
},
"devDependencies": {
"groqfmt-nodejs": "^0.0.4",
"prettier": "^3.0.2",
"prettier-plugin-groq": "^0.2.5",
"rimraf": "^5.0.1",
"@types/node": "^20.5.9",
"tslib": "^2.6.2",

@@ -24,3 +25,9 @@ "typescript": "^5.2.2"

"dependencies": {
"rimraf": "^5.0.1",
"commander": "^11.0.0",
"consola": "^3.2.3",
"defu": "^6.1.2",
"groqfmt-nodejs": "^0.0.5",
"prettier": "^3.0.3",
"serialize-javascript": "^6.0.1",
"ts-node": "^10.9.1",

@@ -27,0 +34,0 @@ "tsconfig-paths": "^4.2.0"

@@ -1,43 +0,25 @@

# Sanity Generator
Sanity Generator is a codegen tool for [Sanity](https://www.sanity.io) to automatically generate GROQ queries from a schema perspective.
Sanity Generator aims to simplify the process of defining schemas and queries when working with [Sanity](https://www.sanity.io/) CMS.
**Note: Currently in beta. Be cautious when using it in production.**
It is based on the assumption that a given document schema shape, is not that different from it's corresponding query shape. Moreover it assumes that if you need to reshape a specific field type, you probably would like to do this on all occurences of that type through out all queries.
## Minimal Configuration
Certainly, this can be done with simple exporting, importing and composing of template literals. But this is quite repetitivie, and error prone. Sanity Generator is a CLI tool that aims to automate this while still providing all flexiblities of GROQ.
To get started, define your document schemas and queries like this:
```TypeScript
// sanity-generator.config.ts
export default createConfig({
schemas: {
page: pageSchema,
},
queries: {
getPages: ({ schemas }) => /* groq */ `
*[_type == "${schemas.page.name}"] {
${schemas.page.projection}
}
`,
},
});
```
**Disclaimer: This is still Beta. Use with caution**
When you run `$ npx sanity-generator`, it creates queries with a spread operator to return all fields.
## Basic Usage
```TypeScript
// sanity-generator/queries/getPages.ts
export const getPages = /* groq */ `
*[_type == "page"] {
...
}
`
```
Install from npm:
## Resolver for Custom Types (aka. the actual use case)
`$ npm install sanity-generator --save-dev`
Define resolvers for field types from your Sanity schema. These resolvers are functions that return custom GROQ projections:
`$ yarn add sanity-generator --dev`
Create a config file:
`$ npx sanity-generator init`
Customize the config:
```TypeScript

@@ -47,6 +29,9 @@ // sanity-generator.config.ts

schemas: {
// All sanity document schemas you want to query go here.
// The object key is for later referencing.
page: pageSchema,
},
resolvers: {
// Resolve all field types of "localString" with this projection...
// All field types you want to apply a custom GROQ query go here.
// The object key must match a field type.
localeString: (name: string) => /* groq */ `

@@ -57,2 +42,3 @@ "${name}": coalesce(${name}[$lang], ${name}.en)

queries: {
// Every property in here will be exported as a GROQ query. The function recieves a single argument that is an object with all processed schemas. Here you can get the projection with all resolvers applied, and for convinece also the schema name.
getPages: ({ schemas }) => /* groq */ `

@@ -67,6 +53,19 @@ *[_type == "${schemas.page.name}"] {

If you run `$ npx sanity-generator` now, it expands the projection to transform all types with their associated resolver:
## How it works
Sanity Generator simple traverses all branches of the document schema. If a branch holds no types that have a corresponding resolver, it uses the spread operator (`...`). If a branch holds a type that should be resolved differently, it writes the corresponding projections just as far as needed.
Here is an example with the same query, without and with custom resolver. (Both with option `inlineResolver: true`)
```TypeScript
// sanity-generator/queries/getPages.ts
// Generated query with no resolver
export const getPages = /* groq */ `
*[_type == "page"] {
...
}
`
```
```TypeScript
// Same query, with resolver
export const getPages = /* groq */ `

@@ -92,1 +91,29 @@ *[_type == "page"] {

```
## Options
### CLI
`$ npx sanity-generator`
or
`$ npx sg`
| Command | Option | Description |
| ---------- | ------------------ | ---------------------------------------------------------------------------------- |
| `generate` | | Run the generator assuming the config is here: `~/sanity-generator.config.ts` |
| `generate` | `--config` `-c` | Specify a path to the config file |
| `init` | | Create a default config |
### Config
The module exports a `createConfig` function to provide better typesupport for the configuration object.
| Property | Default | Description |
| --------------- | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| schemas | {} | See basic usage |
| resolvers | {} | See basic usage |
| queries | {} | See basic usage |
| outPath | './sanity-generator` | Path to the destination folder for the queries |
| inlineResolvers | false | By default, resolvers are imported as a function into the final query. Setting this to false, will inline the resolvers as a string into the query. |
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc