@svadmin/sanity 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -4
- package/src/data-provider.ts +21 -10
- package/src/index.ts +1 -1
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svadmin/sanity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Sanity DataProvider for svadmin",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
|
-
"files": [
|
|
7
|
+
"files": [
|
|
8
|
+
"src/**/*"
|
|
9
|
+
],
|
|
8
10
|
"main": "src/index.ts",
|
|
9
11
|
"types": "src/index.ts",
|
|
10
12
|
"exports": {
|
|
@@ -14,7 +16,12 @@
|
|
|
14
16
|
}
|
|
15
17
|
},
|
|
16
18
|
"peerDependencies": {
|
|
17
|
-
"@svadmin/core": "^0.
|
|
19
|
+
"@svadmin/core": "^0.4.1"
|
|
18
20
|
},
|
|
19
|
-
"license": "MIT"
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "https://github.com/zuohuadong/svadmin.git",
|
|
25
|
+
"directory": "packages/sanity"
|
|
26
|
+
}
|
|
20
27
|
}
|
package/src/data-provider.ts
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
DataProvider, GetListParams, GetListResult, GetOneParams, GetOneResult,
|
|
3
3
|
CreateParams, CreateResult, UpdateParams, UpdateResult, DeleteParams, DeleteResult,
|
|
4
|
-
GetManyParams, GetManyResult, CustomParams, CustomResult
|
|
4
|
+
GetManyParams, GetManyResult, CustomParams, CustomResult, Filter, Sort
|
|
5
5
|
} from '@svadmin/core';
|
|
6
6
|
|
|
7
|
+
interface SanityDoc {
|
|
8
|
+
_id: string;
|
|
9
|
+
_type: string;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface SanityMutationResult {
|
|
14
|
+
results?: Array<{ id: string }>;
|
|
15
|
+
}
|
|
16
|
+
|
|
7
17
|
// Sanity.io uses GROQ query language
|
|
8
|
-
function buildGroqFilter(filters?:
|
|
18
|
+
function buildGroqFilter(filters?: Filter[]): string {
|
|
9
19
|
if (!filters?.length) return '';
|
|
10
20
|
return filters
|
|
11
|
-
.filter((f:
|
|
12
|
-
.map((f
|
|
21
|
+
.filter((f): f is Filter & { field: string } => 'field' in f)
|
|
22
|
+
.map((f) => {
|
|
13
23
|
switch (f.operator) {
|
|
14
24
|
case 'eq': return `${f.field} == "${f.value}"`;
|
|
15
25
|
case 'ne': return `${f.field} != "${f.value}"`;
|
|
@@ -28,7 +38,7 @@ export function createSanityDataProvider(projectId: string, dataset: string, tok
|
|
|
28
38
|
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
29
39
|
if (token) headers['Authorization'] = `Bearer ${token}`;
|
|
30
40
|
|
|
31
|
-
async function query(groq: string, params: Record<string, unknown> = {}): Promise<
|
|
41
|
+
async function query(groq: string, params: Record<string, unknown> = {}): Promise<unknown> {
|
|
32
42
|
const url = new URL(`${apiUrl}/data/query/${dataset}`);
|
|
33
43
|
url.searchParams.set('query', groq);
|
|
34
44
|
for (const [k, v] of Object.entries(params)) url.searchParams.set(`$${k}`, JSON.stringify(v));
|
|
@@ -38,7 +48,7 @@ export function createSanityDataProvider(projectId: string, dataset: string, tok
|
|
|
38
48
|
return data.result;
|
|
39
49
|
}
|
|
40
50
|
|
|
41
|
-
async function mutate(mutations:
|
|
51
|
+
async function mutate(mutations: Record<string, unknown>[]): Promise<SanityMutationResult> {
|
|
42
52
|
const res = await fetch(`${apiUrl}/data/mutate/${dataset}`, {
|
|
43
53
|
method: 'POST', headers, body: JSON.stringify({ mutations }),
|
|
44
54
|
});
|
|
@@ -56,17 +66,18 @@ export function createSanityDataProvider(projectId: string, dataset: string, tok
|
|
|
56
66
|
const filterStr = buildGroqFilter(filters);
|
|
57
67
|
const typeFilter = `_type == "${resource}"`;
|
|
58
68
|
const where = filterStr ? `${typeFilter} && ${filterStr}` : typeFilter;
|
|
59
|
-
const orderStr = sorters?.length ? ` | order(${sorters.map(s => `${s.field} ${s.order}`).join(', ')})` : '';
|
|
69
|
+
const orderStr = sorters?.length ? ` | order(${sorters.map((s: Sort) => `${s.field} ${s.order}`).join(', ')})` : '';
|
|
60
70
|
|
|
61
71
|
const [data, total] = await Promise.all([
|
|
62
72
|
query(`*[${where}]${orderStr}[${start}...${end}]`),
|
|
63
73
|
query(`count(*[${where}])`),
|
|
64
74
|
]);
|
|
65
|
-
return { data: (data ?? []).map((d
|
|
75
|
+
return { data: ((data as SanityDoc[]) ?? []).map((d) => ({ ...d, id: d._id })) as T[], total: (total as number) ?? 0 };
|
|
66
76
|
},
|
|
67
77
|
|
|
68
78
|
async getOne<T>({ resource, id }: GetOneParams): Promise<GetOneResult<T>> {
|
|
69
|
-
const
|
|
79
|
+
const raw = await query(`*[_type == "${resource}" && _id == $id][0]`, { id });
|
|
80
|
+
const data = raw as SanityDoc;
|
|
70
81
|
return { data: { ...data, id: data._id } as T };
|
|
71
82
|
},
|
|
72
83
|
|
|
@@ -89,7 +100,7 @@ export function createSanityDataProvider(projectId: string, dataset: string, tok
|
|
|
89
100
|
|
|
90
101
|
async getMany<T>({ resource, ids }: GetManyParams): Promise<GetManyResult<T>> {
|
|
91
102
|
const data = await query(`*[_type == "${resource}" && _id in $ids]`, { ids });
|
|
92
|
-
return { data: (data ?? []).map((d
|
|
103
|
+
return { data: ((data as SanityDoc[]) ?? []).map((d) => ({ ...d, id: d._id })) as T[] };
|
|
93
104
|
},
|
|
94
105
|
|
|
95
106
|
async custom<T>({ url, method, payload, headers: h }: CustomParams): Promise<CustomResult<T>> {
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { createSanityDataProvider } from './data-provider';
|
|
1
|
+
export { createSanityDataProvider } from './data-provider';
|