Skip to content

Commit

Permalink
Add API
Browse files Browse the repository at this point in the history
  • Loading branch information
lylwx committed Nov 8, 2023
1 parent b6a3c8b commit bcfe4d1
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Wox.UI.Tauri/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@mui/styled-engine-sc": "6.0.0-alpha.4",
"@tauri-apps/api": "^1.5.0",
"@uidotdev/usehooks": "^2.4.1",
"axios": "^1.6.0",
"lodash": "^4.17.21",
"mousetrap": "^1.6.5",
"pinyin-pro": "^3.17.0",
Expand All @@ -33,7 +34,9 @@
"react-markdown": "^9.0.0",
"react-router": "^6.16.0",
"react-router-dom": "^6.16.0",
"store2": "^2.14.2",
"styled-components": "^6.0.8",
"swr": "^2.2.4",
"usehooks-ts": "^2.9.1",
"uuid": "^9.0.1"
},
Expand Down
96 changes: 96 additions & 0 deletions Wox.UI.Tauri/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Wox.UI.Tauri/src/WoxEntrance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import "./assets/index.css"
import { WoxMessageHelper } from "./utils/WoxMessageHelper.ts"
import React from "react"
import WoxLauncher from "./components/WoxLauncher.tsx"
import { WoxTauriHelper } from "./utils/WoxTauriHelper.ts"
import { WoxThemeHelper } from "./utils/WoxThemeHelper.ts"
import store from "store2"
import { WoxTauriHelper } from "./utils/WoxTauriHelper.ts"

WoxTauriHelper.getInstance().invoke("get_server_port").then((serverPort) => {
store.set("serverPort", serverPort as string)
WoxMessageHelper.getInstance().initialize(serverPort as string)
}).catch(_ => {
WoxMessageHelper.getInstance().initialize("34987")
Expand Down
7 changes: 7 additions & 0 deletions Wox.UI.Tauri/src/api/WoxAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { WoxMessageHelper } from "../utils/WoxMessageHelper.ts"
import WoxRequest from "../utils/WoxRequest.ts"
import { Theme } from "../entity/Theme.typings"

export async function getTheme() {
return WoxRequest.post<Theme>(`http://localhost:${WoxMessageHelper.getInstance().getPort()}/theme`)
}
10 changes: 5 additions & 5 deletions Wox.UI.Tauri/src/components/WoxLauncher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ const Style = styled.div`
.wox-result-border {
border-top: 1px solid #dedede !important;
}
body, html {
background-color: ${WoxThemeHelper.getInstance().getTheme().BackgroundColor};
}
//
// body, html {
// background-color: ${WoxThemeHelper.getInstance().getTheme().BackgroundColor};
// }
`
6 changes: 3 additions & 3 deletions Wox.UI.Tauri/src/components/WoxQueryResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,9 @@ const Style = styled.div`
line-height: 15px;
}
ul li.active {
background-color: ${WoxThemeHelper.getInstance().getTheme().ResultActiveBackgroundColor};
}
// ul li.active {
// background-color: ${WoxThemeHelper.getInstance().getTheme().ResultActiveBackgroundColor};
// }
.wox-query-result-preview {
position: relative;
Expand Down
5 changes: 5 additions & 0 deletions Wox.UI.Tauri/src/utils/WoxMessageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,9 @@ export class WoxMessageHelper {
}
}

public getPort() {
return this.port
}


}
87 changes: 87 additions & 0 deletions Wox.UI.Tauri/src/utils/WoxRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"
import axios from "axios"

type Result<T> = {
code: number;
message: string;
result: T;
};

export class Request {
instance: AxiosInstance
baseConfig: AxiosRequestConfig = { timeout: 60000 }

constructor(config: AxiosRequestConfig) {
this.instance = axios.create(Object.assign(this.baseConfig, config))

this.instance.interceptors.response.use(
(res: AxiosResponse) => {
return res.data
},
(err: any) => {
switch (err.response.status) {
case 400:
break
case 401:
break
case 403:
break
case 404:
break
case 408:
break
case 500:
break
case 501:
break
case 502:
break
case 503:
break
case 504:
break
case 505:
break
default:
}
return Promise.reject(err.response.data)
}
)
}

public request(config: AxiosRequestConfig): Promise<AxiosResponse> {
return this.instance.request(config)
}

public get<T = any>(
url: string,
config?: AxiosRequestConfig
): Promise<AxiosResponse<Result<T>>> {
return this.instance.get(url, config)
}

public post<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<AxiosResponse<Result<T>>> {
return this.instance.post(url, data, config)
}

public put<T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig
): Promise<AxiosResponse<Result<T>>> {
return this.instance.put(url, data, config)
}

public delete<T = any>(
url: string,
config?: AxiosRequestConfig
): Promise<AxiosResponse<Result<T>>> {
return this.instance.delete(url, config)
}
}

export default new Request({})

0 comments on commit bcfe4d1

Please sign in to comment.