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-24 18:11:27 +00:00
|
|
|
|
|
|
|
interface IInit {
|
2020-10-25 06:50:32 +00:00
|
|
|
useCache?: boolean
|
2020-10-29 14:43:27 +00:00
|
|
|
endpoint: (...restURLfuncArgs: string[]) => 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-29 14:43:27 +00:00
|
|
|
const resp = await fetch(endpoint(...restURLfuncArgs), {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bot ${client.token}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const jsonParsed = await resp.json()
|
2020-10-25 17:03:53 +00:00
|
|
|
|
2020-10-29 14:43:27 +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)
|
|
|
|
|
2020-10-29 14:43:27 +00:00
|
|
|
const resp = await fetch(endpoint(...restURLfuncArgs), {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bot ${client.token}`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const jsonParsed = await resp.json()
|
|
|
|
|
|
|
|
this.readFromData(jsonParsed)
|
2020-10-25 17:03:53 +00:00
|
|
|
|
|
|
|
return oldOne
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshFromData (data: { [k: string]: any }): this {
|
|
|
|
const oldOne = Object.assign(Object.create(this), this)
|
2020-10-29 14:43:27 +00:00
|
|
|
this.readFromData(data)
|
2020-10-25 17:03:53 +00:00
|
|
|
return oldOne
|
2020-10-24 15:00:42 +00:00
|
|
|
}
|
2020-10-25 17:03:53 +00:00
|
|
|
|
2020-10-29 14:43:27 +00:00
|
|
|
readFromData (data: { [k: string]: any }): void {}
|
2020-10-25 17:03:53 +00:00
|
|
|
|
|
|
|
// toJSON() {}
|
2020-10-21 16:30:42 +00:00
|
|
|
}
|