web_lib

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

commit 950c8c372cbb8b214929b4fe2e4f1b6e2e133238
parent a5a5ed5cae646dec0706fe8893de7afd7d7d581e
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Sat, 19 Oct 2024 10:26:42 +0000

client: adds client os and device apis, edit http, window apis

Diffstat:
Mclient/package.json | 1+
Aclient/src/device/tauri.ts | 13+++++++++++++
Aclient/src/device/types.ts | 10++++++++++
Mclient/src/http/tauri.ts | 33+++++++++++++++++++++++++++------
Mclient/src/http/types.ts | 11++++-------
Mclient/src/index.ts | 4++++
Aclient/src/os/tauri.ts | 28++++++++++++++++++++++++++++
Aclient/src/os/types.ts | 9+++++++++
Mclient/src/window/types.ts | 3---
9 files changed, 96 insertions(+), 16 deletions(-)

diff --git a/client/package.json b/client/package.json @@ -21,6 +21,7 @@ "@tauri-apps/plugin-haptics": "^2.0.0-rc", "@tauri-apps/plugin-http": "^2.0.0-rc", "@tauri-apps/plugin-notification": "^2.0.0-rc", + "@tauri-apps/plugin-os": "^2.0.0-rc", "@tauri-apps/plugin-store": "^2.0.0-rc", "nostr-tools": "^2.7.2" }, diff --git a/client/src/device/tauri.ts b/client/src/device/tauri.ts @@ -0,0 +1,13 @@ +import type { IClientDevice, IClientDeviceMetadata } from "./types"; + +export class TauriClientDevice implements IClientDevice { + private _metadata: IClientDeviceMetadata | undefined = undefined; + + public async init(opts: IClientDeviceMetadata): Promise<void> { + this._metadata = opts; + } + + public get metadata() { + return this._metadata; + } +} diff --git a/client/src/device/types.ts b/client/src/device/types.ts @@ -0,0 +1,9 @@ +export type IClientDeviceMetadata = { + version: string; + platform: string; + locale: string; +}; + +export type IClientDevice = { + init(opts: IClientDeviceMetadata): Promise<void>; +}; +\ No newline at end of file diff --git a/client/src/http/tauri.ts b/client/src/http/tauri.ts @@ -1,11 +1,12 @@ -import { err_msg, type ErrorMessage } from '@radroots/utils'; +import { err_msg, type ErrorMessage, type FieldRecord } from '@radroots/utils'; import { type ClientOptions, fetch } from '@tauri-apps/plugin-http'; +import { IClientDeviceMetadata } from '../device/types'; import type { IClientHttp, IClientHttpOpts, IClientHttpResponse } from './types'; -const parse_headers = (headers: Headers): Record<string, string> => { - const record: Record<string, string> = {}; - headers.forEach((value, key) => record[key] = value); - return record; +const parse_headers = (headers: Headers): FieldRecord => { + const acc: FieldRecord = {}; + headers.forEach((value, key) => acc[key] = value); + return acc; }; const to_bodyinit = (data: any): BodyInit => { @@ -23,15 +24,35 @@ const to_bodyinit = (data: any): BodyInit => { return JSON.stringify(data); } } + export class TauriClientHttp implements IClientHttp { + private _headers: FieldRecord; + + constructor() { + this._headers = {}; + } + + public async init(opts: IClientDeviceMetadata): Promise<void> { + this._headers = { + "X-Client-Version": opts.version, + "X-Client-Platform": opts.platform, + "X-Client-Locale": opts.locale + }; + } + public async fetch(opts: IClientHttpOpts): Promise<IClientHttpResponse | ErrorMessage<string>> { try { const { url } = opts; + const headers: FieldRecord = { + ...this._headers, + ...opts.headers, + }; + if (opts.authorization) headers['Authorization'] = `Bearer ${encodeURIComponent(opts.authorization)}`; const options: RequestInit & ClientOptions = { method: opts.method ? opts.method.toUpperCase() : `GET`, + headers, } if (opts.data) options.body = to_bodyinit(opts.data); - if (opts.headers) options.headers = opts.headers; if (opts.connect_timeout) options.connectTimeout = opts.connect_timeout; const response = await fetch(url, options); switch (response.ok) { diff --git a/client/src/http/types.ts b/client/src/http/types.ts @@ -1,4 +1,4 @@ -import type { ErrorMessage } from "@radroots/utils"; +import type { ErrorMessage, FieldRecord } from "@radroots/utils"; export type IClientHttpOpts = { url: string; @@ -7,18 +7,15 @@ export type IClientHttpOpts = { [key: string]: string | string[]; }; data?: Record<string, string | number | boolean>; - headers?: { - [key: string]: string; - }; + authorization?: string; + headers?: FieldRecord; connect_timeout?: number; }; export type IClientHttpResponse = { status: number; data: any; - headers: { - [key: string]: string; - }; + headers: FieldRecord; url: string; }; diff --git a/client/src/index.ts b/client/src/index.ts @@ -1,6 +1,8 @@ export * from "./database/tauri" export * from "./database/types" +export * from "./device/tauri" +export * from "./device/types" export * from "./dialog/tauri" export * from "./dialog/types" export * from "./geolocation/tauri" @@ -21,6 +23,8 @@ export * from "./nostr/lib" export * from "./nostr/types" export * from "./notification/tauri" export * from "./notification/types" +export * from "./os/tauri" +export * from "./os/types" export * from "./types" export * from "./utils" export * from "./window/tauri" diff --git a/client/src/os/tauri.ts b/client/src/os/tauri.ts @@ -0,0 +1,28 @@ +import { err_msg, ErrorMessage, ResultObj } from '@radroots/utils'; +import { arch, hostname, platform, version } from '@tauri-apps/plugin-os'; +import type { IClientOs } from "./types"; + +export class TauriClientOs implements IClientOs { + public version(): string { + return version(); + } + + public platform(): string { + return platform(); + } + + public arch(): string { + return arch(); + } + + public async hostname(): Promise<ResultObj<string> | ErrorMessage<string>> { + try { + const result = await hostname(); + if (!result) return err_msg(`*-result`); + return { result } + } catch (e) { + return err_msg(`*`); + }; + } + +} diff --git a/client/src/os/types.ts b/client/src/os/types.ts @@ -0,0 +1,8 @@ +import type { ErrorMessage, ResultObj } from "@radroots/utils"; + +export type IClientOs = { + version(): string; + platform(): string; + arch(): string; + hostname(): Promise<ResultObj<string> | ErrorMessage<string>> +}; +\ No newline at end of file diff --git a/client/src/window/types.ts b/client/src/window/types.ts @@ -1,7 +1,4 @@ export type IClientWindow = { splash_hide(): Promise<void>; splash_show(showDuration?: number): Promise<void>; - //status_hide(): Promise<void>; - //status_show(): Promise<void>; - //status_style(style: "light" | "dark"): Promise<void>; }; \ No newline at end of file