-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
import { defineCommand } from 'citty'; | ||
import consola from 'consola'; | ||
import userConfig from '../utils/userConfig'; | ||
import usersService from '../services/users'; | ||
|
||
export default defineCommand({ | ||
meta: { | ||
name: 'whoami', | ||
description: 'Show current user', | ||
}, | ||
run: async () => { | ||
const { username } = userConfig.read(); | ||
if (!username) { | ||
consola.error('Not logged in'); | ||
const { token } = userConfig.read(); | ||
if (token) { | ||
try { | ||
const user = await usersService.me(); | ||
consola.info(`Logged in as ${user.email}.`); | ||
} catch (error) { | ||
consola.error('Token is invalid. Please sign in again.'); | ||
} | ||
} else { | ||
consola.info(`Logged in as ${username}`); | ||
consola.error('Not logged in.'); | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { UserDto } from '../types'; | ||
import httpClient, { HttpClient } from '../utils/http-client'; | ||
import authorizationService from './authorization-service'; | ||
|
||
export interface UsersService { | ||
me(): Promise<UserDto>; | ||
} | ||
|
||
class UsersServiceImpl implements UsersService { | ||
private readonly httpClient: HttpClient; | ||
|
||
constructor(httpClient: HttpClient) { | ||
this.httpClient = httpClient; | ||
} | ||
|
||
async me(): Promise<UserDto> { | ||
const response = await this.httpClient.get<UserDto>('/users/me', { | ||
headers: { | ||
Authorization: `Bearer ${authorizationService.getCurrentAuthorizationToken()}`, | ||
}, | ||
}); | ||
if (!response.success) { | ||
throw response.error; | ||
} | ||
return response.data; | ||
} | ||
} | ||
|
||
const usersService = new UsersServiceImpl(httpClient); | ||
|
||
export default usersService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface UserDto { | ||
id: string; | ||
email: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters