-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.ts
52 lines (45 loc) · 1.33 KB
/
service.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
import { PlatformMap } from '../proof'
import { KVClient } from './client'
import { BaseInfo } from './types'
export interface KVServiceOptions<Platform extends string> extends BaseInfo {
readonly platform: Platform
readonly client: KVClient
}
export class KVService<Platform extends string = keyof PlatformMap> {
public readonly client: KVClient
public readonly avatar: string
public readonly platform: Platform
public readonly identity: string
constructor(options: KVServiceOptions<Platform>) {
this.client = options.client
this.avatar = options.avatar
this.platform = options.platform
this.identity = options.identity
}
health() {
return this.client.health()
}
get() {
return this.client.get(this.avatar)
}
async set<Patch>(patch: Patch, options: SignatureOptions) {
const payload = await this.client.getPayload<Patch>({
avatar: this.avatar,
platform: this.platform,
identity: this.identity,
patch,
})
return this.client.set<Patch>({
avatar: this.avatar,
platform: this.platform,
identity: this.identity,
uuid: payload.uuid,
created_at: payload.created_at,
signature: await options.onSignature(payload.sign_payload),
patch,
})
}
}
interface SignatureOptions {
onSignature(payload: string): string | Promise<string>
}