-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.ts
78 lines (68 loc) · 2.21 KB
/
client.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
import { buildHeaders, buildURL } from '../utils'
import { KVError } from './errors'
import type { GetResposne, HealthResposne, QueryPayload, QueryPayloadResponse, SetOptions } from './types'
export class KVClient {
private readonly baseURL: URL
private readonly fetch: typeof fetch
static production(fetcher = fetch) {
return new KVClient('https://kv-service.next.id', fetcher)
}
static development(fetcher = fetch) {
return new KVClient('https://kv-service.nextnext.id', fetcher)
}
constructor(baseURL: string | URL, fetcher: typeof fetch) {
this.baseURL = new URL(baseURL)
this.fetch = fetcher
}
/**
* General info
*/
health() {
return this.request<HealthResposne>('healthz', {
method: 'GET',
})
}
/**
* Get current KV of an avatar
* @param avatar Avatar public key (hex-string started with `0x`)
* @link https://github.com/nextdotid/kv_server/blob/cb109b/docs/api.apib#L27
*/
get(avatar: string): Promise<GetResposne> {
if (!avatar.startsWith('0x')) return Promise.reject(new KVError('started with `0x`', 400))
return this.request('v1/kv', {
method: 'GET',
searchParams: { avatar },
})
}
/**
* Get signature payload for updating
* @link https://github.com/nextdotid/kv_server/blob/cb109b/docs/api.apib#L74
*/
getPayload<Patch>(options: QueryPayload<Patch>): Promise<QueryPayloadResponse> {
return this.request('v1/kv/payload', {
method: 'POST',
body: JSON.stringify(options),
})
}
/**
* Update a full set of key-value pairs
* @link https://github.com/nextdotid/kv_server/blob/cb109b/docs/api.apib#L121
*/
set<Patch>(options: SetOptions<Patch>) {
return this.request<void>('v1/kv', {
method: 'POST',
body: JSON.stringify(options),
})
}
protected async request<T>(pathname: string, init?: RequestInitModified) {
const response = await this.fetch(buildURL(pathname, this.baseURL, init?.searchParams), {
...init,
headers: buildHeaders(init?.headers),
})
if (!response.ok) throw KVError.from(response)
return response.json() as Promise<T>
}
}
interface RequestInitModified extends RequestInit {
searchParams?: Record<string, string | undefined>
}