Update api

This commit is contained in:
jeffvli
2022-06-03 17:43:01 -07:00
parent eb646237d0
commit 8c01bb4a4f
8 changed files with 129 additions and 26 deletions

View File

@@ -0,0 +1,10 @@
import axios from 'renderer/lib/axios';
const getAlbum = async (albumId: number) => {
const { data } = await axios.get(`/albums/${albumId}`);
return data;
};
export const albumsApi = {
getAlbum,
};

View File

@@ -1,29 +1,29 @@
import axios from 'axios';
import axios from 'renderer/lib/axios';
import getServerUrl from 'renderer/utils/getServerUrl';
import { PingResponse, UserResponse } from './types';
const login = async (options: {
server: string;
username: string;
password: string;
}) => {
const { server, username, password } = options;
const url = getServerUrl(server);
const { data } = await axios.post(
`${url}/auth/login`,
{ username, password },
{ withCredentials: true }
const login = async (
server: string,
body: {
password: string;
username: string;
}
) => {
const serverUrl = getServerUrl(server);
const { data } = await axios.post<UserResponse>(
`${serverUrl}/auth/login`,
body,
{
withCredentials: true,
}
);
return data;
};
const ping = async (options: { server: string }) => {
const { server } = options;
const url = getServerUrl(server);
const { data } = await axios.get(`${url}/auth/ping`, {
const ping = async (server: string) => {
const serverUrl = getServerUrl(server);
const { data } = await axios.get<PingResponse>(`${serverUrl}/auth/ping`, {
timeout: 2000,
});

View File

@@ -1,2 +1,5 @@
export * from './authApi';
export * from './usersApi';
export * from './serversApi';
export * from './queries/useAlbum';
export * from './queries/queryKeys';

View File

@@ -0,0 +1,4 @@
export const queryKeys = {
album: (albumId: number) => ['album', albumId] as const,
servers: ['servers'] as const,
};

View File

@@ -0,0 +1,10 @@
import { useQuery } from 'react-query';
import { albumsApi } from '../albumsApi';
import { queryKeys } from './queryKeys';
export const useAlbum = (albumId: number) => {
return useQuery({
queryFn: () => albumsApi.getAlbum(albumId),
queryKey: queryKeys.album(albumId),
});
};

View File

@@ -0,0 +1,23 @@
import axios from 'renderer/lib/axios';
import { ServerResponse, ServersResponse } from './types';
const getServers = async () => {
const { data } = await axios.get<ServersResponse>('/servers');
return data;
};
const create = async (body: {
name: string;
remoteUserId: string;
token: string;
url: string;
username: string;
}) => {
const { data } = await axios.post<ServerResponse>('/servers', body);
return data;
};
export const serversApi = {
create,
getServers,
};

56
src/renderer/api/types.ts Normal file
View File

@@ -0,0 +1,56 @@
export type BaseResponse = {
data: any;
error?: any;
response: 'Success' | 'Error';
statusCode: number;
};
export type ServerResponse = Server;
export type ServersResponse = Server[];
export type UserResponse = User;
export type UsersResponse = User[];
export type PingResponse = Ping;
export interface Server {
createdAt: string;
id: number;
name: string;
remoteUserId: string;
serverFolder?: ServerFolder[];
serverType: string;
token: string;
updatedAt: string;
url: string;
username: string;
}
export interface ServerFolder {
createdAt: string;
enabled: boolean;
id: number;
isPublic: boolean;
name: string;
remoteId: string;
serverId: number;
updatedAt: string;
}
export interface User {
createdAt: string;
enabled: boolean;
id: number;
isAdmin: boolean;
password: string;
updatedAt: string;
username: string;
}
export interface Ping {
description: string;
name: string;
version: string;
}

View File

@@ -1,14 +1,11 @@
import axios from 'renderer/lib/axios';
import useStore from 'store/useStore';
const { serverUrl } = useStore.getState();
const get = async () => {
const { data } = await axios.get('/users', { baseURL: serverUrl });
import { UserResponse } from './types';
const getUsers = async () => {
const { data } = await axios.get<UserResponse>('/users');
return data;
};
export const usersApi = {
get,
getUsers,
};