@raphiniert/ra-data-postgrest 2.0.0-alpha.1 → 2.0.0-alpha.3

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/esm/index.js CHANGED
@@ -9,234 +9,241 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
- import { fetchUtils, } from 'ra-core';
13
12
  import { getPrimaryKey, parseFilters, getOrderBy, dataWithId, getQuery, getKeyData, encodeId, decodeId, isCompoundKey, } from './urlBuilder';
14
13
  import qs from 'qs';
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 '@raphiniert/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
- */
50
- var defaultPrimaryKeys = new Map();
51
- export default (function (apiUrl, httpClient, defaultListOp, primaryKeys) {
52
- if (httpClient === void 0) { httpClient = fetchUtils.fetchJson; }
53
- if (defaultListOp === void 0) { defaultListOp = 'eq'; }
54
- if (primaryKeys === void 0) { primaryKeys = defaultPrimaryKeys; }
55
- return ({
56
- getList: function (resource, params) {
57
- var _a;
58
- if (params === void 0) { params = {}; }
59
- var primaryKey = getPrimaryKey(resource, primaryKeys);
60
- var _b = params.pagination, page = _b.page, perPage = _b.perPage;
61
- var _c = params.sort || {}, field = _c.field, order = _c.order;
62
- var _d = parseFilters(params, defaultListOp), filter = _d.filter, select = _d.select;
63
- var query = __assign({ offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
64
- if (field) {
65
- query.order = getOrderBy(field, order, primaryKey);
66
- }
67
- if (select) {
68
- query.select = select;
14
+ export var defaultPrimaryKeys = new Map();
15
+ export var defaultSchema = function () { return ''; };
16
+ var useCustomSchema = function (schema, metaSchema, method) {
17
+ var _a;
18
+ var funcHeaderSchema = schema;
19
+ if (metaSchema !== undefined) {
20
+ funcHeaderSchema = function () { return metaSchema; };
21
+ }
22
+ if (funcHeaderSchema().length > 0) {
23
+ var schemaHeader = '';
24
+ if (['GET', 'HEAD'].includes(method)) {
25
+ schemaHeader = 'Accept-Profile';
26
+ }
27
+ else if (['POST', 'PATCH', 'PUT', 'DELETE'].includes(method)) {
28
+ schemaHeader = 'Content-Profile';
29
+ }
30
+ else
31
+ return {};
32
+ return _a = {}, _a[schemaHeader] = funcHeaderSchema(), _a;
33
+ }
34
+ else
35
+ return {};
36
+ };
37
+ export default (function (config) { return ({
38
+ getList: function (resource, params) {
39
+ var _a, _b;
40
+ if (params === void 0) { params = {}; }
41
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
42
+ var _c = params.pagination, page = _c.page, perPage = _c.perPage;
43
+ var _d = params.sort || {}, field = _d.field, order = _d.order;
44
+ var _e = parseFilters(params, config.defaultListOp), filter = _e.filter, select = _e.select;
45
+ var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
46
+ var query = __assign({ offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
47
+ if (field) {
48
+ query.order = getOrderBy(field, order, primaryKey);
49
+ }
50
+ if (select) {
51
+ query.select = select;
52
+ }
53
+ // add header that Content-Range is in returned header
54
+ var options = {
55
+ headers: new Headers(__assign(__assign({ Accept: 'application/json', Prefer: 'count=exact' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'GET'))),
56
+ };
57
+ var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
58
+ return config.httpClient(url, options).then(function (_a) {
59
+ var headers = _a.headers, json = _a.json;
60
+ if (!headers.has('content-range')) {
61
+ throw new Error("The Content-Range header is missing in the HTTP Response. The postgREST data provider expects \n responses for lists of resources to contain this header with the total number of results to build \n the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?");
69
62
  }
70
- // add header that Content-Range is in returned header
71
- var options = {
72
- headers: new Headers(__assign({ Accept: 'application/json', Prefer: 'count=exact' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
63
+ return {
64
+ data: json.map(function (obj) { return dataWithId(obj, primaryKey); }),
65
+ total: parseInt(headers.get('content-range').split('/').pop(), 10),
73
66
  };
74
- var url = "".concat(apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
75
- return httpClient(url, options).then(function (_a) {
76
- var headers = _a.headers, json = _a.json;
77
- if (!headers.has('content-range')) {
78
- throw new Error("The Content-Range header is missing in the HTTP Response. The postgREST data provider expects \n responses for lists of resources to contain this header with the total number of results to build \n the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?");
79
- }
80
- return {
81
- data: json.map(function (obj) { return dataWithId(obj, primaryKey); }),
82
- total: parseInt(headers.get('content-range').split('/').pop(), 10),
83
- };
84
- });
85
- },
86
- getOne: function (resource, params) {
87
- var _a;
88
- if (params === void 0) { params = {}; }
89
- var id = params.id, meta = params.meta;
90
- var primaryKey = getPrimaryKey(resource, primaryKeys);
91
- var query = getQuery(primaryKey, id, resource, meta);
92
- var url = "".concat(apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
93
- return httpClient(url, {
94
- headers: new Headers(__assign({ accept: 'application/vnd.pgrst.object+json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
95
- }).then(function (_a) {
96
- var json = _a.json;
97
- return ({
98
- data: dataWithId(json, primaryKey),
99
- });
67
+ });
68
+ },
69
+ getOne: function (resource, params) {
70
+ var _a, _b;
71
+ if (params === void 0) { params = {}; }
72
+ var id = params.id, meta = params.meta;
73
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
74
+ var query = getQuery(primaryKey, id, resource, meta);
75
+ var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
76
+ var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
77
+ return config
78
+ .httpClient(url, {
79
+ headers: new Headers(__assign(__assign({ accept: 'application/vnd.pgrst.object+json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'GET'))),
80
+ })
81
+ .then(function (_a) {
82
+ var json = _a.json;
83
+ return ({
84
+ data: dataWithId(json, primaryKey),
100
85
  });
101
- },
102
- getMany: function (resource, params) {
103
- if (params === void 0) { params = {}; }
104
- var ids = params.ids;
105
- var primaryKey = getPrimaryKey(resource, primaryKeys);
106
- var query = getQuery(primaryKey, ids, resource, params.meta);
107
- var url = "".concat(apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
108
- return httpClient(url).then(function (_a) {
109
- var json = _a.json;
110
- return ({
111
- data: json.map(function (data) { return dataWithId(data, primaryKey); }),
112
- });
86
+ });
87
+ },
88
+ getMany: function (resource, params) {
89
+ var _a;
90
+ if (params === void 0) { params = {}; }
91
+ var ids = params.ids;
92
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
93
+ var query = getQuery(primaryKey, ids, resource, params.meta);
94
+ var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
95
+ var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
96
+ return config
97
+ .httpClient(url, {
98
+ headers: new Headers(__assign({}, useCustomSchema(config.schema, metaSchema, 'GET'))),
99
+ })
100
+ .then(function (_a) {
101
+ var json = _a.json;
102
+ return ({
103
+ data: json.map(function (data) { return dataWithId(data, primaryKey); }),
113
104
  });
114
- },
115
- getManyReference: function (resource, params) {
116
- var _a;
117
- var _b;
118
- if (params === void 0) { params = {}; }
119
- var _c = params.pagination, page = _c.page, perPage = _c.perPage;
120
- var _d = params.sort, field = _d.field, order = _d.order;
121
- var _e = parseFilters(params, defaultListOp), filter = _e.filter, select = _e.select;
122
- var primaryKey = getPrimaryKey(resource, primaryKeys);
123
- var query = params.target
124
- ? __assign((_a = {}, _a[params.target] = "eq.".concat(params.id), _a.order = getOrderBy(field, order, primaryKey), _a.offset = String((page - 1) * perPage), _a.limit = String(perPage), _a), filter) : __assign({ order: getOrderBy(field, order, primaryKey), offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
125
- if (select) {
126
- query.select = select;
105
+ });
106
+ },
107
+ getManyReference: function (resource, params) {
108
+ var _a;
109
+ var _b, _c;
110
+ if (params === void 0) { params = {}; }
111
+ var _d = params.pagination, page = _d.page, perPage = _d.perPage;
112
+ var _e = params.sort, field = _e.field, order = _e.order;
113
+ var _f = parseFilters(params, config.defaultListOp), filter = _f.filter, select = _f.select;
114
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
115
+ var metaSchema = (_b = params.meta) === null || _b === void 0 ? void 0 : _b.schema;
116
+ var query = params.target
117
+ ? __assign((_a = {}, _a[params.target] = "eq.".concat(params.id), _a.order = getOrderBy(field, order, primaryKey), _a.offset = String((page - 1) * perPage), _a.limit = String(perPage), _a), filter) : __assign({ order: getOrderBy(field, order, primaryKey), offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
118
+ if (select) {
119
+ query.select = select;
120
+ }
121
+ // add header that Content-Range is in returned header
122
+ var options = {
123
+ headers: new Headers(__assign(__assign({ Accept: 'application/json', Prefer: 'count=exact' }, (((_c = params.meta) === null || _c === void 0 ? void 0 : _c.headers) || {})), useCustomSchema(config.schema, metaSchema, 'GET'))),
124
+ };
125
+ var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
126
+ return config.httpClient(url, options).then(function (_a) {
127
+ var headers = _a.headers, json = _a.json;
128
+ if (!headers.has('content-range')) {
129
+ throw new Error("The Content-Range header is missing in the HTTP Response. The postgREST data provider expects \n responses for lists of resources to contain this header with the total number of results to build \n the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?");
127
130
  }
128
- // add header that Content-Range is in returned header
129
- var options = {
130
- headers: new Headers(__assign({ Accept: 'application/json', Prefer: 'count=exact' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {}))),
131
+ return {
132
+ data: json.map(function (data) { return dataWithId(data, primaryKey); }),
133
+ total: parseInt(headers.get('content-range').split('/').pop(), 10),
131
134
  };
132
- var url = "".concat(apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
133
- return httpClient(url, options).then(function (_a) {
134
- var headers = _a.headers, json = _a.json;
135
- if (!headers.has('content-range')) {
136
- throw new Error("The Content-Range header is missing in the HTTP Response. The postgREST data provider expects \n responses for lists of resources to contain this header with the total number of results to build \n the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?");
137
- }
138
- return {
139
- data: json.map(function (data) { return dataWithId(data, primaryKey); }),
140
- total: parseInt(headers.get('content-range').split('/').pop(), 10),
141
- };
142
- });
143
- },
144
- update: function (resource, params) {
145
- var _a;
146
- if (params === void 0) { params = {}; }
147
- var id = params.id, data = params.data, meta = params.meta;
148
- var primaryKey = getPrimaryKey(resource, primaryKeys);
149
- var query = getQuery(primaryKey, id, resource, meta);
150
- var primaryKeyData = getKeyData(primaryKey, data);
151
- var url = "".concat(apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
152
- var body = JSON.stringify(__assign(__assign({}, data), primaryKeyData));
153
- return httpClient(url, {
154
- method: 'PATCH',
155
- headers: new Headers(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
156
- body: body,
157
- }).then(function (_a) {
158
- var json = _a.json;
159
- return ({ data: dataWithId(json, primaryKey) });
160
- });
161
- },
162
- updateMany: function (resource, params) {
163
- var _a;
164
- if (params === void 0) { params = {}; }
165
- var ids = params.ids;
166
- var primaryKey = getPrimaryKey(resource, primaryKeys);
167
- var body = JSON.stringify(params.ids.map(function (id) {
168
- var data = params.data;
169
- var primaryKeyParams = decodeId(id, primaryKey);
170
- var primaryKeyData = {};
171
- if (isCompoundKey(primaryKey)) {
172
- primaryKey.forEach(function (key, index) {
173
- primaryKeyData[key] = primaryKeyParams[index];
174
- });
175
- }
176
- else {
177
- primaryKeyData[primaryKey[0]] = primaryKeyParams[0];
178
- }
179
- return __assign(__assign({}, data), primaryKeyData);
180
- }));
181
- var url = "".concat(apiUrl, "/").concat(resource);
182
- return httpClient(url, {
183
- method: 'PATCH',
184
- headers: new Headers(__assign({ Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
185
- body: body,
186
- }).then(function (_a) {
187
- var json = _a.json;
188
- return ({
189
- data: json.map(function (data) { return encodeId(data, primaryKey); }),
190
- });
191
- });
192
- },
193
- create: function (resource, params) {
194
- var _a;
195
- if (params === void 0) { params = {}; }
196
- var primaryKey = getPrimaryKey(resource, primaryKeys);
197
- var url = "".concat(apiUrl, "/").concat(resource);
198
- return httpClient(url, {
199
- method: 'POST',
200
- headers: new Headers(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
201
- body: JSON.stringify(params.data),
202
- }).then(function (_a) {
203
- var json = _a.json;
204
- return ({
205
- data: __assign(__assign({}, json), { id: encodeId(json, primaryKey) }),
135
+ });
136
+ },
137
+ update: function (resource, params) {
138
+ var _a, _b;
139
+ if (params === void 0) { params = {}; }
140
+ var id = params.id, data = params.data, meta = params.meta;
141
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
142
+ var query = getQuery(primaryKey, id, resource, meta);
143
+ var primaryKeyData = getKeyData(primaryKey, data);
144
+ var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
145
+ var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
146
+ var body = JSON.stringify(__assign(__assign({}, data), primaryKeyData));
147
+ return config
148
+ .httpClient(url, {
149
+ method: 'PATCH',
150
+ headers: new Headers(__assign(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'PATCH'))),
151
+ body: body,
152
+ })
153
+ .then(function (_a) {
154
+ var json = _a.json;
155
+ return ({ data: dataWithId(json, primaryKey) });
156
+ });
157
+ },
158
+ updateMany: function (resource, params) {
159
+ var _a, _b;
160
+ if (params === void 0) { params = {}; }
161
+ var ids = params.ids;
162
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
163
+ var body = JSON.stringify(params.ids.map(function (id) {
164
+ var data = params.data;
165
+ var primaryKeyParams = decodeId(id, primaryKey);
166
+ var primaryKeyData = {};
167
+ if (isCompoundKey(primaryKey)) {
168
+ primaryKey.forEach(function (key, index) {
169
+ primaryKeyData[key] = primaryKeyParams[index];
206
170
  });
171
+ }
172
+ else {
173
+ primaryKeyData[primaryKey[0]] = primaryKeyParams[0];
174
+ }
175
+ return __assign(__assign({}, data), primaryKeyData);
176
+ }));
177
+ var url = "".concat(config.apiUrl, "/").concat(resource);
178
+ var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
179
+ return config
180
+ .httpClient(url, {
181
+ method: 'PATCH',
182
+ headers: new Headers(__assign(__assign({ Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'PATCH'))),
183
+ body: body,
184
+ })
185
+ .then(function (_a) {
186
+ var json = _a.json;
187
+ return ({
188
+ data: json.map(function (data) { return encodeId(data, primaryKey); }),
207
189
  });
208
- },
209
- delete: function (resource, params) {
210
- var _a;
211
- if (params === void 0) { params = {}; }
212
- var id = params.id, meta = params.meta;
213
- var primaryKey = getPrimaryKey(resource, primaryKeys);
214
- var query = getQuery(primaryKey, id, resource, meta);
215
- var url = "".concat(apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
216
- return httpClient(url, {
217
- method: 'DELETE',
218
- headers: new Headers(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
219
- }).then(function (_a) {
220
- var json = _a.json;
221
- return ({ data: dataWithId(json, primaryKey) });
190
+ });
191
+ },
192
+ create: function (resource, params) {
193
+ var _a, _b;
194
+ if (params === void 0) { params = {}; }
195
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
196
+ var url = "".concat(config.apiUrl, "/").concat(resource);
197
+ var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
198
+ return config
199
+ .httpClient(url, {
200
+ method: 'POST',
201
+ headers: new Headers(__assign(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'POST'))),
202
+ body: JSON.stringify(params.data),
203
+ })
204
+ .then(function (_a) {
205
+ var json = _a.json;
206
+ return ({
207
+ data: __assign(__assign({}, json), { id: encodeId(json, primaryKey) }),
222
208
  });
223
- },
224
- deleteMany: function (resource, params) {
225
- var _a;
226
- if (params === void 0) { params = {}; }
227
- var ids = params.ids, meta = params.meta;
228
- var primaryKey = getPrimaryKey(resource, primaryKeys);
229
- var query = getQuery(primaryKey, ids, resource, meta);
230
- var url = "".concat(apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
231
- return httpClient(url, {
232
- method: 'DELETE',
233
- headers: new Headers(__assign({ Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_a = params.meta) === null || _a === void 0 ? void 0 : _a.headers) || {}))),
234
- }).then(function (_a) {
235
- var json = _a.json;
236
- return ({
237
- data: json.map(function (data) { return encodeId(data, primaryKey); }),
238
- });
209
+ });
210
+ },
211
+ delete: function (resource, params) {
212
+ var _a, _b;
213
+ if (params === void 0) { params = {}; }
214
+ var id = params.id, meta = params.meta;
215
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
216
+ var query = getQuery(primaryKey, id, resource, meta);
217
+ var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
218
+ var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
219
+ return config
220
+ .httpClient(url, {
221
+ method: 'DELETE',
222
+ headers: new Headers(__assign(__assign({ Accept: 'application/vnd.pgrst.object+json', Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'DELETE'))),
223
+ })
224
+ .then(function (_a) {
225
+ var json = _a.json;
226
+ return ({ data: dataWithId(json, primaryKey) });
227
+ });
228
+ },
229
+ deleteMany: function (resource, params) {
230
+ var _a, _b;
231
+ if (params === void 0) { params = {}; }
232
+ var ids = params.ids, meta = params.meta;
233
+ var primaryKey = getPrimaryKey(resource, config.primaryKeys);
234
+ var query = getQuery(primaryKey, ids, resource, meta);
235
+ var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
236
+ var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
237
+ return config
238
+ .httpClient(url, {
239
+ method: 'DELETE',
240
+ headers: new Headers(__assign(__assign({ Prefer: 'return=representation', 'Content-Type': 'application/json' }, (((_b = params.meta) === null || _b === void 0 ? void 0 : _b.headers) || {})), useCustomSchema(config.schema, metaSchema, 'DELETE'))),
241
+ })
242
+ .then(function (_a) {
243
+ var json = _a.json;
244
+ return ({
245
+ data: json.map(function (data) { return encodeId(data, primaryKey); }),
239
246
  });
240
- },
241
- });
242
- });
247
+ });
248
+ },
249
+ }); });
package/esm/urlBuilder.js CHANGED
@@ -57,19 +57,48 @@ export var parseFilters = function (params, defaultListOp) {
57
57
  // we split the search term in words
58
58
  values = filter[key].trim().split(/\s+/);
59
59
  }
60
+ // CASE: Logical operator
61
+ else if (['or', 'and'].includes(operation)) {
62
+ // we extract each value entries and make it dot separated string.
63
+ var subFilter = parseFilters({ filter: filter[key] }, defaultListOp).filter;
64
+ // something like { "age@lt": 18 } and { "age@gt": 21 }
65
+ var filterExpressions_1 = [];
66
+ Object.entries(subFilter).forEach(function (_a) {
67
+ var op = _a[0], val = _a[1];
68
+ if (Array.isArray(val))
69
+ filterExpressions_1.push.apply(filterExpressions_1, val.map(function (v) { return [op, v].join('.'); }));
70
+ else
71
+ filterExpressions_1.push([op, val].join('.'));
72
+ });
73
+ // finally we flatten all as single string and enclose with bracket.
74
+ values = ["(".concat(filterExpressions_1.join(','), ")")];
75
+ }
60
76
  else {
61
77
  values = [filter[key]];
62
78
  }
63
79
  values.forEach(function (value) {
64
- // if operator is intentionally blank, rpc syntax
65
- var op = operation.includes('like')
66
- ? "".concat(operation, ".*").concat(value, "*")
67
- : operation.length == 0
68
- ? "".concat(value)
69
- : "".concat(operation, ".").concat(value);
80
+ var op = (function () {
81
+ // if operator is intentionally blank, rpc syntax
82
+ if (operation.length === 0)
83
+ return "".concat(value);
84
+ if (operation.includes('like'))
85
+ return "".concat(operation, ".*").concat(value, "*");
86
+ if (['and', 'or'].includes(operation))
87
+ return "".concat(value);
88
+ if (['cs', 'cd'].includes(operation))
89
+ return "".concat(operation, ".{").concat(value, "}");
90
+ return "".concat(operation, ".").concat(value);
91
+ })();
92
+ // If resulting filter haven't contains the fieldname, then add it.
70
93
  if (result.filter[splitKey[0]] === undefined) {
71
- // first operator for the key, we add it to the dict
72
- result.filter[splitKey[0]] = op;
94
+ // first operator for the key,
95
+ if (['and', 'or'].includes(operation)) {
96
+ result.filter[operation] = op;
97
+ }
98
+ else {
99
+ // we add it to the dict
100
+ result.filter[splitKey[0]] = op;
101
+ }
73
102
  }
74
103
  else {
75
104
  if (!Array.isArray(result[splitKey[0]])) {
package/lib/index.d.ts CHANGED
@@ -1,9 +1,60 @@
1
- import { fetchUtils, DataProvider } from 'ra-core';
1
+ import { DataProvider } from 'ra-core';
2
2
  import { PrimaryKey, PostgRestOperator } from './urlBuilder';
3
- declare const _default: (apiUrl: string, httpClient?: (url: any, options?: fetchUtils.Options) => Promise<{
4
- status: number;
5
- headers: Headers;
6
- body: string;
7
- json: any;
8
- }>, defaultListOp?: PostgRestOperator, primaryKeys?: Map<string, PrimaryKey>) => DataProvider;
3
+ /**
4
+ * Maps react-admin queries to a postgrest REST API
5
+ *
6
+ * This REST dialect uses postgrest syntax
7
+ *
8
+ * @see https://postgrest.org/en/stable/api.html#embedded-filters
9
+ *
10
+ * @example
11
+ *
12
+ * getList => GET http://my.api.url/posts?order=title.asc&offset=0&limit=24&filterField=eq.value
13
+ * getOne => GET http://my.api.url/posts?id=eq.123
14
+ * getMany => GET http://my.api.url/posts?id=in.(123,456,789)
15
+ * getManyReference => GET http://my.api.url/posts?author_id=eq.345
16
+ * create => POST http://my.api.url/posts
17
+ * update => PATCH http://my.api.url/posts?id=eq.123
18
+ * updateMany => PATCH http://my.api.url/posts?id=in.(123,456,789)
19
+ * delete => DELETE http://my.api.url/posts?id=eq.123
20
+ * deleteMany => DELETE http://my.api.url/posts?id=in.(123,456,789)
21
+ *
22
+ * @example
23
+ *
24
+ * import * as React from 'react';
25
+ * import { Admin, Resource, fetchUtils } from 'react-admin';
26
+ * import postgrestRestProvider,
27
+ * { IDataProviderConfig,
28
+ * defaultPrimaryKeys,
29
+ * defaultSchema } from '@raphiniert/ra-data-postgrest';
30
+ *
31
+ * import { PostList } from './posts';
32
+ *
33
+ * const config: IDataProviderConfig = {
34
+ * apiUrl: 'http://path.to.my.api/',
35
+ * httpClient: fetchUtils.fetchJson,
36
+ * defaultListOp: 'eq',
37
+ * primaryKeys: defaultPrimaryKeys,
38
+ * schema: defaultSchema
39
+ * }
40
+ *
41
+ * const App = () => (
42
+ * <Admin dataProvider={postgrestRestProvider(config)}>
43
+ * <Resource name="posts" list={PostList} />
44
+ * </Admin>
45
+ * );
46
+ *
47
+ * export default App;
48
+ */
49
+ export type MetaOption = string | undefined;
50
+ export declare const defaultPrimaryKeys: Map<string, PrimaryKey>;
51
+ export declare const defaultSchema: () => string;
52
+ export interface IDataProviderConfig {
53
+ apiUrl: string;
54
+ httpClient: (string: any, Options: any) => Promise<any>;
55
+ defaultListOp: PostgRestOperator;
56
+ primaryKeys: Map<string, PrimaryKey>;
57
+ schema: () => string;
58
+ }
59
+ declare const _default: (config: IDataProviderConfig) => DataProvider;
9
60
  export default _default;