commit 6ddb8f06db93f142d7b3528bc6a130e1256a5f17
parent 9e3d8afcd403f67b626e35782008e57ab142662a
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Mon, 16 Sep 2024 13:10:29 +0000
client: edit iclienthttp api
Diffstat:
2 files changed, 17 insertions(+), 26 deletions(-)
diff --git a/client/src/capacitor/http.ts b/client/src/capacitor/http.ts
@@ -1,11 +1,11 @@
-import { CapacitorHttp } from '@capacitor/core';
+import { CapacitorHttp, type HttpOptions } from '@capacitor/core';
import type { IClientHttp, IClientHttpOpts, IClientHttpResponse } from '../types';
export class CapacitorClientHttp implements IClientHttp {
- public async get(opts: IClientHttpOpts): Promise<IClientHttpResponse | undefined> {
+ public async fetch(opts: IClientHttpOpts): Promise<IClientHttpResponse | string> {
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({
+ const { url, method, params, data, headers, read_timeout: readTimeout, connect_timeout: connectTimeout } = opts;
+ const options: HttpOptions = {
url,
method,
params,
@@ -13,24 +13,16 @@ export class CapacitorClientHttp implements IClientHttp {
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) { };
+ };
+ if (method === `get`) {
+ const res: IClientHttpResponse = await CapacitorHttp.get(options);
+ return res;
+ } else {
+ const res: IClientHttpResponse = await CapacitorHttp.post(options);
+ return res;
+ }
+ } catch (e) {
+ return String(e);
+ };
}
}
\ No newline at end of file
diff --git a/client/src/types.ts b/client/src/types.ts
@@ -153,7 +153,7 @@ export type IClientGeolocation = {
export type IClientHttpOpts = {
url: string;
- method?: string;
+ method: `get` | `post`;
params?: {
[key: string]: string | string[];
};
@@ -175,8 +175,7 @@ export type IClientHttpResponse = {
};
export type IClientHttp = {
- get(opts: IClientHttpOpts): Promise<IClientHttpResponse | undefined>;
- post(opts: IClientHttpOpts): Promise<IClientHttpResponse | undefined>;
+ fetch(opts: IClientHttpOpts): Promise<IClientHttpResponse | string>;
};
export type IClientWindow = {