@svadmin/sanity 0.4.1 → 0.5.13
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 +2 -2
- package/src/data-provider.ts +11 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svadmin/sanity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.13",
|
|
4
4
|
"description": "Sanity DataProvider for svadmin",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@svadmin/core": "^0.
|
|
19
|
+
"@svadmin/core": "^0.5.0"
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"repository": {
|
package/src/data-provider.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
DataProvider, GetListParams, GetListResult, GetOneParams, GetOneResult,
|
|
3
3
|
CreateParams, CreateResult, UpdateParams, UpdateResult, DeleteParams, DeleteResult,
|
|
4
|
-
GetManyParams, GetManyResult, CustomParams, CustomResult, Filter, Sort
|
|
4
|
+
GetManyParams, GetManyResult, CustomParams, CustomResult, Filter, Sort, BaseRecord
|
|
5
5
|
} from '@svadmin/core';
|
|
6
6
|
|
|
7
7
|
interface SanityDoc {
|
|
@@ -59,7 +59,7 @@ export function createSanityDataProvider(projectId: string, dataset: string, tok
|
|
|
59
59
|
return {
|
|
60
60
|
getApiUrl: () => apiUrl,
|
|
61
61
|
|
|
62
|
-
async getList<T>({ resource, pagination, sorters, filters }: GetListParams): Promise<GetListResult<T>> {
|
|
62
|
+
async getList<T extends BaseRecord = BaseRecord>({ resource, pagination, sorters, filters }: GetListParams): Promise<GetListResult<T>> {
|
|
63
63
|
const { current = 1, pageSize = 10 } = pagination ?? {};
|
|
64
64
|
const start = (current - 1) * pageSize;
|
|
65
65
|
const end = start + pageSize;
|
|
@@ -75,35 +75,35 @@ export function createSanityDataProvider(projectId: string, dataset: string, tok
|
|
|
75
75
|
return { data: ((data as SanityDoc[]) ?? []).map((d) => ({ ...d, id: d._id })) as T[], total: (total as number) ?? 0 };
|
|
76
76
|
},
|
|
77
77
|
|
|
78
|
-
async getOne<T>({ resource, id }: GetOneParams): Promise<GetOneResult<T>> {
|
|
78
|
+
async getOne<T extends BaseRecord = BaseRecord>({ resource, id }: GetOneParams): Promise<GetOneResult<T>> {
|
|
79
79
|
const raw = await query(`*[_type == "${resource}" && _id == $id][0]`, { id });
|
|
80
80
|
const data = raw as SanityDoc;
|
|
81
81
|
return { data: { ...data, id: data._id } as T };
|
|
82
82
|
},
|
|
83
83
|
|
|
84
|
-
async create<T>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
|
|
85
|
-
const doc = { _type: resource, ...variables };
|
|
84
|
+
async create<T extends BaseRecord = BaseRecord>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
|
|
85
|
+
const doc = { _type: resource, ...(variables as Record<string, unknown>) };
|
|
86
86
|
const result = await mutate([{ create: doc }]);
|
|
87
87
|
const id = result.results?.[0]?.id;
|
|
88
|
-
return { data: { ...variables, id, _id: id } as T };
|
|
88
|
+
return { data: { ...(variables as Record<string, unknown>), id, _id: id } as unknown as T };
|
|
89
89
|
},
|
|
90
90
|
|
|
91
|
-
async update<T>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
|
|
91
|
+
async update<T extends BaseRecord = BaseRecord>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
|
|
92
92
|
await mutate([{ patch: { id, set: variables } }]);
|
|
93
|
-
return { data: { ...variables, id, _id: id } as T };
|
|
93
|
+
return { data: { ...(variables as Record<string, unknown>), id, _id: id } as unknown as T };
|
|
94
94
|
},
|
|
95
95
|
|
|
96
|
-
async deleteOne<T>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
|
|
96
|
+
async deleteOne<T extends BaseRecord = BaseRecord>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
|
|
97
97
|
await mutate([{ delete: { id } }]);
|
|
98
98
|
return { data: { id } as T };
|
|
99
99
|
},
|
|
100
100
|
|
|
101
|
-
async getMany<T>({ resource, ids }: GetManyParams): Promise<GetManyResult<T>> {
|
|
101
|
+
async getMany<T extends BaseRecord = BaseRecord>({ resource, ids }: GetManyParams): Promise<GetManyResult<T>> {
|
|
102
102
|
const data = await query(`*[_type == "${resource}" && _id in $ids]`, { ids });
|
|
103
103
|
return { data: ((data as SanityDoc[]) ?? []).map((d) => ({ ...d, id: d._id })) as T[] };
|
|
104
104
|
},
|
|
105
105
|
|
|
106
|
-
async custom<T>({ url, method, payload, headers: h }: CustomParams): Promise<CustomResult<T>> {
|
|
106
|
+
async custom<T = unknown>({ url, method, payload, headers: h }: CustomParams): Promise<CustomResult<T>> {
|
|
107
107
|
const res = await fetch(url, { method: method.toUpperCase(), headers: { ...headers, ...h }, body: payload ? JSON.stringify(payload) : undefined });
|
|
108
108
|
if (!res.ok) throw new Error(`Custom request failed: ${res.status}`);
|
|
109
109
|
return { data: (await res.json()) as T };
|