web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit a5a5ed5cae646dec0706fe8893de7afd7d7d581e
parent ec594f693e29974c897af21a8cfab83f1c0e4b97
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri, 18 Oct 2024 10:12:36 +0000

client: edit http fetch method adding conditional branch on response ok, json parse if response json is string type, record type for opts data. edit keystore adding entries method and update return types

Diffstat:
Mclient/src/http/tauri.ts | 27++++++++++++++++++++-------
Mclient/src/http/types.ts | 2+-
Mclient/src/keyring/tauri.ts | 2+-
Mclient/src/keystore/tauri.ts | 39+++++++++++++++++++++++++--------------
Mclient/src/keystore/types.ts | 11++++++-----
5 files changed, 53 insertions(+), 28 deletions(-)

diff --git a/client/src/http/tauri.ts b/client/src/http/tauri.ts @@ -27,7 +27,6 @@ export class TauriClientHttp implements IClientHttp { public async fetch(opts: IClientHttpOpts): Promise<IClientHttpResponse | ErrorMessage<string>> { try { const { url } = opts; - const options: RequestInit & ClientOptions = { method: opts.method ? opts.method.toUpperCase() : `GET`, } @@ -35,13 +34,27 @@ export class TauriClientHttp implements IClientHttp { if (opts.headers) options.headers = opts.headers; if (opts.connect_timeout) options.connectTimeout = opts.connect_timeout; const response = await fetch(url, options); - return { - status: response.status, - url: response.url, - data: await response.json(), - headers: parse_headers(response.headers) - }; + switch (response.ok) { + case true: { + const response_json = await response.json(); + return { + status: response.status, + url: response.url, + data: typeof response_json === `string` ? JSON.parse(response_json) : response_json, + headers: parse_headers(response.headers) + }; + } + case false: { + return { + status: response.status, + url: response.url, + data: null, + headers: parse_headers(response.headers) + }; + } + } } catch (e) { + console.log(`e fetch`, e) return err_msg(String(e)); }; } diff --git a/client/src/http/types.ts b/client/src/http/types.ts @@ -6,7 +6,7 @@ export type IClientHttpOpts = { params?: { [key: string]: string | string[]; }; - data?: any; + data?: Record<string, string | number | boolean>; headers?: { [key: string]: string; }; diff --git a/client/src/keyring/tauri.ts b/client/src/keyring/tauri.ts @@ -18,7 +18,7 @@ export class TauriClientKeying implements IClientKeyring { const response = await invoke<any>("keyring_nostr_key_get", { publicKeyHex: public_key_hex }); console.log(`response `, response); if (response && typeof response === `string`) return { result: response }; - return err_msg(`*-result`);; + return err_msg(`*-result`); } catch (e) { return err_msg(`*`); } diff --git a/client/src/keystore/tauri.ts b/client/src/keystore/tauri.ts @@ -1,4 +1,4 @@ -import { err_msg, type ErrorMessage } from '@radroots/utils'; +import { err_msg, ResultObj, ResultPass, ResultsList, type ErrorMessage } from '@radroots/utils'; import { createStore, Store } from '@tauri-apps/plugin-store'; import type { IClientKeystore, IClientKeystoreUnlisten } from './types'; @@ -14,7 +14,7 @@ export class TauriClientKeystore implements IClientKeystore { this._store = await createStore(this._store_path); } - public async set(key: string, value: string): Promise<{ pass: true } | ErrorMessage<string>> { + public async set(key: string, value: string): Promise<ResultPass | ErrorMessage<string>> { try { if (!this._store) return err_msg(`*-store`); await this._store.set(key, { value }); @@ -22,39 +22,50 @@ export class TauriClientKeystore implements IClientKeystore { return { pass: true }; } catch (e) { return err_msg(`*`); - } + }; } - public async get(key: string): Promise<{ result: string } | ErrorMessage<string>> { + public async get(key: string): Promise<ResultObj<string> | ErrorMessage<string>> { try { if (!this._store) return err_msg(`*-store`); const result = await this._store.get<{ value: any }>(key); if (result && typeof result.value === `string`) return { result: result.value }; - return err_msg(`*-result`);; + return err_msg(`*-result`); } catch (e) { return err_msg(`*`); - } + }; } - public async keys(): Promise<{ results: string[] } | ErrorMessage<string>> { + public async remove(key: string): Promise<ResultPass | ErrorMessage<string>> { + try { + if (!this._store) return err_msg(`*-store`); + const res = await this._store.delete(key); + if (!res) return err_msg(`*-pass`); + await this._store.save(); + return { pass: true }; + } catch (e) { + return err_msg(`*`); + }; + } + + public async keys(): Promise<ResultsList<string> | ErrorMessage<string>> { try { if (!this._store) return err_msg(`*-store`); const results = await this._store.keys(); return { results }; } catch (e) { return err_msg(`*`); - } + }; } - public async remove(key: string): Promise<boolean | ErrorMessage<string>> { + public async entries(): Promise<ResultsList<[string, unknown]> | ErrorMessage<string>> { try { if (!this._store) return err_msg(`*-store`); - const res = await this._store.delete(key); - if (res) await this._store.save(); - return res; + const results = await this._store.entries(); + return { results }; } catch (e) { return err_msg(`*`); - } + }; } public async on_key_change(key: string, callback: (value: string | null) => Promise<void>): Promise<IClientKeystoreUnlisten | ErrorMessage<string>> { @@ -64,6 +75,6 @@ export class TauriClientKeystore implements IClientKeystore { return res; } catch (e) { return err_msg(`*`); - } + }; } } diff --git a/client/src/keystore/types.ts b/client/src/keystore/types.ts @@ -1,13 +1,14 @@ -import type { ErrorMessage } from "@radroots/utils"; +import type { ErrorMessage, ResultObj, ResultPass, ResultsList } from "@radroots/utils"; import type { UnlistenFn } from "@tauri-apps/api/event"; export type IClientKeystoreUnlisten = UnlistenFn; export type IClientKeystore = { init: () => Promise<void>; - set(key: string, val: string): Promise<{ pass: true } | ErrorMessage<string>>; - get(key: string): Promise<{ result: string } | ErrorMessage<string>>; - keys(): Promise<{ results: string[] } | ErrorMessage<string>>; - remove(key: string): Promise<boolean | ErrorMessage<string>>; + set(key: string, val: string): Promise<ResultPass | ErrorMessage<string>>; + get(key: string): Promise<ResultObj<string> | ErrorMessage<string>>; + keys(): Promise<ResultsList<string> | ErrorMessage<string>>; + entries(): Promise<ResultsList<[string, unknown]> | ErrorMessage<string>> + remove(key: string): Promise<ResultPass | ErrorMessage<string>>; on_key_change(key: string, callback: (value: string | null) => Promise<void>): Promise<IClientKeystoreUnlisten | ErrorMessage<string>>; }; \ No newline at end of file