@svadmin/simple-rest 0.8.0 → 0.9.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 +2 -2
- package/src/data-provider.ts +30 -30
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svadmin/simple-rest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "Simple REST adapters — fetch-based DataProvider and JWT/Cookie AuthProvider",
|
|
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.19.3"
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"author": "zuohuadong",
|
package/src/data-provider.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
CreateParams, CreateResult, UpdateParams, UpdateResult, DeleteParams, DeleteResult,
|
|
7
7
|
GetManyParams, GetManyResult, CreateManyParams, CreateManyResult,
|
|
8
8
|
UpdateManyParams, UpdateManyResult, DeleteManyParams, DeleteManyResult,
|
|
9
|
-
CustomParams, CustomResult,
|
|
9
|
+
CustomParams, CustomResult, BaseRecord
|
|
10
10
|
} from '@svadmin/core';
|
|
11
11
|
|
|
12
12
|
export interface SimpleRestOptions {
|
|
@@ -32,7 +32,7 @@ export function createSimpleRestDataProvider(opts: SimpleRestOptions): DataProvi
|
|
|
32
32
|
return {
|
|
33
33
|
getApiUrl: () => apiUrl,
|
|
34
34
|
|
|
35
|
-
async getList<T>({ resource, pagination, sorters, filters }: GetListParams): Promise<GetListResult<T>> {
|
|
35
|
+
async getList<T extends BaseRecord = BaseRecord>({ resource, pagination, sorters, filters }: GetListParams): Promise<GetListResult<T>> {
|
|
36
36
|
const params = new URLSearchParams();
|
|
37
37
|
const { current = 1, pageSize = 10 } = pagination ?? {};
|
|
38
38
|
params.set('_page', String(current));
|
|
@@ -55,83 +55,83 @@ export function createSimpleRestDataProvider(opts: SimpleRestOptions): DataProvi
|
|
|
55
55
|
const response = await fetch(url, { headers: getHeaders(opts) });
|
|
56
56
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
57
57
|
|
|
58
|
-
const data = await response.json() as T[];
|
|
58
|
+
const data = await response.json() as unknown as unknown as T[];
|
|
59
59
|
const total = parseInt(response.headers.get('X-Total-Count') ?? String(data.length), 10);
|
|
60
60
|
return { data, total };
|
|
61
61
|
},
|
|
62
62
|
|
|
63
|
-
async getOne<T>({ resource, id }: GetOneParams): Promise<GetOneResult<T>> {
|
|
64
|
-
const data = await request<
|
|
65
|
-
return { data };
|
|
63
|
+
async getOne<T extends BaseRecord = BaseRecord>({ resource, id }: GetOneParams): Promise<GetOneResult<T>> {
|
|
64
|
+
const data = await request<unknown>(`${apiUrl}/${resource}/${id}`);
|
|
65
|
+
return { data: data as unknown as unknown as T };
|
|
66
66
|
},
|
|
67
67
|
|
|
68
|
-
async create<T>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
|
|
69
|
-
const data = await request<
|
|
68
|
+
async create<T extends BaseRecord = BaseRecord>({ resource, variables }: CreateParams): Promise<CreateResult<T>> {
|
|
69
|
+
const data = await request<unknown>(`${apiUrl}/${resource}`, {
|
|
70
70
|
method: 'POST',
|
|
71
71
|
body: JSON.stringify(variables),
|
|
72
72
|
});
|
|
73
|
-
return { data };
|
|
73
|
+
return { data: data as unknown as unknown as T };
|
|
74
74
|
},
|
|
75
75
|
|
|
76
|
-
async update<T>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
|
|
77
|
-
const data = await request<
|
|
76
|
+
async update<T extends BaseRecord = BaseRecord>({ resource, id, variables }: UpdateParams): Promise<UpdateResult<T>> {
|
|
77
|
+
const data = await request<unknown>(`${apiUrl}/${resource}/${id}`, {
|
|
78
78
|
method: 'PATCH',
|
|
79
79
|
body: JSON.stringify(variables),
|
|
80
80
|
});
|
|
81
|
-
return { data };
|
|
81
|
+
return { data: data as unknown as unknown as T };
|
|
82
82
|
},
|
|
83
83
|
|
|
84
|
-
async deleteOne<T>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
|
|
85
|
-
const data = await request<
|
|
86
|
-
return { data };
|
|
84
|
+
async deleteOne<T extends BaseRecord = BaseRecord>({ resource, id }: DeleteParams): Promise<DeleteResult<T>> {
|
|
85
|
+
const data = await request<unknown>(`${apiUrl}/${resource}/${id}`, { method: 'DELETE' });
|
|
86
|
+
return { data: data as unknown as unknown as T };
|
|
87
87
|
},
|
|
88
88
|
|
|
89
|
-
async getMany<T>({ resource, ids }: GetManyParams): Promise<GetManyResult<T>> {
|
|
89
|
+
async getMany<T extends BaseRecord = BaseRecord>({ resource, ids }: GetManyParams): Promise<GetManyResult<T>> {
|
|
90
90
|
const params = ids.map(id => `id=${id}`).join('&');
|
|
91
|
-
const data = await request<
|
|
92
|
-
return { data };
|
|
91
|
+
const data = await request<unknown[]>(`${apiUrl}/${resource}?${params}`);
|
|
92
|
+
return { data: data as unknown as unknown as T[] };
|
|
93
93
|
},
|
|
94
94
|
|
|
95
|
-
async createMany<T>({ resource, variables }: CreateManyParams): Promise<CreateManyResult<T>> {
|
|
95
|
+
async createMany<T extends BaseRecord = BaseRecord>({ resource, variables }: CreateManyParams): Promise<CreateManyResult<T>> {
|
|
96
96
|
const results: T[] = [];
|
|
97
97
|
for (const vars of variables) {
|
|
98
|
-
const data = await request<
|
|
98
|
+
const data = await request<unknown>(`${apiUrl}/${resource}`, {
|
|
99
99
|
method: 'POST',
|
|
100
100
|
body: JSON.stringify(vars),
|
|
101
101
|
});
|
|
102
|
-
results.push(data);
|
|
102
|
+
results.push(data as unknown as T);
|
|
103
103
|
}
|
|
104
104
|
return { data: results };
|
|
105
105
|
},
|
|
106
106
|
|
|
107
|
-
async updateMany<T>({ resource, ids, variables }: UpdateManyParams): Promise<UpdateManyResult<T>> {
|
|
107
|
+
async updateMany<T extends BaseRecord = BaseRecord>({ resource, ids, variables }: UpdateManyParams): Promise<UpdateManyResult<T>> {
|
|
108
108
|
const results: T[] = [];
|
|
109
109
|
for (const id of ids) {
|
|
110
|
-
const data = await request<
|
|
110
|
+
const data = await request<unknown>(`${apiUrl}/${resource}/${id}`, {
|
|
111
111
|
method: 'PATCH',
|
|
112
112
|
body: JSON.stringify(variables),
|
|
113
113
|
});
|
|
114
|
-
results.push(data);
|
|
114
|
+
results.push(data as unknown as T);
|
|
115
115
|
}
|
|
116
116
|
return { data: results };
|
|
117
117
|
},
|
|
118
118
|
|
|
119
|
-
async deleteMany<T>({ resource, ids }: DeleteManyParams): Promise<DeleteManyResult<T>> {
|
|
119
|
+
async deleteMany<T extends BaseRecord = BaseRecord>({ resource, ids }: DeleteManyParams): Promise<DeleteManyResult<T>> {
|
|
120
120
|
const results: T[] = [];
|
|
121
121
|
for (const id of ids) {
|
|
122
|
-
const data = await request<
|
|
123
|
-
results.push(data);
|
|
122
|
+
const data = await request<unknown>(`${apiUrl}/${resource}/${id}`, { method: 'DELETE' });
|
|
123
|
+
results.push(data as unknown as T);
|
|
124
124
|
}
|
|
125
125
|
return { data: results };
|
|
126
126
|
},
|
|
127
127
|
|
|
128
|
-
async custom<T>({ url, method, payload, headers }: CustomParams): Promise<CustomResult<T>> {
|
|
129
|
-
const data = await request<
|
|
128
|
+
async custom<T = unknown>({ url, method, payload, headers }: CustomParams): Promise<CustomResult<T>> {
|
|
129
|
+
const data = await request<unknown>(url, {
|
|
130
130
|
method: method.toUpperCase(),
|
|
131
131
|
headers,
|
|
132
132
|
body: payload ? JSON.stringify(payload) : undefined,
|
|
133
133
|
});
|
|
134
|
-
return { data };
|
|
134
|
+
return { data: data as unknown as unknown as T };
|
|
135
135
|
},
|
|
136
136
|
};
|
|
137
137
|
}
|