Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
#186 achieve tauri feature parity
Browse files Browse the repository at this point in the history
  • Loading branch information
neil authored and neil committed Feb 7, 2023
1 parent 957cba2 commit 39ad639
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 5 deletions.
105 changes: 105 additions & 0 deletions modules/desktop/src/libs/api/electron.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion modules/desktop/src/libs/teaDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
6 changes: 4 additions & 2 deletions modules/desktop/src/libs/v1Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion modules/desktop/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"]
}
Expand Down
2 changes: 1 addition & 1 deletion modules/desktop/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 39ad639

Please sign in to comment.