refactor(frontend): extract shared stores and components into packages
- Create @multica/fetch package for HTTP client and URL config - Migrate hub store and hub-init hook to @multica/store - Move HubSidebar component to @multica/ui for web/desktop reuse - Update web app imports to use shared packages - Remove counter store example and its component-example usage Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
46b7906272
commit
63861d03c6
15 changed files with 143 additions and 127 deletions
15
packages/fetch/src/config.ts
Normal file
15
packages/fetch/src/config.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
let consoleUrl = "http://localhost:4000"
|
||||
let gatewayUrl = "http://localhost:3000"
|
||||
|
||||
export function setConfig(config: { consoleUrl?: string; gatewayUrl?: string }) {
|
||||
if (config.consoleUrl) consoleUrl = config.consoleUrl
|
||||
if (config.gatewayUrl) gatewayUrl = config.gatewayUrl
|
||||
}
|
||||
|
||||
export function getConsoleUrl(): string {
|
||||
return consoleUrl
|
||||
}
|
||||
|
||||
export function getGatewayUrl(): string {
|
||||
return gatewayUrl
|
||||
}
|
||||
28
packages/fetch/src/http-client.ts
Normal file
28
packages/fetch/src/http-client.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { getConsoleUrl } from "./config"
|
||||
|
||||
export class HttpError extends Error {
|
||||
constructor(
|
||||
public status: number,
|
||||
public statusText: string,
|
||||
) {
|
||||
super(`HTTP ${status}: ${statusText}`)
|
||||
}
|
||||
}
|
||||
|
||||
async function request<T>(method: string, path: string, body?: unknown): Promise<T> {
|
||||
const res = await fetch(`${getConsoleUrl()}${path}`, {
|
||||
method,
|
||||
headers: body ? { "Content-Type": "application/json" } : undefined,
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
})
|
||||
if (!res.ok) throw new HttpError(res.status, res.statusText)
|
||||
return res.json()
|
||||
}
|
||||
|
||||
/** Console REST API */
|
||||
export const consoleApi = {
|
||||
get: <T>(path: string) => request<T>("GET", path),
|
||||
post: <T>(path: string, body?: unknown) => request<T>("POST", path, body),
|
||||
put: <T>(path: string, body: unknown) => request<T>("PUT", path, body),
|
||||
delete: <T>(path: string) => request<T>("DELETE", path),
|
||||
}
|
||||
2
packages/fetch/src/index.ts
Normal file
2
packages/fetch/src/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
export { setConfig, getConsoleUrl, getGatewayUrl } from "./config"
|
||||
export { consoleApi, HttpError } from "./http-client"
|
||||
Loading…
Add table
Add a link
Reference in a new issue