-
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.
- Loading branch information
Showing
42 changed files
with
448 additions
and
144 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
36 changes: 8 additions & 28 deletions
36
packages/mern-sample-app/app-node-express/src/infrastructure/decorators/contract.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,36 +1,16 @@ | ||
import type { AppRoute } from "@ts-rest/core"; | ||
import type { RouteHandler, RouteMiddlewareFactory } from "@org/app-node-express/lib/@ts-rest"; | ||
import type { AppRoute } from "@org/lib-api-client"; | ||
|
||
import { IocRegistry } from "@org/app-node-express/ioc"; | ||
import * as TsRest from "@org/app-node-express/lib/@ts-rest"; | ||
import { MongoDatabaseService } from "@org/app-node-express/lib/mongodb"; | ||
import { getTypedError } from "@org/lib-api-client"; | ||
import { TsRestRouterService, createContractHandler } from "@org/app-node-express/lib/@ts-rest"; | ||
|
||
export function contract<const Route extends AppRoute, This, Fn extends TsRest.RouteHandler<Route>>( | ||
export function contract<const Route extends AppRoute, This, Fn extends RouteHandler<Route>>( | ||
contract: Route, | ||
...middlewareData: (TsRest.RouteMiddlewareFactory | TsRest.RouteMiddlewareFactory[])[] | ||
...middlewareData: (RouteMiddlewareFactory | RouteMiddlewareFactory[])[] | ||
) { | ||
const middlewareFactories = middlewareData.flat(); | ||
|
||
return function (target: Fn, context: ClassMethodDecoratorContext<This, Fn>) { | ||
async function handler(data: unknown): Promise<unknown> { | ||
const databaseService = MongoDatabaseService.getInstance(); | ||
const session = databaseService.client.startSession(); | ||
try { | ||
databaseService.startTransaction(session); | ||
const container = IocRegistry.getInstance().inject(context); | ||
const result = await target.call(container, data as TsRest.RouteInput<Route>); | ||
await databaseService.commitTransaction(session); | ||
return result; | ||
} catch (error: unknown) { | ||
await databaseService.rollbackTransaction(session); | ||
const typedError = getTypedError(error); | ||
return { status: typedError.content.status, body: typedError.content }; | ||
} finally { | ||
session.endSession(); | ||
} | ||
} | ||
|
||
TsRest.TsRestRouterService.getInstance().addRouter(contract, handler, middlewareFactories); | ||
const middleware = middlewareData.flat(); | ||
const handler = createContractHandler({ contract, middleware, context, target }); | ||
TsRestRouterService.getInstance().addRouter(contract, handler, middleware); | ||
return handler as Fn; | ||
}; | ||
} |
41 changes: 41 additions & 0 deletions
41
...s/mern-sample-app/app-node-express/src/infrastructure/decorators/createContractHandler.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { | ||
RouteHandler, | ||
RouteInput, | ||
RouteMiddlewareFactory, | ||
} from "@org/app-node-express/lib/@ts-rest"; | ||
import type { AppRoute } from "@org/lib-api-client"; | ||
|
||
import { IocRegistry } from "@org/app-node-express/ioc"; | ||
import { MongoDatabaseService } from "@org/app-node-express/lib/mongodb"; | ||
import { getTypedError } from "@org/lib-api-client"; | ||
|
||
export type ContractEndpointProps<Route extends AppRoute, This, Fn extends RouteHandler<Route>> = { | ||
contract: Route; | ||
middleware: RouteMiddlewareFactory[]; | ||
target: Fn; | ||
context: ClassMethodDecoratorContext<This, Fn>; | ||
}; | ||
|
||
export function createContractHandler< | ||
const Route extends AppRoute, | ||
This, | ||
Fn extends RouteHandler<Route>, | ||
>({ context, target }: ContractEndpointProps<Route, This, Fn>) { | ||
return async function (data: unknown): Promise<unknown> { | ||
const databaseService = MongoDatabaseService.getInstance(); | ||
const session = databaseService.client.startSession(); | ||
try { | ||
databaseService.startTransaction(session); | ||
const container = IocRegistry.getInstance().inject(context); | ||
const result = await target.call(container, data as RouteInput<Route>); | ||
await databaseService.commitTransaction(session); | ||
return result; | ||
} catch (error: unknown) { | ||
await databaseService.rollbackTransaction(session); | ||
const typedError = getTypedError(error); | ||
return { status: typedError.content.status, body: typedError.content }; | ||
} finally { | ||
session.endSession(); | ||
} | ||
}; | ||
} |
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
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
2 changes: 1 addition & 1 deletion
2
packages/mern-sample-app/app-node-express/src/infrastructure/repository/UserRepository.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
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
3 changes: 2 additions & 1 deletion
3
...rc/infrastructure/decorators/autowired.ts → ...e-express/src/ioc/decorators/autowired.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
2 changes: 2 additions & 0 deletions
2
packages/mern-sample-app/app-node-express/src/ioc/decorators/index.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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./autowired"; | ||
export * from "./inject"; |
2 changes: 1 addition & 1 deletion
2
...s/src/infrastructure/decorators/inject.ts → ...node-express/src/ioc/decorators/inject.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
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
41 changes: 41 additions & 0 deletions
41
packages/mern-sample-app/app-node-express/src/lib/@ts-rest/TsRestContractHandler.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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { | ||
RouteHandler, | ||
RouteInput, | ||
RouteMiddlewareFactory, | ||
} from "@org/app-node-express/lib/@ts-rest"; | ||
import type { AppRoute } from "@org/lib-api-client"; | ||
|
||
import { IocRegistry } from "@org/app-node-express/ioc"; | ||
import { MongoDatabaseService } from "@org/app-node-express/lib/mongodb"; | ||
import { getTypedError } from "@org/lib-api-client"; | ||
|
||
export type ContractEndpointProps<Route extends AppRoute, This, Fn extends RouteHandler<Route>> = { | ||
contract: Route; | ||
middleware: RouteMiddlewareFactory[]; | ||
target: Fn; | ||
context: ClassMethodDecoratorContext<This, Fn>; | ||
}; | ||
|
||
export function createContractHandler< | ||
const Route extends AppRoute, | ||
This, | ||
Fn extends RouteHandler<Route>, | ||
>({ context, target }: ContractEndpointProps<Route, This, Fn>) { | ||
return async function (data: unknown): Promise<unknown> { | ||
const databaseService = MongoDatabaseService.getInstance(); | ||
const session = databaseService.client.startSession(); | ||
try { | ||
databaseService.startTransaction(session); | ||
const container = IocRegistry.getInstance().inject(context); | ||
const result = await target.call(container, data as RouteInput<Route>); | ||
await databaseService.commitTransaction(session); | ||
return result; | ||
} catch (error: unknown) { | ||
await databaseService.rollbackTransaction(session); | ||
const typedError = getTypedError(error); | ||
return { status: typedError.content.status, body: typedError.content }; | ||
} finally { | ||
session.endSession(); | ||
} | ||
}; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/mern-sample-app/app-node-express/src/lib/@ts-rest/TsRestRouterService.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
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,2 +1,6 @@ | ||
VITE_API_CLIENT_URL=https://monorepo-mern-railway-starter-backend.up.railway.app | ||
# Railway development server | ||
#VITE_API_CLIENT_URL=https://monorepo-mern-railway-starter-backend.up.railway.app | ||
|
||
# Local | ||
VITE_API_CLIENT_URL=http://localhost:8081 | ||
VITE_API_KEYCLOAK_URL=http://localhost:8080 |
32 changes: 32 additions & 0 deletions
32
packages/mern-sample-app/app-vite-react/src/app/components/Header/LogoutButton.tsx
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,32 @@ | ||
import type { FC } from "react"; | ||
|
||
import { Logout } from "@mui/icons-material"; | ||
import { MenuItem, ListItemIcon, Typography } from "@mui/material"; | ||
import { useTheme } from "@mui/material/styles"; | ||
|
||
interface LogoutMenuItemProps { | ||
handleLogout: () => void; | ||
} | ||
|
||
const LogoutButton: FC<LogoutMenuItemProps> = ({ handleLogout }) => { | ||
const theme = useTheme(); | ||
|
||
return ( | ||
<MenuItem | ||
onClick={handleLogout} | ||
sx={{ | ||
color: theme.palette.warning.main, | ||
justifyContent: "center", // Added justifyContent: center | ||
"&:hover": { | ||
//backgroundColor: theme.palette.warning.light, | ||
}, | ||
}} | ||
> | ||
<Typography variant="inherit" sx={{ fontWeight: "bold", textAlign: "center" }}> | ||
Logout | ||
</Typography> | ||
</MenuItem> | ||
); | ||
}; | ||
|
||
export default LogoutButton; |
Oops, something went wrong.