Skip to content

Commit 806fb9a

Browse files
committed
add support for ranged sanity queries
1 parent 0a41d02 commit 806fb9a

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

.changeset/calm-toes-repair.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bluecadet/launchpad-content": minor
3+
---
4+
5+
Add support for ranged sanity queries (like `*[_type == "article"][0...100]` or `*[_type == "article"][0]`)

docs/src/reference/content/sources/sanity-source.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ An array of queries to fetch. Each query can be either:
7474
- A string representing a document type (e.g., `'article'` will fetch all documents of type 'article')
7575
- An object with a custom GROQ query and an ID for the result set
7676

77+
> [!NOTE] Note:
78+
> if the query includes a range (e.g., `*[_type == "article"][0...100]` or `*[_type == "article"][0]`), the `limit` and `maxNumPages` options will be ignored, and the query will be executed as is.
79+
7780
### `useCdn`
7881

7982
- **Type:** `boolean`

packages/content/src/sources/__tests__/sanity-source.test.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("sanitySource", () => {
3434
// missing projectId and apiToken
3535
});
3636

37-
expect(result).rejects.toThrow();
37+
await expect(result).rejects.toThrow();
3838
});
3939

4040
it("should fetch data with simple type queries", async () => {
@@ -93,7 +93,7 @@ describe("sanitySource", () => {
9393
useCdn: false,
9494
});
9595

96-
const result = source.fetch(createFetchContext());
96+
const result = await source.fetch(createFetchContext());
9797

9898
expect(result).toHaveLength(2);
9999

@@ -181,7 +181,7 @@ describe("sanitySource", () => {
181181

182182
const result = source.fetch(createFetchContext());
183183

184-
expect(result[0]!.data).rejects.toThrow();
184+
await expect(result[0]!.data).rejects.toThrow();
185185
});
186186

187187
it("should respect pagination options", async () => {
@@ -227,4 +227,47 @@ describe("sanitySource", () => {
227227
expect((await data.next()).value).toEqual([{ _type: "test", title: "Test Document 50" }]);
228228
expect((await data.next()).done).toBe(true);
229229
});
230+
231+
it("should support single item responses", async () => {
232+
server.use(
233+
http.get(
234+
"https://test-project.api.sanity.io/v2021-10-21/data/query/production",
235+
({ request }) => {
236+
return HttpResponse.json({
237+
result: { _type: "test", title: "Test Document" },
238+
ms: 15,
239+
});
240+
},
241+
),
242+
);
243+
244+
const source = await sanitySource({
245+
id: "test-sanity",
246+
projectId: "test-project",
247+
apiToken: "test-token",
248+
queries: [
249+
{
250+
id: "custom1",
251+
query: '*[_type == "custom"][0]',
252+
},
253+
{
254+
id: "custom2",
255+
query: '*[_type == "custom"][0..5]',
256+
},
257+
],
258+
limit: 50,
259+
mergePages: false,
260+
useCdn: false,
261+
});
262+
263+
const result = source.fetch(createFetchContext());
264+
expect(result).toHaveLength(2);
265+
266+
const data1 = (await result[0]!.data);
267+
const data2 = (await result[0]!.data);
268+
269+
// it should return the single item directly instead of an async iterator
270+
expect(data1).toEqual({ _type: "test", title: "Test Document" });
271+
expect(data2).toEqual({ _type: "test", title: "Test Document" });
272+
});
230273
});

packages/content/src/sources/sanity-source.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ export default async function sanitySource(options: z.input<typeof sanitySourceS
5656
id = query.id;
5757
}
5858

59+
// if full query already contains a number range, no need to paginate
60+
if (fullQuery.match(/\[\d+(\.\.\d+)?\]/)) {
61+
ctx.logger.debug(`Query '${fullQuery}' already contains a number range. No pagination.`);
62+
return {
63+
id,
64+
data: sanityClient.fetch<unknown>(fullQuery),
65+
};
66+
}
67+
5968
return {
6069
id,
6170
data: fetchPaginated({

0 commit comments

Comments
 (0)