-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(users): improve api readability using safeApiCall reusable f…
…unction
- Loading branch information
1 parent
02895a0
commit 21f84c9
Showing
10 changed files
with
118 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './safe-api-call.function' |
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,21 @@ | ||
import { AppResponse } from '../types' | ||
|
||
type ApiHandler<T> = () => Promise<T> | ||
|
||
export async function safeApiCall<T>( | ||
handler: ApiHandler<T>, | ||
res: AppResponse<T>, | ||
): Promise<AppResponse<T>> { | ||
try { | ||
const data = await handler() | ||
return res.status(200).json({ | ||
success: true, | ||
data, | ||
}) | ||
} catch (err: any) { | ||
return res.status(500).json({ | ||
success: false, | ||
error: err, | ||
}) | ||
} | ||
} |
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 +1,2 @@ | ||
export * from './usecase.type' | ||
export * from './network.types' | ||
export * from './usecase.types' |
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,20 @@ | ||
import { User } from '@/modules/users/core' | ||
import { Request, Response } from 'express' | ||
|
||
export interface AppRequest< | ||
P extends { [key: string]: string } = { | ||
[key: string]: string | ||
}, | ||
T = any, | ||
R = any, | ||
S = any, | ||
> extends Request<P, T, R, S> { | ||
user?: User | ||
token?: string | ||
} | ||
|
||
export type AppResponse<T = any> = Response<{ | ||
success: boolean | ||
data?: T | ||
error?: { message: string; code?: string } | ||
}> |
2 changes: 1 addition & 1 deletion
2
src/common/types/usecase.type.ts → src/common/types/usecase.types.ts
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,3 +1,3 @@ | ||
export interface UseCase<R, P> { | ||
invoke(params: P): Promise<R> | ||
} | ||
} |
36 changes: 28 additions & 8 deletions
36
src/modules/users/adapters/dataproviders/user.dataprovider.ts
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
45 changes: 39 additions & 6 deletions
45
src/modules/users/application/controllers/users.controller.ts
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,12 +1,45 @@ | ||
import { Request, Response } from 'express' | ||
import { AppRequest, AppResponse } from '@/common/types' | ||
|
||
import { UserDataProvider } from '../../adapters/dataproviders/user.dataprovider' | ||
import { | ||
PrismaUser, | ||
UserDataProvider, | ||
} from '../../adapters/dataproviders/user.dataprovider' | ||
|
||
import { safeApiCall } from '@/common/functions' | ||
|
||
export class UsersController { | ||
constructor(private _userProvider: UserDataProvider) {} | ||
|
||
async getUser(req: Request, res: Response) {} | ||
async getAllUsers() {} | ||
async updateUser() {} | ||
async deleteUser() {} | ||
getUser = async (req: AppRequest, res: AppResponse<PrismaUser>) => { | ||
return await safeApiCall( | ||
() => this._userProvider.getUserById(req.user?.id!), | ||
res, | ||
) | ||
} | ||
|
||
getAllUsers = async (_req: AppRequest, res: AppResponse<PrismaUser[]>) => { | ||
return await safeApiCall(() => this._userProvider.getAllUsers(), res) | ||
} | ||
|
||
updateUser = async ( | ||
req: AppRequest<{ id: string }>, | ||
res: AppResponse<PrismaUser>, | ||
) => { | ||
const { id } = req.params | ||
const { name, email } = req.body | ||
|
||
return await safeApiCall( | ||
() => | ||
this._userProvider.update(id, { | ||
name, | ||
email, | ||
}), | ||
res, | ||
) | ||
} | ||
|
||
deleteUser = async (req: AppRequest, res: AppResponse) => { | ||
const { id } = req.params | ||
return await safeApiCall(() => this._userProvider.delete(id), res) | ||
} | ||
} |
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,5 +1,5 @@ | ||
export type User = { | ||
id?: string | ||
id: string | ||
email: string | ||
createdAt: Date | ||
password: string | ||
|