2020-10-22 15:50:47 +00:00
|
|
|
import { Client } from '../models/client.ts'
|
2020-10-24 15:00:42 +00:00
|
|
|
import * as cache from '../models/cache.ts'
|
2020-10-25 17:03:53 +00:00
|
|
|
import endpoints from '../types/endpoint.ts'
|
2020-10-24 18:11:27 +00:00
|
|
|
|
|
|
|
interface IInit {
|
2020-10-25 06:50:32 +00:00
|
|
|
useCache?: boolean
|
2020-10-25 17:03:53 +00:00
|
|
|
endpoint: string
|
2020-10-24 18:11:27 +00:00
|
|
|
restURLfuncArgs: string[]
|
|
|
|
}
|
2020-10-21 16:30:42 +00:00
|
|
|
|
2020-10-22 15:50:47 +00:00
|
|
|
export class Base {
|
|
|
|
client: Client
|
2020-10-25 17:03:53 +00:00
|
|
|
static cacheName?: string
|
|
|
|
propertyConverterOverride: { [k: string]: string } = {}
|
2020-10-25 06:50:32 +00:00
|
|
|
static useCache?: boolean = true
|
2020-10-25 17:03:53 +00:00
|
|
|
static restFunc?: (...restURLfuncArgs: string[]) => string
|
2020-10-24 15:00:42 +00:00
|
|
|
|
2020-10-25 08:02:24 +00:00
|
|
|
constructor (client: Client, _data?: any) {
|
2020-10-22 15:50:47 +00:00
|
|
|
this.client = client
|
2020-10-21 16:30:42 +00:00
|
|
|
}
|
2020-10-24 15:00:42 +00:00
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
static async autoInit (
|
|
|
|
client: Client,
|
|
|
|
{ useCache, endpoint, restURLfuncArgs }: IInit
|
|
|
|
): Promise<any> {
|
|
|
|
this.useCache = useCache
|
|
|
|
const cacheID = restURLfuncArgs.join(':')
|
|
|
|
if (this.useCache !== undefined) {
|
|
|
|
const cached = cache.get(this.cacheName ?? this.name, cacheID)
|
2020-10-25 00:15:33 +00:00
|
|
|
if (cached !== undefined) {
|
2020-10-24 15:00:42 +00:00
|
|
|
return cached
|
|
|
|
}
|
|
|
|
}
|
2020-10-25 17:03:53 +00:00
|
|
|
this.restFunc = endpoints.find(v => v.name === endpoint)
|
2020-10-25 08:02:24 +00:00
|
|
|
// TODO: Make error for this
|
2020-10-25 17:03:53 +00:00
|
|
|
if (this.restFunc !== undefined) {
|
|
|
|
const resp = await fetch(this.restFunc(...restURLfuncArgs), {
|
2020-10-25 08:02:24 +00:00
|
|
|
headers: {
|
|
|
|
Authorization: `Bot ${client.token}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const jsonParsed = await resp.json()
|
2020-10-24 15:00:42 +00:00
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
cache.set(
|
|
|
|
this.cacheName ?? this.name,
|
|
|
|
cacheID,
|
|
|
|
new this(client, jsonParsed)
|
|
|
|
)
|
|
|
|
|
2020-10-25 08:02:24 +00:00
|
|
|
return new this(client, jsonParsed)
|
|
|
|
}
|
2020-10-24 18:11:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 17:03:53 +00:00
|
|
|
async refreshFromAPI (
|
|
|
|
client: Client,
|
|
|
|
{ endpoint, restURLfuncArgs }: IInit
|
|
|
|
): Promise<this> {
|
|
|
|
const oldOne = Object.assign(Object.create(this), this)
|
|
|
|
const restFunc:
|
|
|
|
| ((...restURLfuncArgs: string[]) => string)
|
|
|
|
| undefined = endpoints.find(v => v.name === endpoint)
|
2020-10-25 08:02:24 +00:00
|
|
|
// TODO: Make error for this
|
2020-10-25 17:03:53 +00:00
|
|
|
if (restFunc !== undefined) {
|
|
|
|
const resp = await fetch(restFunc(...restURLfuncArgs), {
|
2020-10-25 08:02:24 +00:00
|
|
|
headers: {
|
|
|
|
Authorization: `Bot ${client.token}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const jsonParsed = await resp.json()
|
2020-10-25 17:03:53 +00:00
|
|
|
const result: { [k: string]: any } = {}
|
|
|
|
Object.keys(jsonParsed).forEach(key => {
|
|
|
|
result[this.convertPropertyNameToStandard(key)] = jsonParsed[key]
|
|
|
|
})
|
|
|
|
|
|
|
|
Object.assign(this, result)
|
2020-10-25 08:02:24 +00:00
|
|
|
}
|
2020-10-25 17:03:53 +00:00
|
|
|
|
|
|
|
return oldOne
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshFromData (data: { [k: string]: any }): this {
|
|
|
|
const oldOne = Object.assign(Object.create(this), this)
|
|
|
|
const result: { [k: string]: any } = {}
|
|
|
|
Object.keys(data).forEach(key => {
|
|
|
|
result[this.convertPropertyNameToStandard(key)] = data[key]
|
|
|
|
})
|
|
|
|
|
|
|
|
Object.assign(this, result)
|
|
|
|
return oldOne
|
2020-10-24 15:00:42 +00:00
|
|
|
}
|
2020-10-25 17:03:53 +00:00
|
|
|
|
|
|
|
convertPropertyNameToStandard (name: string): string {
|
|
|
|
if (name in this.propertyConverterOverride) {
|
|
|
|
return this.propertyConverterOverride[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
name = name.replaceAll('_id', 'ID')
|
|
|
|
name = name
|
|
|
|
.split('_')
|
|
|
|
.map((value, index) => {
|
|
|
|
if (index !== 0) {
|
|
|
|
value = value[0].toUpperCase() + value.slice(1)
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
.join('')
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// toJSON() {}
|
2020-10-21 16:30:42 +00:00
|
|
|
}
|
2020-10-25 17:03:53 +00:00
|
|
|
|
|
|
|
// 오류를 해결하기 위해 저는 2개로 접속하겠습니다. VS2019
|