UNPKG

stitch-ui

Version:

468 lines (445 loc) 13.4 kB
import React from "react"; // eslint-disable-line no-unused-vars import { List, Map, OrderedMap } from "immutable"; import * as svg from "../svg"; import EditMongoRules from "./mongodb/components/EditRules"; import EditHTTPIncomingWebhookOptions from "./http/components/EditIncomingWebhook"; import HTTPCompleter from "./http/completion"; import EditGithubIncomingWebhookOptions from "./github/components/EditIncomingWebhook"; import EditTwilioConfig from "./twilio/components/EditConfig"; import TwilioCompleter from "./twilio/completion"; import EditAWSConfig from "./aws/components/EditConfig"; import EditPubNubConfig from "./pubnub/components/EditConfig"; import EditMongoConfig from "./mongodb/components/EditConfig"; import EditMongoAtlasConfig from "./mongodb-atlas/components/EditConfig"; import EditSlackConfig from "./slack/components/EditConfig"; import EditSlackIncomingWebhookOptions from "./slack/components/EditIncomingWebhook"; import EditMailgunConfig from "./mailgun/components/EditConfig"; import EditGCMConfig from "./push/gcm/components/EditConfig"; import { AvailableService, Action, IncomingWebhook, Pipeline, PipelineStage } from "../models"; export const SVCTYPE_MONGODB = "mongodb"; export const SVCTYPE_MONGODB_ATLAS = "mongodb-atlas"; export const SVCTYPE_PUBNUB = "pubnub"; export const SVCTYPE_TWILIO = "twilio"; export const SVCTYPE_HTTP = "http"; export const SVCTYPE_AWS_S3 = "aws-s3"; export const SVCTYPE_AWS_SES = "aws-ses"; export const SVCTYPE_AWS_SQS = "aws-sqs"; export const SVCTYPE_GITHUB = "github"; export const SVCTYPE_SLACK = "slack"; export const SVCTYPE_MAILGUN = "mailgun"; export const SVCTYPE_GCM = "gcm"; export const CONFIG_TAB = "config"; export const INCOMING_WEBHOOK_TAB = "incomingWebhooks"; export const RULES_TAB = "rules"; export const isMongoService = serviceType => serviceType === SVCTYPE_MONGODB || serviceType === SVCTYPE_MONGODB_ATLAS; const makeActionsMap = actions => OrderedMap(actions.map(action => [action.name, action])); const mongoDefaultArgs = Map({ database: "dbname", collection: "c_name" }); const httpDefaultArgs = Map({ url: "http://httpbin.org/post" }); const sqsDefaultArgs = Map({ args: { queueURL: "http://sqs.us-east-1.amazonaws.com/123456789012/MyQueue" } }); // TODO build this list dynamically using data from server. const servicesList = [ new AvailableService({ type: SVCTYPE_MONGODB, description: "Store and query data in MongoDB instance", displayName: "MongoDB", defaultTab: CONFIG_TAB, rulesEditor: EditMongoRules, configEditor: EditMongoConfig, incomingWebhooks: false, actions: makeActionsMap([ new Action({ name: "find", defaultArgs: mongoDefaultArgs.merge( Map({ query: {}, project: {}, limit: 10 }) ) }), new Action({ name: "insert", defaultArgs: mongoDefaultArgs }), new Action({ name: "update", defaultArgs: mongoDefaultArgs.merge( Map({ query: {}, update: { $inc: { counter: 1 } }, upsert: false, multi: false }) ) }), new Action({ name: "aggregate", defaultArgs: mongoDefaultArgs.merge( Map({ pipeline: [{ $group: { _id: "name", count: { $sum: 1 } } }] }) ) }), new Action({ name: "delete", defaultArgs: mongoDefaultArgs.merge( Map({ singleDoc: true, query: { counter: 10 } }) ) }) ]), hideAdd: true, hideSidebar: true, icon: svg.MongoDB }), new AvailableService({ type: SVCTYPE_MONGODB_ATLAS, description: "Store and query data in a MongoDB Atlas instance", displayName: "MongoDB Atlas", defaultTab: CONFIG_TAB, rulesEditor: EditMongoRules, configEditor: EditMongoAtlasConfig, incomingWebhooks: false, actions: makeActionsMap([ new Action({ name: "find", defaultArgs: mongoDefaultArgs.merge( Map({ query: {}, project: {}, limit: 10 }) ) }), new Action({ name: "insert", defaultArgs: mongoDefaultArgs }), new Action({ name: "update", defaultArgs: mongoDefaultArgs.merge( Map({ query: {}, update: { $inc: { counter: 1 } }, upsert: false, multi: false }) ) }), new Action({ name: "aggregate", defaultArgs: mongoDefaultArgs.merge( Map({ pipeline: [{ $group: { _id: "name", count: { $sum: 1 } } }] }) ) }), new Action({ name: "delete", defaultArgs: mongoDefaultArgs.merge( Map({ singleDoc: true, query: { counter: 10 } }) ) }) ]), icon: svg.MongoDB, hideAdd: true, hideSidebar: true }), new AvailableService({ type: SVCTYPE_PUBNUB, description: "Publish and subscribe over realtime messaging channels", displayName: "PubNub", defaultTab: CONFIG_TAB, actions: makeActionsMap([ new Action({ name: "publish", defaultArgs: Map({ channel: "testchannel", message: "helloworld!" }) }) ]), incomingWebhooks: false, configEditor: EditPubNubConfig, icon: svg.PubNub }), new AvailableService({ type: SVCTYPE_TWILIO, description: "Send and receive text messages", ruleCompleter: TwilioCompleter.rules, displayName: "Twilio", defaultTab: CONFIG_TAB, actions: makeActionsMap([ new Action({ name: "send", defaultArgs: Map({ to: "+12013705553", from: "+12018675309", body: "hello from twilio!" }) }) ]), incomingWebhooks: true, configEditor: EditTwilioConfig, defaultIncomingWebhook: new IncomingWebhook({ pipeline: new Pipeline({ output: "array", stages: new List([ new PipelineStage({ service: "", action: "literal", args: { items: ["%%vars.foo"] }, let: { foo: "%%args" } }), new PipelineStage({ service: "mongodb1", action: "insert", args: { database: "test1", collection: "twilio" } }) ]) }), respondResult: false, output: "array" }), icon: svg.Twilio }), new AvailableService({ type: SVCTYPE_HTTP, description: "Send GET and POST requests over HTTP", displayName: "HTTP", defaultTab: INCOMING_WEBHOOK_TAB, actions: makeActionsMap([ new Action({ name: "get", defaultArgs: httpDefaultArgs }), new Action({ name: "post", defaultArgs: httpDefaultArgs }), new Action({ name: "put", defaultArgs: httpDefaultArgs }), new Action({ name: "delete", defaultArgs: httpDefaultArgs }), new Action({ name: "patch", defaultArgs: httpDefaultArgs }), new Action({ name: "head", defaultArgs: httpDefaultArgs }) ]), ruleCompleter: HTTPCompleter.rules, incomingWebhookOptionsEditor: EditHTTPIncomingWebhookOptions, incomingWebhooks: true, defaultIncomingWebhook: new IncomingWebhook({ pipeline: new Pipeline({ output: "array", stages: new List([ new PipelineStage({ service: "", action: "literal", args: { items: ["%%vars"] }, let: { body: "%%args.body", query: "%%args.query" } }) ]) }), options: new Map({ secret: "", verifyPayload: true }), respondResult: false, output: "array" }), icon: svg.Code }), new AvailableService({ type: SVCTYPE_AWS_S3, description: "Upload files into an S3 bucket", displayName: "S3", defaultTab: CONFIG_TAB, longDisplayName: "AWS - S3", configEditor: EditAWSConfig, actions: makeActionsMap([ new Action({ name: "put" }), new Action({ name: "signPolicy" }) ]), incomingWebhooks: false, icon: svg.AWS }), new AvailableService({ type: SVCTYPE_AWS_SES, description: "Send e-mails with SES", displayName: "SES", defaultTab: CONFIG_TAB, actions: makeActionsMap([new Action({ name: "send" })]), incomingWebhooks: false, configEditor: EditAWSConfig, icon: svg.AWS }), new AvailableService({ type: SVCTYPE_AWS_SQS, description: "Send and receive queue events", displayName: "SQS", defaultTab: CONFIG_TAB, configEditor: EditAWSConfig, incomingWebhooks: false, actions: makeActionsMap([ new Action({ name: "send", defaultArgs: sqsDefaultArgs }), new Action({ name: "receive", defaultArgs: sqsDefaultArgs }) ]), longDisplayName: "AWS - SQS", icon: svg.SQS }), new AvailableService({ type: SVCTYPE_GITHUB, description: "Respond to Github events", displayName: "Github", defaultTab: INCOMING_WEBHOOK_TAB, actions: OrderedMap(), incomingWebhooks: true, longDisplayName: "Github", incomingWebhookOptionsEditor: EditGithubIncomingWebhookOptions, defaultIncomingWebhook: new IncomingWebhook({ pipeline: new Pipeline({ output: "array", stages: new List([ new PipelineStage({ service: "", action: "literal", args: { items: ["%%vars.payload"] }, let: { payload: "%%args" } }), new PipelineStage({ service: "", action: "match", args: { expression: { pull_request: { "%exists": true } }, action: { "%in": ["opened", "synchronize"] } } }) ]) }), options: new Map({ secret: "" }), respondResult: false, output: "array" }), icon: svg.Github }), new AvailableService({ type: SVCTYPE_MAILGUN, description: "Send emails via Mailgun", displayName: "Mailgun", defaultTab: CONFIG_TAB, actions: makeActionsMap([ new Action({ name: "send", defaultArgs: Map({ to: ["steve@apple.com"], from: "bill@microsoft.com", subject: "hi", text: "whats up" }) }) ]), configEditor: EditMailgunConfig, icon: svg.Mailgun }), new AvailableService({ type: SVCTYPE_SLACK, description: "Send and receive messages with a Slack channel", displayName: "Slack", defaultTab: CONFIG_TAB, actions: makeActionsMap([ new Action({ name: "post", defaultArgs: Map({ channel: "watercooler", username: "dan", text: "hi slack!" }) }) ]), incomingWebhooks: true, incomingWebhookOptionsEditor: EditSlackIncomingWebhookOptions, defaultIncomingWebhook: new IncomingWebhook({ pipeline: new Pipeline({ output: "array", stages: new List([ new PipelineStage({ service: "", action: "literal", args: { items: ["%%vars"] }, let: { text: "%%args.text" } }) ]) }), options: new Map({ token: "" }), respondResult: false, output: "array" }), configEditor: EditSlackConfig, icon: svg.Slack }), new AvailableService({ type: SVCTYPE_GCM, description: "Push Notifications via Google Cloud Messaging", displayName: "GCM", defaultTab: CONFIG_TAB, longDisplayName: "Google Cloud Messaging", actions: makeActionsMap([ new Action({ name: "send", defaultArgs: Map({ userIds: ["58f8dab21d0e26622d8fbd79"], notification: { body: "hello!" } }) }) ]), incomingWebhooks: false, configEditor: EditGCMConfig, hideAdd: true, hideSidebar: true, icon: svg.PushNotifications }) ]; export const nopService = new AvailableService({ actions: makeActionsMap([]) }); export const GlobalService = new AvailableService({ type: "", description: "", displayName: "", actions: makeActionsMap([ new Action({ name: "match", defaultArgs: Map({ expression: { x: { "%gt": 0 } } }) }), new Action({ name: "literal", defaultArgs: Map({ items: [{ x: 1, y: "foo" }] }) }), new Action({ name: "expr", defaultArgs: Map({ expression: { y: { "%concat": ["%%vars.x", "%%vars.y"] } } }), defaultLet: Map({ item: "%%item" }) }), new Action({ name: "project", defaultArgs: Map({ projection: { name: 1, age: 1, "location.city": 1 } }) }), new Action({ name: "namedPipeline", defaultArgs: Map({ name: "myPipeline", args: { x: 1, y: 2 } }) }), new Action({ name: "null" }), new Action({ name: "reader" }), new Action({ name: "binary" }), new Action({ name: "encode" }) ]) }); export const servicesByType = OrderedMap( servicesList.map(svc => [svc.type, svc]) ); export const findServiceDefinition = (serviceName, appServices) => { if (serviceName === "") return GlobalService; if (!Object.prototype.hasOwnProperty.call(appServices, serviceName)) { return nopService; } const appSvc = appServices[serviceName]; if (!servicesByType.has(appSvc.type)) { return nopService; } return servicesByType.get(appSvc.type); };