web_lib

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

commit 3ea8a32ca494bf06cae57061235cade4a520b59a
parent dbf96d227ac269e7c4958f1ceae75bd6d6cd2267
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Sat,  2 Nov 2024 10:28:32 +0000

client: update client database add `nostr_profile_relay` methods, edit iclientdatabase types. add client fs info method. edit http client fetch method and add fetch_image method. add client device methods

Diffstat:
Mclient/src/database/tauri.ts | 66+++++++++++++++++++++++++++++++++---------------------------------
Mclient/src/database/types.ts | 40+++++++++++++++++++++-------------------
Mclient/src/device/tauri.ts | 13+++++++++++++
Mclient/src/device/types.ts | 2++
Mclient/src/fs/tauri.ts | 14++++++++++++--
Mclient/src/fs/types.ts | 5++++-
Mclient/src/http/tauri.ts | 52+++++++++++++++++++++++++++++++++++++++++++++++++---
Mclient/src/http/types.ts | 11++++++++++-
8 files changed, 144 insertions(+), 59 deletions(-)

diff --git a/client/src/database/tauri.ts b/client/src/database/tauri.ts @@ -1,17 +1,17 @@ -import { type INostrProfileQueryBindValues, type INostrRelayQueryBindValues, type ILocationGcsAdd, type ILocationGcsAddResolve, type ILocationGcsGet, type ILocationGcsGetResolve, type ILocationGcsDelete, type ILocationGcsDeleteResolve, type ILocationGcsUpdate, type ILocationGcsUpdateResolve, type ITradeProductAdd, type ITradeProductAddResolve, type ITradeProductGet, type ITradeProductGetResolve, type ITradeProductDelete, type ITradeProductDeleteResolve, type ITradeProductUpdate, type ITradeProductUpdateResolve, type INostrProfileAdd, type INostrProfileAddResolve, type INostrProfileGet, type INostrProfileGetResolve, type INostrProfileDelete, type INostrProfileDeleteResolve, type INostrProfileUpdate, type INostrProfileUpdateResolve, type INostrRelayAdd, type INostrRelayAddResolve, type INostrRelayGet, type INostrRelayGetResolve, type INostrRelayDelete, type INostrRelayDeleteResolve, type INostrRelayUpdate, type INostrRelayUpdateResolve, type IModelsQueryBindValue, type IModelsQueryValue, type IModelsQueryBindValueTuple, type LocationGcsFields, type LocationGcsFormFields, LocationGcsSchema, LocationGcsUpdateSchema, parse_location_gcs_form_fields, parse_location_gcs_list, type TradeProductFields, type TradeProductFormFields, TradeProductSchema, TradeProductUpdateSchema, parse_trade_product_form_fields, parse_trade_product_list, type NostrProfileFields, type NostrProfileFormFields, NostrProfileSchema, NostrProfileUpdateSchema, parse_nostr_profile_form_fields, parse_nostr_profile_list, type NostrRelayFields, type NostrRelayFormFields, NostrRelaySchema, NostrRelayUpdateSchema, parse_nostr_relay_form_fields, parse_nostr_relay_list } from "@radroots/models"; -import { err_msg, type ErrorMessage, type ResultPass } from "@radroots/utils"; +import { LocationGcsSchema, LocationGcsUpdateSchema, NostrProfileSchema, NostrProfileUpdateSchema, NostrRelaySchema, NostrRelayUpdateSchema, TradeProductSchema, TradeProductUpdateSchema, parse_location_gcs_form_fields, parse_location_gcs_list, parse_nostr_profile_form_fields, parse_nostr_profile_list, parse_nostr_relay_form_fields, parse_nostr_relay_list, parse_trade_product_form_fields, parse_trade_product_list, type ILocationGcsAdd, type ILocationGcsAddResolve, type ILocationGcsDelete, type ILocationGcsDeleteResolve, type ILocationGcsGet, type ILocationGcsGetResolve, type ILocationGcsUpdate, type ILocationGcsUpdateResolve, type IModelsQueryBindValueTuple, type IModelsQueryValue, type INostrProfileAdd, type INostrProfileAddResolve, type INostrProfileDelete, type INostrProfileDeleteResolve, type INostrProfileGet, type INostrProfileGetResolve, type INostrProfileRelayRelation, type INostrProfileRelayRelationResolve, type INostrProfileUpdate, type INostrProfileUpdateResolve, type INostrRelayAdd, type INostrRelayAddResolve, type INostrRelayDelete, type INostrRelayDeleteResolve, type INostrRelayGet, type INostrRelayGetResolve, type INostrRelayUpdate, type INostrRelayUpdateResolve, type ITradeProductAdd, type ITradeProductAddResolve, type ITradeProductDelete, type ITradeProductDeleteResolve, type ITradeProductGet, type ITradeProductGetResolve, type ITradeProductUpdate, type ITradeProductUpdateResolve, type LocationGcsFields, type LocationGcsFormFields, type NostrProfileFields, type NostrProfileFormFields, type NostrRelayFields, type NostrRelayFormFields, type TradeProductFields, type TradeProductFormFields } from "@radroots/models"; +import { err_msg, type ErrorMessage } from "@radroots/utils"; import { invoke } from "@tauri-apps/api/core"; -import type { IClientDb, IClientDbMessage } from "./types"; +import type { IClientDatabase, IClientDatabaseMessage } from "./types"; -export class TauriClientDb implements IClientDb { - private append_logs(scope: string, opts: any, error: any): IClientDbMessage { +export class TauriClientDatabase implements IClientDatabase { + private append_logs(scope: string, opts: any, error: any): IClientDatabaseMessage { console.log("todo... append_logs"); const error_msg = String(error); return error_msg; } - - private handle_errors(scope: string, opts: any, e: any): ErrorMessage<IClientDbMessage> { + + private handle_errors(scope: string, opts: any, e: any): ErrorMessage<IClientDatabaseMessage> { const error = this.append_logs(scope, opts, e); if (error.includes("UNIQUE constraint failed: location_gcs.geohash")) return err_msg("*-location-gcs-geohash-unique"); else if (error.includes("UNIQUE constraint failed: nostr_relay.url")) return err_msg("*-nostr-relay-url-unique"); @@ -21,7 +21,7 @@ export class TauriClientDb implements IClientDb { private filter_bind_value_fields(fields: IModelsQueryBindValueTuple[]): IModelsQueryBindValueTuple[] { return fields.filter(([_, v]) => !!v); } - + private location_gcs_add_validate(fields: LocationGcsFormFields): LocationGcsFields | string[] { const fields_r = Object.entries(fields).filter(([_, v]) => !!v).reduce((acc: Record<string, IModelsQueryValue>, i) => { const [key, val] = parse_location_gcs_form_fields(i); @@ -36,7 +36,7 @@ export class TauriClientDb implements IClientDb { }; } - public async location_gcs_add(opts: ILocationGcsAdd): Promise<ILocationGcsAddResolve<IClientDbMessage>> { + public async location_gcs_add(opts: ILocationGcsAdd): Promise<ILocationGcsAddResolve<IClientDatabaseMessage>> { const err_s = this.location_gcs_add_validate(opts); if (Array.isArray(err_s)) return { err_s }; const fields = this.filter_bind_value_fields(Object.entries(err_s)); @@ -51,7 +51,7 @@ export class TauriClientDb implements IClientDb { }; } - public async location_gcs_get(opts: ILocationGcsGet): Promise<ILocationGcsGetResolve<IClientDbMessage>> { + public async location_gcs_get(opts: ILocationGcsGet): Promise<ILocationGcsGetResolve<IClientDatabaseMessage>> { try { const response = await invoke<any>("model_location_gcs_get", { opts: "list" in opts ? { list: { of: opts.list, sort: opts.sort } } : { on: { ...opts } } }); if (typeof response === "string") return err_msg(response); @@ -62,7 +62,7 @@ export class TauriClientDb implements IClientDb { }; } - public async location_gcs_delete(opts: ILocationGcsDelete): Promise<ILocationGcsDeleteResolve<IClientDbMessage>> { + public async location_gcs_delete(opts: ILocationGcsDelete): Promise<ILocationGcsDeleteResolve<IClientDatabaseMessage>> { try { const response = await invoke<any>("model_location_gcs_delete", { opts }); if (response === null) return { pass: true }; @@ -86,7 +86,7 @@ export class TauriClientDb implements IClientDb { }; } - public async location_gcs_update(opts: ILocationGcsUpdate): Promise<ILocationGcsUpdateResolve<IClientDbMessage>> { + public async location_gcs_update(opts: ILocationGcsUpdate): Promise<ILocationGcsUpdateResolve<IClientDatabaseMessage>> { const err_s = this.location_gcs_update_validate(opts.fields); if (Array.isArray(err_s)) return { err_s }; const fields = this.filter_bind_value_fields(Object.entries(err_s)); @@ -114,7 +114,7 @@ export class TauriClientDb implements IClientDb { }; } - public async trade_product_add(opts: ITradeProductAdd): Promise<ITradeProductAddResolve<IClientDbMessage>> { + public async trade_product_add(opts: ITradeProductAdd): Promise<ITradeProductAddResolve<IClientDatabaseMessage>> { const err_s = this.trade_product_add_validate(opts); if (Array.isArray(err_s)) return { err_s }; const fields = this.filter_bind_value_fields(Object.entries(err_s)); @@ -129,7 +129,7 @@ export class TauriClientDb implements IClientDb { }; } - public async trade_product_get(opts: ITradeProductGet): Promise<ITradeProductGetResolve<IClientDbMessage>> { + public async trade_product_get(opts: ITradeProductGet): Promise<ITradeProductGetResolve<IClientDatabaseMessage>> { try { const response = await invoke<any>("model_trade_product_get", { opts: "list" in opts ? { list: { of: opts.list, sort: opts.sort } } : { on: { ...opts } } }); if (typeof response === "string") return err_msg(response); @@ -140,7 +140,7 @@ export class TauriClientDb implements IClientDb { }; } - public async trade_product_delete(opts: ITradeProductDelete): Promise<ITradeProductDeleteResolve<IClientDbMessage>> { + public async trade_product_delete(opts: ITradeProductDelete): Promise<ITradeProductDeleteResolve<IClientDatabaseMessage>> { try { const response = await invoke<any>("model_trade_product_delete", { opts }); if (response === null) return { pass: true }; @@ -164,7 +164,7 @@ export class TauriClientDb implements IClientDb { }; } - public async trade_product_update(opts: ITradeProductUpdate): Promise<ITradeProductUpdateResolve<IClientDbMessage>> { + public async trade_product_update(opts: ITradeProductUpdate): Promise<ITradeProductUpdateResolve<IClientDatabaseMessage>> { const err_s = this.trade_product_update_validate(opts.fields); if (Array.isArray(err_s)) return { err_s }; const fields = this.filter_bind_value_fields(Object.entries(err_s)); @@ -192,7 +192,7 @@ export class TauriClientDb implements IClientDb { }; } - public async nostr_profile_add(opts: INostrProfileAdd): Promise<INostrProfileAddResolve<IClientDbMessage>> { + public async nostr_profile_add(opts: INostrProfileAdd): Promise<INostrProfileAddResolve<IClientDatabaseMessage>> { const err_s = this.nostr_profile_add_validate(opts); if (Array.isArray(err_s)) return { err_s }; const fields = this.filter_bind_value_fields(Object.entries(err_s)); @@ -207,7 +207,7 @@ export class TauriClientDb implements IClientDb { }; } - public async nostr_profile_get(opts: INostrProfileGet): Promise<INostrProfileGetResolve<IClientDbMessage>> { + public async nostr_profile_get(opts: INostrProfileGet): Promise<INostrProfileGetResolve<IClientDatabaseMessage>> { try { const response = await invoke<any>("model_nostr_profile_get", { opts: "list" in opts ? { list: { of: opts.list, sort: opts.sort } } : { on: { ...opts } } }); if (typeof response === "string") return err_msg(response); @@ -218,7 +218,7 @@ export class TauriClientDb implements IClientDb { }; } - public async nostr_profile_delete(opts: INostrProfileDelete): Promise<INostrProfileDeleteResolve<IClientDbMessage>> { + public async nostr_profile_delete(opts: INostrProfileDelete): Promise<INostrProfileDeleteResolve<IClientDatabaseMessage>> { try { const response = await invoke<any>("model_nostr_profile_delete", { opts }); if (response === null) return { pass: true }; @@ -242,7 +242,7 @@ export class TauriClientDb implements IClientDb { }; } - public async nostr_profile_update(opts: INostrProfileUpdate): Promise<INostrProfileUpdateResolve<IClientDbMessage>> { + public async nostr_profile_update(opts: INostrProfileUpdate): Promise<INostrProfileUpdateResolve<IClientDatabaseMessage>> { const err_s = this.nostr_profile_update_validate(opts.fields); if (Array.isArray(err_s)) return { err_s }; const fields = this.filter_bind_value_fields(Object.entries(err_s)); @@ -270,7 +270,7 @@ export class TauriClientDb implements IClientDb { }; } - public async nostr_relay_add(opts: INostrRelayAdd): Promise<INostrRelayAddResolve<IClientDbMessage>> { + public async nostr_relay_add(opts: INostrRelayAdd): Promise<INostrRelayAddResolve<IClientDatabaseMessage>> { const err_s = this.nostr_relay_add_validate(opts); if (Array.isArray(err_s)) return { err_s }; const fields = this.filter_bind_value_fields(Object.entries(err_s)); @@ -285,7 +285,7 @@ export class TauriClientDb implements IClientDb { }; } - public async nostr_relay_get(opts: INostrRelayGet): Promise<INostrRelayGetResolve<IClientDbMessage>> { + public async nostr_relay_get(opts: INostrRelayGet): Promise<INostrRelayGetResolve<IClientDatabaseMessage>> { try { const response = await invoke<any>("model_nostr_relay_get", { opts: "list" in opts ? { list: { of: opts.list, sort: opts.sort } } : { on: { ...opts } } }); if (typeof response === "string") return err_msg(response); @@ -296,7 +296,7 @@ export class TauriClientDb implements IClientDb { }; } - public async nostr_relay_delete(opts: INostrRelayDelete): Promise<INostrRelayDeleteResolve<IClientDbMessage>> { + public async nostr_relay_delete(opts: INostrRelayDelete): Promise<INostrRelayDeleteResolve<IClientDatabaseMessage>> { try { const response = await invoke<any>("model_nostr_relay_delete", { opts }); if (response === null) return { pass: true }; @@ -320,7 +320,7 @@ export class TauriClientDb implements IClientDb { }; } - public async nostr_relay_update(opts: INostrRelayUpdate): Promise<INostrRelayUpdateResolve<IClientDbMessage>> { + public async nostr_relay_update(opts: INostrRelayUpdate): Promise<INostrRelayUpdateResolve<IClientDatabaseMessage>> { const err_s = this.nostr_relay_update_validate(opts.fields); if (Array.isArray(err_s)) return { err_s }; const fields = this.filter_bind_value_fields(Object.entries(err_s)); @@ -335,25 +335,25 @@ export class TauriClientDb implements IClientDb { }; } - public async set_nostr_profile_relay(opts: { nostr_profile: INostrProfileQueryBindValues; nostr_relay: INostrRelayQueryBindValues; }): Promise<ResultPass | ErrorMessage<IClientDbMessage>> { + public async nostr_profile_relay_set(opts: INostrProfileRelayRelation): Promise<INostrProfileRelayRelationResolve<IClientDatabaseMessage>> { try { - const response = await invoke<any>("model_set_nostr_profile_relay", { opts }); - if (response === null) return { pass: true }; + const response = await invoke<any>("model_nostr_profile_relay_set", { opts }); + if (response === true) return { pass: true }; else if (typeof response === "string") return err_msg(response); return err_msg("*-result"); } catch (e) { - return this.handle_errors("model_set_nostr_profile_relay", opts, e); + return this.handle_errors("model_nostr_profile_relay_set", opts, e); }; - }; + }; - public async unset_nostr_profile_relay(opts: { nostr_profile: INostrProfileQueryBindValues; nostr_relay: INostrRelayQueryBindValues; }): Promise<ResultPass | ErrorMessage<IClientDbMessage>> { + public async nostr_profile_relay_unset(opts: INostrProfileRelayRelation): Promise<INostrProfileRelayRelationResolve<IClientDatabaseMessage>> { try { - const response = await invoke<any>("model_unset_nostr_profile_relay", { opts }); - if (response === null) return { pass: true }; + const response = await invoke<any>("model_nostr_profile_relay_unset", { opts }); + if (response === true) return { pass: true }; else if (typeof response === "string") return err_msg(response); return err_msg("*-result"); } catch (e) { - return this.handle_errors("model_unset_nostr_profile_relay", opts, e); + return this.handle_errors("model_nostr_profile_relay_unset", opts, e); }; }; } \ No newline at end of file diff --git a/client/src/database/types.ts b/client/src/database/types.ts @@ -1,25 +1,27 @@ -import { type ILocationGcsAdd, type ILocationGcsAddResolve, type ILocationGcsGet, type ILocationGcsGetResolve, type ILocationGcsDelete, type ILocationGcsDeleteResolve, type ILocationGcsUpdate, type ILocationGcsUpdateResolve, type ITradeProductAdd, type ITradeProductAddResolve, type ITradeProductGet, type ITradeProductGetResolve, type ITradeProductDelete, type ITradeProductDeleteResolve, type ITradeProductUpdate, type ITradeProductUpdateResolve, type INostrProfileAdd, type INostrProfileAddResolve, type INostrProfileGet, type INostrProfileGetResolve, type INostrProfileDelete, type INostrProfileDeleteResolve, type INostrProfileUpdate, type INostrProfileUpdateResolve, type INostrRelayAdd, type INostrRelayAddResolve, type INostrRelayGet, type INostrRelayGetResolve, type INostrRelayDelete, type INostrRelayDeleteResolve, type INostrRelayUpdate, type INostrRelayUpdateResolve } from "@radroots/models"; +import type { ILocationGcsAdd, ILocationGcsAddResolve, ILocationGcsDelete, ILocationGcsDeleteResolve, ILocationGcsGet, ILocationGcsGetResolve, ILocationGcsUpdate, ILocationGcsUpdateResolve, INostrProfileAdd, INostrProfileAddResolve, INostrProfileDelete, INostrProfileDeleteResolve, INostrProfileGet, INostrProfileGetResolve, INostrProfileRelayRelation, INostrProfileRelayRelationResolve, INostrProfileUpdate, INostrProfileUpdateResolve, INostrRelayAdd, INostrRelayAddResolve, INostrRelayDelete, INostrRelayDeleteResolve, INostrRelayGet, INostrRelayGetResolve, INostrRelayUpdate, INostrRelayUpdateResolve, ITradeProductAdd, ITradeProductAddResolve, ITradeProductDelete, ITradeProductDeleteResolve, ITradeProductGet, ITradeProductGetResolve, ITradeProductUpdate, ITradeProductUpdateResolve } from "@radroots/models"; -export type IClientDbMessage = +export type IClientDatabaseMessage = | string | "*-fields" | "*-result"; -export type IClientDb = { - location_gcs_add(opts: ILocationGcsAdd): Promise<ILocationGcsAddResolve<IClientDbMessage>>; - location_gcs_get(opts: ILocationGcsGet): Promise<ILocationGcsGetResolve<IClientDbMessage>>; - location_gcs_delete(opts: ILocationGcsDelete): Promise<ILocationGcsDeleteResolve<IClientDbMessage>>; - location_gcs_update(opts: ILocationGcsUpdate): Promise<ILocationGcsUpdateResolve<IClientDbMessage>>; - trade_product_add(opts: ITradeProductAdd): Promise<ITradeProductAddResolve<IClientDbMessage>>; - trade_product_get(opts: ITradeProductGet): Promise<ITradeProductGetResolve<IClientDbMessage>>; - trade_product_delete(opts: ITradeProductDelete): Promise<ITradeProductDeleteResolve<IClientDbMessage>>; - trade_product_update(opts: ITradeProductUpdate): Promise<ITradeProductUpdateResolve<IClientDbMessage>>; - nostr_profile_add(opts: INostrProfileAdd): Promise<INostrProfileAddResolve<IClientDbMessage>>; - nostr_profile_get(opts: INostrProfileGet): Promise<INostrProfileGetResolve<IClientDbMessage>>; - nostr_profile_delete(opts: INostrProfileDelete): Promise<INostrProfileDeleteResolve<IClientDbMessage>>; - nostr_profile_update(opts: INostrProfileUpdate): Promise<INostrProfileUpdateResolve<IClientDbMessage>>; - nostr_relay_add(opts: INostrRelayAdd): Promise<INostrRelayAddResolve<IClientDbMessage>>; - nostr_relay_get(opts: INostrRelayGet): Promise<INostrRelayGetResolve<IClientDbMessage>>; - nostr_relay_delete(opts: INostrRelayDelete): Promise<INostrRelayDeleteResolve<IClientDbMessage>>; - nostr_relay_update(opts: INostrRelayUpdate): Promise<INostrRelayUpdateResolve<IClientDbMessage>>; +export type IClientDatabase = { + location_gcs_add(opts: ILocationGcsAdd): Promise<ILocationGcsAddResolve<IClientDatabaseMessage>>; + location_gcs_get(opts: ILocationGcsGet): Promise<ILocationGcsGetResolve<IClientDatabaseMessage>>; + location_gcs_delete(opts: ILocationGcsDelete): Promise<ILocationGcsDeleteResolve<IClientDatabaseMessage>>; + location_gcs_update(opts: ILocationGcsUpdate): Promise<ILocationGcsUpdateResolve<IClientDatabaseMessage>>; + trade_product_add(opts: ITradeProductAdd): Promise<ITradeProductAddResolve<IClientDatabaseMessage>>; + trade_product_get(opts: ITradeProductGet): Promise<ITradeProductGetResolve<IClientDatabaseMessage>>; + trade_product_delete(opts: ITradeProductDelete): Promise<ITradeProductDeleteResolve<IClientDatabaseMessage>>; + trade_product_update(opts: ITradeProductUpdate): Promise<ITradeProductUpdateResolve<IClientDatabaseMessage>>; + nostr_profile_add(opts: INostrProfileAdd): Promise<INostrProfileAddResolve<IClientDatabaseMessage>>; + nostr_profile_get(opts: INostrProfileGet): Promise<INostrProfileGetResolve<IClientDatabaseMessage>>; + nostr_profile_delete(opts: INostrProfileDelete): Promise<INostrProfileDeleteResolve<IClientDatabaseMessage>>; + nostr_profile_update(opts: INostrProfileUpdate): Promise<INostrProfileUpdateResolve<IClientDatabaseMessage>>; + nostr_relay_add(opts: INostrRelayAdd): Promise<INostrRelayAddResolve<IClientDatabaseMessage>>; + nostr_relay_get(opts: INostrRelayGet): Promise<INostrRelayGetResolve<IClientDatabaseMessage>>; + nostr_relay_delete(opts: INostrRelayDelete): Promise<INostrRelayDeleteResolve<IClientDatabaseMessage>>; + nostr_relay_update(opts: INostrRelayUpdate): Promise<INostrRelayUpdateResolve<IClientDatabaseMessage>>; + nostr_profile_relay_set(opts: INostrProfileRelayRelation): Promise<INostrProfileRelayRelationResolve<IClientDatabaseMessage>>; + nostr_profile_relay_unset(opts: INostrProfileRelayRelation): Promise<INostrProfileRelayRelationResolve<IClientDatabaseMessage>>; }; \ No newline at end of file diff --git a/client/src/device/tauri.ts b/client/src/device/tauri.ts @@ -1,3 +1,4 @@ +import { app } from '@tauri-apps/api'; import type { IClientDevice, IClientDeviceMetadata } from "./types"; export class TauriClientDevice implements IClientDevice { @@ -10,4 +11,16 @@ export class TauriClientDevice implements IClientDevice { public get metadata() { return this._metadata; } + + public async get_name(): Promise<string> { + return await app.getName(); + } + + public async get_version(): Promise<string> { + return await app.getVersion(); + } + + //public async set_theme(theme: `light` | `dark`) { + // return app.setTheme(theme); + //} } diff --git a/client/src/device/types.ts b/client/src/device/types.ts @@ -6,4 +6,6 @@ export type IClientDeviceMetadata = { export type IClientDevice = { init(opts: IClientDeviceMetadata): Promise<void>; + get_name(): Promise<string>; + get_version(): Promise<string>; }; \ No newline at end of file diff --git a/client/src/fs/tauri.ts b/client/src/fs/tauri.ts @@ -1,5 +1,5 @@ -import { BaseDirectory, exists, open, readFile } from '@tauri-apps/plugin-fs'; -import type { IClientFs, IClientFsOpenResult } from "./types"; +import { BaseDirectory, exists, open, readFile, stat } from '@tauri-apps/plugin-fs'; +import type { IClientFs, IClientFsFileInfo, IClientFsOpenResult } from "./types"; export class TauriClientFs implements IClientFs { public async exists(path: string): Promise<boolean> { @@ -31,4 +31,14 @@ export class TauriClientFs implements IClientFs { return undefined; }; } + + public async info(path: string): Promise<IClientFsFileInfo | undefined> { + try { + const res = await stat(path, { baseDir: BaseDirectory.AppData }); + return res; + } catch (e) { + console.log(`e open`, e) + return undefined; + }; + } } diff --git a/client/src/fs/types.ts b/client/src/fs/types.ts @@ -1,9 +1,12 @@ -import type { FileHandle } from "@tauri-apps/plugin-fs"; +import type { FileHandle, FileInfo } from "@tauri-apps/plugin-fs"; export type IClientFsOpenResult = FileHandle; +export type IClientFsFileInfo = FileInfo; + export type IClientFs = { exists(path: string): Promise<boolean>; open(path: string): Promise<IClientFsOpenResult | undefined>; read_bin(path: string): Promise<Uint8Array | undefined>; + info(path: string): Promise<IClientFsFileInfo | undefined>; }; \ No newline at end of file diff --git a/client/src/http/tauri.ts b/client/src/http/tauri.ts @@ -1,7 +1,7 @@ import { err_msg, type ErrorMessage, type FieldRecord } from '@radroots/utils'; import { type ClientOptions, fetch } from '@tauri-apps/plugin-http'; import type { IClientDeviceMetadata } from '../device/types'; -import type { IClientHttp, IClientHttpOpts, IClientHttpResponse } from './types'; +import type { IClientHttp, IClientHttpImageResponse, IClientHttpOpts, IClientHttpResponse } from './types'; const parse_headers = (headers: Headers): FieldRecord => { const acc: FieldRecord = {}; @@ -53,15 +53,26 @@ export class TauriClientHttp implements IClientHttp { headers, } if (opts.data) options.body = to_bodyinit(opts.data); + if (opts.data_bin) options.body = opts.data_bin; if (opts.connect_timeout) options.connectTimeout = opts.connect_timeout; const response = await fetch(url, options); switch (response.ok) { case true: { - const response_json = await response.json(); + let data: any = null; + try { + const res_json = await response.json(); + data = typeof res_json === `string` ? JSON.parse(res_json) : res_json; + } catch { } + if (!data) { + try { + const res_text = await response.text(); + data = res_text; + } catch { } + } return { status: response.status, url: response.url, - data: typeof response_json === `string` ? JSON.parse(response_json) : response_json, + data, headers: parse_headers(response.headers) }; } @@ -79,4 +90,39 @@ export class TauriClientHttp implements IClientHttp { return err_msg(String(e)); }; } + + public async fetch_image(url: string): Promise<IClientHttpImageResponse | ErrorMessage<string>> { + try { + const headers: FieldRecord = { + ...this._headers, + }; + const options: RequestInit & ClientOptions = { + method: `GET`, + headers, + } + const response = await fetch(url, options); + switch (response.ok) { + case true: { + const blob = await response.blob(); + + return { + status: response.status, + url: response.url, + blob, + headers: parse_headers(response.headers) + }; + } + case false: { + return { + status: response.status, + url: response.url, + headers: parse_headers(response.headers) + }; + } + } + } catch (e) { + console.log(`e fetch_image`, e) + return err_msg(String(e)); + }; + } } \ No newline at end of file diff --git a/client/src/http/types.ts b/client/src/http/types.ts @@ -2,16 +2,24 @@ import type { ErrorMessage, FieldRecord } from "@radroots/utils"; export type IClientHttpOpts = { url: string; - method?: `get` | `post`; + method?: `get` | `post` | `put`; params?: { [key: string]: string | string[]; }; data?: Record<string, string | number | boolean>; + data_bin?: Uint8Array; authorization?: string; headers?: FieldRecord; connect_timeout?: number; }; +export type IClientHttpImageResponse = { + status: number; + blob?: Blob; + headers: FieldRecord; + url: string; +}; + export type IClientHttpResponse = { status: number; data: any; @@ -21,4 +29,5 @@ export type IClientHttpResponse = { export type IClientHttp = { fetch(opts: IClientHttpOpts): Promise<IClientHttpResponse | ErrorMessage<string>>; + fetch_image(url: string): Promise<IClientHttpImageResponse | ErrorMessage<string>>; }; \ No newline at end of file