Skip to content

Commit

Permalink
Add initial approach of onPremiseConnection factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwersal committed Feb 28, 2024
1 parent 45911e9 commit c099bad
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/generator-spectral/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
],
"dependencies": {
"@apidevtools/swagger-parser": "10.1.0",
"@prismatic-io/spectral": "8.0.3",
"@prismatic-io/spectral": "8.0.4",
"lodash": "4.17.21",
"number-to-words": "1.2.4",
"openapi-types": "11.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/spectral/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismatic-io/spectral",
"version": "8.0.3",
"version": "8.0.4",
"description": "Utility library for building Prismatic components",
"keywords": [
"prismatic"
Expand Down
11 changes: 11 additions & 0 deletions packages/spectral/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
TriggerPayload,
DataSourceConfigVar,
ConfigPages,
OnPremiseConnectionDefinition,
} from "./types";
import { convertComponent } from "./serverTypes/convert";
import { convertIntegration } from "./serverTypes/convertIntegration";
Expand Down Expand Up @@ -184,6 +185,16 @@ export const connection = <T extends DefaultConnectionDefinition>(
definition: T
): T => definition;

/**
* For information on writing custom component connections using on-premise resources, see
* https://prismatic.io/docs/custom-components/writing-custom-components/#adding-connections.
* @param definition An OnPremiseConnectionDefinition object that describes the type of a connection for a custom component action or trigger, and information on how it should be displayed in the Prismatic WebApp.
* @returns This function validates the shape of the `definition` object provided and returns the same connection object.
*/
export const onPremiseConnection = <T extends OnPremiseConnectionDefinition>(
definition: T
): T => definition;

/**
* For information on writing custom component connections, see
* https://prismatic.io/docs/custom-components/writing-custom-components/#adding-connections.
Expand Down
2 changes: 1 addition & 1 deletion packages/spectral/src/serverTypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface Connection {
comments?: string;
oauth2Type?: OAuth2Type;
iconPath?: string;
inputs: (Input & { shown?: boolean })[];
inputs: (Input & { shown?: boolean; onPremiseControlled?: boolean })[];
}

export interface ConnectionValue {
Expand Down
53 changes: 27 additions & 26 deletions packages/spectral/src/types-tests/ConnectionDefinition.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expectAssignable } from "tsd";
import { connection, KeyValuePair } from "..";
import { expectAssignable, expectNotAssignable } from "tsd";
import { onPremiseConnection, OnPremiseConnectionDefinition } from "..";

const onPremiseConnectionDef = connection({
const valid = onPremiseConnection({
key: "basic",
label: "Basic Connection",
inputs: {
Expand All @@ -11,6 +11,7 @@ const onPremiseConnectionDef = connection({
type: "string",
required: true,
shown: true,
onPremiseControlled: true,
example: "192.168.0.1",
},
port: {
Expand All @@ -19,6 +20,7 @@ const onPremiseConnectionDef = connection({
type: "string",
required: true,
shown: true,
onPremiseControlled: true,
default: "1433",
},
username: {
Expand All @@ -36,28 +38,27 @@ const onPremiseConnectionDef = connection({
shown: true,
},
},
onPremiseConfig: {
replacesInputs: ["host", "port"],
transform: (opc, inputConfig) => {
return {
...inputConfig,
host: opc.host,
port: opc.port,
};
});
expectAssignable<OnPremiseConnectionDefinition>(valid);

const invalid = {
key: "basic",
label: "Basic Connection",
inputs: {
username: {
label: "Username",
placeholder: "Username",
type: "string",
required: false,
shown: true,
},
password: {
label: "Password",
placeholder: "Password",
type: "password",
required: false,
shown: true,
},
},
});
type ExpectedInputValueType =
| string
| string[]
| KeyValuePair<string>[]
| undefined;
type ExpectedConfigType = { [key: string]: ExpectedInputValueType };
type ExpectedOnPrem = { host: string; port: string };
type ExpectedFunc = (
opc: ExpectedOnPrem,
inp: ExpectedConfigType
) => ExpectedConfigType;
expectAssignable<ExpectedFunc | undefined>(
onPremiseConnectionDef.onPremiseConfig?.transform
);
};
expectNotAssignable<OnPremiseConnectionDefinition>(invalid);
29 changes: 11 additions & 18 deletions packages/spectral/src/types/ConnectionDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,19 @@ interface BaseConnectionDefinition {
oauth2Type?: OAuth2Type;
}

interface OnPremiseConnectionInfo {
host: string;
port: string;
export interface DefaultConnectionDefinition extends BaseConnectionDefinition {
inputs: {
[key: string]: ConnectionInput;
};
}

type ConnectionValues<T extends Record<string, ConnectionInput>> = {
[Key in keyof T]: T[Key]["default"];
};

interface OnPremiseConfig<T extends Record<string, ConnectionInput>> {
replacesInputs: (keyof T)[];
transform: (
opa: OnPremiseConnectionInfo,
inConfig: ConnectionValues<T>
) => ConnectionValues<T>;
}
type OnPremiseConnectionInput = ConnectionInput & { onPremiseControlled: true };

export interface DefaultConnectionDefinition extends BaseConnectionDefinition {
onPremiseConfig?: OnPremiseConfig<this["inputs"]>;
export interface OnPremiseConnectionDefinition
extends BaseConnectionDefinition {
inputs: {
host: OnPremiseConnectionInput;
port: OnPremiseConnectionInput;
[key: string]: ConnectionInput;
};
}
Expand Down Expand Up @@ -75,5 +68,5 @@ export type OAuth2ConnectionDefinition =

export type ConnectionDefinition =
| DefaultConnectionDefinition
| OAuth2AuthorizationCodeConnectionDefinition
| OAuth2ClientCredentialConnectionDefinition;
| OnPremiseConnectionDefinition
| OAuth2ConnectionDefinition;

0 comments on commit c099bad

Please sign in to comment.