-
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.
cockpit-actions: Add support for custom http-request actions
- Loading branch information
1 parent
417cf85
commit 04f6210
Showing
3 changed files
with
757 additions
and
0 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,160 @@ | ||
import { | ||
availableCockpitActions, | ||
CockpitAction, | ||
CockpitActionsFunction, | ||
deleteAction, | ||
registerActionCallback, | ||
registerNewAction, | ||
} from '../joystick/protocols/cockpit-actions' | ||
import { getCockpitActionParameterData } from './data-lake' | ||
|
||
const httpRequestActionIdPrefix = 'http-request-action' | ||
|
||
/** | ||
* 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 = `${httpRequestActionIdPrefix} (${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 => { | ||
Object.entries(availableCockpitActions).forEach(([id]) => { | ||
if (id.includes(httpRequestActionIdPrefix)) { | ||
deleteAction(id as CockpitActionsFunction) | ||
} | ||
}) | ||
|
||
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() |
Oops, something went wrong.