-
Notifications
You must be signed in to change notification settings - Fork 7
/
WebdisStore.ts
48 lines (39 loc) · 1.36 KB
/
WebdisStore.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
import Store from './Store.ts'
import { SessionData } from '../Session.ts'
type WebdisOptions = {
url : string,
keyPrefix?: string,
}
export default class WebdisStore implements Store {
url: string
keyPrefix: string
constructor(options : WebdisOptions) {
this.url = options.url
this.keyPrefix = options.keyPrefix || 'session_'
}
async sessionExists(sessionId : string) {
const payload = await fetch(this.url + '/GET/' + this.keyPrefix + sessionId)
const payloadJSON = await payload.json()
const session = payloadJSON.GET
return session ? true : false
}
async getSessionById(sessionId : string) {
const payload = await fetch(this.url + '/GET/' + this.keyPrefix + sessionId)
const payloadJSON = await payload.json()
const session = payloadJSON.GET
if (session) {
return JSON.parse(session) as SessionData
} else {
return null
}
}
async createSession(sessionId : string, initialData : SessionData) {
await fetch(this.url + '/SET/' + this.keyPrefix + sessionId + '/'+JSON.stringify(initialData))
}
async deleteSession(sessionId : string) {
await fetch(this.url + '/DEL/' + this.keyPrefix + sessionId)
}
async persistSessionData(sessionId : string, sessionData : SessionData) {
await fetch(this.url + '/SET/' + this.keyPrefix + sessionId + '/' + encodeURI(JSON.stringify(sessionData)))
}
}