cal/packages/app-store/vital/lib/client.ts
alannnc 8c9096b55b
Vital App - Auto reschedule based on health data (#2500)
* Add vital integration

* Tidy up client_user_id creation

* Rename vital app to vitalother to follow name rules

* Added env var

* App vital reschedule

* Fix on app structure and api calls

* Implemented user identification from webhook

* WIP fix api call and read me

* Save vital settings via api

* Now saving userVitalSettings and trigger reschedule on selected param

* Added translations

* Fix type for vitalSettings

* Using api to get env vars required for url, fix display of vital settings

* Fix hours placeholder, translation not working

* Renames vital app

* Update seed-app-store.ts

* Update package.json

* Update yarn.lock

* Refactored env variables

* Update README.md

* Migrates to api_keys

* Extracts AppConfiguration

* vitalClient fixes

* Update index.ts

* Update metadata.ts

* Update index.ts

* Update metadata.ts

* Added namespace vital for translations

Co-authored-by: Maitham <maithamdib@gmail.com>
Co-authored-by: zomars <zomars@me.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-05-06 17:21:30 +00:00

35 lines
1.1 KiB
TypeScript

import { VitalClient } from "@tryvital/vital-node";
import type { ClientConfig } from "@tryvital/vital-node/dist/lib/models";
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
type VitalEnv = ClientConfig & {
mode: string;
webhook_secret: string;
};
export let vitalClient: VitalClient | null = null;
export let vitalEnv: VitalEnv | null = null;
export async function initVitalClient(): Promise<VitalClient> {
if (vitalClient) return vitalClient;
const appKeys = (await getAppKeysFromSlug("vital-automation")) as unknown as VitalEnv;
if (
typeof appKeys !== "object" ||
typeof appKeys.api_key !== "string" ||
typeof appKeys.webhook_secret !== "string" ||
typeof appKeys.region !== "string" ||
typeof appKeys.mode !== "string"
)
throw Error("Missing properties in vital-automation DB keys");
vitalEnv = appKeys;
vitalClient = new VitalClient({
region: appKeys.region,
api_key: appKeys.api_key || "",
environment: (appKeys.mode as ClientConfig["environment"]) || "sandbox",
});
return vitalClient;
}
export default vitalClient;