Repository navigation
Expand file tree
/
Copy pathbase.ts
More file actions
127 lines (107 loc) · 3.37 KB
/
Copy pathbase.ts
File metadata and controls
127 lines (107 loc) · 3.37 KB
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
import {
PutResponse,
DeleteResponse,
UpdateObject,
UpdateResponse,
QueryResponse
} from './base.types.ts'
import urlJoin from 'https://esm.sh/url-join'
import { toArray } from './utils/autil.ts'
// deno-lint-ignore no-explicit-any
export default class Base<T = Record<any, any>> {
private key: string;
private id: string;
// private options: Options;
private name: string;
private get baseUrl() { return `https://database.deta.sh/v1/${this.id}/${this.name}` }
constructor(key: string, id: string, name: string) {
this.key = key;
this.id = id;
// this.options = options;
this.name = name;
}
private async fetcher(options: BaseFetcherOptions) {
const { body, urlParams, method } = options
const url = urlParams ? urlParams.reduce((p, c) => urlJoin(p, c), this.baseUrl) : this.baseUrl
const response = await fetch(url, {
method: method ? method : 'GET',
headers: {
'X-API-Key': this.key,
'Content-Type': 'application/json'
},
[body ? 'body' : '']: JSON.stringify(body)
})
const statusCode = response.status
const data = await response.json()
if (statusCode >= 200 && statusCode < 300) {
return data
} else {
return Promise.reject(data)
}
}
/**
* Stores multiple items in a single request. This request overwrites an item if the key already exists.
* @param `items` An array of items object to be stored.
*/
async put(items: T | T[]): Promise<PutResponse> {
items = toArray(items)
const body = { items }
return await this.fetcher({
body,
method: "PUT",
urlParams: ["items"]
})
}
/**
* Get a stored item.
* @param key The key (aka. ID) of the item you want to retrieve
*/
async get(key: string) : Promise<T> {
return await this.fetcher({
urlParams: ["items", key]
})
}
/**
* Delete a stored item.
* @param key The key (aka. ID) of the item you want to delete.
*/
async delete(key: string): Promise<DeleteResponse> {
return await this.fetcher({
urlParams: ["items", key],
method : "DELETE"
})
}
/**
* Creates a new item only if no item with the same `key` exists.
* @param item The item to be stored.
*/
async insert(item: T) : Promise<T> {
const body = { item }
return await this.fetcher({
urlParams: ["items"],
method: "POST",
body
})
}
/**
* Updates an item only if an item with key exists.
* @param key The key of the item
* @param body the update object
*/
async update(key: string, body: UpdateObject<T>) : Promise<UpdateResponse<T>> {
return await this.fetcher({
method: "PATCH",
urlParams: ["items", key],
body : body as DefaultObject
})
}
async query<T>(query: DefaultObject[] | DefaultObject = [], limit?: number, last?: string) : Promise<QueryResponse<T>> {
query = toArray(query)
const body = { query, limit, last }
return await this.fetcher({
method: "POST",
urlParams: ["query"],
body
})
}
}