-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcreate.ts
More file actions
47 lines (43 loc) · 1.68 KB
/
create.ts
File metadata and controls
47 lines (43 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Command } from '@commander-js/extra-typings';
import { runCreate } from '../../lib/actions';
import type { GlobalOpts } from '../../lib/client';
import { buildHelpText } from '../../lib/help-text';
import { requireText } from '../../lib/prompts';
import { safeTerminalText } from '../../lib/safe-terminal-text';
export const createSegmentCommand = new Command('create')
.description('Create a new segment')
.option('--name <name>', 'Segment name (required)')
.addHelpText(
'after',
buildHelpText({
context: `Segments are named groups of contacts. Broadcasts target segments via segment_id.
Contacts can belong to multiple segments. Audiences are deprecated — use segments instead.
Non-interactive: --name is required.`,
output: ` {"object":"segment","id":"<uuid>","name":"<name>"}`,
errorCodes: ['auth_error', 'missing_name', 'create_error'],
examples: [
'resend segments create --name "Newsletter Subscribers"',
'resend segments create --name "Beta Users" --json',
],
}),
)
.action(async (opts, cmd) => {
const globalOpts = cmd.optsWithGlobals() as GlobalOpts;
const name = await requireText(
opts.name,
{ message: 'Segment name', placeholder: 'e.g. Newsletter Subscribers' },
{ message: 'Missing --name flag.', code: 'missing_name' },
globalOpts,
);
await runCreate(
{
loading: 'Creating segment...',
sdkCall: (resend) => resend.segments.create({ name }),
onInteractive: (data) => {
console.log(`Segment created: ${safeTerminalText(data.id)}`);
console.log(`Name: ${safeTerminalText(data.name)}`);
},
},
globalOpts,
);
});