jan/web/services/restService.ts
Louis 5250061c11
feat: Jan Server, API and decoupled clients (#948)
* chore: expose fs apis

* chore: correct electron import path

* update download api

Signed-off-by: James <james@jan.ai>

* update chat_completion

Signed-off-by: James <james@jan.ai>

* fix electron import

Signed-off-by: James <james@jan.ai>

* feat: adding API support at 1337 (#991)

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>

* feat: Add /chat/completion api and handler

* chore: add todo for modelList

* chore: read engine.json for openai chat_completion (#1030)

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>

* refactor: move routes to shared node module

* refactor: exported modules from core with types (#1172)

* refactor: exported modules from core with types

* fix: fix file reading args

* refactor: fileManager handles

* fix: app issues with server refactoring

* refactor: shared server module (#1210)

* chore: resolve main

* chore: update makefile

---------

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
Co-authored-by: NamH <NamNh0122@gmail.com>
Co-authored-by: hiro <vuonghoainam.work@gmail.com>
2023-12-28 17:57:39 +07:00

60 lines
1.7 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import {
AppRoute,
DownloadRoute,
ExtensionRoute,
FileSystemRoute,
} from '@janhq/core'
import { safeJsonParse } from '@/utils/json'
// Function to open an external URL in a new browser window
export function openExternalUrl(url: string) {
window?.open(url, '_blank')
}
// Async function to get the application version
export async function appVersion() {
return Promise.resolve(VERSION)
}
// Define API routes based on different route types
export const APIRoutes = [
...Object.values(AppRoute).map((r) => ({ path: 'app', route: r })),
...Object.values(DownloadRoute).map((r) => ({ path: `download`, route: r })),
...Object.values(ExtensionRoute).map((r) => ({
path: `extension`,
route: r,
})),
...Object.values(FileSystemRoute).map((r) => ({ path: `fs`, route: r })),
]
// Define the restAPI object with methods for each API route
export const restAPI = {
...Object.values(APIRoutes).reduce((acc, proxy) => {
return {
...acc,
[proxy.route]: (...args: any) => {
// For each route, define a function that sends a request to the API
return fetch(`${API_BASE_URL}/v1/${proxy.path}/${proxy.route}`, {
method: 'POST',
body: JSON.stringify(args),
headers: { contentType: 'application/json' },
}).then(async (res) => {
try {
if (proxy.path === 'fs') {
const text = await res.text()
return safeJsonParse(text) ?? text
}
return await res.json()
} catch (err) {
console.debug('Op: ', proxy, args, err)
}
})
},
}
}, {}),
openExternalUrl,
appVersion,
}