Skip to content

Commit

Permalink
Merge pull request #1 from joinflux/feature/validate-webhook-types
Browse files Browse the repository at this point in the history
Add validation and autocompletion for Trigger Types
  • Loading branch information
gugahoi authored Jul 12, 2023
2 parents 1a05fe3 + 3edcb8f commit 697fc39
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,6 @@ import (
)

// CreateResponse represents a response to the create request in Webflow.
//
// The following are available trigger types:
//
// - form_submission
// - site_publish
// - ecomm_new_order
// - ecomm_order_changed
// - ecomm_inventory_changed
// - memberships_user_account_added
// - memberships_user_account_updated
// - memberships_user_account_deleted
// - collection_item_created
// - collection_item_changed
// - collection_item_deleted
// - collection_item_unpublished
//
// See: https://developers.webflow.com/reference/create-webhook.
type CreateResponse struct {
CreatedOn string
Expand All @@ -36,19 +20,71 @@ type CreateResponse struct {
TriggerType string
}

// TriggerTypes is a list of all available trigger types that can be created in Webflow.
var TriggerTypes = []string{
"form_submission",
"site_publish",
"ecomm_new_order",
"ecomm_order_changed",
"ecomm_inventory_changed",
"memberships_user_account_added",
"memberships_user_account_updated",
"memberships_user_account_deleted",
"collection_item_created",
"collection_item_changed",
"collection_item_deleted",
"collection_item_unpublished",
}

// createCmd represents the command to create a webhook for a site in Webflow.
var createCmd = &cobra.Command{
Use: "create [site_id] [trigger_type] [url]",
Short: "create webhooks for a site",
Args: cobra.ExactArgs(3),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 1 {
var candidates = []string{}
// for autocompletion, we will suggest anything that contains the string
// we are typing regardless of where in the string we match.
// For example, if someone types: "item"
// We will suggest:
// - "collection_item_created"
// - "collection_item_changed"
// - "collection_item_deleted"
// - "collection_item_unpublished"
for _, value := range TriggerTypes {
if strings.Contains(value, toComplete) {
candidates = append(candidates, value)
}
}
// if there are no valid suggestions, suggest all available options
if len(candidates) == 0 {
candidates = TriggerTypes
}
return candidates, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveError
},
PreRunE: func(cmd *cobra.Command, args []string) error {
triggerType := args[1]
for _, b := range TriggerTypes {
if b == triggerType {
return nil
}
}
return fmt.Errorf("unknown Trigger Type: '%s'.\ntrigger_type must be one of: %+q\n", triggerType, TriggerTypes)
},
Run: func(cmd *cobra.Command, args []string) {
siteId := args[0]
triggerType := args[1]
url := args[2]

c := internal.NewClient(ApiToken)

payload := strings.NewReader(fmt.Sprintf(`{
"triggerType": "%s",
"url": "%s"
}`, args[1], args[2]))
}`, triggerType, url))

body, err := c.Post([]string{"sites", siteId, "webhooks"}, payload)

Expand Down

0 comments on commit 697fc39

Please sign in to comment.