0.0.29 • Published 7 months ago

@runmorph/cdk v0.0.29

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
-
Last release
7 months ago

Morph – Connector Development Kit (CDK)

npm version License: Proprietary

The Morph Connector Development Kit (CDK) is a powerful toolkit for building and managing unified third-party connectors. It provides developers with a robust set of tools and utilities to streamline the process of creating, testing, and deploying connectors for various services and platforms.

FieldMapper

The FieldMapper class is a utility for mapping fields between remote data sources and unified models. It handles reading field values from remote data and writing field values back to remote data.

Usage

import { FieldMapper, FieldType, createField } from "@runmorph/cdk";

// Example remote data type
interface HubspotContact {
  id: string;
  properties: {
    firstname: string;
    lastname: string;
    email: string;
    phone: string;
    jobtitle: string;
    company: string;
  };
}

// Create a field mapper for the firstName field
const firstNameField = new FieldMapper<HubspotContact>({
  key: "firstName",
  name: "First Name",
  description: "The contact's first name",
  required: true,
  type: FieldType.TEXT,
  read: (from) => from("properties.firstname"),
  write: (to) => to("properties.firstname"),
});

// Create a field mapper for a select field with options
const jobTitleField = createField<HubspotContact>({
  key: "jobTitle",
  name: "Job Title",
  description: "The contact's job title",
  type: FieldType.SELECT,
  options: [
    { label: "CEO", value: "ceo" },
    { label: "CTO", value: "cto" },
    { label: "Developer", value: "developer" },
  ],
  read: (from) => from("properties.jobtitle"),
  write: (to) => to("properties.jobtitle"),
});

// Example usage: Reading data
const hubspotContact: HubspotContact = {
  id: "12345",
  properties: {
    firstname: "John",
    lastname: "Doe",
    email: "john.doe@example.com",
    phone: "+1234567890",
    jobtitle: "cto",
    company: "Acme Inc.",
  },
};

const firstName = firstNameField.read(hubspotContact);
console.log(firstName); // "John"

const jobTitle = jobTitleField.read(hubspotContact);
console.log(jobTitle); // "cto"

// Example usage: Writing data
const firstNameUpdate = firstNameField.write("Jane");
console.log(firstNameUpdate); // { properties: { firstname: 'Jane' } }

const jobTitleUpdate = jobTitleField.write("developer");
console.log(jobTitleUpdate); // { properties: { jobtitle: 'developer' } }

Advanced Transformations

You can also apply transformations when reading from or writing to remote data:

// Create a field mapper with transformations
const dateField = new FieldMapper<HubspotContact>({
  key: "createdAt",
  name: "Created At",
  description: "The date when the contact was created",
  type: FieldType.DATE,
  read: (from) => from("properties.createdate", (value) => new Date(value)),
  write: (to) => to("properties.createdate", (value) => value.toISOString()),
});

// Create a field mapper for a boolean field with transformation
const isCustomerField = createField<HubspotContact>({
  key: "isCustomer",
  name: "Is Customer",
  description: "Whether the contact is a customer",
  type: FieldType.BOOLEAN,
  read: (from) =>
    from("properties.hs_is_customer", (value) => value === "true"),
  write: (to) =>
    to("properties.hs_is_customer", (value) => (value ? "true" : "false")),
});

Validation

You can add validation to fields using Zod schemas:

import { z } from "zod";

// Create a field mapper with validation
const emailField = new FieldMapper<HubspotContact>({
  key: "email",
  name: "Email",
  description: "The contact's email address",
  required: true,
  type: FieldType.TEXT,
  validation: z.string().email(),
  read: (from) => from("properties.email"),
  write: (to) => to("properties.email"),
});

// Validate an email
const isValid = emailField.validate("not-an-email"); // false
const isValidEmail = emailField.validate("user@example.com"); // true
0.0.20

1 year ago

0.0.21

1 year ago

0.0.22

1 year ago

0.0.23

1 year ago

0.0.24

1 year ago

0.0.25

1 year ago

0.0.19

1 year ago

0.0.26

12 months ago

0.0.27

9 months ago

0.0.28

7 months ago

0.0.29

7 months ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.11

1 year ago

0.0.3

1 year ago

0.0.10

1 year ago

0.0.2

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.1

1 year ago