|
| 1 | +import type { |
| 2 | + App, ConfigurableProp, ConfigurablePropApp, ConfigurablePropBoolean, ConfigurablePropInteger, ConfigurablePropString, ConfigurablePropStringArray, PropValue, |
| 3 | +} from "@pipedream/sdk"; |
| 4 | + |
| 5 | +export type PropOptionValue<T> = { |
| 6 | + __lv: { |
| 7 | + value: T |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +export function valueFromOption<T>(value: T | PropOptionValue<T>): T | undefined | null { |
| 12 | + if (typeof value === "object" && value && "__lv" in value) { |
| 13 | + return (value as PropOptionValue<T>).__lv.value |
| 14 | + } |
| 15 | + return value |
| 16 | +} |
| 17 | + |
| 18 | +export type PropOption<T> = { |
| 19 | + emitValue: T | PropOptionValue<T> |
| 20 | +} |
| 21 | +export type PropOptions<T> = { |
| 22 | + selectedOptions: PropOption<T>[] |
| 23 | +} |
| 24 | + |
| 25 | +export function valuesFromOptions<T>(value: unknown | T[] | PropOptions<T>): T[] { |
| 26 | + if (typeof value === "object" && value && "selectedOptions" in value && Array.isArray(value.selectedOptions)) { |
| 27 | + const selectedOptions = value.selectedOptions as PropOption<T>[] |
| 28 | + const results = [] |
| 29 | + for (const so of selectedOptions) { |
| 30 | + if (typeof so === "object" && so && "emitValue" in so) { |
| 31 | + const emitValue = so.emitValue as T | PropOptionValue<T> |
| 32 | + if (typeof emitValue === "object" && emitValue && "__lv" in emitValue) { |
| 33 | + results.push(emitValue.__lv.value) |
| 34 | + } |
| 35 | + results.push(emitValue) |
| 36 | + } |
| 37 | + throw "unexpected value" |
| 38 | + } |
| 39 | + } |
| 40 | + if (!Array.isArray(value)) |
| 41 | + throw "unexpected value" |
| 42 | + return value as T[] |
| 43 | +} |
| 44 | + |
| 45 | +export type ValidationOpts<T extends ConfigurableProp> = { |
| 46 | + prop: T |
| 47 | + value: unknown |
| 48 | + app?: App |
| 49 | +} |
| 50 | + |
| 51 | +export function arrayPropErrors(opts: ValidationOpts<ConfigurablePropStringArray>): string[] | undefined { |
| 52 | + const _values = valuesFromOptions(opts.value) |
| 53 | + if (typeof _values === "undefined") { |
| 54 | + return [ |
| 55 | + "required", |
| 56 | + ] |
| 57 | + } |
| 58 | + if (Array.isArray(_values) && !_values.length) return [ |
| 59 | + "empty array", |
| 60 | + ] |
| 61 | +} |
| 62 | + |
| 63 | +export function booleanPropErrors(opts: ValidationOpts<ConfigurablePropBoolean>): string[] | undefined { |
| 64 | + const _value = valueFromOption(opts.value) |
| 65 | + if (_value == null || typeof _value === "undefined") return [ |
| 66 | + "required", |
| 67 | + ] |
| 68 | +} |
| 69 | + |
| 70 | +export function integerPropErrors(opts: ValidationOpts<ConfigurablePropInteger>): string[] | undefined { |
| 71 | + const { |
| 72 | + prop, value: valueOpt, |
| 73 | + } = opts |
| 74 | + const value = valueFromOption(valueOpt) |
| 75 | + |
| 76 | + if (value == null || typeof value === "undefined") return [ |
| 77 | + "required", |
| 78 | + ] |
| 79 | + |
| 80 | + const _value: number = typeof value === "number" |
| 81 | + ? value |
| 82 | + : parseInt(String(value)) |
| 83 | + |
| 84 | + if (_value !== _value) return [ |
| 85 | + "not a number", |
| 86 | + ] // NaN |
| 87 | + const errs = [] |
| 88 | + if (typeof prop.min === "number" && _value < prop.min) errs.push("number too small") |
| 89 | + if (typeof prop.max === "number" && _value > prop.max) errs.push("number too big") |
| 90 | + return errs |
| 91 | +} |
| 92 | + |
| 93 | +export function stringPropErrors(opts: ValidationOpts<ConfigurablePropString>): string[] | undefined { |
| 94 | + const _value = valueFromOption(opts.value) |
| 95 | + if (!_value) return [ |
| 96 | + "string must not be empty", |
| 97 | + ] |
| 98 | +} |
| 99 | + |
| 100 | +type AppWithExtractedCustomFields = App & { |
| 101 | + extracted_custom_fields_names: string[] |
| 102 | +} |
| 103 | + |
| 104 | +type AppCustomField = { |
| 105 | + name: string |
| 106 | + optional?: boolean |
| 107 | +} |
| 108 | + |
| 109 | +type OauthAppPropValue = PropValue<"app"> & { |
| 110 | + oauth_access_token: string |
| 111 | +} |
| 112 | + |
| 113 | +type AppPropValueWithCustomFields<T extends AppCustomField[]> = PropValue<"app"> & { |
| 114 | + [K in T[number]["name"]]: T[number] |
| 115 | +} |
| 116 | + |
| 117 | +function getCustomFields(app: App): AppCustomField[] { |
| 118 | + const isOauth = app.auth_type === "oauth" |
| 119 | + const userDefinedCustomFields = JSON.parse(app.custom_fields_json || "[]") |
| 120 | + if ("extracted_custom_fields_names" in app && app.extracted_custom_fields_names) { |
| 121 | + const extractedCustomFields = ((app as AppWithExtractedCustomFields).extracted_custom_fields_names || []).map( |
| 122 | + (name) => ({ |
| 123 | + name, |
| 124 | + }), |
| 125 | + ) |
| 126 | + userDefinedCustomFields.push(...extractedCustomFields) |
| 127 | + } |
| 128 | + return userDefinedCustomFields.map((cf: AppCustomField) => { |
| 129 | + return { |
| 130 | + ...cf, |
| 131 | + // if oauth, treat all as optional (they are usually needed for getting access token) |
| 132 | + optional: cf.optional || isOauth, |
| 133 | + } |
| 134 | + }) |
| 135 | +} |
| 136 | + |
| 137 | +export function appPropErrors(opts: ValidationOpts<ConfigurablePropApp>): string[] | undefined { |
| 138 | + const { |
| 139 | + app, value, |
| 140 | + } = opts |
| 141 | + if (!app) { |
| 142 | + return [ |
| 143 | + "app field not registered", |
| 144 | + ] |
| 145 | + } |
| 146 | + if (!value) { |
| 147 | + return [ |
| 148 | + "no app configured", |
| 149 | + ] |
| 150 | + } |
| 151 | + if (typeof value !== "object") { |
| 152 | + return [ |
| 153 | + "not an app", |
| 154 | + ] |
| 155 | + } |
| 156 | + const _value = value as PropValue<"app"> |
| 157 | + if ("authProvisionId" in _value && !_value.authProvisionId) { |
| 158 | + if (app.auth_type) { |
| 159 | + const errs = [] |
| 160 | + if (app.auth_type === "oauth" && !(_value as OauthAppPropValue).oauth_access_token) { |
| 161 | + errs.push("missing oauth token") |
| 162 | + } |
| 163 | + if (app.auth_type === "oauth" || app.auth_type === "keys") { |
| 164 | + const customFields = getCustomFields(app) |
| 165 | + const _valueWithCustomFields = _value as AppPropValueWithCustomFields<typeof customFields> |
| 166 | + for (const cf of customFields) { |
| 167 | + if (!cf.optional && !_valueWithCustomFields[cf.name]) { |
| 168 | + errs.push(`missing custom field: ${cf.name}`) |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + if (app.auth_type !== "none") |
| 173 | + errs.push("no auth provision configured") |
| 174 | + return errs |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments