@svadmin/medusa 0.8.0 → 0.9.0
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 +1 -1
- package/src/data-provider.ts +14 -14
package/package.json
CHANGED
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
|
|
4
|
+
GetManyParams, GetManyResult, CustomParams, CustomResult, Sort, Filter, BaseRecord
|
|
5
5
|
} from '@svadmin/core';
|
|
6
6
|
|
|
7
7
|
export function createMedusaDataProvider(apiUrl: string, apiKey?: string): DataProvider {
|
|
@@ -17,7 +17,7 @@ export function createMedusaDataProvider(apiUrl: string, apiKey?: string): DataP
|
|
|
17
17
|
return {
|
|
18
18
|
getApiUrl: () => apiUrl,
|
|
19
19
|
|
|
20
|
-
async getList<T>({ resource, pagination, sorters, filters }: GetListParams): Promise<GetListResult<T>> {
|
|
20
|
+
async getList<T extends BaseRecord = BaseRecord>({ resource, pagination, sorters, filters }: GetListParams): Promise<GetListResult<T>> {
|
|
21
21
|
const { current = 1, pageSize = 10 } = pagination ?? {};
|
|
22
22
|
const params = new URLSearchParams();
|
|
23
23
|
params.set('offset', String((current - 1) * pageSize));
|
|
@@ -32,42 +32,42 @@ export function createMedusaDataProvider(apiUrl: string, apiKey?: string): DataP
|
|
|
32
32
|
return { data: data[resource] ?? data.data ?? [], total: data.count ?? data[resource]?.length ?? 0 };
|
|
33
33
|
},
|
|
34
34
|
|
|
35
|
-
async getOne<T>({ resource, id }: GetOneParams): Promise<GetOneResult<T>> {
|
|
35
|
+
async getOne<T extends BaseRecord = BaseRecord>({ resource, id }: GetOneParams): Promise<GetOneResult<T>> {
|
|
36
36
|
const singular = resource.replace(/s$/, '');
|
|
37
37
|
const data = await request(`/admin/${resource}/${id}`);
|
|
38
|
-
return { data: (data[singular] ?? data) as T };
|
|
38
|
+
return { data: (data[singular] ?? data) as unknown as unknown as T };
|
|
39
39
|
},
|
|
40
40
|
|
|
41
|
-
async create<T>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
|
|
41
|
+
async create<T extends BaseRecord = BaseRecord>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
|
|
42
42
|
const singular = resource.replace(/s$/, '');
|
|
43
43
|
const data = await request(`/admin/${resource}`, { method: 'POST', body: JSON.stringify(variables) });
|
|
44
|
-
return { data: (data[singular] ?? data) as T };
|
|
44
|
+
return { data: (data[singular] ?? data) as unknown as unknown as T };
|
|
45
45
|
},
|
|
46
46
|
|
|
47
|
-
async update<T>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
|
|
47
|
+
async update<T extends BaseRecord = BaseRecord>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
|
|
48
48
|
const singular = resource.replace(/s$/, '');
|
|
49
49
|
const data = await request(`/admin/${resource}/${id}`, { method: 'POST', body: JSON.stringify(variables) });
|
|
50
|
-
return { data: (data[singular] ?? data) as T };
|
|
50
|
+
return { data: (data[singular] ?? data) as unknown as unknown as T };
|
|
51
51
|
},
|
|
52
52
|
|
|
53
|
-
async deleteOne<T>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
|
|
53
|
+
async deleteOne<T extends BaseRecord = BaseRecord>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
|
|
54
54
|
const data = await request(`/admin/${resource}/${id}`, { method: 'DELETE' });
|
|
55
|
-
return { data: data as T };
|
|
55
|
+
return { data: data as unknown as unknown as T };
|
|
56
56
|
},
|
|
57
57
|
|
|
58
|
-
async getMany<T>({ resource, ids }: GetManyParams): Promise<GetManyResult<T>> {
|
|
58
|
+
async getMany<T extends BaseRecord = BaseRecord>({ resource, ids }: GetManyParams): Promise<GetManyResult<T>> {
|
|
59
59
|
const results = await Promise.all(ids.map(async (id: string | number) => {
|
|
60
60
|
const singular = resource.replace(/s$/, '');
|
|
61
61
|
const data = await request(`/admin/${resource}/${id}`);
|
|
62
|
-
return (data[singular] ?? data) as T;
|
|
62
|
+
return (data[singular] ?? data) as unknown as unknown as T;
|
|
63
63
|
}));
|
|
64
64
|
return { data: results };
|
|
65
65
|
},
|
|
66
66
|
|
|
67
|
-
async custom<T>({ url, method, payload, headers: h }: CustomParams): Promise<CustomResult<T>> {
|
|
67
|
+
async custom<T = unknown>({ url, method, payload, headers: h }: CustomParams): Promise<CustomResult<T>> {
|
|
68
68
|
const res = await fetch(url, { method: method.toUpperCase(), headers: { ...headers, ...h }, body: payload ? JSON.stringify(payload) : undefined });
|
|
69
69
|
if (!res.ok) throw new Error(`Custom request failed: ${res.status}`);
|
|
70
|
-
return { data: (await res.json()) as T };
|
|
70
|
+
return { data: (await res.json()) as unknown as unknown as T };
|
|
71
71
|
},
|
|
72
72
|
};
|
|
73
73
|
}
|