-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBaserow.ts
225 lines (197 loc) · 5.82 KB
/
Baserow.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
export type BaserowRecordType<Item> = Item & { id: number };
export interface BaserowListResponse<Item> {
count: number;
next: null | string;
previous: null | string;
results: BaserowRecordType<Item>[];
}
export interface BaserowListParams<Item> {
page?: number;
size?: number;
search?: string;
orderBy?: keyof BaserowRecordType<Item>;
orderDir?: 'ASC' | 'DESC';
}
interface BaserowCreateParams {
page?: number;
size?: number;
search?: string;
orderBy?: string | number | symbol;
orderDir?: 'ASC' | 'DESC';
}
export class BaserowRecord<Item> {
private readonly record: BaserowRecordType<Item>;
constructor(record: BaserowRecordType<Item>) {
this.record = record;
}
public getID(): number {
return this.record['id'];
}
public getStringValue(key: keyof Item): string {
return this.record[key] as string;
}
public getNumberValue(key: keyof Item): number {
return parseFloat(this.record[key] as string);
}
}
export class BaserowTable<Item> {
private baserow: Baserow;
private readonly tableID: number;
private readonly apiUrl: string;
constructor(baserow: Baserow, { tableID }: { tableID: number }) {
this.baserow = baserow;
this.tableID = tableID;
this.apiUrl = `${baserow.apiUrl}/api/database/rows/table/`;
}
private getQueryParams({
page = 1,
size = 100,
search = '',
orderBy = 'id',
orderDir = 'ASC',
}: BaserowCreateParams): string {
const params = new URLSearchParams();
params.append('page', page.toString());
params.append('size', size.toString());
params.append(
'user_field_names',
this.baserow.showUserFieldNames.toString()
);
if (search || search !== '') {
params.append('search', search);
}
if (orderBy !== 'id') {
params.append(
'order_by',
`${orderDir === 'ASC' ? '+' : '-'}${String(orderBy)}`
);
}
return params.toString();
}
public async list(
params: BaserowListParams<Item> = {}
): Promise<BaserowRecord<Item>[]> {
const response: Response = await fetch(
`${this.apiUrl}${this.tableID}/?${this.getQueryParams(params)}`,
{
method: 'GET',
headers: { Authorization: `Token ${this.baserow.apiKey}` },
}
);
const data = (await response.json()) as BaserowListResponse<Item>;
return data.results.map((item) => new BaserowRecord<Item>(item));
}
public async get(id: number): Promise<BaserowRecord<Item>> {
const response: Response = await fetch(
`${this.apiUrl}${this.tableID}/${id}/?user_field_names=${this.baserow.showUserFieldNames}`,
{
method: 'GET',
headers: { Authorization: `Token ${this.baserow.apiKey}` },
}
);
const data = (await response.json()) as BaserowRecordType<Item>;
return new BaserowRecord<Item>(data);
}
public async create(
fields: Record<
keyof Omit<Item, 'id'>,
string | number | Array<number | string> | undefined | boolean
>
): Promise<BaserowRecord<Item>> {
const response: Response = await fetch(
`${this.apiUrl}${this.tableID}/?user_field_names=${this.baserow.showUserFieldNames}`,
{
method: 'POST',
headers: {
Authorization: `Token ${this.baserow.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(fields),
}
);
const data = (await response.json()) as BaserowRecordType<Item>;
return new BaserowRecord<Item>(data);
}
public async update(
id: number,
fields: Partial<
Record<
keyof Item,
string | number | Array<number | string> | undefined | boolean
>
>
): Promise<BaserowRecord<Item>> {
const response: Response = await fetch(
`${this.apiUrl}${this.tableID}/${id}/?user_field_names=${this.baserow.showUserFieldNames}`,
{
method: 'PATCH',
headers: {
Authorization: `Token ${this.baserow.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(fields),
}
);
const data = (await response.json()) as BaserowRecordType<Item>;
return new BaserowRecord<Item>(data);
}
public async move(
id: number,
beforeId: number
): Promise<BaserowRecord<Item>> {
const response: Response = await fetch(
`${this.apiUrl}${this.tableID}/${id}/move/?user_field_names=${this.baserow.showUserFieldNames}&before_id=${beforeId}`,
{
method: 'PATCH',
headers: { Authorization: `Token ${this.baserow.apiKey}` },
}
);
const data = (await response.json()) as BaserowRecordType<Item>;
return new BaserowRecord<Item>(data);
}
public async delete(id: number): Promise<BaserowRecord<Item>> {
const response: Response = await fetch(
`${this.apiUrl}${this.tableID}/${id}`,
{
method: 'DELETE',
headers: { Authorization: `Token ${this.baserow.apiKey}` },
}
);
const data = (await response.json()) as BaserowRecordType<Item>;
return new BaserowRecord<Item>(data);
}
}
class Baserow {
private readonly API_KEY: string;
private readonly API_URL: string;
private readonly SHOW_USER_FIELD_NAMES: boolean;
constructor({
apiKey,
apiUrl = 'https://api.baserow.io',
showUserFieldNames = true,
}: {
apiKey?: string;
apiUrl?: string;
showUserFieldNames?: boolean;
}) {
if (!apiKey) {
throw new Error('Cant create Baserow instance without apiKey');
}
this.API_KEY = apiKey;
this.API_URL = apiUrl;
this.SHOW_USER_FIELD_NAMES = showUserFieldNames;
}
get apiKey(): string {
return this.API_KEY;
}
get apiUrl(): string {
return this.API_URL;
}
get showUserFieldNames(): boolean {
return this.SHOW_USER_FIELD_NAMES;
}
public table<Item>(tableID: number): BaserowTable<Item> {
return new BaserowTable<Item>(this, { tableID });
}
}
export default Baserow;