asasvirtuais 2.0.1 → 2.1.1
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 +1 -1
- package/packages/fetch-interface.tsx +75 -0
package/package.json
CHANGED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import type { TableInterface, TableSchema } from './interface'
|
|
3
|
+
|
|
4
|
+
export interface FetchInterfaceConfig {
|
|
5
|
+
baseUrl?: string
|
|
6
|
+
headers?: Record<string, string>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function fetchInterface<Schema extends TableSchema, Table extends string>({
|
|
10
|
+
schema, defaultTable, baseUrl = '/api/v1', headers = {}
|
|
11
|
+
}: {
|
|
12
|
+
schema: Schema
|
|
13
|
+
defaultTable?: Table
|
|
14
|
+
baseUrl?: string
|
|
15
|
+
headers?: Record<string, string>
|
|
16
|
+
}): TableInterface<z.infer<TableSchema['readable']>, z.infer<TableSchema['writable']>> {
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
async find({ table = defaultTable as string, id }) {
|
|
20
|
+
const response = await fetch(`${baseUrl}/${table}/${id}`, {
|
|
21
|
+
headers
|
|
22
|
+
})
|
|
23
|
+
if (response.ok)
|
|
24
|
+
return response.json()
|
|
25
|
+
else
|
|
26
|
+
throw new Error(await response.json())
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
async create({ table = defaultTable as string, data }) {
|
|
30
|
+
const response = await fetch(`${baseUrl}/${table}`, {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
headers: { 'Content-Type': 'application/json', ...headers },
|
|
33
|
+
body: JSON.stringify(data)
|
|
34
|
+
})
|
|
35
|
+
if (response.ok)
|
|
36
|
+
return response.json()
|
|
37
|
+
else
|
|
38
|
+
throw new Error(await response.json())
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
async update({ table = defaultTable as string, id, data }) {
|
|
42
|
+
const response = await fetch(`${baseUrl}/${table}/${id}`, {
|
|
43
|
+
method: 'PATCH',
|
|
44
|
+
headers: { 'Content-Type': 'application/json', ...headers },
|
|
45
|
+
body: JSON.stringify(data)
|
|
46
|
+
})
|
|
47
|
+
if (response.ok)
|
|
48
|
+
return response.json()
|
|
49
|
+
else
|
|
50
|
+
throw new Error(await response.json())
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
async remove({ table = defaultTable as string, id }) {
|
|
54
|
+
const response = await fetch(`${baseUrl}/${table}/${id}`, {
|
|
55
|
+
method: 'DELETE',
|
|
56
|
+
headers
|
|
57
|
+
})
|
|
58
|
+
if (response.ok)
|
|
59
|
+
return response.json()
|
|
60
|
+
else
|
|
61
|
+
throw new Error(await response.json())
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
async list({ table = defaultTable as string, query }) {
|
|
65
|
+
const params = new URLSearchParams(query as any)
|
|
66
|
+
const response = await fetch(`${baseUrl}/${table}?${params}`, {
|
|
67
|
+
headers
|
|
68
|
+
})
|
|
69
|
+
if (response.ok)
|
|
70
|
+
return response.json()
|
|
71
|
+
else
|
|
72
|
+
throw new Error(await response.json())
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|