@raphiniert/ra-data-postgrest 1.1.9 → 2.0.0-alpha.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/CHANGELOG.md +15 -1
- package/README.md +11 -4
- package/esm/index.d.ts +9 -4
- package/esm/index.js +241 -4
- package/esm/urlBuilder.d.ts +18 -0
- package/esm/urlBuilder.js +183 -0
- package/lib/index.d.ts +9 -4
- package/lib/index.js +240 -7
- package/lib/urlBuilder.d.ts +18 -0
- package/lib/urlBuilder.js +195 -0
- package/package.json +13 -5
- package/src/index.ts +302 -4
- package/src/urlBuilder.ts +221 -0
- package/esm/authProvider/index.d.ts +0 -11
- package/esm/authProvider/index.js +0 -58
- package/esm/dataProvider/index.d.ts +0 -9
- package/esm/dataProvider/index.js +0 -360
- package/lib/authProvider/index.d.ts +0 -11
- package/lib/authProvider/index.js +0 -60
- package/lib/dataProvider/index.d.ts +0 -9
- package/lib/dataProvider/index.js +0 -362
- package/src/authProvider/index.ts +0 -56
- package/src/dataProvider/index.ts +0 -412
package/src/index.ts
CHANGED
|
@@ -1,5 +1,303 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { fetchUtils, DataProvider } from 'ra-core';
|
|
2
|
+
import {
|
|
3
|
+
PrimaryKey,
|
|
4
|
+
PostgRestOperator,
|
|
5
|
+
getPrimaryKey,
|
|
6
|
+
parseFilters,
|
|
7
|
+
getOrderBy,
|
|
8
|
+
dataWithId,
|
|
9
|
+
getQuery,
|
|
10
|
+
getKeyData,
|
|
11
|
+
encodeId,
|
|
12
|
+
decodeId,
|
|
13
|
+
isCompoundKey,
|
|
14
|
+
} from './urlBuilder';
|
|
15
|
+
/**
|
|
16
|
+
* Maps react-admin queries to a postgrest REST API
|
|
17
|
+
*
|
|
18
|
+
* This REST dialect uses postgrest syntax
|
|
19
|
+
*
|
|
20
|
+
* @see https://postgrest.org/en/stable/api.html#embedded-filters
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
*
|
|
24
|
+
* getList => GET http://my.api.url/posts?order=title.asc&offset=0&limit=24&filterField=eq.value
|
|
25
|
+
* getOne => GET http://my.api.url/posts?id=eq.123
|
|
26
|
+
* getMany => GET http://my.api.url/posts?id=in.(123,456,789)
|
|
27
|
+
* getManyReference => GET http://my.api.url/posts?author_id=eq.345
|
|
28
|
+
* create => POST http://my.api.url/posts
|
|
29
|
+
* update => PATCH http://my.api.url/posts?id=eq.123
|
|
30
|
+
* updateMany => PATCH http://my.api.url/posts?id=in.(123,456,789)
|
|
31
|
+
* delete => DELETE http://my.api.url/posts?id=eq.123
|
|
32
|
+
* deleteMany => DELETE http://my.api.url/posts?id=in.(123,456,789)
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
*
|
|
36
|
+
* import * as React from 'react';
|
|
37
|
+
* import { Admin, Resource } from 'react-admin';
|
|
38
|
+
* import postgrestRestProvider from '@promitheus/ra-data-postgrest';
|
|
39
|
+
*
|
|
40
|
+
* import { PostList } from './posts';
|
|
41
|
+
*
|
|
42
|
+
* const App = () => (
|
|
43
|
+
* <Admin dataProvider={postgrestRestProvider('http://path.to.my.api/')}>
|
|
44
|
+
* <Resource name="posts" list={PostList} />
|
|
45
|
+
* </Admin>
|
|
46
|
+
* );
|
|
47
|
+
*
|
|
48
|
+
* export default App;
|
|
49
|
+
*/
|
|
3
50
|
|
|
4
|
-
|
|
5
|
-
|
|
51
|
+
const defaultPrimaryKeys = new Map<string, PrimaryKey>();
|
|
52
|
+
|
|
53
|
+
export default (
|
|
54
|
+
apiUrl: string,
|
|
55
|
+
httpClient = fetchUtils.fetchJson,
|
|
56
|
+
defaultListOp: PostgRestOperator = 'eq',
|
|
57
|
+
primaryKeys: Map<string, PrimaryKey> = defaultPrimaryKeys
|
|
58
|
+
): DataProvider => ({
|
|
59
|
+
getList: (resource, params) => {
|
|
60
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
61
|
+
|
|
62
|
+
const { page, perPage } = params.pagination;
|
|
63
|
+
const { field, order } = params.sort;
|
|
64
|
+
const parsedFilter = parseFilters(params.filter, defaultListOp);
|
|
65
|
+
|
|
66
|
+
const query = {
|
|
67
|
+
order: getOrderBy(field, order, primaryKey),
|
|
68
|
+
offset: String((page - 1) * perPage),
|
|
69
|
+
limit: String(perPage),
|
|
70
|
+
// append filters
|
|
71
|
+
...parsedFilter,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// add header that Content-Range is in returned header
|
|
75
|
+
const options = {
|
|
76
|
+
headers: new Headers({
|
|
77
|
+
Accept: 'application/json',
|
|
78
|
+
Prefer: 'count=exact',
|
|
79
|
+
}),
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const url = `${apiUrl}/${resource}?${new URLSearchParams(query)}`;
|
|
83
|
+
|
|
84
|
+
return httpClient(url, options).then(({ headers, json }) => {
|
|
85
|
+
if (!headers.has('content-range')) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`The Content-Range header is missing in the HTTP Response. The postgREST data provider expects
|
|
88
|
+
responses for lists of resources to contain this header with the total number of results to build
|
|
89
|
+
the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?`
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
data: json.map(obj => dataWithId(obj, primaryKey)),
|
|
94
|
+
total: parseInt(
|
|
95
|
+
headers.get('content-range').split('/').pop(),
|
|
96
|
+
10
|
|
97
|
+
),
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
|
|
102
|
+
getOne: (resource, params) => {
|
|
103
|
+
const id = params.id;
|
|
104
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
105
|
+
|
|
106
|
+
const query = getQuery(primaryKey, id, resource);
|
|
107
|
+
|
|
108
|
+
const url = `${apiUrl}/${resource}?${query}`;
|
|
109
|
+
|
|
110
|
+
return httpClient(url, {
|
|
111
|
+
headers: new Headers({
|
|
112
|
+
accept: 'application/vnd.pgrst.object+json',
|
|
113
|
+
}),
|
|
114
|
+
}).then(({ json }) => ({
|
|
115
|
+
data: dataWithId(json, primaryKey),
|
|
116
|
+
}));
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
getMany: (resource, params) => {
|
|
120
|
+
const ids = params.ids;
|
|
121
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
122
|
+
|
|
123
|
+
const query = getQuery(primaryKey, ids, resource);
|
|
124
|
+
|
|
125
|
+
const url = `${apiUrl}/${resource}?${query}`;
|
|
126
|
+
|
|
127
|
+
return httpClient(url).then(({ json }) => ({
|
|
128
|
+
data: json.map(data => dataWithId(data, primaryKey)),
|
|
129
|
+
}));
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
getManyReference: (resource, params) => {
|
|
133
|
+
const { page, perPage } = params.pagination;
|
|
134
|
+
const { field, order } = params.sort;
|
|
135
|
+
const parsedFilter = parseFilters(params.filter, defaultListOp);
|
|
136
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
137
|
+
|
|
138
|
+
const query = params.target
|
|
139
|
+
? {
|
|
140
|
+
[params.target]: `eq.${params.id}`,
|
|
141
|
+
order: getOrderBy(field, order, primaryKey),
|
|
142
|
+
offset: String((page - 1) * perPage),
|
|
143
|
+
limit: String(perPage),
|
|
144
|
+
...parsedFilter,
|
|
145
|
+
}
|
|
146
|
+
: {
|
|
147
|
+
order: getOrderBy(field, order, primaryKey),
|
|
148
|
+
offset: String((page - 1) * perPage),
|
|
149
|
+
limit: String(perPage),
|
|
150
|
+
...parsedFilter,
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// add header that Content-Range is in returned header
|
|
154
|
+
const options = {
|
|
155
|
+
headers: new Headers({
|
|
156
|
+
Accept: 'application/json',
|
|
157
|
+
Prefer: 'count=exact',
|
|
158
|
+
}),
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const url = `${apiUrl}/${resource}?${new URLSearchParams(query)}`;
|
|
162
|
+
|
|
163
|
+
return httpClient(url, options).then(({ headers, json }) => {
|
|
164
|
+
if (!headers.has('content-range')) {
|
|
165
|
+
throw new Error(
|
|
166
|
+
`The Content-Range header is missing in the HTTP Response. The postgREST data provider expects
|
|
167
|
+
responses for lists of resources to contain this header with the total number of results to build
|
|
168
|
+
the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
data: json.map(data => dataWithId(data, primaryKey)),
|
|
173
|
+
total: parseInt(
|
|
174
|
+
headers.get('content-range').split('/').pop(),
|
|
175
|
+
10
|
|
176
|
+
),
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
|
|
181
|
+
update: (resource, params) => {
|
|
182
|
+
const { id, data } = params;
|
|
183
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
184
|
+
|
|
185
|
+
const query = getQuery(primaryKey, id, resource);
|
|
186
|
+
|
|
187
|
+
const primaryKeyData = getKeyData(primaryKey, data);
|
|
188
|
+
|
|
189
|
+
const url = `${apiUrl}/${resource}?${query}`;
|
|
190
|
+
|
|
191
|
+
const body = JSON.stringify({
|
|
192
|
+
...data,
|
|
193
|
+
...primaryKeyData,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
return httpClient(url, {
|
|
197
|
+
method: 'PATCH',
|
|
198
|
+
headers: new Headers({
|
|
199
|
+
Accept: 'application/vnd.pgrst.object+json',
|
|
200
|
+
Prefer: 'return=representation',
|
|
201
|
+
'Content-Type': 'application/json',
|
|
202
|
+
}),
|
|
203
|
+
body,
|
|
204
|
+
}).then(({ json }) => ({ data: dataWithId(json, primaryKey) }));
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
updateMany: (resource, params) => {
|
|
208
|
+
const ids = params.ids;
|
|
209
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
210
|
+
|
|
211
|
+
const body = JSON.stringify(
|
|
212
|
+
params.ids.map(id => {
|
|
213
|
+
const { data } = params;
|
|
214
|
+
const primaryKeyParams = decodeId(id, primaryKey);
|
|
215
|
+
|
|
216
|
+
const primaryKeyData = {};
|
|
217
|
+
if (isCompoundKey(primaryKey)) {
|
|
218
|
+
primaryKey.forEach((key, index) => {
|
|
219
|
+
primaryKeyData[key] = primaryKeyParams[index];
|
|
220
|
+
});
|
|
221
|
+
} else {
|
|
222
|
+
primaryKeyData[primaryKey[0]] = primaryKeyParams[0];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
...data,
|
|
227
|
+
...primaryKeyData,
|
|
228
|
+
};
|
|
229
|
+
})
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const url = `${apiUrl}/${resource}`;
|
|
233
|
+
|
|
234
|
+
return httpClient(url, {
|
|
235
|
+
method: 'PATCH',
|
|
236
|
+
headers: new Headers({
|
|
237
|
+
Prefer: 'return=representation',
|
|
238
|
+
'Content-Type': 'application/json',
|
|
239
|
+
}),
|
|
240
|
+
body,
|
|
241
|
+
}).then(({ json }) => ({
|
|
242
|
+
data: json.map(data => encodeId(data, primaryKey)),
|
|
243
|
+
}));
|
|
244
|
+
},
|
|
245
|
+
|
|
246
|
+
create: (resource, params) => {
|
|
247
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
248
|
+
|
|
249
|
+
const url = `${apiUrl}/${resource}`;
|
|
250
|
+
|
|
251
|
+
return httpClient(url, {
|
|
252
|
+
method: 'POST',
|
|
253
|
+
headers: new Headers({
|
|
254
|
+
Accept: 'application/vnd.pgrst.object+json',
|
|
255
|
+
Prefer: 'return=representation',
|
|
256
|
+
'Content-Type': 'application/json',
|
|
257
|
+
}),
|
|
258
|
+
body: JSON.stringify(params.data),
|
|
259
|
+
}).then(({ json }) => ({
|
|
260
|
+
data: {
|
|
261
|
+
...params.data,
|
|
262
|
+
id: encodeId(json, primaryKey),
|
|
263
|
+
},
|
|
264
|
+
}));
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
delete: (resource, params) => {
|
|
268
|
+
const id = params.id;
|
|
269
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
270
|
+
|
|
271
|
+
const query = getQuery(primaryKey, id, resource);
|
|
272
|
+
|
|
273
|
+
const url = `${apiUrl}/${resource}?${query}`;
|
|
274
|
+
|
|
275
|
+
return httpClient(url, {
|
|
276
|
+
method: 'DELETE',
|
|
277
|
+
headers: new Headers({
|
|
278
|
+
Accept: 'application/vnd.pgrst.object+json',
|
|
279
|
+
Prefer: 'return=representation',
|
|
280
|
+
'Content-Type': 'application/json',
|
|
281
|
+
}),
|
|
282
|
+
}).then(({ json }) => ({ data: dataWithId(json, primaryKey) }));
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
deleteMany: (resource, params) => {
|
|
286
|
+
const ids = params.ids;
|
|
287
|
+
const primaryKey = getPrimaryKey(resource, primaryKeys);
|
|
288
|
+
|
|
289
|
+
const query = getQuery(primaryKey, ids, resource);
|
|
290
|
+
|
|
291
|
+
const url = `${apiUrl}/${resource}?${query}`;
|
|
292
|
+
|
|
293
|
+
return httpClient(url, {
|
|
294
|
+
method: 'DELETE',
|
|
295
|
+
headers: new Headers({
|
|
296
|
+
Prefer: 'return=representation',
|
|
297
|
+
'Content-Type': 'application/json',
|
|
298
|
+
}),
|
|
299
|
+
}).then(({ json }) => ({
|
|
300
|
+
data: json.map(data => encodeId(data, primaryKey)),
|
|
301
|
+
}));
|
|
302
|
+
},
|
|
303
|
+
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Identifier } from 'ra-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interface to clearly seperate the operator like < oder >
|
|
5
|
+
* from the name and the value of the filter
|
|
6
|
+
*/
|
|
7
|
+
interface PostgRestFilter {
|
|
8
|
+
name: string;
|
|
9
|
+
operator?: PostgRestOperator;
|
|
10
|
+
value: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* List of all possible operators in PostgRest
|
|
15
|
+
* (https://postgrest.org/en/stable/api.html#operators)
|
|
16
|
+
*/
|
|
17
|
+
const postgrestOperators = [
|
|
18
|
+
'eq',
|
|
19
|
+
'gt',
|
|
20
|
+
'gte',
|
|
21
|
+
'lt',
|
|
22
|
+
'lte',
|
|
23
|
+
'neq',
|
|
24
|
+
'like',
|
|
25
|
+
'ilike',
|
|
26
|
+
'match',
|
|
27
|
+
'imatch',
|
|
28
|
+
'in',
|
|
29
|
+
'is',
|
|
30
|
+
'fts',
|
|
31
|
+
'plfts',
|
|
32
|
+
'phfts',
|
|
33
|
+
'wfts',
|
|
34
|
+
'cs',
|
|
35
|
+
'cd',
|
|
36
|
+
'ov',
|
|
37
|
+
'sl',
|
|
38
|
+
'sr',
|
|
39
|
+
'nxr',
|
|
40
|
+
'nxl',
|
|
41
|
+
'adj',
|
|
42
|
+
'not',
|
|
43
|
+
'or',
|
|
44
|
+
'and',
|
|
45
|
+
] as const;
|
|
46
|
+
|
|
47
|
+
export type PostgRestOperator = typeof postgrestOperators[number];
|
|
48
|
+
|
|
49
|
+
export const parseFilters = (
|
|
50
|
+
filter: Record<string, any>,
|
|
51
|
+
defaultListOp: PostgRestOperator
|
|
52
|
+
) => {
|
|
53
|
+
let result = {};
|
|
54
|
+
Object.keys(filter).forEach(function (key) {
|
|
55
|
+
// key: the name of the object key
|
|
56
|
+
|
|
57
|
+
const splitKey = key.split('@');
|
|
58
|
+
const operation: PostgRestOperator =
|
|
59
|
+
splitKey.length == 2
|
|
60
|
+
? (splitKey[1] as PostgRestOperator)
|
|
61
|
+
: defaultListOp;
|
|
62
|
+
|
|
63
|
+
let values: Array<string>;
|
|
64
|
+
if (['like', 'ilike'].includes(operation)) {
|
|
65
|
+
// we split the search term in words
|
|
66
|
+
values = filter[key].trim().split(/\s+/);
|
|
67
|
+
} else {
|
|
68
|
+
values = [filter[key]];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
values.forEach(value => {
|
|
72
|
+
// if operator is intentionally blank, rpc syntax
|
|
73
|
+
let op = operation.includes('like')
|
|
74
|
+
? `${operation}.*${value}*`
|
|
75
|
+
: operation.length == 0
|
|
76
|
+
? `${value}`
|
|
77
|
+
: `${operation}.${value}`;
|
|
78
|
+
|
|
79
|
+
if (result[splitKey[0]] === undefined) {
|
|
80
|
+
// first operator for the key, we add it to the dict
|
|
81
|
+
result[splitKey[0]] = op;
|
|
82
|
+
} else {
|
|
83
|
+
if (!Array.isArray(result[splitKey[0]])) {
|
|
84
|
+
// second operator, we transform to an array
|
|
85
|
+
result[splitKey[0]] = [result[splitKey[0]], op];
|
|
86
|
+
} else {
|
|
87
|
+
// third and subsequent, we add to array
|
|
88
|
+
result[splitKey[0]].push(op);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return result;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// compound keys capability
|
|
98
|
+
export type PrimaryKey = Array<string>;
|
|
99
|
+
|
|
100
|
+
export const getPrimaryKey = (
|
|
101
|
+
resource: string,
|
|
102
|
+
primaryKeys: Map<string, PrimaryKey>
|
|
103
|
+
) => {
|
|
104
|
+
return primaryKeys.get(resource) || ['id'];
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const decodeId = (
|
|
108
|
+
id: Identifier,
|
|
109
|
+
primaryKey: PrimaryKey
|
|
110
|
+
): string[] | number[] => {
|
|
111
|
+
if (isCompoundKey(primaryKey)) {
|
|
112
|
+
return JSON.parse(id.toString());
|
|
113
|
+
} else {
|
|
114
|
+
return [id.toString()];
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const encodeId = (data: any, primaryKey: PrimaryKey): Identifier => {
|
|
119
|
+
if (isCompoundKey(primaryKey)) {
|
|
120
|
+
return JSON.stringify(primaryKey.map(key => data[key]));
|
|
121
|
+
} else {
|
|
122
|
+
return data[primaryKey[0]];
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export const dataWithId = (data: any, primaryKey: PrimaryKey) => {
|
|
127
|
+
if (JSON.stringify(primaryKey) === JSON.stringify(['id'])) {
|
|
128
|
+
return data;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return Object.assign(data, {
|
|
132
|
+
id: encodeId(data, primaryKey),
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const isCompoundKey = (primaryKey: PrimaryKey): Boolean => {
|
|
137
|
+
return primaryKey.length > 1;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export const getQuery = (
|
|
141
|
+
primaryKey: PrimaryKey,
|
|
142
|
+
ids: Identifier | Array<Identifier>,
|
|
143
|
+
resource: string
|
|
144
|
+
): string => {
|
|
145
|
+
if (Array.isArray(ids) && ids.length > 1) {
|
|
146
|
+
// no standardized query with multiple ids possible for rpc endpoints which are api-exposed database functions
|
|
147
|
+
if (resource.startsWith('rpc/')) {
|
|
148
|
+
console.error(
|
|
149
|
+
"PostgREST's rpc endpoints are not intended to be handled as views. Therefore, no query generation for multiple key values implemented!"
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (isCompoundKey(primaryKey)) {
|
|
156
|
+
// TODO: Should be URL encoded
|
|
157
|
+
return `or=(${ids.map(id => {
|
|
158
|
+
const primaryKeyParams = decodeId(id, primaryKey);
|
|
159
|
+
return `and(${primaryKey
|
|
160
|
+
.map((key, i) => `${key}.eq.${primaryKeyParams[i]}`)
|
|
161
|
+
.join(',')})`;
|
|
162
|
+
})})`;
|
|
163
|
+
} else {
|
|
164
|
+
return new URLSearchParams({
|
|
165
|
+
[primaryKey[0]]: `in.(${ids.join(',')})`,
|
|
166
|
+
}).toString();
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
// if ids is one Identifier
|
|
170
|
+
const id: Identifier = ids.toString();
|
|
171
|
+
const primaryKeyParams = decodeId(id, primaryKey);
|
|
172
|
+
|
|
173
|
+
if (isCompoundKey(primaryKey)) {
|
|
174
|
+
if (resource.startsWith('rpc/'))
|
|
175
|
+
// TODO: Should be URL encoded
|
|
176
|
+
return `${primaryKey
|
|
177
|
+
.map(
|
|
178
|
+
(key: string, i: any) => `${key}=${primaryKeyParams[i]}`
|
|
179
|
+
)
|
|
180
|
+
.join('&')}`;
|
|
181
|
+
// TODO: Should be URL encoded
|
|
182
|
+
else
|
|
183
|
+
return `and=(${primaryKey
|
|
184
|
+
.map(
|
|
185
|
+
(key: string, i: any) =>
|
|
186
|
+
`${key}.eq.${primaryKeyParams[i]}`
|
|
187
|
+
)
|
|
188
|
+
.join(',')})`;
|
|
189
|
+
} else {
|
|
190
|
+
return new URLSearchParams([
|
|
191
|
+
[primaryKey[0], `eq.${id}`],
|
|
192
|
+
]).toString();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
export const getKeyData = (primaryKey: PrimaryKey, data: object): object => {
|
|
198
|
+
if (isCompoundKey(primaryKey)) {
|
|
199
|
+
return primaryKey.reduce(
|
|
200
|
+
(keyData, key) => ({
|
|
201
|
+
...keyData,
|
|
202
|
+
[key]: data[key],
|
|
203
|
+
}),
|
|
204
|
+
{}
|
|
205
|
+
);
|
|
206
|
+
} else {
|
|
207
|
+
return { [primaryKey[0]]: data[primaryKey[0]] };
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
export const getOrderBy = (
|
|
212
|
+
field: string,
|
|
213
|
+
order: string,
|
|
214
|
+
primaryKey: PrimaryKey
|
|
215
|
+
) => {
|
|
216
|
+
if (field == 'id') {
|
|
217
|
+
return primaryKey.map(key => `${key}.${order.toLowerCase()}`).join(',');
|
|
218
|
+
} else {
|
|
219
|
+
return `${field}.${order.toLowerCase()}`;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
declare const _default: {
|
|
2
|
-
login: ({ username, password }: {
|
|
3
|
-
username: any;
|
|
4
|
-
password: any;
|
|
5
|
-
}) => Promise<void>;
|
|
6
|
-
logout: (params: any) => Promise<void>;
|
|
7
|
-
checkAuth: (params: any) => Promise<void>;
|
|
8
|
-
checkError: (error: any) => Promise<void>;
|
|
9
|
-
getPermissions: (params: any) => Promise<any>;
|
|
10
|
-
};
|
|
11
|
-
export default _default;
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
login: function (_a) {
|
|
3
|
-
var username = _a.username, password = _a.password;
|
|
4
|
-
var request = new Request('rest/rpc/login', {
|
|
5
|
-
method: 'POST',
|
|
6
|
-
body: JSON.stringify({ email: username, password: password }),
|
|
7
|
-
headers: new Headers({
|
|
8
|
-
'Content-Type': 'application/json',
|
|
9
|
-
'Accept': 'application/vnd.pgrst.object+json',
|
|
10
|
-
'Prefer': 'return=representation'
|
|
11
|
-
}),
|
|
12
|
-
});
|
|
13
|
-
return fetch(request)
|
|
14
|
-
.then(function (response) {
|
|
15
|
-
if (response.status < 200 || response.status >= 300) {
|
|
16
|
-
throw new Error(response.statusText);
|
|
17
|
-
}
|
|
18
|
-
return response.json();
|
|
19
|
-
})
|
|
20
|
-
.then(function (data) {
|
|
21
|
-
localStorage.setItem('me', JSON.stringify(data));
|
|
22
|
-
});
|
|
23
|
-
},
|
|
24
|
-
logout: function (params) {
|
|
25
|
-
var request = new Request('rest/rpc/logout', {
|
|
26
|
-
method: 'POST',
|
|
27
|
-
headers: new Headers({
|
|
28
|
-
'Content-Type': 'application/json',
|
|
29
|
-
'Accept': 'application/vnd.pgrst.object+json',
|
|
30
|
-
'Prefer': 'return=representation'
|
|
31
|
-
}),
|
|
32
|
-
});
|
|
33
|
-
return fetch(request)
|
|
34
|
-
.then(function (response) {
|
|
35
|
-
if (response.status < 200 || response.status >= 300) {
|
|
36
|
-
throw new Error(response.statusText);
|
|
37
|
-
}
|
|
38
|
-
return response.json();
|
|
39
|
-
})
|
|
40
|
-
.then(function (data) {
|
|
41
|
-
localStorage.removeItem('me');
|
|
42
|
-
});
|
|
43
|
-
},
|
|
44
|
-
checkAuth: function (params) { return (localStorage.getItem('me') ? Promise.resolve() : Promise.reject()); },
|
|
45
|
-
checkError: function (error) {
|
|
46
|
-
var status = error.status;
|
|
47
|
-
if (status === 401 || status === 403) {
|
|
48
|
-
localStorage.removeItem('me');
|
|
49
|
-
return Promise.reject();
|
|
50
|
-
}
|
|
51
|
-
return Promise.resolve();
|
|
52
|
-
},
|
|
53
|
-
getPermissions: function (params) {
|
|
54
|
-
var _a;
|
|
55
|
-
var role = (_a = JSON.parse(localStorage.getItem('me'))) === null || _a === void 0 ? void 0 : _a.role;
|
|
56
|
-
return role ? Promise.resolve(role) : Promise.reject();
|
|
57
|
-
},
|
|
58
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { fetchUtils, DataProvider } from 'ra-core';
|
|
2
|
-
declare type PrimaryKey = Array<string>;
|
|
3
|
-
declare const _default: (apiUrl: any, httpClient?: (url: any, options?: fetchUtils.Options) => Promise<{
|
|
4
|
-
status: number;
|
|
5
|
-
headers: Headers;
|
|
6
|
-
body: string;
|
|
7
|
-
json: any;
|
|
8
|
-
}>, defaultListOp?: string, primaryKeys?: Map<string, PrimaryKey>) => DataProvider;
|
|
9
|
-
export default _default;
|