Skip to content

Commit 1fd58bd

Browse files
committed
feat(sdk): improve configurable_prop types
1 parent ae1b6f8 commit 1fd58bd

File tree

1 file changed

+113
-30
lines changed

1 file changed

+113
-30
lines changed

packages/sdk/src/shared/component.ts

Lines changed: 113 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ type BaseConfigurableProp = {
5454
withLabel?: boolean;
5555
};
5656

57-
// XXX fix duplicating mapping to value type here and with PropValue
58-
type Defaultable<T> = { default?: T; options?: T[]; };
57+
type Defaultable<T> = { default?: T; options?: T[] };
58+
59+
// Define a type for string-based property options
60+
export type StringPropOptionObject = { label?: string; value: string };
61+
export type StringPropOption = string | StringPropOptionObject;
5962

6063
export type ConfigurablePropAlert = BaseConfigurableProp & {
6164
type: "alert";
@@ -69,7 +72,9 @@ export type ConfigurablePropApp = BaseConfigurableProp & {
6972
type: "app";
7073
app: string;
7174
};
72-
export type ConfigurablePropBoolean = BaseConfigurableProp & { type: "boolean"; };
75+
export type ConfigurablePropBoolean = BaseConfigurableProp & {
76+
type: "boolean";
77+
} & Defaultable<boolean>;
7378
export type ConfigurablePropInteger = BaseConfigurableProp & {
7479
type: "integer";
7580
min?: number;
@@ -81,54 +86,129 @@ export type ConfigurablePropObject = BaseConfigurableProp & {
8186
export type ConfigurablePropString = BaseConfigurableProp & {
8287
type: "string";
8388
secret?: boolean;
84-
} & Defaultable<string>;
89+
default?: string;
90+
options?: StringPropOption[];
91+
};
8592
export type ConfigurablePropStringArray = BaseConfigurableProp & {
8693
type: "string[]";
8794
secret?: boolean; // TODO is this supported
88-
} & Defaultable<string[]>; // TODO
89-
// | { type: "$.interface.http" } // source only
90-
// | { type: "$.interface.timer" } // source only
91-
// | { type: "$.service.db" }
92-
// | { type: "data_store" }
93-
// | { type: "http_request" }
94-
// | { type: "sql" } -- not in component api docs!
95+
default?: string[];
96+
options?: StringPropOption[];
97+
};
98+
export type ConfigurablePropNumber = BaseConfigurableProp & {
99+
type: "number";
100+
min?: number;
101+
max?: number;
102+
} & Defaultable<number>;
103+
export type ConfigurablePropNumberArray = BaseConfigurableProp & {
104+
type: "number[]";
105+
} & Defaultable<number>;
106+
export type ConfigurablePropIntegerArray = BaseConfigurableProp & {
107+
type: "integer[]";
108+
} & Defaultable<number>;
109+
export type ConfigurablePropBooleanArray = BaseConfigurableProp & {
110+
type: "boolean[]";
111+
} & Defaultable<boolean>;
112+
export type ConfigurablePropDiscordChannel = BaseConfigurableProp & {
113+
type: "$.discord.channel";
114+
[key: string]: unknown;
115+
} & Defaultable<string>;
116+
export type ConfigurablePropDiscordChannelArray = BaseConfigurableProp & {
117+
type: "$.discord.channel[]";
118+
[key: string]: unknown;
119+
} & Defaultable<string>;
120+
121+
export type ConfigurablePropInterfaceHttp = BaseConfigurableProp & {
122+
type: "$.interface.http";
123+
[key: string]: unknown;
124+
};
125+
export type ConfigurablePropInterfaceTimer = BaseConfigurableProp & {
126+
type: "$.interface.timer";
127+
[key: string]: unknown;
128+
};
129+
export type ConfigurablePropServiceDb = BaseConfigurableProp & {
130+
type: "$.service.db";
131+
[key: string]: unknown;
132+
};
133+
export type ConfigurablePropDataStore = BaseConfigurableProp & {
134+
type: "datastore";
135+
[key: string]: unknown;
136+
};
137+
export type ConfigurablePropHttpRequest = BaseConfigurableProp & {
138+
type: "http_request";
139+
[key: string]: unknown;
140+
};
141+
export type ConfigurablePropSql = BaseConfigurableProp & { type: "sql" };
142+
95143
export type ConfigurableProp =
96144
| ConfigurablePropAlert
97145
| ConfigurablePropAny
98146
| ConfigurablePropApp
99147
| ConfigurablePropBoolean
148+
| ConfigurablePropBooleanArray
100149
| ConfigurablePropInteger
150+
| ConfigurablePropIntegerArray
151+
| ConfigurablePropNumber
152+
| ConfigurablePropNumberArray
101153
| ConfigurablePropObject
102154
| ConfigurablePropString
103155
| ConfigurablePropStringArray
104-
| (BaseConfigurableProp & { type: "$.discord.channel"; });
156+
| ConfigurablePropDiscordChannel
157+
| ConfigurablePropDiscordChannelArray
158+
| ConfigurablePropInterfaceHttp
159+
| ConfigurablePropInterfaceTimer
160+
| ConfigurablePropServiceDb
161+
| ConfigurablePropDataStore
162+
| ConfigurablePropHttpRequest
163+
| ConfigurablePropSql;
105164

106165
export type ConfigurableProps = Readonly<ConfigurableProp[]>;
107166

108167
export type PropValue<T extends ConfigurableProp["type"]> = T extends "alert"
109168
? never
110169
: T extends "any"
111-
? any // eslint-disable-line @typescript-eslint/no-explicit-any
112-
: T extends "app"
113-
? { authProvisionId: string; }
114-
: T extends "boolean"
115-
? boolean
116-
: T extends "integer"
117-
? number
118-
: T extends "object"
119-
? object
120-
: T extends "string"
121-
? string
122-
: T extends "string[]"
123-
? string[] // XXX support arrays differently?
124-
: never;
170+
? any // eslint-disable-line @typescript-eslint/no-explicit-any
171+
: T extends "app"
172+
? { authProvisionId: string }
173+
: T extends "boolean"
174+
? boolean
175+
: T extends "boolean[]"
176+
? boolean[]
177+
: T extends "integer"
178+
? number
179+
: T extends "integer[]"
180+
? number[]
181+
: T extends "object"
182+
? object
183+
: T extends "string"
184+
? string
185+
: T extends "string[]"
186+
? string[]
187+
: T extends "number"
188+
? number
189+
: T extends "number[]"
190+
? number[]
191+
: T extends "$.discord.channel"
192+
? string
193+
: T extends "$.discord.channel[]"
194+
? string[]
195+
: T extends
196+
| "$.interface.http"
197+
| "$.interface.timer"
198+
| "$.service.db"
199+
| "datastore"
200+
| "http_request"
201+
| "sql"
202+
? unknown
203+
: never;
125204

126205
export type ConfiguredProps<T extends ConfigurableProps> = {
127-
[K in T[number] as K["name"]]?: PropValue<K["type"]>
206+
[K in T[number] as K["name"]]?: PropValue<K["type"]>;
128207
};
129208

130209
// as returned by API (configurable_props_json from `afterSave`)
131-
export type V1Component<T extends ConfigurableProps = any> = { // eslint-disable-line @typescript-eslint/no-explicit-any
210+
export type V1Component<T extends ConfigurableProps = ConfigurableProps> = {
211+
// eslint-disable-line @typescript-eslint/no-explicit-any
132212
name: string;
133213
key: string;
134214
version: string;
@@ -137,7 +217,10 @@ export type V1Component<T extends ConfigurableProps = any> = { // eslint-disable
137217
component_type?: string;
138218
};
139219

140-
export type V1DeployedComponent<T extends ConfigurableProps = any> = { // eslint-disable-line @typescript-eslint/no-explicit-any
220+
export type V1DeployedComponent<
221+
T extends ConfigurableProps = ConfigurableProps,
222+
> = {
223+
// eslint-disable-line @typescript-eslint/no-explicit-any
141224
id: string;
142225
owner_id: string;
143226
component_id: string;
@@ -171,4 +254,4 @@ export type V1EmittedEvent = {
171254
* The event's unique ID.
172255
*/
173256
id: string;
174-
}
257+
};

0 commit comments

Comments
 (0)