|
1 | | -import type { ActionMenuOptionOptions } from '../models' |
2 | | - |
3 | | -import { AgentMessage, IsValidMessageType, parseMessageType } from '@credo-ts/core' |
4 | | -import { Expose, Type } from 'class-transformer' |
5 | | -import { IsInstance, IsOptional, IsString } from 'class-validator' |
| 1 | +import { AgentMessage, parseMessageType, utils } from '@credo-ts/core' |
| 2 | +import { z } from 'zod' |
6 | 3 |
|
7 | 4 | import { ActionMenuOption } from '../models' |
| 5 | +import { actionMenuOptionSchema } from '../models/ActionMenuOption' |
| 6 | +import { arrIntoCls } from '../models/ActionMenuOptionForm' |
| 7 | + |
| 8 | +const menuMessageSchema = z |
| 9 | + .object({ |
| 10 | + id: z.string().default(utils.uuid()), |
| 11 | + title: z.string(), |
| 12 | + description: z.string(), |
| 13 | + errormsg: z.string().optional(), |
| 14 | + options: z.array(actionMenuOptionSchema).transform(arrIntoCls<ActionMenuOption>(ActionMenuOption)), |
| 15 | + threadId: z.string().optional(), |
| 16 | + }) |
| 17 | + .transform((o) => ({ |
| 18 | + ...o, |
| 19 | + errorMessage: o.errormsg, |
| 20 | + })) |
| 21 | + |
| 22 | +export type MenuMessageOptions = z.input<typeof menuMessageSchema> |
8 | 23 |
|
9 | | -/** |
10 | | - * @internal |
11 | | - */ |
12 | | -export interface MenuMessageOptions { |
13 | | - id?: string |
14 | | - title: string |
15 | | - description: string |
16 | | - errorMessage?: string |
17 | | - options: ActionMenuOptionOptions[] |
18 | | - threadId?: string |
19 | | -} |
20 | | - |
21 | | -/** |
22 | | - * @internal |
23 | | - */ |
24 | 24 | export class MenuMessage extends AgentMessage { |
25 | | - public constructor(options: MenuMessageOptions) { |
26 | | - super() |
27 | | - |
28 | | - if (options) { |
29 | | - this.id = options.id ?? this.generateId() |
30 | | - this.title = options.title |
31 | | - this.description = options.description |
32 | | - this.errorMessage = options.errorMessage |
33 | | - this.options = options.options.map((p) => new ActionMenuOption(p)) |
34 | | - if (options.threadId) { |
35 | | - this.setThread({ |
36 | | - threadId: options.threadId, |
37 | | - }) |
38 | | - } |
39 | | - } |
40 | | - } |
41 | | - |
42 | | - @IsValidMessageType(MenuMessage.type) |
43 | 25 | public readonly type = MenuMessage.type.messageTypeUri |
44 | 26 | public static readonly type = parseMessageType('https://didcomm.org/action-menu/1.0/menu') |
45 | 27 |
|
46 | | - @IsString() |
47 | | - public title!: string |
48 | | - |
49 | | - @IsString() |
50 | | - public description!: string |
51 | | - |
52 | | - @Expose({ name: 'errormsg' }) |
53 | | - @IsString() |
54 | | - @IsOptional() |
| 28 | + public title: string |
| 29 | + public description: string |
55 | 30 | public errorMessage?: string |
| 31 | + public options: Array<ActionMenuOption> |
56 | 32 |
|
57 | | - @IsInstance(ActionMenuOption, { each: true }) |
58 | | - @Type(() => ActionMenuOption) |
59 | | - public options!: ActionMenuOption[] |
| 33 | + public constructor(options: MenuMessageOptions) { |
| 34 | + super() |
| 35 | + |
| 36 | + const parsedOptions = menuMessageSchema.parse(options) |
| 37 | + this.id = parsedOptions.id |
| 38 | + this.title = parsedOptions.title |
| 39 | + this.description = parsedOptions.description |
| 40 | + this.errorMessage = parsedOptions.errorMessage |
| 41 | + this.options = parsedOptions.options |
| 42 | + } |
60 | 43 | } |
0 commit comments