From 62a04e9510e65ca554133433caac3dd2192dbbc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Guti=C3=A9rrez=20Hermoso?= Date: Mon, 22 Jul 2024 10:01:57 -0400 Subject: [PATCH] ConfigAPI: new class to handle frontend requests to config backend This new API is somewhat patterned after the InstallAPI, but simpler whenever possible. --- app/common/ConfigAPI.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 app/common/ConfigAPI.ts diff --git a/app/common/ConfigAPI.ts b/app/common/ConfigAPI.ts new file mode 100644 index 00000000..a8a12e83 --- /dev/null +++ b/app/common/ConfigAPI.ts @@ -0,0 +1,31 @@ +import {BaseAPI, IOptions} from "app/common/BaseAPI"; +import {addCurrentOrgToPath} from 'app/common/urlUtils'; + +/** + * An API for accessing the internal Grist configuration, stored in + * config.json. + */ +export class ConfigAPI extends BaseAPI { + constructor(private _homeUrl: string, options: IOptions = {}) { + super(options); + } + + public async getValue(key: string): Promise { + return (await this.requestJson(`${this._url}/api/config/${key}`, {method: 'GET'})).value; + } + + public async setValue(value: any, restart=false): Promise { + await this.request(`${this._url}/api/config`, { + method: 'PATCH', + body: JSON.stringify({config: value, restart}), + }); + } + + public async restartServer(): Promise { + await this.request(`${this._url}/api/admin/restart`, {method: 'POST'}); + } + + private get _url(): string { + return addCurrentOrgToPath(this._homeUrl); + } +}