Zapier add description to labels (#3787)
* Use object metadata graphql api to fetch input fields * Clean code * Clean code * Remove targetColumnMap * Remove duplicated testing * Fix labels
This commit is contained in:
@ -1,63 +1,156 @@
|
||||
import { labelling } from '../utils/labelling';
|
||||
import {
|
||||
FieldMetadataType,
|
||||
InputField,
|
||||
Node,
|
||||
NodeField,
|
||||
} from '../utils/data.types';
|
||||
|
||||
type Infos = {
|
||||
properties: {
|
||||
[field: string]: {
|
||||
type: string;
|
||||
properties?: { [field: string]: { type: string } };
|
||||
items?: { [$ref: string]: string };
|
||||
};
|
||||
};
|
||||
example: object;
|
||||
required: string[];
|
||||
const getTypeFromFieldMetadataType = (
|
||||
fieldMetadataType: string,
|
||||
): string | undefined => {
|
||||
switch (fieldMetadataType) {
|
||||
case FieldMetadataType.UUID:
|
||||
case FieldMetadataType.TEXT:
|
||||
case FieldMetadataType.PHONE:
|
||||
case FieldMetadataType.EMAIL:
|
||||
case FieldMetadataType.LINK:
|
||||
case FieldMetadataType.RATING:
|
||||
return 'string';
|
||||
case FieldMetadataType.DATE_TIME:
|
||||
return 'datetime';
|
||||
case FieldMetadataType.BOOLEAN:
|
||||
return 'boolean';
|
||||
case FieldMetadataType.NUMBER:
|
||||
return 'integer';
|
||||
case FieldMetadataType.NUMERIC:
|
||||
case FieldMetadataType.PROBABILITY:
|
||||
return 'number';
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const get_subfieldsFromField = (nodeField: NodeField): NodeField[] => {
|
||||
switch (nodeField.type) {
|
||||
case FieldMetadataType.FULL_NAME: {
|
||||
const firstName: NodeField = {
|
||||
type: 'TEXT',
|
||||
name: 'firstName',
|
||||
label: 'First Name',
|
||||
description: 'First Name',
|
||||
isNullable: true,
|
||||
defaultValue: null,
|
||||
};
|
||||
const lastName: NodeField = {
|
||||
type: 'TEXT',
|
||||
name: 'lastName',
|
||||
label: 'Last Name',
|
||||
description: 'Last Name',
|
||||
isNullable: true,
|
||||
defaultValue: null,
|
||||
};
|
||||
return [firstName, lastName];
|
||||
}
|
||||
case FieldMetadataType.LINK: {
|
||||
const url: NodeField = {
|
||||
type: 'TEXT',
|
||||
name: 'url',
|
||||
label: 'Url',
|
||||
description: 'Link Url',
|
||||
isNullable: true,
|
||||
defaultValue: null,
|
||||
};
|
||||
const label: NodeField = {
|
||||
type: 'TEXT',
|
||||
name: 'label',
|
||||
label: 'Label',
|
||||
description: 'Link Label',
|
||||
isNullable: true,
|
||||
defaultValue: null,
|
||||
};
|
||||
return [url, label];
|
||||
}
|
||||
case FieldMetadataType.CURRENCY: {
|
||||
const amountMicros: NodeField = {
|
||||
type: 'NUMBER',
|
||||
name: 'amountMicros',
|
||||
label: 'Amount Micros',
|
||||
description: 'Amount Micros. eg: set 3210000 for 3.21$',
|
||||
isNullable: true,
|
||||
defaultValue: null,
|
||||
};
|
||||
const currencyCode: NodeField = {
|
||||
type: 'TEXT',
|
||||
name: 'currencyCode',
|
||||
label: 'Currency Code',
|
||||
description: 'Currency Code. eg: USD, EUR, etc...',
|
||||
isNullable: true,
|
||||
defaultValue: null,
|
||||
};
|
||||
return [amountMicros, currencyCode];
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unknown nodeField type: ${nodeField.type}`);
|
||||
}
|
||||
};
|
||||
|
||||
const isFieldRequired = (nodeField: NodeField): boolean => {
|
||||
return !nodeField.isNullable && !nodeField.defaultValue;
|
||||
};
|
||||
|
||||
export const computeInputFields = (
|
||||
infos: Infos,
|
||||
idRequired = false,
|
||||
): object[] => {
|
||||
node: Node,
|
||||
isRequired = false,
|
||||
): InputField[] => {
|
||||
const result = [];
|
||||
|
||||
for (const fieldName of Object.keys(infos.properties)) {
|
||||
switch (infos.properties[fieldName].type) {
|
||||
case 'array':
|
||||
break;
|
||||
case 'object':
|
||||
if (!infos.properties[fieldName].properties) {
|
||||
break;
|
||||
}
|
||||
for (const subFieldName of Object.keys(
|
||||
infos.properties[fieldName].properties || {},
|
||||
)) {
|
||||
for (const field of node.fields.edges) {
|
||||
const nodeField = field.node;
|
||||
switch (nodeField.type) {
|
||||
case FieldMetadataType.FULL_NAME:
|
||||
case FieldMetadataType.LINK:
|
||||
case FieldMetadataType.CURRENCY:
|
||||
for (const subNodeField of get_subfieldsFromField(nodeField)) {
|
||||
const field = {
|
||||
key: `${fieldName}__${subFieldName}`,
|
||||
label: `${labelling(fieldName)}: ${labelling(subFieldName)}`,
|
||||
type: infos.properties[fieldName].properties?.[subFieldName].type,
|
||||
required: false,
|
||||
};
|
||||
if (infos.required?.includes(fieldName)) {
|
||||
field.required = true;
|
||||
}
|
||||
key: `${nodeField.name}__${subNodeField.name}`,
|
||||
label: `${nodeField.label}: ${subNodeField.label}`,
|
||||
type: getTypeFromFieldMetadataType(subNodeField.type),
|
||||
helpText: `${nodeField.description}: ${subNodeField.description}`,
|
||||
required: isFieldRequired(subNodeField),
|
||||
} as InputField;
|
||||
result.push(field);
|
||||
}
|
||||
break;
|
||||
default: {
|
||||
const field = {
|
||||
key: fieldName,
|
||||
label: labelling(fieldName),
|
||||
type: infos.properties[fieldName].type,
|
||||
required: false,
|
||||
};
|
||||
if (
|
||||
(idRequired && fieldName === 'id') ||
|
||||
(!idRequired && infos.required?.includes(fieldName))
|
||||
) {
|
||||
field.required = true;
|
||||
case FieldMetadataType.UUID:
|
||||
case FieldMetadataType.TEXT:
|
||||
case FieldMetadataType.PHONE:
|
||||
case FieldMetadataType.EMAIL:
|
||||
case FieldMetadataType.DATE_TIME:
|
||||
case FieldMetadataType.BOOLEAN:
|
||||
case FieldMetadataType.NUMBER:
|
||||
case FieldMetadataType.NUMERIC:
|
||||
case FieldMetadataType.PROBABILITY:
|
||||
case FieldMetadataType.RATING: {
|
||||
const nodeFieldType = getTypeFromFieldMetadataType(nodeField.type);
|
||||
if (!nodeFieldType) {
|
||||
break;
|
||||
}
|
||||
const required =
|
||||
(isRequired && nodeField.name === 'id') ||
|
||||
(!isRequired && isFieldRequired(nodeField));
|
||||
const field = {
|
||||
key: nodeField.name,
|
||||
label: nodeField.label,
|
||||
type: nodeFieldType,
|
||||
helpText: nodeField.description,
|
||||
required,
|
||||
};
|
||||
result.push(field);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result.sort((a, _) => (a.key === 'id' ? -1 : 1));
|
||||
};
|
||||
|
||||
@ -1,17 +0,0 @@
|
||||
import { Bundle, ZObject } from 'zapier-platform-core';
|
||||
|
||||
import { computeInputFields } from '../../utils/computeInputFields';
|
||||
import { requestSchema } from '../../utils/requestDb';
|
||||
import { capitalize } from '../capitalize';
|
||||
|
||||
export const recordInputFields = async (
|
||||
z: ZObject,
|
||||
bundle: Bundle,
|
||||
idRequired = false,
|
||||
) => {
|
||||
const schema = await requestSchema(z, bundle);
|
||||
const infos =
|
||||
schema.components.schemas[capitalize(bundle.inputData.nameSingular)];
|
||||
|
||||
return computeInputFields(infos, idRequired);
|
||||
};
|
||||
@ -1,2 +1,58 @@
|
||||
export type InputData = { [x: string]: any };
|
||||
|
||||
export type ObjectData = { id: string } | { [x: string]: any };
|
||||
|
||||
export type NodeField = {
|
||||
type: string;
|
||||
name: string;
|
||||
label: string;
|
||||
description: string | null;
|
||||
isNullable: boolean;
|
||||
defaultValue: object | null;
|
||||
};
|
||||
|
||||
export type Node = {
|
||||
nameSingular: string;
|
||||
namePlural: string;
|
||||
labelSingular: string;
|
||||
fields: {
|
||||
edges: {
|
||||
node: NodeField;
|
||||
}[];
|
||||
};
|
||||
};
|
||||
|
||||
export type InputField = {
|
||||
key: string;
|
||||
label: string;
|
||||
type: string;
|
||||
helpText: string | null;
|
||||
required: boolean;
|
||||
};
|
||||
|
||||
export enum FieldMetadataType {
|
||||
UUID = 'UUID',
|
||||
TEXT = 'TEXT',
|
||||
PHONE = 'PHONE',
|
||||
EMAIL = 'EMAIL',
|
||||
DATE_TIME = 'DATE_TIME',
|
||||
BOOLEAN = 'BOOLEAN',
|
||||
NUMBER = 'NUMBER',
|
||||
NUMERIC = 'NUMERIC',
|
||||
PROBABILITY = 'PROBABILITY',
|
||||
LINK = 'LINK',
|
||||
CURRENCY = 'CURRENCY',
|
||||
FULL_NAME = 'FULL_NAME',
|
||||
RATING = 'RATING',
|
||||
SELECT = 'SELECT',
|
||||
MULTI_SELECT = 'MULTI_SELECT',
|
||||
RELATION = 'RELATION',
|
||||
}
|
||||
|
||||
export type Schema = {
|
||||
data: {
|
||||
objects: {
|
||||
edges: { node: Node }[];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
import { capitalize } from '../utils/capitalize';
|
||||
|
||||
export const labelling = (str: string): string => {
|
||||
return str
|
||||
.replace(/[A-Z]/g, (letter) => ` ${letter.toLowerCase()}`)
|
||||
.split(' ')
|
||||
.map((word) => capitalize(word))
|
||||
.join(' ');
|
||||
};
|
||||
@ -1,17 +1,36 @@
|
||||
import { Bundle, HttpRequestOptions, ZObject } from 'zapier-platform-core';
|
||||
|
||||
export const requestSchema = async (z: ZObject, bundle: Bundle) => {
|
||||
const options = {
|
||||
url: `${process.env.SERVER_BASE_URL}/open-api`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
Authorization: `Bearer ${bundle.authData.apiKey}`,
|
||||
},
|
||||
} satisfies HttpRequestOptions;
|
||||
import { Schema } from '../utils/data.types';
|
||||
|
||||
return z.request(options).then((response) => response.json);
|
||||
export const requestSchema = async (
|
||||
z: ZObject,
|
||||
bundle: Bundle,
|
||||
): Promise<Schema> => {
|
||||
const query = `query GetObjects {
|
||||
objects(paging: {first: 1000}, filter: {isActive: {is:true}}) {
|
||||
edges {
|
||||
node {
|
||||
nameSingular
|
||||
namePlural
|
||||
labelSingular
|
||||
fields(paging: {first: 1000}, filter: {isActive: {is:true}}) {
|
||||
edges {
|
||||
node {
|
||||
type
|
||||
name
|
||||
label
|
||||
description
|
||||
isNullable
|
||||
defaultValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`;
|
||||
const endpoint = 'metadata';
|
||||
return await requestDb(z, bundle, query, endpoint);
|
||||
};
|
||||
|
||||
const requestDb = async (
|
||||
|
||||
@ -2,7 +2,10 @@ import { Bundle, ZObject } from 'zapier-platform-core';
|
||||
|
||||
import { ObjectData } from '../../utils/data.types';
|
||||
import handleQueryParams from '../../utils/handleQueryParams';
|
||||
import requestDb, { requestDbViaRestApi } from '../../utils/requestDb';
|
||||
import requestDb, {
|
||||
requestDbViaRestApi,
|
||||
requestSchema,
|
||||
} from '../../utils/requestDb';
|
||||
|
||||
export enum Operation {
|
||||
create = 'create',
|
||||
@ -61,21 +64,7 @@ const getNamePluralFromNameSingular = async (
|
||||
bundle: Bundle,
|
||||
nameSingular: string,
|
||||
): Promise<string> => {
|
||||
const result = await requestDb(
|
||||
z,
|
||||
bundle,
|
||||
`query GetObjects {
|
||||
objects(paging: {first: 1000}) {
|
||||
edges {
|
||||
node {
|
||||
nameSingular
|
||||
namePlural
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
'metadata',
|
||||
);
|
||||
const result = await requestSchema(z, bundle);
|
||||
for (const object of result.data.objects.edges) {
|
||||
if (object.node.nameSingular === nameSingular) {
|
||||
return object.node.namePlural;
|
||||
|
||||
Reference in New Issue
Block a user