Skip to content

Commit

Permalink
add dist files to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslantalpa committed Oct 20, 2023
1 parent 9a1a04b commit 8380cbc
Show file tree
Hide file tree
Showing 8 changed files with 1,238 additions and 0 deletions.
60 changes: 60 additions & 0 deletions esm/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { DataProvider } from 'ra-core';
import { PrimaryKey, PostgRestOperator } from './urlBuilder';
/**
* Maps react-admin queries to a postgrest REST API
*
* This REST dialect uses postgrest syntax
*
* @see https://postgrest.org/en/stable/api.html#embedded-filters
*
* @example
*
* getList => GET http://my.api.url/posts?order=title.asc&offset=0&limit=24&filterField=eq.value
* getOne => GET http://my.api.url/posts?id=eq.123
* getMany => GET http://my.api.url/posts?id=in.(123,456,789)
* getManyReference => GET http://my.api.url/posts?author_id=eq.345
* create => POST http://my.api.url/posts
* update => PATCH http://my.api.url/posts?id=eq.123
* updateMany => PATCH http://my.api.url/posts?id=in.(123,456,789)
* delete => DELETE http://my.api.url/posts?id=eq.123
* deleteMany => DELETE http://my.api.url/posts?id=in.(123,456,789)
*
* @example
*
* import * as React from 'react';
* import { Admin, Resource, fetchUtils } from 'react-admin';
* import postgrestRestProvider,
* { IDataProviderConfig,
* defaultPrimaryKeys,
* defaultSchema } from '@raphiniert/ra-data-postgrest';
*
* import { PostList } from './posts';
*
* const config: IDataProviderConfig = {
* apiUrl: 'http://path.to.my.api/',
* httpClient: fetchUtils.fetchJson,
* defaultListOp: 'eq',
* primaryKeys: defaultPrimaryKeys,
* schema: defaultSchema
* }
*
* const App = () => (
* <Admin dataProvider={postgrestRestProvider(config)}>
* <Resource name="posts" list={PostList} />
* </Admin>
* );
*
* export default App;
*/
export type MetaOption = string | undefined;
export declare const defaultPrimaryKeys: Map<string, PrimaryKey>;
export declare const defaultSchema: () => string;
export interface IDataProviderConfig {
apiUrl: string;
httpClient: (string: any, Options: any) => Promise<any>;
defaultListOp: PostgRestOperator;
primaryKeys: Map<string, PrimaryKey>;
schema: () => string;
}
declare const _default: (config: IDataProviderConfig) => DataProvider;
export default _default;
251 changes: 251 additions & 0 deletions esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { getPrimaryKey, parseFilters, getOrderBy, dataWithId, dataWithoutId, getQuery, getKeyData, encodeId, decodeId, isCompoundKey, } from './urlBuilder';
import qs from 'qs';
export var defaultPrimaryKeys = new Map();
export var defaultSchema = function () { return ''; };
var useCustomSchema = function (schema, metaSchema, method) {
var _a;
var funcHeaderSchema = schema;
if (metaSchema !== undefined) {
funcHeaderSchema = function () { return metaSchema; };
}
if (funcHeaderSchema().length > 0) {
var schemaHeader = '';
if (['GET', 'HEAD'].includes(method)) {
schemaHeader = 'Accept-Profile';
}
else if (['POST', 'PATCH', 'PUT', 'DELETE'].includes(method)) {
schemaHeader = 'Content-Profile';
}
else
return {};
return _a = {}, _a[schemaHeader] = funcHeaderSchema(), _a;
}
else
return {};
};
export default (function (config) { return ({
getList: function (resource, params) {
var _a, _b;
if (params === void 0) { params = {}; }
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var _c = params.pagination, page = _c.page, perPage = _c.perPage;
var _d = params.sort || {}, field = _d.field, order = _d.order;
var _e = parseFilters(params, config.defaultListOp), filter = _e.filter, select = _e.select;
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
var query = __assign({ offset: String((page - 1) * perPage), limit: String(perPage) }, filter);
if (field) {
query.order = getOrderBy(field, order, primaryKey);
}
if (select) {
query.select = select;
}
// add header that Content-Range is in returned header
var options = {
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'))),
};
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
return config.httpClient(url, options).then(function (_a) {
var headers = _a.headers, json = _a.json;
if (!headers.has('content-range')) {
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?");
}
return {
data: json.map(function (obj) { return dataWithId(obj, primaryKey); }),
total: parseInt(headers.get('content-range').split('/').pop(), 10),
};
});
},
getOne: function (resource, params) {
var _a, _b;
if (params === void 0) { params = {}; }
var id = params.id, meta = params.meta;
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var query = getQuery(primaryKey, id, resource, meta);
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
return config
.httpClient(url, {
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'))),
})
.then(function (_a) {
var json = _a.json;
return ({
data: dataWithId(json, primaryKey),
});
});
},
getMany: function (resource, params) {
var _a;
if (params === void 0) { params = {}; }
var ids = params.ids;
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var query = getQuery(primaryKey, ids, resource, params.meta);
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
return config
.httpClient(url, {
headers: new Headers(__assign({}, useCustomSchema(config.schema, metaSchema, 'GET'))),
})
.then(function (_a) {
var json = _a.json;
return ({
data: json.map(function (data) { return dataWithId(data, primaryKey); }),
});
});
},
getManyReference: function (resource, params) {
var _a;
var _b, _c;
if (params === void 0) { params = {}; }
var _d = params.pagination, page = _d.page, perPage = _d.perPage;
var _e = params.sort, field = _e.field, order = _e.order;
var _f = parseFilters(params, config.defaultListOp), filter = _f.filter, select = _f.select;
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var metaSchema = (_b = params.meta) === null || _b === void 0 ? void 0 : _b.schema;
var query = params.target
? __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);
if (select) {
query.select = select;
}
// add header that Content-Range is in returned header
var options = {
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'))),
};
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
return config.httpClient(url, options).then(function (_a) {
var headers = _a.headers, json = _a.json;
if (!headers.has('content-range')) {
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?");
}
return {
data: json.map(function (data) { return dataWithId(data, primaryKey); }),
total: parseInt(headers.get('content-range').split('/').pop(), 10),
};
});
},
update: function (resource, params) {
var _a, _b;
if (params === void 0) { params = {}; }
var id = params.id, data = params.data, meta = params.meta;
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var query = getQuery(primaryKey, id, resource, meta);
var primaryKeyData = getKeyData(primaryKey, data);
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
var body = JSON.stringify(__assign(__assign({}, dataWithoutId(data, primaryKey)), primaryKeyData));
return config
.httpClient(url, {
method: 'PATCH',
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'))),
body: body,
})
.then(function (_a) {
var json = _a.json;
return ({ data: dataWithId(json, primaryKey) });
});
},
updateMany: function (resource, params) {
var _a, _b;
if (params === void 0) { params = {}; }
var ids = params.ids;
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var body = JSON.stringify(params.ids.map(function (id) {
var data = params.data;
var primaryKeyParams = decodeId(id, primaryKey);
var primaryKeyData = {};
if (isCompoundKey(primaryKey)) {
primaryKey.forEach(function (key, index) {
primaryKeyData[key] = primaryKeyParams[index];
});
}
else {
primaryKeyData[primaryKey[0]] = primaryKeyParams[0];
}
return __assign(__assign({}, dataWithoutId(data, primaryKey)), primaryKeyData);
}));
var url = "".concat(config.apiUrl, "/").concat(resource);
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
return config
.httpClient(url, {
method: 'PATCH',
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'))),
body: body,
})
.then(function (_a) {
var json = _a.json;
return ({
data: json.map(function (data) { return encodeId(data, primaryKey); }),
});
});
},
create: function (resource, params) {
var _a, _b;
if (params === void 0) { params = {}; }
var meta = params.meta;
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var query = getQuery(primaryKey, undefined, resource, meta);
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
return config
.httpClient(url, {
method: 'POST',
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'))),
body: JSON.stringify(dataWithoutId(params.data, primaryKey)),
})
.then(function (_a) {
var json = _a.json;
return ({
data: __assign(__assign({}, json), { id: encodeId(json, primaryKey) }),
});
});
},
delete: function (resource, params) {
var _a, _b;
if (params === void 0) { params = {}; }
var id = params.id, meta = params.meta;
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var query = getQuery(primaryKey, id, resource, meta);
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
return config
.httpClient(url, {
method: 'DELETE',
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'))),
})
.then(function (_a) {
var json = _a.json;
return ({ data: dataWithId(json, primaryKey) });
});
},
deleteMany: function (resource, params) {
var _a, _b;
if (params === void 0) { params = {}; }
var ids = params.ids, meta = params.meta;
var primaryKey = getPrimaryKey(resource, config.primaryKeys);
var query = getQuery(primaryKey, ids, resource, meta);
var url = "".concat(config.apiUrl, "/").concat(resource, "?").concat(qs.stringify(query));
var metaSchema = (_a = params.meta) === null || _a === void 0 ? void 0 : _a.schema;
return config
.httpClient(url, {
method: 'DELETE',
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'))),
})
.then(function (_a) {
var json = _a.json;
return ({
data: json.map(function (data) { return encodeId(data, primaryKey); }),
});
});
},
}); });
23 changes: 23 additions & 0 deletions esm/urlBuilder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Identifier } from 'ra-core';
/**
* List of all possible operators in PostgRest
* (https://postgrest.org/en/stable/api.html#operators)
*/
declare const postgrestOperators: readonly ["eq", "gt", "gte", "lt", "lte", "neq", "like", "ilike", "match", "imatch", "in", "is", "fts", "plfts", "phfts", "wfts", "cs", "cd", "ov", "sl", "sr", "nxr", "nxl", "adj", "not", "or", "and"];
type ParsedFiltersResult = {
filter: any;
select: any;
};
export type PostgRestOperator = (typeof postgrestOperators)[number];
export declare const parseFilters: (params: any, defaultListOp: PostgRestOperator) => Partial<ParsedFiltersResult>;
export type PrimaryKey = Array<string>;
export declare const getPrimaryKey: (resource: string, primaryKeys: Map<string, PrimaryKey>) => PrimaryKey;
export declare const decodeId: (id: Identifier, primaryKey: PrimaryKey) => string[] | number[];
export declare const encodeId: (data: any, primaryKey: PrimaryKey) => Identifier;
export declare const dataWithId: (data: any, primaryKey: PrimaryKey) => any;
export declare const dataWithoutId: (data: any, primaryKey: PrimaryKey) => any;
export declare const isCompoundKey: (primaryKey: PrimaryKey) => Boolean;
export declare const getQuery: (primaryKey: PrimaryKey, ids: Identifier | Array<Identifier> | undefined, resource: string, meta?: any) => any;
export declare const getKeyData: (primaryKey: PrimaryKey, data: object) => object;
export declare const getOrderBy: (field: string, order: string, primaryKey: PrimaryKey) => string;
export {};
Loading

0 comments on commit 8380cbc

Please sign in to comment.