Skip to content

Commit

Permalink
Feature parity between InputField and GlobalSetting (#2505)
Browse files Browse the repository at this point in the history
* Extend GlobalSetting with InputField type. This grants GlobalSetting all UI capabilities of InputField

* Omits two fields from GlobalSetting which did not previously exist on it, but do exist on InputFieldJSONSchema

* Updates GlobalSetting.dynamic to be a function rather than a boolean. This avoids the need to introduce a new dynamicFields option on the destination definition

* Updates cli type usages to fix build

* Adds some comments on why certain inherited fields are excluded from the GlobalSetting type
  • Loading branch information
nick-Ag authored Nov 18, 2024
1 parent 92a2cd3 commit b647b36
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
14 changes: 11 additions & 3 deletions packages/cli/src/commands/generate/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Command, flags } from '@oclif/command'
import { fieldsToJsonSchema } from '@segment/actions-core'
import type { InputField, DestinationDefinition as CloudDestinationDefinition } from '@segment/actions-core'
import type {
InputField,
DestinationDefinition as CloudDestinationDefinition,
GlobalSetting
} from '@segment/actions-core'
import type { BrowserDestinationDefinition } from '@segment/destinations-manifest'
import chokidar from 'chokidar'
import fs from 'fs-extra'
Expand Down Expand Up @@ -186,7 +190,11 @@ export default class GenerateTypes extends Command {
}
}

async function generateTypes(fields: Record<string, InputField> = {}, name: string, bannerComment?: string) {
async function generateTypes(
fields: Record<string, InputField | GlobalSetting> = {},
name: string,
bannerComment?: string
) {
const schema = prepareSchema(fields)

return compile(schema, name, {
Expand All @@ -195,7 +203,7 @@ async function generateTypes(fields: Record<string, InputField> = {}, name: stri
})
}

function prepareSchema(fields: Record<string, InputField>): JSONSchema4 {
function prepareSchema(fields: Record<string, InputField | GlobalSetting>): JSONSchema4 {
let schema = fieldsToJsonSchema(fields, { tsType: true })
// Remove extra properties so it produces cleaner output
schema = removeExtra(schema)
Expand Down
10 changes: 7 additions & 3 deletions packages/cli/src/commands/scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { renderTemplates } from '../lib/templates'
import fs from 'fs-extra'
import { camelCase, startCase } from 'lodash'
import { fieldsToJsonSchema } from '@segment/actions-core'
import type { InputField, DestinationDefinition as CloudDestinationDefinition } from '@segment/actions-core'
import type {
InputField,
DestinationDefinition as CloudDestinationDefinition,
GlobalSetting
} from '@segment/actions-core'
import type { BrowserDestinationDefinition } from '@segment/destinations-manifest'
import { JSONSchema4 } from 'json-schema'
import { compile } from 'json-schema-to-typescript'
Expand Down Expand Up @@ -253,7 +257,7 @@ export default class Init extends Command {
}
}

async function generateTypes(fields: Record<string, InputField> = {}, name: string) {
async function generateTypes(fields: Record<string, InputField | GlobalSetting> = {}, name: string) {
const schema = prepareSchema(fields)

return compile(schema, name, {
Expand All @@ -262,7 +266,7 @@ async function generateTypes(fields: Record<string, InputField> = {}, name: stri
})
}

function prepareSchema(fields: Record<string, InputField>): JSONSchema4 {
function prepareSchema(fields: Record<string, InputField | GlobalSetting>): JSONSchema4 {
let schema = fieldsToJsonSchema(fields, { tsType: true })
// Remove extra properties so it produces cleaner output
schema = removeExtra(schema)
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/destination-kit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
TransactionContext,
EngageDestinationCache,
ActionHookType,
SubscriptionMetadata
SubscriptionMetadata,
RequestFn
} from './index'
import type { RequestOptions } from '../request-client'
import type { JSONLikeObject, JSONObject } from '../json-object'
Expand Down Expand Up @@ -101,15 +102,15 @@ export interface DynamicFieldItem {
}

/** The shape of authentication and top-level settings */
export interface GlobalSetting {
/** A short, human-friendly label for the field */
label: string
/** A human-friendly description of the field */
description: string
export interface GlobalSetting
extends Omit<
InputField,
| 'additionalProperties' // Settings cannot be an object
| 'defaultObjectUI' // Settings cannot be an object
| 'dynamic' // This type is redeclared
> {
/** A subset of the available DestinationMetadataOption types */
type: 'boolean' | 'string' | 'password' | 'number'
/** Whether or not the field accepts more than one of its `type` */
multiple?: boolean
/**
* A predefined set of options for the setting.
* Only relevant for `type: 'string'` or `type: 'number'`.
Expand All @@ -120,11 +121,9 @@ export interface GlobalSetting {
/** A human-friendly label for the option */
label: string
}>
required?: boolean
default?: string | number | boolean
properties?: InputField['properties']
format?: InputField['format']
depends_on?: InputField['depends_on']

dynamic?: RequestFn<Record<string, boolean | string | number>, {}>
}

/** The supported field type names */
Expand Down

0 comments on commit b647b36

Please sign in to comment.