#186 achieve tauri feature parity

This commit is contained in:
neil 2023-02-07 15:56:00 +08:00
parent 957cba2a67
commit 39ad639e56
5 changed files with 112 additions and 5 deletions

View file

@ -0,0 +1,105 @@
/**
* this is the main api integration, anything added here
* should be mock replicated in ./mock.ts
* why? to make it easier to verify features without us always
* going through
* the build->download->install->test loop
* thus saving us so much time
*
* primary concerns here are any method that does the following:
* - connect to remote api(api.tea.xyz) and returns a data
* - connect to a local platform api and returns a data
*/
import axios from 'axios';
import type { Package, Review, AirtablePost, Bottle } from '@tea/ui/types';
import type { GUIPackage, Course, Category, DeviceAuth } from '../types';
import * as mock from './mock';
import { PackageStates } from '../types';
import { getInstalledPackages } from '$libs/teaDir';
import { installPackageCommand } from '$libs/cli';
import { get as apiGet } from '$libs/v1Client';
export async function getPackages(): Promise<GUIPackage[]> {
const [packages, installedPackages] = await Promise.all([
apiGet<Package[]>('packages'),
getInstalledPackages()
]);
return (packages || []).map((pkg) => {
const found = installedPackages.find((p) => p.full_name === pkg.full_name);
return {
...pkg,
state: found ? PackageStates.INSTALLED : PackageStates.AVAILABLE,
installed_version: found ? found.version : ''
};
});
}
export async function getFeaturedPackages(): Promise<Package[]> {
const packages = await mock.getFeaturedPackages();
return packages;
}
export async function getPackageReviews(full_name: string): Promise<Review[]> {
console.log(`getting reviews for ${full_name}`);
const reviews: Review[] = await apiGet<Review[]>(
`packages/${full_name.replaceAll('/', ':')}/reviews`
);
return reviews;
}
export async function installPackage(full_name: string) {
try {
await installPackageCommand(full_name);
} catch (error) {
console.error(error);
}
}
export async function getFeaturedCourses(): Promise<Course[]> {
const posts = await apiGet<AirtablePost[]>('posts', { tag: 'featured_course' });
return posts.map((post) => {
return {
title: post.title,
sub_title: post.sub_title,
banner_image_url: post.thumb_image_url,
link: post.link
} as Course;
});
}
export async function getTopPackages(): Promise<GUIPackage[]> {
const packages = await mock.getTopPackages();
return packages;
}
export async function getAllPosts(tag?: string): Promise<AirtablePost[]> {
// add filter here someday: tag = news | course
const posts = await apiGet<AirtablePost[]>('posts', tag ? { tag } : {});
return posts;
}
export async function getCategorizedPackages(): Promise<Category[]> {
const categories = await apiGet<Category[]>('/packages/categorized');
return categories;
}
export async function getDeviceAuth(deviceId: string): Promise<DeviceAuth> {
const data = await apiGet<DeviceAuth>(`/auth/device/${deviceId}`);
return data;
}
export async function getPackageBottles(packageName: string): Promise<Bottle[]> {
console.log('getting bottles for ', packageName);
const req = await axios.get(`https://app.tea.xyz/api/bottles/${packageName}`);
return req.data as Bottle[];
}
export async function registerDevice(): Promise<string> {
const { deviceId } = await apiGet<{ deviceId: string }>('/auth/registerDevice');
return deviceId;
}

View file

@ -11,7 +11,7 @@ type Dir = {
const { ipcRenderer } = window.require('electron');
export async function getInstalledPackages() {
const pkgs = await ipcRenderer.invoke('get-installed-packages');
return pkgs as { version: string; full_name: string };
return pkgs as { version: string; full_name: string }[];
}
const semverTest =

View file

@ -5,7 +5,7 @@ import { getSession } from '$libs/stores/auth';
export const baseUrl = 'https://api.tea.xyz/v1';
export async function get<T>(urlPath: string, query?: { [key: string]: string }) {
export async function get<T>(urlPath: string, params?: { [key: string]: string }) {
console.log(`GET /v1/${urlPath}`);
const [session] = await Promise.all([getSession()]);
@ -18,7 +18,9 @@ export async function get<T>(urlPath: string, query?: { [key: string]: string })
const req = await axios.request({
method: 'GET',
baseURL: 'https://api.tea.xyz',
url: ['v1', ...urlPath.split('/')].filter((p) => p).join('/')
url: ['v1', ...urlPath.split('/')].filter((p) => p).join('/'),
headers,
params
});
return req.data as T;

View file

@ -14,7 +14,7 @@
"paths": {
"$appcss": ["src/app.css"],
"$libs/*": ["src/libs/*"],
"@api": ["src/libs/api/mock.ts"],
"@api": ["src/libs/api/electron.ts"],
"$components/*": ["src/components/*"],
"@tea/ui/*": ["../ui/src/*"]
}

View file

@ -12,7 +12,7 @@ const config: UserConfig = {
// this dynamic-ish static importing is followed by the svelte build
// but for vscode editing intellisense tsconfig.json is being used
// TODO: replace it with correct api
'@api': path.resolve('src/libs/api/mock.ts'),
'@api': path.resolve('src/libs/api/electron.ts'),
$components: path.resolve('./src/components'),
$libs: path.resolve('./src/libs'),
$appcss: path.resolve('./src/app.css')