@m5kdev/backend 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/modules/ai/ai.service.d.ts +11 -13
- package/dist/src/modules/ai/ai.service.js +6 -6
- package/dist/src/modules/ai/ai.trpc.d.ts +1 -1
- package/dist/src/modules/auth/auth.lib.d.ts +4 -8
- package/dist/src/modules/auth/auth.lib.js +2 -2
- package/dist/src/modules/auth/auth.service.d.ts +17 -47
- package/dist/src/modules/auth/auth.service.js +79 -66
- package/dist/src/modules/auth/auth.trpc.d.ts +1 -1
- package/dist/src/modules/base/base.actor.d.ts +68 -0
- package/dist/src/modules/base/base.actor.js +99 -0
- package/dist/src/modules/base/base.actor.test.d.ts +1 -0
- package/dist/src/modules/base/base.actor.test.js +58 -0
- package/dist/src/modules/base/base.grants.d.ts +3 -7
- package/dist/src/modules/base/base.grants.js +22 -10
- package/dist/src/modules/base/base.grants.test.js +16 -45
- package/dist/src/modules/base/base.procedure.d.ts +17 -20
- package/dist/src/modules/base/base.procedure.js +36 -24
- package/dist/src/modules/base/base.service.d.ts +7 -19
- package/dist/src/modules/base/base.service.js +19 -12
- package/dist/src/modules/base/base.service.test.js +89 -61
- package/dist/src/modules/billing/billing.service.d.ts +4 -25
- package/dist/src/modules/billing/billing.service.js +6 -6
- package/dist/src/modules/billing/billing.trpc.d.ts +2 -2
- package/dist/src/modules/billing/billing.trpc.js +4 -6
- package/dist/src/modules/connect/connect.service.d.ts +19 -11
- package/dist/src/modules/connect/connect.service.js +10 -8
- package/dist/src/modules/connect/connect.trpc.d.ts +2 -2
- package/dist/src/modules/recurrence/recurrence.service.d.ts +36 -6
- package/dist/src/modules/recurrence/recurrence.service.js +13 -10
- package/dist/src/modules/recurrence/recurrence.trpc.d.ts +1 -1
- package/dist/src/modules/social/social.service.d.ts +3 -4
- package/dist/src/modules/social/social.service.js +3 -3
- package/dist/src/modules/tag/tag.service.d.ts +16 -12
- package/dist/src/modules/tag/tag.service.js +4 -4
- package/dist/src/modules/tag/tag.trpc.d.ts +1 -1
- package/dist/src/modules/workflow/workflow.service.d.ts +48 -8
- package/dist/src/modules/workflow/workflow.service.js +6 -6
- package/dist/src/modules/workflow/workflow.trpc.d.ts +2 -2
- package/dist/src/types.d.ts +4 -4
- package/dist/src/utils/trpc.d.ts +31 -41
- package/dist/src/utils/trpc.js +95 -0
- package/dist/src/utils/trpc.test.d.ts +1 -0
- package/dist/src/utils/trpc.test.js +154 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
|
@@ -34,19 +34,22 @@ class RecurrenceService extends base_service_1.BaseService {
|
|
|
34
34
|
list = this.procedure("list")
|
|
35
35
|
.addContextFilter(["user"])
|
|
36
36
|
.handle(({ input }) => this.repository.recurrence.queryList(input));
|
|
37
|
-
|
|
37
|
+
create = this.procedure("create")
|
|
38
|
+
.requireAuth()
|
|
39
|
+
.handle(({ input, ctx }) => {
|
|
40
|
+
const { actor } = ctx;
|
|
38
41
|
const recurrenceData = {
|
|
39
|
-
name:
|
|
40
|
-
kind:
|
|
41
|
-
enabled:
|
|
42
|
-
metadata:
|
|
43
|
-
userId:
|
|
44
|
-
organizationId:
|
|
45
|
-
teamId:
|
|
42
|
+
name: input.name,
|
|
43
|
+
kind: input.kind,
|
|
44
|
+
enabled: input.enabled,
|
|
45
|
+
metadata: input.metadata ?? null,
|
|
46
|
+
userId: actor.userId,
|
|
47
|
+
organizationId: actor.organizationId,
|
|
48
|
+
teamId: actor.teamId,
|
|
46
49
|
};
|
|
47
|
-
const rulesData =
|
|
50
|
+
const rulesData = input.recurrenceRules.map(mapRuleToInsert);
|
|
48
51
|
return this.repository.recurrence.createWithRules(recurrenceData, rulesData);
|
|
49
|
-
}
|
|
52
|
+
});
|
|
50
53
|
async findById(id) {
|
|
51
54
|
const result = await this.repository.recurrence.findById(id);
|
|
52
55
|
if (result.isErr())
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type TRPCMethods } from "../../utils/trpc";
|
|
2
2
|
import type { RecurrenceService } from "./recurrence.service";
|
|
3
3
|
export declare function createRecurrenceTRPC({ router, privateProcedure: procedure }: TRPCMethods, recurrenceService: RecurrenceService): import("@trpc/server").TRPCBuiltRouter<{
|
|
4
|
-
ctx: import("
|
|
4
|
+
ctx: import("../../utils/trpc").Context;
|
|
5
5
|
meta: any;
|
|
6
6
|
errorShape: import("@trpc/server").TRPCDefaultErrorShape;
|
|
7
7
|
transformer: true;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { RequiredServiceActor } from "../base/base.actor";
|
|
1
2
|
import type { ServerResultAsync } from "../base/base.dto";
|
|
2
3
|
import { BaseService } from "../base/base.service";
|
|
3
4
|
import type { ConnectRepository } from "../connect/connect.repository";
|
|
@@ -19,10 +20,8 @@ export declare class SocialService extends BaseService<{
|
|
|
19
20
|
file: FileService;
|
|
20
21
|
}, providers: SocialProvider[]);
|
|
21
22
|
getProvider(id: string): SocialProvider | null;
|
|
22
|
-
postToProvider(providerId: string, input: SocialPostInput, {
|
|
23
|
-
|
|
24
|
-
id: string;
|
|
25
|
-
};
|
|
23
|
+
postToProvider(providerId: string, input: SocialPostInput, { actor }: {
|
|
24
|
+
actor: RequiredServiceActor<"user">;
|
|
26
25
|
}): ServerResultAsync<SocialPostResult>;
|
|
27
26
|
private ensureFreshConnection;
|
|
28
27
|
}
|
|
@@ -12,14 +12,14 @@ class SocialService extends base_service_1.BaseService {
|
|
|
12
12
|
getProvider(id) {
|
|
13
13
|
return this.providers.get(id) ?? null;
|
|
14
14
|
}
|
|
15
|
-
async postToProvider(providerId, input, {
|
|
15
|
+
async postToProvider(providerId, input, { actor }) {
|
|
16
16
|
return this.throwableAsync(async () => {
|
|
17
17
|
const provider = this.getProvider(providerId);
|
|
18
18
|
if (!provider) {
|
|
19
19
|
return this.error("BAD_REQUEST", `Unknown provider: ${providerId}`);
|
|
20
20
|
}
|
|
21
21
|
const connectionResult = await this.repository.connect.list({
|
|
22
|
-
userId:
|
|
22
|
+
userId: actor.userId,
|
|
23
23
|
providers: [providerId],
|
|
24
24
|
});
|
|
25
25
|
if (connectionResult.isErr()) {
|
|
@@ -46,7 +46,7 @@ class SocialService extends base_service_1.BaseService {
|
|
|
46
46
|
const result = await provider.post({
|
|
47
47
|
deps: { fileService: this.service.file },
|
|
48
48
|
context: {
|
|
49
|
-
userId:
|
|
49
|
+
userId: actor.userId,
|
|
50
50
|
connection: connection.value,
|
|
51
51
|
accessToken,
|
|
52
52
|
},
|
|
@@ -25,9 +25,10 @@ export declare class TagService extends BaseService<{
|
|
|
25
25
|
resourceType?: string | undefined;
|
|
26
26
|
resourceId?: string | undefined;
|
|
27
27
|
}, {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
actor?: import("../base/base.actor").AuthenticatedActor | null;
|
|
29
|
+
} & Record<string, unknown> & {
|
|
30
|
+
actor: import("../base/base.actor").UserActor;
|
|
31
|
+
}, {
|
|
31
32
|
id: string;
|
|
32
33
|
createdAt: Date;
|
|
33
34
|
updatedAt: Date | null;
|
|
@@ -48,9 +49,10 @@ export declare class TagService extends BaseService<{
|
|
|
48
49
|
color?: string | undefined;
|
|
49
50
|
assignableTo?: string[] | undefined;
|
|
50
51
|
}, {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
actor?: import("../base/base.actor").AuthenticatedActor | null;
|
|
53
|
+
} & Record<string, unknown> & {
|
|
54
|
+
actor: import("../base/base.actor").UserActor;
|
|
55
|
+
}, {
|
|
54
56
|
id: string;
|
|
55
57
|
createdAt: Date;
|
|
56
58
|
updatedAt: Date | null;
|
|
@@ -70,9 +72,10 @@ export declare class TagService extends BaseService<{
|
|
|
70
72
|
resourceType: string;
|
|
71
73
|
resourceId: string;
|
|
72
74
|
}, {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
actor?: import("../base/base.actor").AuthenticatedActor | null;
|
|
76
|
+
} & Record<string, unknown> & {
|
|
77
|
+
actor: import("../base/base.actor").UserActor;
|
|
78
|
+
}, {
|
|
76
79
|
id: string;
|
|
77
80
|
createdAt: Date;
|
|
78
81
|
tagId: string;
|
|
@@ -86,9 +89,10 @@ export declare class TagService extends BaseService<{
|
|
|
86
89
|
resourceType: string;
|
|
87
90
|
resourceId: string;
|
|
88
91
|
}, {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
+
actor?: import("../base/base.actor").AuthenticatedActor | null;
|
|
93
|
+
} & Record<string, unknown> & {
|
|
94
|
+
actor: import("../base/base.actor").UserActor;
|
|
95
|
+
}, {
|
|
92
96
|
id: string;
|
|
93
97
|
createdAt: Date;
|
|
94
98
|
updatedAt: Date | null;
|
|
@@ -12,17 +12,17 @@ class TagService extends base_service_1.BaseService {
|
|
|
12
12
|
create = this.procedure("create")
|
|
13
13
|
.requireAuth()
|
|
14
14
|
.handle(({ input, ctx }) => {
|
|
15
|
-
return this.repository.tag.create({ ...input, userId: ctx.
|
|
15
|
+
return this.repository.tag.create({ ...input, userId: ctx.actor.userId });
|
|
16
16
|
});
|
|
17
17
|
update = this.procedure("update")
|
|
18
18
|
.requireAuth()
|
|
19
19
|
.handle(({ input, ctx }) => {
|
|
20
|
-
return this.repository.tag.update({ ...input, userId: ctx.
|
|
20
|
+
return this.repository.tag.update({ ...input, userId: ctx.actor.userId });
|
|
21
21
|
});
|
|
22
22
|
link = this.procedure("link")
|
|
23
23
|
.requireAuth()
|
|
24
24
|
.handle(({ input, ctx }) => {
|
|
25
|
-
return this.repository.tag.link({ ...input, userId: ctx.
|
|
25
|
+
return this.repository.tag.link({ ...input, userId: ctx.actor.userId });
|
|
26
26
|
});
|
|
27
27
|
async linkBulk(data) {
|
|
28
28
|
return this.repository.tag.linkBulk(data);
|
|
@@ -33,7 +33,7 @@ class TagService extends base_service_1.BaseService {
|
|
|
33
33
|
unlink = this.procedure("unlink")
|
|
34
34
|
.requireAuth()
|
|
35
35
|
.handle(({ input, ctx }) => {
|
|
36
|
-
return this.repository.tag.unlink({ ...input, userId: ctx.
|
|
36
|
+
return this.repository.tag.unlink({ ...input, userId: ctx.actor.userId });
|
|
37
37
|
});
|
|
38
38
|
async delete(data) {
|
|
39
39
|
return this.repository.tag.softDeleteById(data.id);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type TRPCMethods } from "../../utils/trpc";
|
|
2
2
|
import type { TagService } from "./tag.service";
|
|
3
3
|
export declare function createTagTRPC({ router, privateProcedure: procedure }: TRPCMethods, tagService: TagService): import("@trpc/server").TRPCBuiltRouter<{
|
|
4
|
-
ctx: import("
|
|
4
|
+
ctx: import("../../utils/trpc").Context;
|
|
5
5
|
meta: any;
|
|
6
6
|
errorShape: import("@trpc/server").TRPCDefaultErrorShape;
|
|
7
7
|
transformer: true;
|
|
@@ -1,18 +1,58 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { WorkflowReadOutputSchema } from "@m5kdev/commons/modules/workflow/workflow.schema";
|
|
2
2
|
import type { Job } from "bullmq";
|
|
3
|
-
import type { User } from "../auth/auth.lib";
|
|
4
3
|
import type { ServerResultAsync } from "../base/base.dto";
|
|
5
4
|
import { BaseService } from "../base/base.service";
|
|
6
5
|
import type { WorkflowRepository } from "./workflow.repository";
|
|
7
6
|
export declare class WorkflowService extends BaseService<{
|
|
8
7
|
workflow: WorkflowRepository;
|
|
9
8
|
}, never> {
|
|
10
|
-
read
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
readonly read: import("../base/base.procedure").ServiceProcedure<{
|
|
10
|
+
jobId: string;
|
|
11
|
+
}, {
|
|
12
|
+
actor?: import("../base/base.actor").AuthenticatedActor | null;
|
|
13
|
+
} & Record<string, unknown> & {
|
|
14
|
+
actor: import("../base/base.actor").UserActor;
|
|
15
|
+
}, {
|
|
16
|
+
id: string;
|
|
17
|
+
userId: string | null;
|
|
18
|
+
jobId: string;
|
|
19
|
+
jobName: string;
|
|
20
|
+
queueName: string;
|
|
21
|
+
tags: string[] | null;
|
|
22
|
+
input: unknown;
|
|
23
|
+
output: unknown;
|
|
24
|
+
status: "queued" | "running" | "completed" | "failed";
|
|
25
|
+
error: string | null;
|
|
26
|
+
retries: number;
|
|
27
|
+
finishedAt: Date | null;
|
|
28
|
+
processedAt: Date | null;
|
|
29
|
+
createdAt: Date;
|
|
30
|
+
updatedAt: Date;
|
|
31
|
+
}>;
|
|
32
|
+
readonly list: import("../base/base.procedure").ServiceProcedure<{
|
|
33
|
+
status?: ("queued" | "running" | "completed" | "failed")[] | undefined;
|
|
34
|
+
jobName?: string | undefined;
|
|
35
|
+
}, {
|
|
36
|
+
actor?: import("../base/base.actor").AuthenticatedActor | null;
|
|
37
|
+
} & Record<string, unknown> & {
|
|
38
|
+
actor: import("../base/base.actor").UserActor;
|
|
39
|
+
}, {
|
|
40
|
+
id: string;
|
|
41
|
+
userId: string | null;
|
|
42
|
+
jobId: string;
|
|
43
|
+
jobName: string;
|
|
44
|
+
queueName: string;
|
|
45
|
+
tags: string[] | null;
|
|
46
|
+
input: unknown;
|
|
47
|
+
output: unknown;
|
|
48
|
+
status: "queued" | "running" | "completed" | "failed";
|
|
49
|
+
error: string | null;
|
|
50
|
+
retries: number;
|
|
51
|
+
finishedAt: Date | null;
|
|
52
|
+
processedAt: Date | null;
|
|
53
|
+
createdAt: Date;
|
|
54
|
+
updatedAt: Date;
|
|
55
|
+
}[]>;
|
|
16
56
|
added(params: Parameters<WorkflowRepository["added"]>[0]): ServerResultAsync<WorkflowReadOutputSchema>;
|
|
17
57
|
addedMany(params: Parameters<WorkflowRepository["addedMany"]>[0]): ServerResultAsync<WorkflowReadOutputSchema[]>;
|
|
18
58
|
started(job: Job): ServerResultAsync<WorkflowReadOutputSchema>;
|
|
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.WorkflowService = void 0;
|
|
4
4
|
const base_service_1 = require("../base/base.service");
|
|
5
5
|
class WorkflowService extends base_service_1.BaseService {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
read = this.procedure("workflowRead")
|
|
7
|
+
.requireAuth()
|
|
8
|
+
.handle(({ input, ctx }) => this.repository.workflow.read({ ...input, userId: ctx.actor.userId }));
|
|
9
|
+
list = this.procedure("workflowList")
|
|
10
|
+
.requireAuth()
|
|
11
|
+
.handle(({ input, ctx }) => this.repository.workflow.list({ ...input, userId: ctx.actor.userId }));
|
|
12
12
|
async added(params) {
|
|
13
13
|
return this.repository.workflow.added(params);
|
|
14
14
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { WorkflowService } from "./workflow.service";
|
|
2
1
|
import { type TRPCMethods } from "../../utils/trpc";
|
|
2
|
+
import type { WorkflowService } from "./workflow.service";
|
|
3
3
|
export declare function createWorkflowTRPC({ router, privateProcedure: procedure }: TRPCMethods, workflowService: WorkflowService): import("@trpc/server").TRPCBuiltRouter<{
|
|
4
|
-
ctx: import("
|
|
4
|
+
ctx: import("../../utils/trpc").Context;
|
|
5
5
|
meta: any;
|
|
6
6
|
errorShape: import("@trpc/server").TRPCDefaultErrorShape;
|
|
7
7
|
transformer: true;
|
package/dist/src/types.d.ts
CHANGED
|
@@ -4,13 +4,13 @@ import type { AuthService } from "./modules/auth/auth.service";
|
|
|
4
4
|
import type { BillingService } from "./modules/billing/billing.service";
|
|
5
5
|
import type { TRPCMethods } from "./utils/trpc";
|
|
6
6
|
export declare const createAuthTRPCRouter: <MastraInstance extends Mastra>(trpcMethods: TRPCMethods, authService: AuthService, aiService: AIService<MastraInstance>, billingService: BillingService) => import("@trpc/server").TRPCBuiltRouter<{
|
|
7
|
-
ctx: import("./
|
|
7
|
+
ctx: import("./utils/trpc").Context;
|
|
8
8
|
meta: any;
|
|
9
9
|
errorShape: import("@trpc/server").TRPCDefaultErrorShape;
|
|
10
10
|
transformer: true;
|
|
11
11
|
}, import("@trpc/server").TRPCDecorateCreateRouterOptions<{
|
|
12
12
|
auth: import("@trpc/server").TRPCBuiltRouter<{
|
|
13
|
-
ctx: import("./
|
|
13
|
+
ctx: import("./utils/trpc").Context;
|
|
14
14
|
meta: any;
|
|
15
15
|
errorShape: import("@trpc/server").TRPCDefaultErrorShape;
|
|
16
16
|
transformer: true;
|
|
@@ -300,7 +300,7 @@ export declare const createAuthTRPCRouter: <MastraInstance extends Mastra>(trpcM
|
|
|
300
300
|
}>;
|
|
301
301
|
}>>;
|
|
302
302
|
ai: import("@trpc/server").TRPCBuiltRouter<{
|
|
303
|
-
ctx: import("./
|
|
303
|
+
ctx: import("./utils/trpc").Context;
|
|
304
304
|
meta: any;
|
|
305
305
|
errorShape: import("@trpc/server").TRPCDefaultErrorShape;
|
|
306
306
|
transformer: true;
|
|
@@ -319,7 +319,7 @@ export declare const createAuthTRPCRouter: <MastraInstance extends Mastra>(trpcM
|
|
|
319
319
|
}>;
|
|
320
320
|
}>>;
|
|
321
321
|
billing: import("@trpc/server").TRPCBuiltRouter<{
|
|
322
|
-
ctx: import("./
|
|
322
|
+
ctx: import("./utils/trpc").Context;
|
|
323
323
|
meta: any;
|
|
324
324
|
errorShape: import("@trpc/server").TRPCDefaultErrorShape;
|
|
325
325
|
transformer: true;
|
package/dist/src/utils/trpc.d.ts
CHANGED
|
@@ -2,8 +2,29 @@ import type { transformer } from "@m5kdev/commons/utils/trpc";
|
|
|
2
2
|
import type { TRPCRootObject } from "@trpc/server";
|
|
3
3
|
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express";
|
|
4
4
|
import type { Result } from "neverthrow";
|
|
5
|
-
import type { BetterAuth,
|
|
5
|
+
import type { BetterAuth, Session, User } from "../modules/auth/auth.lib";
|
|
6
|
+
import { type OrganizationActor, type TeamActor, type UserActor } from "../modules/base/base.actor";
|
|
6
7
|
import { ServerError } from "./errors";
|
|
8
|
+
export type RequestContext = {
|
|
9
|
+
session: Session | null;
|
|
10
|
+
user: User | null;
|
|
11
|
+
actor: UserActor | null;
|
|
12
|
+
};
|
|
13
|
+
export type Context = {
|
|
14
|
+
session: Session;
|
|
15
|
+
user: User;
|
|
16
|
+
actor: UserActor;
|
|
17
|
+
};
|
|
18
|
+
export type OrganizationContext = {
|
|
19
|
+
session: Session;
|
|
20
|
+
user: User;
|
|
21
|
+
actor: OrganizationActor;
|
|
22
|
+
};
|
|
23
|
+
export type TeamContext = {
|
|
24
|
+
session: Session;
|
|
25
|
+
user: User;
|
|
26
|
+
actor: TeamActor;
|
|
27
|
+
};
|
|
7
28
|
type TRPCCreate = TRPCRootObject<Context, any, {
|
|
8
29
|
transformer: typeof transformer;
|
|
9
30
|
}>;
|
|
@@ -13,46 +34,15 @@ export type TRPCMethods = {
|
|
|
13
34
|
privateProcedure: TRPCCreate["procedure"];
|
|
14
35
|
adminProcedure: TRPCCreate["procedure"];
|
|
15
36
|
};
|
|
16
|
-
export declare function createAuthContext(auth: BetterAuth): ({ req }: CreateExpressContextOptions) => Promise<
|
|
17
|
-
session: {
|
|
18
|
-
id: string;
|
|
19
|
-
createdAt: Date;
|
|
20
|
-
updatedAt: Date;
|
|
21
|
-
expiresAt: Date;
|
|
22
|
-
token: string;
|
|
23
|
-
ipAddress: string | null;
|
|
24
|
-
userAgent: string | null;
|
|
25
|
-
userId: string;
|
|
26
|
-
impersonatedBy: string | null;
|
|
27
|
-
activeOrganizationId: string | null;
|
|
28
|
-
activeOrganizationRole: string | null;
|
|
29
|
-
activeTeamId: string | null;
|
|
30
|
-
activeTeamRole: string | null;
|
|
31
|
-
};
|
|
32
|
-
user: {
|
|
33
|
-
id: string;
|
|
34
|
-
name: string;
|
|
35
|
-
email: string;
|
|
36
|
-
emailVerified: boolean;
|
|
37
|
-
image: string | null;
|
|
38
|
-
createdAt: Date;
|
|
39
|
-
updatedAt: Date;
|
|
40
|
-
role: string | null;
|
|
41
|
-
banned: boolean | null;
|
|
42
|
-
banReason: string | null;
|
|
43
|
-
banExpires: Date | null;
|
|
44
|
-
stripeCustomerId: string | null;
|
|
45
|
-
paymentCustomerId: string | null;
|
|
46
|
-
paymentPlanTier: string | null;
|
|
47
|
-
paymentPlanExpiresAt: Date | null;
|
|
48
|
-
preferences: string | null;
|
|
49
|
-
metadata: Record<string, unknown>;
|
|
50
|
-
onboarding: number | null;
|
|
51
|
-
flags: string | null;
|
|
52
|
-
};
|
|
53
|
-
}>;
|
|
37
|
+
export declare function createAuthContext(auth: BetterAuth): ({ req }: CreateExpressContextOptions) => Promise<RequestContext>;
|
|
54
38
|
export declare function handleAsyncTRPCResult<T>(result: Promise<Result<T, ServerError>>): Promise<T>;
|
|
55
39
|
export declare function handleTRPCResult<T>(result: Result<T, ServerError>): T;
|
|
56
|
-
export declare function verifyProtectedProcedureContext(ctx:
|
|
57
|
-
export declare function
|
|
40
|
+
export declare function verifyProtectedProcedureContext(ctx: RequestContext): Context;
|
|
41
|
+
export declare function verifyOrganizationProcedureContext(ctx: Context): OrganizationContext;
|
|
42
|
+
export declare function verifyTeamProcedureContext(ctx: Context): TeamContext;
|
|
43
|
+
export declare function verifyAdminProcedureContext(ctx: RequestContext): Context;
|
|
44
|
+
export declare function requireRequestUser(ctx: RequestContext): User;
|
|
45
|
+
export declare function requireRequestActor(ctx: RequestContext): UserActor;
|
|
46
|
+
export declare function requireRequestActor(ctx: RequestContext, scope: "organization"): OrganizationActor;
|
|
47
|
+
export declare function requireRequestActor(ctx: RequestContext, scope: "team"): TeamActor;
|
|
58
48
|
export {};
|
package/dist/src/utils/trpc.js
CHANGED
|
@@ -4,8 +4,13 @@ exports.createAuthContext = createAuthContext;
|
|
|
4
4
|
exports.handleAsyncTRPCResult = handleAsyncTRPCResult;
|
|
5
5
|
exports.handleTRPCResult = handleTRPCResult;
|
|
6
6
|
exports.verifyProtectedProcedureContext = verifyProtectedProcedureContext;
|
|
7
|
+
exports.verifyOrganizationProcedureContext = verifyOrganizationProcedureContext;
|
|
8
|
+
exports.verifyTeamProcedureContext = verifyTeamProcedureContext;
|
|
7
9
|
exports.verifyAdminProcedureContext = verifyAdminProcedureContext;
|
|
10
|
+
exports.requireRequestUser = requireRequestUser;
|
|
11
|
+
exports.requireRequestActor = requireRequestActor;
|
|
8
12
|
const node_1 = require("better-auth/node");
|
|
13
|
+
const base_actor_1 = require("../modules/base/base.actor");
|
|
9
14
|
const errors_1 = require("./errors");
|
|
10
15
|
const logger_1 = require("./logger");
|
|
11
16
|
function createAuthContext(auth) {
|
|
@@ -15,9 +20,11 @@ function createAuthContext(auth) {
|
|
|
15
20
|
});
|
|
16
21
|
const user = data?.user || null;
|
|
17
22
|
const session = data?.session || null;
|
|
23
|
+
const actor = user && session ? (0, base_actor_1.createActorFromContext)({ user, session }, "user") : null;
|
|
18
24
|
return {
|
|
19
25
|
session,
|
|
20
26
|
user,
|
|
27
|
+
actor,
|
|
21
28
|
};
|
|
22
29
|
};
|
|
23
30
|
}
|
|
@@ -37,6 +44,34 @@ function handleTRPCResult(result) {
|
|
|
37
44
|
return result.value;
|
|
38
45
|
}
|
|
39
46
|
function verifyProtectedProcedureContext(ctx) {
|
|
47
|
+
if (!ctx.user || !ctx.session || !ctx.actor) {
|
|
48
|
+
throw new errors_1.ServerError({
|
|
49
|
+
code: "UNAUTHORIZED",
|
|
50
|
+
layer: "controller",
|
|
51
|
+
layerName: "TRPCController",
|
|
52
|
+
}).toTRPC();
|
|
53
|
+
}
|
|
54
|
+
return ctx;
|
|
55
|
+
}
|
|
56
|
+
function verifyOrganizationProcedureContext(ctx) {
|
|
57
|
+
if (!ctx.user || !ctx.session) {
|
|
58
|
+
throw new errors_1.ServerError({
|
|
59
|
+
code: "UNAUTHORIZED",
|
|
60
|
+
layer: "controller",
|
|
61
|
+
layerName: "TRPCController",
|
|
62
|
+
}).toTRPC();
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const actor = (0, base_actor_1.createActorFromContext)({ user: ctx.user, session: ctx.session }, "organization");
|
|
66
|
+
return { ...ctx, actor };
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
if (e instanceof errors_1.ServerError)
|
|
70
|
+
throw e.toTRPC();
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function verifyTeamProcedureContext(ctx) {
|
|
40
75
|
if (!ctx.user || !ctx.session) {
|
|
41
76
|
throw new errors_1.ServerError({
|
|
42
77
|
code: "UNAUTHORIZED",
|
|
@@ -44,6 +79,15 @@ function verifyProtectedProcedureContext(ctx) {
|
|
|
44
79
|
layerName: "TRPCController",
|
|
45
80
|
}).toTRPC();
|
|
46
81
|
}
|
|
82
|
+
try {
|
|
83
|
+
const actor = (0, base_actor_1.createActorFromContext)({ user: ctx.user, session: ctx.session }, "team");
|
|
84
|
+
return { ...ctx, actor };
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
if (e instanceof errors_1.ServerError)
|
|
88
|
+
throw e.toTRPC();
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
47
91
|
}
|
|
48
92
|
function verifyAdminProcedureContext(ctx) {
|
|
49
93
|
if (!ctx.user || !ctx.session) {
|
|
@@ -60,4 +104,55 @@ function verifyAdminProcedureContext(ctx) {
|
|
|
60
104
|
layerName: "TRPCController",
|
|
61
105
|
}).toTRPC();
|
|
62
106
|
}
|
|
107
|
+
if (!ctx.actor) {
|
|
108
|
+
throw new errors_1.ServerError({
|
|
109
|
+
code: "UNAUTHORIZED",
|
|
110
|
+
layer: "controller",
|
|
111
|
+
layerName: "TRPCController",
|
|
112
|
+
}).toTRPC();
|
|
113
|
+
}
|
|
114
|
+
return ctx;
|
|
115
|
+
}
|
|
116
|
+
function requireRequestUser(ctx) {
|
|
117
|
+
return verifyProtectedProcedureContext(ctx).user;
|
|
118
|
+
}
|
|
119
|
+
function requireRequestActor(ctx, scope = "user") {
|
|
120
|
+
const verified = verifyProtectedProcedureContext(ctx);
|
|
121
|
+
if (scope === "user") {
|
|
122
|
+
if (!(0, base_actor_1.validateActor)(verified.actor, "user")) {
|
|
123
|
+
throw new errors_1.ServerError({
|
|
124
|
+
code: "FORBIDDEN",
|
|
125
|
+
layer: "controller",
|
|
126
|
+
layerName: "TRPCController",
|
|
127
|
+
}).toTRPC();
|
|
128
|
+
}
|
|
129
|
+
return verified.actor;
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
if (scope === "organization") {
|
|
133
|
+
const actor = (0, base_actor_1.createActorFromContext)({ user: verified.user, session: verified.session }, "organization");
|
|
134
|
+
if (!(0, base_actor_1.validateActor)(actor, "organization")) {
|
|
135
|
+
throw new errors_1.ServerError({
|
|
136
|
+
code: "FORBIDDEN",
|
|
137
|
+
layer: "controller",
|
|
138
|
+
layerName: "TRPCController",
|
|
139
|
+
}).toTRPC();
|
|
140
|
+
}
|
|
141
|
+
return actor;
|
|
142
|
+
}
|
|
143
|
+
const actor = (0, base_actor_1.createActorFromContext)({ user: verified.user, session: verified.session }, "team");
|
|
144
|
+
if (!(0, base_actor_1.validateActor)(actor, "team")) {
|
|
145
|
+
throw new errors_1.ServerError({
|
|
146
|
+
code: "FORBIDDEN",
|
|
147
|
+
layer: "controller",
|
|
148
|
+
layerName: "TRPCController",
|
|
149
|
+
}).toTRPC();
|
|
150
|
+
}
|
|
151
|
+
return actor;
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
if (e instanceof errors_1.ServerError)
|
|
155
|
+
throw e.toTRPC();
|
|
156
|
+
throw e;
|
|
157
|
+
}
|
|
63
158
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|