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

@dangreaves/groq-query-builder

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dangreaves/groq-query-builder - npm Package Compare versions

Comparing version 0.16.0 to 0.17.0

102

dist/index.d.ts

@@ -7,10 +7,27 @@ import { TSchema, TUnion, TNull, TArray, TObject, TIntersect, TString, Static, TProperties, TLiteral } from '@sinclair/typebox';

*/
declare const TypeAttribute$4: unique symbol;
declare const InnerSchemaAttribute: unique symbol;
declare const GroqSymbol: unique symbol;
declare const TypeSymbol: unique symbol;
declare const SliceSymbol: unique symbol;
declare const FilterSymbol: unique symbol;
declare const ExpandSymbol: unique symbol;
declare const ConditionsSymbol: unique symbol;
declare const NeedsIntersectUnwrapSymbol: unique symbol;
/**
* Symbol used to cache GROQ string on a schema.
*/
declare const QueryCacheSymbol: unique symbol;
/**
* Make query cache optionally available on all schemas.
*/
declare module "@sinclair/typebox" {
interface TSchema {
[QueryCacheSymbol]?: string;
}
}
/**
* Additional attributes added to underlying schema.
*/
type AdditionalAttributes$4 = {
[TypeAttribute$4]: "Nullable";
[InnerSchemaAttribute]: TSchema;
[TypeSymbol]: "Nullable";
};

@@ -42,16 +59,9 @@ /**

/**
* Symbols for additional attributes on schema.
*/
declare const TypeAttribute$3: unique symbol;
declare const SliceAttribute$1: unique symbol;
declare const FilterAttribute$1: unique symbol;
declare const NeedsIntersectUnwrapAttribute: unique symbol;
/**
* Additional attributes added to underlying schema.
*/
type AdditionalAttributes$3 = {
[TypeAttribute$3]: "Collection";
[NeedsIntersectUnwrapAttribute]: boolean;
[SliceAttribute$1]: TCollectionOptions["slice"];
[FilterAttribute$1]: TCollectionOptions["filter"];
[TypeSymbol]: "Collection";
[NeedsIntersectUnwrapSymbol]: boolean;
[SliceSymbol]: TCollectionOptions["slice"];
[FilterSymbol]: TCollectionOptions["filter"];
};

@@ -101,8 +111,2 @@ /**

/**
* Symbols for additional attributes on schema.
*/
declare const TypeAttribute$2: unique symbol;
declare const ExpandAttibute: unique symbol;
declare const ConditionsAttribute: unique symbol;
/**
* Additional attributes added to underlying schema.

@@ -112,5 +116,5 @@ */

/** Array of conditions in order of the schemas added to the union */
[ConditionsAttribute]: string[];
[TypeAttribute$2]: "ConditionalUnion";
[ExpandAttibute]: TConditionalUnionOptions["expand"];
[ConditionsSymbol]: string[];
[TypeSymbol]: "ConditionalUnion";
[ExpandSymbol]: TConditionalUnionOptions["expand"];
};

@@ -151,16 +155,9 @@ /**

/**
* Symbols for additional attributes on schema.
*/
declare const TypeAttribute$1: unique symbol;
declare const SliceAttribute: unique symbol;
declare const FilterAttribute: unique symbol;
declare const ExpandAttribute: unique symbol;
/**
* Additional attributes added to underlying schema.
*/
type AdditionalAttributes$1 = {
[TypeAttribute$1]: "Projection";
[SliceAttribute]: TProjectionOptions["slice"];
[FilterAttribute]: TProjectionOptions["filter"];
[ExpandAttribute]: TProjectionOptions["expand"];
[TypeSymbol]: "Projection";
[SliceSymbol]: TProjectionOptions["slice"];
[FilterSymbol]: TProjectionOptions["filter"];
[ExpandSymbol]: TProjectionOptions["expand"];
};

@@ -197,12 +194,7 @@ /**

/**
* Symbols for additional attributes on schema.
*/
declare const TypeAttribute: unique symbol;
declare const GroqAttribute: unique symbol;
/**
* Additional attributes added to underlying schema.
*/
type AdditionalAttributes = {
[TypeAttribute]: "Raw";
[GroqAttribute]: string;
[TypeSymbol]: "Raw";
[GroqSymbol]: string;
};

@@ -217,4 +209,4 @@ /**

declare function Raw<T extends TSchema = TSchema>(groq: string, schema: T): T & {
[TypeAttribute]: "Raw";
[GroqAttribute]: string;
[TypeSymbol]: "Raw";
[GroqSymbol]: string;
};

@@ -264,3 +256,3 @@ /**

type SanityParams = Record<string, string | number | null> | undefined;
declare function makeSafeSanityFetch(fn: (query: string, params?: SanityParams) => Promise<any>, { validationMode, logger, }?: {
declare function makeQueryClient(fn: (query: string, params?: SanityParams) => Promise<any>, { validationMode, logger, }?: {
logger?: Logger;

@@ -270,4 +262,20 @@ validationMode?: "ERROR" | "WARN";

declare function serialize(schema: TSchema): string;
/**
* Clone the given schema, removing the query cache and merging the given properties.
*/
declare function cloneSchema<T extends TSchema>(schema: T, merge?: any): T;
export { Collection, ConditionalUnion, type InferFromSchema, Nullable, Projection, Raw, type TCollection, type TConditionalUnion, type TConditionalUnionOptions, type TExpansionOption, type TNullable, type TProjection, type TProjectionOptions, type TRaw, type TTypedProjection, type TTypedUnion, TypedProjection, TypedUnion, expandConditionalUnion, expandProjection, filterCollection, filterProjection, isCollection, isConditionalUnion, isNullable, isProjection, isRaw, makeSafeSanityFetch, serialize, serializeCollection, serializeConditionalUnion, serializeNullable, serializeProjection, serializeRaw, sliceCollection, sliceProjection };
/**
* Serialize the given schema to a GROQ string.
*/
declare function serializeQuery(schema: TSchema): string;
/**
* Make query cache optionally available on all schemas.
*/
declare module "@sinclair/typebox" {
interface TSchema {
[QueryCacheSymbol]?: string;
}
}
export { Collection, ConditionalUnion, type InferFromSchema, Nullable, Projection, Raw, type TCollection, type TConditionalUnion, type TConditionalUnionOptions, type TExpansionOption, type TNullable, type TProjection, type TProjectionOptions, type TRaw, type TTypedProjection, type TTypedUnion, TypedProjection, TypedUnion, cloneSchema, expandConditionalUnion, expandProjection, filterCollection, filterProjection, isCollection, isConditionalUnion, isNullable, isProjection, isRaw, makeQueryClient, serializeCollection, serializeConditionalUnion, serializeNullable, serializeProjection, serializeQuery, serializeRaw, sliceCollection, sliceProjection };

@@ -12,16 +12,26 @@ // src/schemas/Collection.ts

import { TypeGuard } from "@sinclair/typebox";
var TypeAttribute = Symbol("type");
var GroqAttribute = Symbol("groq");
// src/symbols.ts
var GroqSymbol = Symbol("groq");
var TypeSymbol = Symbol("type");
var SliceSymbol = Symbol("slice");
var FilterSymbol = Symbol("filter");
var ExpandSymbol = Symbol("expand");
var ConditionsSymbol = Symbol("conditions");
var NeedsIntersectUnwrapSymbol = Symbol("needsIntersectUnwrap");
var QueryCacheSymbol = Symbol("queryCache");
// src/schemas/Raw.ts
function Raw(groq, schema) {
return {
...schema,
[TypeAttribute]: "Raw",
[GroqAttribute]: groq
[TypeSymbol]: "Raw",
[GroqSymbol]: groq
};
}
function isRaw(value) {
return TypeGuard.IsSchema(value) && "Raw" === value[TypeAttribute];
return TypeGuard.IsSchema(value) && "Raw" === value[TypeSymbol];
}
function serializeRaw(schema) {
return schema[GroqAttribute];
return schema[GroqSymbol];
}

@@ -31,19 +41,24 @@

import { Type, TypeGuard as TypeGuard2 } from "@sinclair/typebox";
var TypeAttribute2 = Symbol("type");
var SliceAttribute = Symbol("slice");
var FilterAttribute = Symbol("filter");
var ExpandAttribute = Symbol("expand");
// src/clone.ts
function cloneSchema(schema, merge) {
const clonedSchema = { ...schema, ...merge };
delete clonedSchema[QueryCacheSymbol];
return clonedSchema;
}
// src/schemas/Projection.ts
function Projection(properties, options) {
return Type.Object(properties, {
[TypeAttribute2]: "Projection",
[SliceAttribute]: options?.slice,
[FilterAttribute]: options?.filter,
[ExpandAttribute]: options?.expand
[TypeSymbol]: "Projection",
[SliceSymbol]: options?.slice,
[FilterSymbol]: options?.filter,
[ExpandSymbol]: options?.expand
});
}
function isProjection(value) {
return TypeGuard2.IsSchema(value) && "Projection" === value[TypeAttribute2];
return TypeGuard2.IsSchema(value) && "Projection" === value[TypeSymbol];
}
function serializeProjection(schema) {
const filter = schema[FilterAttribute]?.startsWith("*") ? schema[FilterAttribute].substring(1) : schema[FilterAttribute];
const filter = schema[FilterSymbol]?.startsWith("*") ? schema[FilterSymbol].substring(1) : schema[FilterSymbol];
const groq = [];

@@ -57,7 +72,7 @@ if (filter) {

const alreadySliced = filter ? /\[\d+\]$/.test(filter) : false;
if (!alreadySliced && (filter || "undefined" !== typeof schema[SliceAttribute])) {
groq.push(`[${schema[SliceAttribute] ?? 0}]`);
if (!alreadySliced && (filter || "undefined" !== typeof schema[SliceSymbol])) {
groq.push(`[${schema[SliceSymbol] ?? 0}]`);
}
const projection = Object.entries(schema.properties).map(([key, value]) => {
const innerGroq = serialize(value);
const innerGroq = serializeQuery(value);
if (innerGroq) {

@@ -70,7 +85,7 @@ if (innerGroq.startsWith("{") || innerGroq.startsWith("["))

}).join(",");
if (true === schema[ExpandAttribute]) {
if (true === schema[ExpandSymbol]) {
groq.push(`{...@->{${projection}}}`);
} else if ("string" === typeof schema[ExpandAttribute]) {
} else if ("string" === typeof schema[ExpandSymbol]) {
groq.push(
`{_type == "${schema[ExpandAttribute]}" => @->{${projection}},_type != "${schema[ExpandAttribute]}" => @{${projection}}}`
`{_type == "${schema[ExpandSymbol]}" => @->{${projection}},_type != "${schema[ExpandSymbol]}" => @{${projection}}}`
);

@@ -83,18 +98,15 @@ } else {

function filterProjection(schema, filter) {
return {
...schema,
[FilterAttribute]: filter
};
return cloneSchema(schema, {
[FilterSymbol]: filter
});
}
function sliceProjection(schema, slice) {
return {
...schema,
[SliceAttribute]: slice
};
return cloneSchema(schema, {
[SliceSymbol]: slice
});
}
function expandProjection(schema, expand) {
return {
...schema,
[ExpandAttribute]: expand ?? true
};
return cloneSchema(schema, {
[ExpandSymbol]: expand ?? true
});
}

@@ -104,18 +116,15 @@

import { Type as Type2, TypeGuard as TypeGuard3 } from "@sinclair/typebox";
var TypeAttribute3 = Symbol("type");
var ExpandAttibute = Symbol("expand");
var ConditionsAttribute = Symbol("conditions");
function ConditionalUnion(conditions, options) {
return Type2.Union(Object.values(conditions), {
[ExpandAttibute]: options?.expand,
[TypeAttribute3]: "ConditionalUnion",
[ConditionsAttribute]: Object.keys(conditions)
[ExpandSymbol]: options?.expand,
[TypeSymbol]: "ConditionalUnion",
[ConditionsSymbol]: Object.keys(conditions)
});
}
function isConditionalUnion(value) {
return TypeGuard3.IsSchema(value) && "ConditionalUnion" === value[TypeAttribute3];
return TypeGuard3.IsSchema(value) && "ConditionalUnion" === value[TypeSymbol];
}
function serializeConditionalUnion(schema) {
const groq = [];
const selectConditions = schema[ConditionsAttribute].map((condition, key) => {
const selectConditions = schema[ConditionsSymbol].map((condition, key) => {
if ("default" == condition)

@@ -126,17 +135,17 @@ return null;

return null;
const conditionGroq = serialize(conditionSchema) || "...";
const conditionGroq = serializeQuery(conditionSchema) || "...";
return `${condition} => ${conditionGroq}`;
}).filter(Boolean);
const defaultConditionIndex = schema[ConditionsAttribute].indexOf("default");
const defaultConditionIndex = schema[ConditionsSymbol].indexOf("default");
const defaultConditionSchema = 0 <= defaultConditionIndex ? schema.anyOf[defaultConditionIndex] : null;
if (defaultConditionSchema) {
const conditionGroq = serialize(defaultConditionSchema) || "...";
const conditionGroq = serializeQuery(defaultConditionSchema) || "...";
selectConditions.push(conditionGroq);
}
const projection = `...select(${selectConditions.join(",")})`;
if (true === schema[ExpandAttibute]) {
if (true === schema[ExpandSymbol]) {
groq.push(`{...@->{${projection}}}`);
} else if ("string" === typeof schema[ExpandAttibute]) {
} else if ("string" === typeof schema[ExpandSymbol]) {
groq.push(
`{_type == "${schema[ExpandAttibute]}" => @->{${projection}},_type != "${schema[ExpandAttibute]}" => @{${projection}}}`
`{_type == "${schema[ExpandSymbol]}" => @->{${projection}},_type != "${schema[ExpandSymbol]}" => @{${projection}}}`
);

@@ -149,44 +158,44 @@ } else {

function expandConditionalUnion(schema, expand) {
return {
...schema,
[ExpandAttibute]: expand ?? true
};
return cloneSchema(schema, {
[ExpandSymbol]: expand ?? true
});
}
// src/serialize.ts
function serialize(schema) {
if (isRaw(schema))
return serializeRaw(schema);
if (isNullable(schema))
return serializeNullable(schema);
if (isCollection(schema))
return serializeCollection(schema);
if (isProjection(schema))
return serializeProjection(schema);
if (isConditionalUnion(schema))
return serializeConditionalUnion(schema);
return "";
function serializeQuery(schema) {
if (schema[QueryCacheSymbol]) {
return schema[QueryCacheSymbol];
}
const query = (() => {
if (isRaw(schema))
return serializeRaw(schema);
if (isNullable(schema))
return serializeNullable(schema);
if (isCollection(schema))
return serializeCollection(schema);
if (isProjection(schema))
return serializeProjection(schema);
if (isConditionalUnion(schema))
return serializeConditionalUnion(schema);
return "";
})();
schema[QueryCacheSymbol] = query;
return query;
}
// src/schemas/Nullable.ts
var TypeAttribute4 = Symbol("type");
var InnerSchemaAttribute = Symbol("innerSchema");
function Nullable(schema) {
return Type3.Union([schema, Type3.Null()], {
[TypeAttribute4]: "Nullable",
[InnerSchemaAttribute]: schema
[TypeSymbol]: "Nullable"
});
}
function isNullable(value) {
return TypeGuard4.IsSchema(value) && "Nullable" === value[TypeAttribute4];
return TypeGuard4.IsSchema(value) && "Nullable" === value[TypeSymbol];
}
function serializeNullable(schema) {
return serialize(schema[InnerSchemaAttribute]);
const innerSchema = schema.anyOf[0];
return serializeQuery(innerSchema);
}
// src/schemas/Collection.ts
var TypeAttribute5 = Symbol("type");
var SliceAttribute2 = Symbol("slice");
var FilterAttribute2 = Symbol("filter");
var NeedsIntersectUnwrapAttribute = Symbol("needsIntersectUnwrap");
function Collection(schema, options) {

@@ -203,13 +212,13 @@ let arrayMemberSchema = schema;

return Type4.Array(arrayMemberSchema, {
[TypeAttribute5]: "Collection",
[SliceAttribute2]: options?.slice,
[FilterAttribute2]: options?.filter,
[NeedsIntersectUnwrapAttribute]: needsIntersectUnwrap
[TypeSymbol]: "Collection",
[SliceSymbol]: options?.slice,
[FilterSymbol]: options?.filter,
[NeedsIntersectUnwrapSymbol]: needsIntersectUnwrap
});
}
function isCollection(value) {
return TypeGuard5.IsSchema(value) && "Collection" === value[TypeAttribute5];
return TypeGuard5.IsSchema(value) && "Collection" === value[TypeSymbol];
}
function serializeCollection(schema) {
const filter = schema[FilterAttribute2]?.startsWith("*") ? schema[FilterAttribute2].substring(1) : schema[FilterAttribute2];
const filter = schema[FilterSymbol]?.startsWith("*") ? schema[FilterSymbol].substring(1) : schema[FilterSymbol];
const groq = [];

@@ -222,13 +231,13 @@ if (filter) {

}
if (schema[SliceAttribute2]) {
groq.push(`[${schema[SliceAttribute2][0]}...${schema[SliceAttribute2][1]}]`);
if (schema[SliceSymbol]) {
groq.push(`[${schema[SliceSymbol][0]}...${schema[SliceSymbol][1]}]`);
}
if (!filter && !schema[SliceAttribute2]) {
if (!filter && !schema[SliceSymbol]) {
groq.push("[]");
}
let innerSchema = schema.items;
if (schema[NeedsIntersectUnwrapAttribute]) {
if (schema[NeedsIntersectUnwrapSymbol]) {
innerSchema = schema.items.allOf[0];
}
const innerGroq = serialize(innerSchema);
const innerGroq = serializeQuery(innerSchema);
if (innerGroq) {

@@ -243,12 +252,10 @@ if (TypeGuard5.IsObject(innerSchema) || TypeGuard5.IsUnion(innerSchema)) {

function filterCollection(schema, filter) {
return {
...schema,
[FilterAttribute2]: filter
};
return cloneSchema(schema, {
[FilterSymbol]: filter
});
}
function sliceCollection(schema, slice) {
return {
...schema,
[SliceAttribute2]: slice
};
return cloneSchema(schema, {
[SliceSymbol]: slice
});
}

@@ -283,3 +290,3 @@

import { Value } from "@sinclair/typebox/value";
function makeSafeSanityFetch(fn, {
function makeQueryClient(fn, {
validationMode = "ERROR",

@@ -290,3 +297,3 @@ logger = pino({ level: "info" })

const resultSchema = Nullable(schema);
const groq = "*" + serialize(schema);
const groq = "*" + serializeQuery(schema);
if (!groq) {

@@ -339,2 +346,3 @@ throw new Error(

TypedUnion,
cloneSchema,
expandConditionalUnion,

@@ -349,4 +357,3 @@ expandProjection,

isRaw,
makeSafeSanityFetch,
serialize,
makeQueryClient,
serializeCollection,

@@ -356,2 +363,3 @@ serializeConditionalUnion,

serializeProjection,
serializeQuery,
serializeRaw,

@@ -358,0 +366,0 @@ sliceCollection,

{
"name": "@dangreaves/groq-query-builder",
"version": "0.16.0",
"version": "0.17.0",
"license": "MIT",

@@ -29,11 +29,11 @@ "author": {

"devDependencies": {
"@sanity/client": "^6.15.7",
"@sanity/client": "^6.15.11",
"@sinclair/typebox": "^0.32.20",
"@tsconfig/node20": "^20.1.4",
"@tsconfig/strictest": "^2.0.5",
"@types/node": "^20.11.30",
"@types/node": "^20.12.5",
"pino": "^8.19.0",
"prettier": "^3.2.5",
"tsup": "^8.0.2",
"typescript": "^5.4.3",
"typescript": "^5.4.4",
"vitest": "^1.4.0"

@@ -40,0 +40,0 @@ },

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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