-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TEMP] a lot of stuff to properly commit
- Loading branch information
1 parent
a64b49a
commit 88cfb54
Showing
7 changed files
with
869 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* A parameter for a Cockpit action | ||
* @param { string } name - The name of the parameter | ||
* @param { 'string' | 'number' | 'boolean' } type - The type of the parameter (string, number or boolean) | ||
* @param { string } description - What the parameter does or means | ||
* @param { string | number | boolean } defaultValue - The default value of the parameter | ||
* @param { boolean } required - Whether the parameter is required or not | ||
* @param { (string | number)[]? } options - The options for the parameter (only if type is string or number). | ||
* @param { number? } min - The minimum value for the parameter (only if type is number). | ||
* @param { number? } max - The maximum value for the parameter (only if type is number). | ||
*/ | ||
class CockpitActionParameter { | ||
id: string | ||
name: string | ||
type: 'string' | 'number' | 'boolean' | ||
required: boolean | ||
description?: string | ||
defaultValue?: string | number | boolean | ||
options?: (string | number)[] | ||
min?: number | ||
max?: number | ||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
constructor( | ||
id: string, | ||
name: string, | ||
type: 'string' | 'number' | 'boolean', | ||
required: boolean, | ||
description?: string, | ||
defaultValue?: string | number | boolean, | ||
options?: (string | number)[], | ||
min?: number, | ||
max?: number | ||
) { | ||
this.id = id | ||
this.name = name | ||
this.type = type | ||
this.description = description | ||
this.defaultValue = defaultValue | ||
this.required = required | ||
this.options = options | ||
this.min = min | ||
this.max = max | ||
} | ||
} | ||
|
||
const cockpitActionParametersInfo: Record<string, CockpitActionParameter> = {} | ||
export const cockpitActionParametersData: Record<string, string | number | boolean> = {} | ||
|
||
export const getCockpitActionParametersInfo = (id: string): CockpitActionParameter | undefined => { | ||
return cockpitActionParametersInfo[id] | ||
} | ||
|
||
export const getAllCockpitActionParametersInfo = (): Record<string, CockpitActionParameter> => { | ||
return cockpitActionParametersInfo | ||
} | ||
|
||
export const getCockpitActionParameterInfo = (id: string): CockpitActionParameter | undefined => { | ||
return cockpitActionParametersInfo[id] | ||
} | ||
|
||
export const setCockpitActionParameterInfo = (id: string, parameter: CockpitActionParameter): void => { | ||
cockpitActionParametersInfo[id] = parameter | ||
} | ||
|
||
export const getCockpitActionParameterData = (id: string): string | number | boolean | undefined => { | ||
return cockpitActionParametersData[id] | ||
} | ||
|
||
export const setCockpitActionParameterData = (id: string, data: string | number | boolean): void => { | ||
cockpitActionParametersData[id] = data | ||
} | ||
|
||
const fakeRovNameInfo = new CockpitActionParameter( | ||
'fakeRovName', | ||
'Placeholder Parameter One', | ||
'string', | ||
true, | ||
'This is a placeholder parameter for the first parameter' | ||
) | ||
|
||
const fakeRovAgeInitialDivesInfo = new CockpitActionParameter( | ||
'fakeRovAgeInitialDives', | ||
'Placeholder Parameter Two', | ||
'number', | ||
true, | ||
'This is a placeholder parameter for the second parameter', | ||
undefined, | ||
undefined, | ||
undefined, | ||
0 | ||
) | ||
|
||
const placeholderParameterThreeInfo = new CockpitActionParameter( | ||
'placeholderParameterThree', | ||
'Placeholder Parameter Three', | ||
'boolean', | ||
true, | ||
'This is a placeholder parameter for the third parameter' | ||
) | ||
|
||
setCockpitActionParameterInfo(fakeRovNameInfo.id, fakeRovNameInfo) | ||
setCockpitActionParameterInfo(fakeRovAgeInitialDivesInfo.id, fakeRovAgeInitialDivesInfo) | ||
setCockpitActionParameterInfo(placeholderParameterThreeInfo.id, placeholderParameterThreeInfo) | ||
|
||
setCockpitActionParameterData(fakeRovNameInfo.id, 'John Doe') | ||
setCockpitActionParameterData(fakeRovAgeInitialDivesInfo.id, 5) | ||
setCockpitActionParameterData(placeholderParameterThreeInfo.id, true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { | ||
CockpitAction, | ||
CockpitActionsFunction, | ||
registerActionCallback, | ||
registerNewAction, | ||
} from '../joystick/protocols/cockpit-actions' | ||
import { getCockpitActionParameterData } from './data-lake' | ||
|
||
/** | ||
* The types of HTTP methods that can be used. | ||
*/ | ||
export enum HttpRequestMethod { | ||
GET = 'GET', | ||
POST = 'POST', | ||
PUT = 'PUT', | ||
DELETE = 'DELETE', | ||
PATCH = 'PATCH', | ||
} | ||
export const availableHttpRequestMethods: HttpRequestMethod[] = Object.values(HttpRequestMethod) | ||
|
||
export type HttpRequestActionConfig = { | ||
/** | ||
* The name of the action. | ||
*/ | ||
name: string | ||
/** | ||
* The URL to send the request to. | ||
*/ | ||
url: string | ||
/** | ||
* The HTTP method to use. | ||
*/ | ||
method: HttpRequestMethod | ||
/** | ||
* The headers to send with the request. | ||
*/ | ||
headers: Record<string, string> | ||
/** | ||
* The URL parameters to send with the request. | ||
*/ | ||
urlParams: Record<string, string> | ||
/** | ||
* The body of the request. | ||
*/ | ||
body: string | ||
} | ||
|
||
let registeredHttpRequestActionConfigs: Record<string, HttpRequestActionConfig> = {} | ||
|
||
export const registerHttpRequestActionConfig = (action: HttpRequestActionConfig): void => { | ||
const id = `http-request-action (${action.name})` | ||
registeredHttpRequestActionConfigs[id] = action | ||
saveHttpRequestActionConfigs() | ||
updateCockpitActions() | ||
} | ||
|
||
export const getHttpRequestActionConfig = (id: string): HttpRequestActionConfig | undefined => { | ||
return registeredHttpRequestActionConfigs[id] | ||
} | ||
|
||
export const getAllHttpRequestActionConfigs = (): Record<string, HttpRequestActionConfig> => { | ||
return registeredHttpRequestActionConfigs | ||
} | ||
|
||
export const deleteHttpRequestActionConfig = (id: string): void => { | ||
delete registeredHttpRequestActionConfigs[id] | ||
saveHttpRequestActionConfigs() | ||
updateCockpitActions() | ||
} | ||
|
||
export const updateHttpRequestActionConfig = (id: string, updatedAction: HttpRequestActionConfig): void => { | ||
registeredHttpRequestActionConfigs[id] = updatedAction | ||
saveHttpRequestActionConfigs() | ||
updateCockpitActions() | ||
} | ||
|
||
export const updateCockpitActions = (): void => { | ||
const httpResquestActions = getAllHttpRequestActionConfigs() | ||
for (const [id, action] of Object.entries(httpResquestActions)) { | ||
try { | ||
const cockpitAction = new CockpitAction(id as CockpitActionsFunction, action.name) | ||
registerNewAction(cockpitAction) | ||
registerActionCallback(cockpitAction, getHttpRequestActionCallback(id)) | ||
} catch (error) { | ||
console.error(`Error registering action ${id}: ${error}`) | ||
} | ||
} | ||
} | ||
|
||
export const loadHttpRequestActionConfigs = (): void => { | ||
const savedActions = localStorage.getItem('cockpit-http-request-actions') | ||
if (savedActions) { | ||
registeredHttpRequestActionConfigs = JSON.parse(savedActions) | ||
} | ||
} | ||
|
||
export const saveHttpRequestActionConfigs = (): void => { | ||
localStorage.setItem('cockpit-http-request-actions', JSON.stringify(registeredHttpRequestActionConfigs)) | ||
} | ||
|
||
export type HttpRequestActionCallback = () => void | ||
|
||
export const getHttpRequestActionCallback = (id: string): HttpRequestActionCallback => { | ||
const action = getHttpRequestActionConfig(id) | ||
if (!action) { | ||
throw new Error(`Action with id ${id} not found.`) | ||
} | ||
|
||
let parsedBody = action.body | ||
const parsedUrlParams = action.urlParams | ||
|
||
const cockpitInputsInBody = action.body.match(/{{\s*([^{}\s]+)\s*}}/g) | ||
if (cockpitInputsInBody) { | ||
for (const input of cockpitInputsInBody) { | ||
const parsedInput = input.replace('{{', '').replace('}}', '').trim() | ||
const inputData = getCockpitActionParameterData(parsedInput) | ||
if (inputData) { | ||
parsedBody = parsedBody.replace(input, inputData.toString()) | ||
} | ||
} | ||
} | ||
|
||
const cockpitInputsInUrlParams = Object.entries(action.urlParams).filter( | ||
([, value]) => typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}') | ||
) | ||
if (cockpitInputsInUrlParams) { | ||
for (const [key, value] of cockpitInputsInUrlParams) { | ||
const parsedInput = value.replace('{{', '').replace('}}', '').trim() | ||
const inputData = getCockpitActionParameterData(parsedInput) | ||
if (inputData) { | ||
parsedUrlParams[key] = inputData.toString() | ||
} | ||
} | ||
} | ||
|
||
const url = new URL(action.url) | ||
|
||
url.search = new URLSearchParams(parsedUrlParams).toString() | ||
|
||
return () => { | ||
fetch(url, { | ||
method: action.method, | ||
headers: action.headers, | ||
body: action.method === HttpRequestMethod.GET ? undefined : parsedBody, | ||
}) | ||
} | ||
} | ||
|
||
loadHttpRequestActionConfigs() | ||
updateCockpitActions() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.