MapComplete/src/UI/Studio/StudioServer.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
3 KiB
TypeScript
Raw Normal View History

import { Utils } from "../../Utils"
import Constants from "../../Models/Constants"
import { LayerConfigJson } from "../../Models/ThemeConfig/Json/LayerConfigJson"
import { Store } from "../../Logic/UIEventSource"
import { LayoutConfigJson } from "../../Models/ThemeConfig/Json/LayoutConfigJson"
export default class StudioServer {
private readonly url: string
private readonly _userId: Store<number>
constructor(url: string, userId: Store<number>) {
this.url = url
this._userId = userId
}
public async fetchOverview(): Promise<
{
id: string
owner: number
category: "layers" | "themes"
}[]
> {
const { allFiles } = <{ allFiles: string[] }>(
2023-10-30 13:45:44 +01:00
await Utils.downloadJson(this.url + "/overview")
)
const layerOverview: {
id: string
owner: number | undefined
category: "layers" | "themes"
}[] = []
for (let file of allFiles) {
2023-10-30 13:45:44 +01:00
let parts = file.split("/")
let owner = Number(parts[0])
if (!isNaN(owner)) {
parts.splice(0, 1)
file = file.substring(file.indexOf("/") + 1)
2023-10-30 13:45:44 +01:00
} else {
owner = undefined
}
2023-10-30 13:45:44 +01:00
const category = <"layers" | "themes">parts[0]
const id = file.substring(file.lastIndexOf("/") + 1, file.length - ".json".length)
if (Constants.priviliged_layers.indexOf(<any>id) > 0) {
continue
}
layerOverview.push({ id, owner, category })
}
return layerOverview
}
async fetch(layerId: string, category: "layers", uid?: number): Promise<LayerConfigJson>
async fetch(layerId: string, category: "themes", uid?: number): Promise<LayoutConfigJson>
2023-10-30 13:45:44 +01:00
async fetch(
layerId: string,
category: "layers" | "themes",
uid?: number
): Promise<LayerConfigJson | LayoutConfigJson> {
try {
2023-10-30 13:45:44 +01:00
return await Utils.downloadJson(this.urlFor(layerId, category, uid))
} catch (e) {
return undefined
}
}
2023-12-02 00:24:55 +01:00
async delete(id: string, category: "layers" | "themes") {
if (id === undefined || id === "") {
return
}
await fetch(this.urlFor(id, category), {
2023-12-19 22:08:00 +01:00
method: "DELETE",
2023-12-02 00:24:55 +01:00
})
}
async update(id: string, config: string, category: "layers" | "themes") {
if (id === undefined || id === "") {
return
}
await fetch(this.urlFor(id, category), {
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: config,
})
}
2023-10-16 15:06:50 +02:00
public layerUrl(id: string) {
return this.urlFor(id, "layers")
}
2023-10-30 13:45:44 +01:00
public urlFor(id: string, category: "layers" | "themes", uid?: number) {
uid ??= this._userId.data
const uidStr = uid !== undefined ? "/" + uid : ""
return `${this.url}${uidStr}/${category}/${id}/${id}.json`
2023-10-16 15:06:50 +02:00
}
}