Skip to content

Commit

Permalink
Merge pull request #226 from getAlby/build-specific-js
Browse files Browse the repository at this point in the history
build specific api files
  • Loading branch information
rolznz authored Jan 22, 2024
2 parents 121c6cf + c2c5f05 commit 6c66bda
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 66 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ frontend/node_modules
frontend/wailsjs
package.json.md5

build
build

frontend/src/utils/request.ts
10 changes: 6 additions & 4 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"dev:wails": "VITE_NWC_APP_TYPE=WAILS vite",
"build": "tsc && vite build",
"build:wails": "tsc && VITE_NWC_APP_TYPE=WAILS vite build",
"dev": "yarn prepare:http && vite",
"dev:wails": "yarn prepare:wails && vite",
"build": "yarn prepare:http && tsc && vite build",
"prepare:wails": "cp ./platform_specific/wails/src/utils/request.ts src/utils/request.ts",
"prepare:http": "cp ./platform_specific/http/src/utils/request.ts src/utils/request.ts",
"build:wails": "yarn prepare:wails && tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
Expand Down
28 changes: 28 additions & 0 deletions frontend/platform_specific/http/src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ErrorResponse } from "src/types";

export const request = async <T>(
...args: Parameters<typeof fetch>
): Promise<T | undefined> => {
try {
const fetchResponse = await fetch(...args);

let body: T | undefined;
try {
body = await fetchResponse.json();
} catch (error) {
console.error(error);
}

if (!fetchResponse.ok) {
throw new Error(
fetchResponse.status +
" " +
((body as ErrorResponse)?.message || "Unknown error")
);
}
return body;
} catch (error) {
console.error("Failed to fetch", error);
throw error;
}
};
23 changes: 23 additions & 0 deletions frontend/platform_specific/wails/src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { WailsRequestRouter } from "wailsjs/go/main/WailsApp";

export const request = async <T>(
...args: Parameters<typeof fetch>
): Promise<T | undefined> => {
try {
const res = await WailsRequestRouter(
args[0].toString(),
args[1]?.method || "GET",
args[1]?.body?.toString() || ""
);

console.log("Wails request", ...args, res);
if (res.error) {
throw new Error(res.error);
}

return res.body;
} catch (error) {
console.error("Failed to fetch", error);
throw error;
}
};
3 changes: 2 additions & 1 deletion frontend/src/screens/Setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Loading from "src/components/Loading";
import { useCSRF } from "src/hooks/useCSRF";
import { useInfo } from "src/hooks/useInfo";
import { BackendType } from "src/types";
import { request, handleRequestError } from "src/utils/request";
import { handleRequestError } from "src/utils/handleRequestError";
import { request } from "src/utils/request"; // build the project for this to appear

export function Setup() {
const [backendType, setBackendType] = React.useState<BackendType>("BREEZ");
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/screens/apps/NewApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { LightningIcon } from "src/components/icons/LightningIcon";
import { InvoiceIcon } from "src/components/icons/InvoiceIcon";
import { SearchIcon } from "src/components/icons/SearchIcon";
import { TransactionsIcon } from "src/components/icons/TransactionsIcon";
import { handleRequestError, request } from "src/utils/request";
import { request } from "src/utils/request"; // build the project for this to appear
import { handleRequestError } from "src/utils/handleRequestError";

const NewApp = () => {
const { data: csrf } = useCSRF();
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/screens/apps/ShowApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { useApp } from "src/hooks/useApp";
import { useCSRF } from "src/hooks/useCSRF";
import toast from "src/components/Toast";
import Loading from "src/components/Loading";
import { handleRequestError, request } from "src/utils/request";
import { request } from "src/utils/request"; // build the project for this to appear
import { handleRequestError } from "src/utils/handleRequestError";

function ShowApp() {
const { data: info } = useInfo();
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/utils/handleRequestError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import toast from "src/components/Toast";

export function handleRequestError(message: string, error: unknown) {
console.error(message, error);
toast.error(message + ": " + error);
}
58 changes: 0 additions & 58 deletions frontend/src/utils/request.ts

This file was deleted.

0 comments on commit 6c66bda

Please sign in to comment.