import { get } from 'YesterTech/api/utils' ; import * as storage from 'YesterTech/localStorage' ; import { User , UserNoPassword , GitHubUser } from 'YesterTech/types' ; export async function login ( username , password ) { const results = await get ( `/users?username=${ username }&password=${ password }` ) ; if ( results . length > 0 ) { const { password , ... user } = results [ 0 ] ; storage . login ( user ) ; return user ; } else { return Promise . reject ( 'User not found' ) ; } } export function getAuthenticatedUser ( ) { const user = storage . getAuthenticatedUser ( ) ; return Promise . resolve ( user ) ; } export function logout ( ) { storage . logout ( ) ; return Promise . resolve ( ) ; } export async function getGitHubUser ( username ) { try { const res = await fetch ( `https://api.github.com/users/${ username }` ) ; if ( res . status === 200 ) { return await res . json ( ) ; } else { return await Promise . reject ( { status : res . status , statusText : res . statusText , } ) ; } } catch ( err ) { return Promise . reject ( err ) ; } }