commit 936b2e921ed7c9e61db2272f82eb95ccac0cc212
parent 85661545f97b8a89c37ac23708f59dcfd84276c4
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Fri, 23 Aug 2024 13:33:39 +0000
client: add capacitor http api
Diffstat:
4 files changed, 75 insertions(+), 1 deletion(-)
diff --git a/client/package.json b/client/package.json
@@ -12,6 +12,7 @@
"@capacitor/core": "^6.1.2",
"@capacitor/device": "^6.0.0",
"@capacitor/dialog": "^6.0.0",
+ "@capacitor/filesystem": "^6.0.0",
"@capacitor/geolocation": "^6.0.0",
"@capacitor/haptics": "^6.0.0",
"@capacitor/network": "^6.0.0",
diff --git a/client/src/capacitor/http.ts b/client/src/capacitor/http.ts
@@ -0,0 +1,36 @@
+import { CapacitorHttp } from '@capacitor/core';
+import type { IClientHttp, IClientHttpOpts, IClientHttpResponse } from '../types';
+
+export class CapacitorClientHttp implements IClientHttp {
+ public async get(opts: IClientHttpOpts): Promise<IClientHttpResponse | undefined> {
+ try {
+ const { url: url, method: method, params: params, data: data, headers: headers, read_timeout: readTimeout, connect_timeout: connectTimeout } = opts;
+ const res: IClientHttpResponse = await CapacitorHttp.get({
+ url,
+ method,
+ params,
+ data,
+ headers,
+ readTimeout,
+ connectTimeout,
+ });
+ return res;
+ } catch (e) { };
+ }
+
+ public async post(opts: IClientHttpOpts): Promise<IClientHttpResponse | undefined> {
+ try {
+ const { url: url, method: method, params: params, data: data, headers: headers, read_timeout: readTimeout, connect_timeout: connectTimeout } = opts;
+ const response: IClientHttpResponse = await CapacitorHttp.post({
+ url,
+ method,
+ params,
+ data,
+ headers,
+ readTimeout,
+ connectTimeout,
+ });
+ return response;
+ } catch (e) { };
+ }
+}
+\ No newline at end of file
diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts
@@ -1,6 +1,6 @@
import { Capacitor } from "@capacitor/core";
-import type { IClient, IClientBrowser, IClientDatePicker, IClientDevice, IClientDialog, IClientGeolocation, IClientHaptics, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare, IClientWifi } from "../types";
+import type { IClient, IClientBrowser, IClientDatePicker, IClientDevice, IClientDialog, IClientGeolocation, IClientHaptics, IClientHttp, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare, IClientWifi } from "../types";
import { parse_platform } from "../utils";
import { CapacitorClientBrowser } from "./browser";
import { CapacitorClientDatePicker } from "./date-picker";
@@ -8,6 +8,7 @@ import { CapacitorClientDevice } from "./device";
import { CapacitorClientDialog } from "./dialog";
import { CapacitorClientGeolocation } from "./geolocation";
import { CapacitorClientHaptics } from "./haptics";
+import { CapacitorClientHttp } from "./http";
import { CapacitorClientKeystore } from "./keystore";
import { CapacitorClientNetwork } from "./network";
import { CapacitorClientPreferences } from "./preferences";
@@ -27,6 +28,7 @@ export class ClientCapacitor implements IClient {
private _browser: IClientBrowser = new CapacitorClientBrowser();
private _dates: IClientDatePicker = new CapacitorClientDatePicker();
private _geo: IClientGeolocation = new CapacitorClientGeolocation();
+ private _http: IClientHttp = new CapacitorClientHttp();
public get platform() {
return this._platform;
@@ -75,4 +77,8 @@ export class ClientCapacitor implements IClient {
public get geo() {
return this._geo;
}
+
+ public get http() {
+ return this._http;
+ }
};
\ No newline at end of file
diff --git a/client/src/types.ts b/client/src/types.ts
@@ -14,6 +14,7 @@ export type IClient = {
browser: IClientBrowser;
dates: IClientDatePicker;
geo: IClientGeolocation;
+ http: IClientHttp;
};
export type IClientPlatform = `androiď` | `ios` | `web`;
@@ -124,3 +125,31 @@ export type IClientGeolocationPosition = {
export type IClientGeolocation = {
current(): Promise<IClientGeolocationPosition | undefined>;
};
+
+export type IClientHttpOpts = {
+ url: string;
+ method?: string;
+ params?: {
+ [key: string]: string | string[];
+ };
+ data?: any;
+ headers?: {
+ [key: string]: string;
+ };
+ read_timeout?: number;
+ connect_timeout?: number;
+};
+
+export type IClientHttpResponse = {
+ data: any;
+ status: number;
+ headers: {
+ [key: string]: string;
+ };
+ url: string;
+};
+
+export type IClientHttp = {
+ get(opts: IClientHttpOpts): Promise<IClientHttpResponse | undefined>;
+ post(opts: IClientHttpOpts): Promise<IClientHttpResponse | undefined>;
+};
+\ No newline at end of file