-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathDatabaseServer.ts
More file actions
67 lines (62 loc) · 2.02 KB
/
DatabaseServer.ts
File metadata and controls
67 lines (62 loc) · 2.02 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
import { fetchFromNotion } from "./lib/fetchFromNotion"
export interface IPromptDefineItem {
text: string
subType?: string
desc?: string
dir?: string
lang_zh?: string
sampleCmds?: string[]
isAlias?: boolean
tags?: string[]
}
export class DatabaseServer {
localPromptDefineMap: { [key: string]: IPromptDefineItem } = {}
notionPromptDefineMap: { [key: string]: IPromptDefineItem } = {}
isReady: null | Promise<boolean> = null
constructor() {}
async ready() {
if (this.isReady != null) return this.isReady
this.isReady = this.init()
return this.isReady
}
async init() {
// localJson
let localPromptDescMap = await (await fetch("./localPromptDefineMap.json")).json()
// console.log('localPromptDescMap',localPromptDescMap)
this.localPromptDefineMap = localPromptDescMap
return true
}
async queryPromptsDefine(prompts: string[]): Promise<IPromptDefineItem[]> {
await this.ready()
let result = []
for (let prompt of prompts) {
let re = this.localPromptDefineMap[prompt?.toLowerCase()]
if (re) {
result.push(re)
} else {
result.push(null)
}
}
return <any>result
}
async getPromptsDefine(options?: { onlyMyNotion?: boolean }) {
await this.ready()
if (options?.onlyMyNotion) {
return this.notionPromptDefineMap
} else {
return this.localPromptDefineMap
}
}
async fetchNotion(options: { apiKey: string; databaseId: string }) {
console.log("fetchNotion options", options)
let { defineMap, me } = await fetchFromNotion(options)
this.notionPromptDefineMap = defineMap
Object.assign(this.localPromptDefineMap, defineMap)
return { defineMap, me }
}
}
let databaseServer: DatabaseServer
export function useDatabaseServer() {
if (!databaseServer) databaseServer = new DatabaseServer()
return databaseServer
}