Skip to content

Commit b784af6

Browse files
committed
fix: mikro-orm context
1 parent dcbd493 commit b784af6

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

modules/module-core/src/utils/context-builder.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ function getHeader(
3636
return header ?? null;
3737
}
3838

39-
export const context = async <IUser extends User = User, Ctx extends object = object>(
39+
export const context = async <
40+
IUserOrContext extends object = User,
41+
Ctx extends object = object,
42+
_User_ extends User = IUserOrContext extends User ? IUserOrContext : User,
43+
_Context_ extends object = IUserOrContext extends User
44+
? IContext<IUserOrContext>
45+
: IUserOrContext,
46+
>(
4047
{
4148
req,
4249
request,
@@ -51,8 +58,8 @@ export const context = async <IUser extends User = User, Ctx extends object = ob
5158
},
5259
{ createOperationController, ctx, ...options }: AccountsContextOptions<Ctx>
5360
): AccountsContextOptions<Ctx> extends { ctx: any }
54-
? Promise<IContext<IUser> & Ctx>
55-
: Promise<IContext<IUser>> => {
61+
? Promise<_Context_ & Ctx>
62+
: Promise<_Context_> => {
5663
const reqOrRequest = request ?? req;
5764
if (!reqOrRequest) {
5865
return {
@@ -63,7 +70,7 @@ export const context = async <IUser extends User = User, Ctx extends object = ob
6370
userAgent: '',
6471
},
6572
...ctx,
66-
};
73+
} satisfies IContext<_User_> as _Context_;
6774
}
6875

6976
const headerName = options.headerName || 'Authorization';
@@ -81,7 +88,7 @@ export const context = async <IUser extends User = User, Ctx extends object = ob
8188
});
8289
try {
8390
user = await controller.injector
84-
.get<AccountsServer<IUser>>(AccountsServer)
91+
.get<AccountsServer<_User_>>(AccountsServer)
8592
.resumeSession(authToken);
8693
} catch (error) {
8794
// Empty catch
@@ -112,5 +119,5 @@ export const context = async <IUser extends User = User, Ctx extends object = ob
112119
ip,
113120
},
114121
...ctx,
115-
};
122+
} satisfies IContext<_User_> as _Context_;
116123
};
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { IContext as AccountsContext, User } from '@accounts/types';
1+
import { IContext as AccountsContext } from '@accounts/types';
22
import { Connection, EntityManager, IDatabaseDriver } from '@mikro-orm/core';
3+
import { IUser } from '@accounts/mikro-orm';
34

4-
export interface IContext<IUser extends User = User> extends AccountsContext<IUser> {
5+
export interface IContext<User extends IUser<any, any, any> = IUser<any, any, any>>
6+
extends Omit<AccountsContext, 'user'> {
7+
user?: User;
58
em?: EntityManager<IDatabaseDriver<Connection>>;
69
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { IUser } from '@accounts/mikro-orm';
12
import { context as contextBase } from '@accounts/module-core';
2-
import { User } from '@accounts/types';
33
import { Connection, EntityManager, IDatabaseDriver } from '@mikro-orm/core';
4+
import { IContext } from '../types';
45

56
export const context = async <
6-
IUser extends User = User,
7+
User extends IUser<any, any, any> = IUser<any, any, any>,
78
Ctx extends { em?: EntityManager<IDatabaseDriver<Connection>> } = object,
89
>(
9-
...args: Parameters<typeof contextBase<IUser, Ctx>>
10-
): ReturnType<typeof contextBase<IUser, Ctx>> => contextBase(...args);
10+
...args: Parameters<typeof contextBase<IContext<User>, Ctx>>
11+
): ReturnType<typeof contextBase<IContext<User>, Ctx>> => contextBase(...args);

0 commit comments

Comments
 (0)