From 3a232dbb5792c28b26747e1ba260fffcdd4a8416 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sat, 8 Apr 2023 21:57:40 +0200 Subject: [PATCH] feat(cli): add getFullResource --- deno-client/mod.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/deno-client/mod.ts b/deno-client/mod.ts index 422aa434cc..45a145fe5d 100644 --- a/deno-client/mod.ts +++ b/deno-client/mod.ts @@ -80,6 +80,31 @@ export async function getResource( } } +/** + * Get the full resource value by path + * @param path path of the resource, default to internal state path + * @param undefinedIfEmpty if the resource does not exist, return undefined instead of throwing an error + * @returns full resource + */ +export async function getFullResource( + path?: string, + undefinedIfEmpty?: boolean +): Promise { + const workspace = getWorkspace(); + path = path ?? getStatePath(); + try { + const resource = await ResourceService.getResource({ workspace, path }); + const value = await _transformLeaf(resource.value); + return { ...resource, value }; + } catch (e: any) { + if (undefinedIfEmpty && e.status === 404) { + return undefined; + } else { + throw Error(`Resource not found at ${path} or not visible to you`); + } + } +} + export function getStatePath(): string { const state_path = Deno.env.get("WM_STATE_PATH"); if (state_path === undefined) {