@svadmin/medusa 0.3.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 +20 -0
- package/src/data-provider.ts +73 -0
- package/src/index.ts +1 -0
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@svadmin/medusa",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Medusa DataProvider for svadmin",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"files": ["src/**/*"],
|
|
8
|
+
"main": "src/index.ts",
|
|
9
|
+
"types": "src/index.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./src/index.ts",
|
|
13
|
+
"default": "./src/index.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@svadmin/core": "^0.3.0"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT"
|
|
20
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DataProvider, GetListParams, GetListResult, GetOneParams, GetOneResult,
|
|
3
|
+
CreateParams, CreateResult, UpdateParams, UpdateResult, DeleteParams, DeleteResult,
|
|
4
|
+
GetManyParams, GetManyResult, CustomParams, CustomResult
|
|
5
|
+
} from '@svadmin/core';
|
|
6
|
+
|
|
7
|
+
export function createMedusaDataProvider(apiUrl: string, apiKey?: string): DataProvider {
|
|
8
|
+
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
9
|
+
if (apiKey) headers['x-medusa-access-token'] = apiKey;
|
|
10
|
+
|
|
11
|
+
async function request(path: string, opts?: RequestInit) {
|
|
12
|
+
const res = await fetch(`${apiUrl}${path}`, { ...opts, headers: { ...headers, ...opts?.headers } });
|
|
13
|
+
if (!res.ok) throw new Error(`Medusa API error: ${res.status}`);
|
|
14
|
+
return res.json();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
getApiUrl: () => apiUrl,
|
|
19
|
+
|
|
20
|
+
async getList<T>({ resource, pagination, sorters, filters }: GetListParams): Promise<GetListResult<T>> {
|
|
21
|
+
const { current = 1, pageSize = 10 } = pagination ?? {};
|
|
22
|
+
const params = new URLSearchParams();
|
|
23
|
+
params.set('offset', String((current - 1) * pageSize));
|
|
24
|
+
params.set('limit', String(pageSize));
|
|
25
|
+
if (sorters?.length) params.set('order', sorters.map(s => `${s.order === 'desc' ? '-' : ''}${s.field}`).join(','));
|
|
26
|
+
if (filters?.length) {
|
|
27
|
+
for (const f of filters) {
|
|
28
|
+
if ('field' in f) params.set(f.field as string, String(f.value));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const data = await request(`/admin/${resource}?${params}`);
|
|
32
|
+
return { data: data[resource] ?? data.data ?? [], total: data.count ?? data[resource]?.length ?? 0 };
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
async getOne<T>({ resource, id }: GetOneParams): Promise<GetOneResult<T>> {
|
|
36
|
+
const singular = resource.replace(/s$/, '');
|
|
37
|
+
const data = await request(`/admin/${resource}/${id}`);
|
|
38
|
+
return { data: (data[singular] ?? data) as T };
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
async create<T>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
|
|
42
|
+
const singular = resource.replace(/s$/, '');
|
|
43
|
+
const data = await request(`/admin/${resource}`, { method: 'POST', body: JSON.stringify(variables) });
|
|
44
|
+
return { data: (data[singular] ?? data) as T };
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
async update<T>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
|
|
48
|
+
const singular = resource.replace(/s$/, '');
|
|
49
|
+
const data = await request(`/admin/${resource}/${id}`, { method: 'POST', body: JSON.stringify(variables) });
|
|
50
|
+
return { data: (data[singular] ?? data) as T };
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
async deleteOne<T>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
|
|
54
|
+
const data = await request(`/admin/${resource}/${id}`, { method: 'DELETE' });
|
|
55
|
+
return { data: data as T };
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
async getMany<T>({ resource, ids }: GetManyParams): Promise<GetManyResult<T>> {
|
|
59
|
+
const results = await Promise.all(ids.map(async (id: string | number) => {
|
|
60
|
+
const singular = resource.replace(/s$/, '');
|
|
61
|
+
const data = await request(`/admin/${resource}/${id}`);
|
|
62
|
+
return (data[singular] ?? data) as T;
|
|
63
|
+
}));
|
|
64
|
+
return { data: results };
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
async custom<T>({ url, method, payload, headers: h }: CustomParams): Promise<CustomResult<T>> {
|
|
68
|
+
const res = await fetch(url, { method: method.toUpperCase(), headers: { ...headers, ...h }, body: payload ? JSON.stringify(payload) : undefined });
|
|
69
|
+
if (!res.ok) throw new Error(`Custom request failed: ${res.status}`);
|
|
70
|
+
return { data: (await res.json()) as T };
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createMedusaDataProvider } from './data-provider';
|