-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ts
74 lines (55 loc) · 1.75 KB
/
create.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { BaseCommand, Flags } from '../../base'
import { URL } from 'node:url'
import { clColor } from '@commercelayer/cli-core'
import type { CommandError } from '@oclif/core/lib/interfaces'
export default class WebhooksCreate extends BaseCommand {
static description = 'create a new webhook'
static aliases = ['wh:create']
static examples = [
'$ commercelayer webhooks:create -t customers.create -u https://callback.url.io',
'$ cl wh:create -t orders.place -u http://myurl.com'
]
static flags = {
...BaseCommand.flags,
topic: Flags.string({
char: 't',
description: 'the identifier of the event that will trigger the webhook',
required: true
}),
url: Flags.string({
char: 'u',
description: 'the callback URL used to POST data',
required: true
}),
include: Flags.string({
char: 'i',
description: 'a comma separated list of related resources to be included',
multiple: true
}),
name: Flags.string({
char: 'n',
description: 'the webhook short name',
default: '' // TEMP: the sdk currently consider this field as mandatory
})
}
async run(): Promise<any> {
const { flags } = await this.parse(WebhooksCreate)
const cl = this.commercelayerInit(flags)
const topic = flags.topic
const url = new URL(flags.url).toString()
const include = flags.include ? flags.include.join(',').split(',') : undefined
const name = flags.name
try {
const webhook = await cl.webhooks.create({
topic,
name,
callback_url: url,
include_resources: include,
})
this.log(`\n${clColor.msg.success('Successfully')} created new webhook with topic ${clColor.bold(topic)} and id ${clColor.api.id(webhook.id)}\n`)
return webhook
} catch (error) {
this.handleError(error as CommandError, flags)
}
}
}