muffin-admin 2.2.0 → 2.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
package/src/buildRA.tsx CHANGED
@@ -37,7 +37,7 @@ import {
37
37
  import { AvatarField, EditableBooleanField, FKField, JsonField } from "./fields"
38
38
  import { FKInput, JsonInput } from "./inputs"
39
39
 
40
- export const UI = {
40
+ export const UI: Record<string, JSX.ElementType> = {
41
41
  // Fields
42
42
  BooleanField,
43
43
  ChipField,
@@ -2,7 +2,14 @@ import { AdminOpts } from "./types"
2
2
  import { APIParams, makeRequest, setupAdmin } from "./utils"
3
3
 
4
4
  export function MuffinDataprovider({ apiUrl }: AdminOpts) {
5
+ async function request(url: string, options?: APIParams) {
6
+ if (!url.startsWith("/")) url = `${apiUrl}/${url}`
7
+ const { json, headers } = await makeRequest(url, options)
8
+ return { data: json, headers }
9
+ }
10
+
5
11
  const methods = {
12
+ request,
6
13
  getList: async (resource: string, params: any) => {
7
14
  const { filter, pagination, sort } = params
8
15
  const query: APIParams["query"] = {}
@@ -16,34 +23,26 @@ export function MuffinDataprovider({ apiUrl }: AdminOpts) {
16
23
  const { field, order } = sort
17
24
  query.sort = order == "ASC" ? field : `-${field}`
18
25
  }
19
- const { headers, json } = await makeRequest(`${apiUrl}/${resource}`, {
20
- query,
21
- })
22
- return {
23
- data: json,
24
- total: parseInt(headers.get("x-total"), 10),
25
- }
26
+ const { headers, data } = await request(resource, { query })
27
+ return { data, total: parseInt(headers.get("x-total"), 10) }
26
28
  },
27
29
 
28
30
  getOne: async (resource, { id }) => {
29
- const { json } = await makeRequest(`${apiUrl}/${resource}/${id}`)
30
- return { data: json }
31
+ return await request(`${resource}/${id}`)
31
32
  },
32
33
 
33
34
  create: async (resource, { data }) => {
34
- const { json } = await makeRequest(`${apiUrl}/${resource}`, {
35
+ return await request(resource, {
35
36
  data,
36
37
  method: "POST",
37
38
  })
38
- return { data: json }
39
39
  },
40
40
 
41
41
  update: async (resource, { id, data }) => {
42
- const { json } = await makeRequest(`${apiUrl}/${resource}/${id}`, {
42
+ return await makeRequest(`${resource}/${id}`, {
43
43
  data,
44
44
  method: "PUT",
45
45
  })
46
- return { data: json }
47
46
  },
48
47
 
49
48
  updateMany: async (resource, { ids, data }) => {
@@ -52,14 +51,12 @@ export function MuffinDataprovider({ apiUrl }: AdminOpts) {
52
51
  },
53
52
 
54
53
  delete: async (resource, { id }) => {
55
- await makeRequest(`${apiUrl}/${resource}/${id}`, {
56
- method: "DELETE",
57
- })
54
+ await request(`${resource}/${id}`, { method: "DELETE" })
58
55
  return { data: { id } }
59
56
  },
60
57
 
61
58
  deleteMany: async (resource, { ids }) => {
62
- await makeRequest(`${apiUrl}/${resource}`, {
59
+ await request(resource, {
63
60
  data: ids,
64
61
  method: "DELETE",
65
62
  })