Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
scheiblr committed Jun 16, 2023
1 parent 4805d98 commit 7167758
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 91 deletions.
184 changes: 98 additions & 86 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,27 @@ import qs from 'qs';
*
* import * as React from 'react';
* import { Admin, Resource, fetchUtils } from 'react-admin';
* import postgrestRestProvider,
* { IDataProviderConfig,
* defaultPrimaryKeys,
* 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;
*/

Expand Down Expand Up @@ -109,9 +109,7 @@ const useCustomSchema = (
} else return {};
};

export default (
config: IDataProviderConfig
): DataProvider => ({
export default (config: IDataProviderConfig): DataProvider => ({
getList: (resource, params: Partial<GetListParams> = {}) => {
const primaryKey = getPrimaryKey(resource, config.primaryKeys);

Expand Down Expand Up @@ -174,15 +172,17 @@ export default (
const url = `${config.apiUrl}/${resource}?${qs.stringify(query)}`;
const metaSchema = params.meta?.schema;

return config.httpClient(url, {
headers: new Headers({
accept: 'application/vnd.pgrst.object+json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'GET'),
}),
}).then(({ json }) => ({
data: dataWithId(json, primaryKey),
}));
return config
.httpClient(url, {
headers: new Headers({
accept: 'application/vnd.pgrst.object+json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'GET'),
}),
})
.then(({ json }) => ({
data: dataWithId(json, primaryKey),
}));
},

getMany: (resource, params: Partial<GetManyParams> = {}) => {
Expand All @@ -194,13 +194,15 @@ export default (
const url = `${config.apiUrl}/${resource}?${qs.stringify(query)}`;
const metaSchema = params.meta?.schema;

return config.httpClient(url, {
headers: new Headers({
...useCustomSchema(config.schema, metaSchema, 'GET'),
}),
}).then(({ json }) => ({
data: json.map(data => dataWithId(data, primaryKey)),
}));
return config
.httpClient(url, {
headers: new Headers({
...useCustomSchema(config.schema, metaSchema, 'GET'),
}),
})
.then(({ json }) => ({
data: json.map(data => dataWithId(data, primaryKey)),
}));
},

getManyReference: (
Expand Down Expand Up @@ -280,17 +282,19 @@ export default (
...primaryKeyData,
});

return config.httpClient(url, {
method: 'PATCH',
headers: new Headers({
Accept: 'application/vnd.pgrst.object+json',
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'PATCH'),
}),
body,
}).then(({ json }) => ({ data: dataWithId(json, primaryKey) }));
return config
.httpClient(url, {
method: 'PATCH',
headers: new Headers({
Accept: 'application/vnd.pgrst.object+json',
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'PATCH'),
}),
body,
})
.then(({ json }) => ({ data: dataWithId(json, primaryKey) }));
},

updateMany: (resource, params: Partial<UpdateManyParams> = {}) => {
Expand Down Expand Up @@ -321,41 +325,45 @@ export default (
const url = `${config.apiUrl}/${resource}`;
const metaSchema = params.meta?.schema;

return config.httpClient(url, {
method: 'PATCH',
headers: new Headers({
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'PATCH'),
}),
body,
}).then(({ json }) => ({
data: json.map(data => encodeId(data, primaryKey)),
}));
return config
.httpClient(url, {
method: 'PATCH',
headers: new Headers({
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'PATCH'),
}),
body,
})
.then(({ json }) => ({
data: json.map(data => encodeId(data, primaryKey)),
}));
},

create: (resource, params: Partial<CreateParams> = {}) => {
const primaryKey = getPrimaryKey(resource, config.primaryKeys);
const url = `${config.apiUrl}/${resource}`;
const metaSchema = params.meta?.schema;

return config.httpClient(url, {
method: 'POST',
headers: new Headers({
Accept: 'application/vnd.pgrst.object+json',
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'POST'),
}),
body: JSON.stringify(params.data),
}).then(({ json }) => ({
data: {
...json,
id: encodeId(json, primaryKey),
},
}));
return config
.httpClient(url, {
method: 'POST',
headers: new Headers({
Accept: 'application/vnd.pgrst.object+json',
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'POST'),
}),
body: JSON.stringify(params.data),
})
.then(({ json }) => ({
data: {
...json,
id: encodeId(json, primaryKey),
},
}));
},

delete: (resource, params: Partial<DeleteParams> = {}) => {
Expand All @@ -367,16 +375,18 @@ export default (
const url = `${config.apiUrl}/${resource}?${qs.stringify(query)}`;
const metaSchema = params.meta?.schema;

return config.httpClient(url, {
method: 'DELETE',
headers: new Headers({
Accept: 'application/vnd.pgrst.object+json',
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'DELETE'),
}),
}).then(({ json }) => ({ data: dataWithId(json, primaryKey) }));
return config
.httpClient(url, {
method: 'DELETE',
headers: new Headers({
Accept: 'application/vnd.pgrst.object+json',
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'DELETE'),
}),
})
.then(({ json }) => ({ data: dataWithId(json, primaryKey) }));
},

deleteMany: (resource, params: Partial<DeleteManyParams> = {}) => {
Expand All @@ -388,16 +398,18 @@ export default (
const url = `${config.apiUrl}/${resource}?${qs.stringify(query)}`;
const metaSchema = params.meta?.schema;

return config.httpClient(url, {
method: 'DELETE',
headers: new Headers({
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'DELETE'),
}),
}).then(({ json }) => ({
data: json.map(data => encodeId(data, primaryKey)),
}));
return config
.httpClient(url, {
method: 'DELETE',
headers: new Headers({
Prefer: 'return=representation',
'Content-Type': 'application/json',
...(params.meta?.headers || {}),
...useCustomSchema(config.schema, metaSchema, 'DELETE'),
}),
})
.then(({ json }) => ({
data: json.map(data => encodeId(data, primaryKey)),
}));
},
});
8 changes: 3 additions & 5 deletions src/urlBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ export const parseFilters = (
values.forEach(value => {
let op: string = (() => {
// if operator is intentionally blank, rpc syntax
if (operation.length === 0)
return `${value}`;

if (operation.length === 0) return `${value}`;

if (operation.includes('like'))
return `${operation}.*${value}*`;
if (['and', 'or'].includes(operation))
return `${value}`;
if (['and', 'or'].includes(operation)) return `${value}`;
if (['cs', 'cd'].includes(operation))
return `${operation}.{${value}}`;
return `${operation}.${value}`;
Expand Down

0 comments on commit 7167758

Please sign in to comment.