generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: store event payload, inputs and outputs of plugins in chain state
- Loading branch information
Showing
5 changed files
with
104 additions
and
64 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
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 was deleted.
Oops, something went wrong.
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,70 @@ | ||
import { EmitterWebhookEvent, EmitterWebhookEventName } from "@octokit/webhooks"; | ||
import { PluginChain } from "./config"; | ||
import { StaticDecode, Type } from "@sinclair/typebox"; | ||
|
||
function jsonString() { | ||
return Type.Transform(Type.String()) | ||
.Decode((value) => JSON.parse(value) as unknown) | ||
.Encode((value) => JSON.stringify(value)); | ||
} | ||
|
||
export const pluginOutputSchema = Type.Object({ | ||
stateId: Type.String(), | ||
output: jsonString(), | ||
}); | ||
|
||
export type PluginOutput = StaticDecode<typeof pluginOutputSchema>; | ||
|
||
export class DelegatedComputeInputs<T extends EmitterWebhookEventName = EmitterWebhookEventName> { | ||
public stateId: string; | ||
public eventName: T; | ||
public event: EmitterWebhookEvent<T>; | ||
public settings: unknown; | ||
public authToken: string; | ||
public ref: string; | ||
|
||
constructor(stateId: string, eventName: T, event: EmitterWebhookEvent<T>, settings: unknown, authToken: string, ref: string) { | ||
this.stateId = stateId; | ||
this.eventName = eventName; | ||
this.event = event; | ||
this.settings = settings; | ||
this.authToken = authToken; | ||
this.ref = ref; | ||
} | ||
|
||
public getInputs() { | ||
return { | ||
stateId: this.stateId, | ||
eventName: this.eventName, | ||
event: JSON.stringify(this.event), | ||
settings: JSON.stringify(this.settings), | ||
authToken: this.authToken, | ||
ref: this.ref, | ||
}; | ||
} | ||
} | ||
|
||
export type PluginChainState<T extends EmitterWebhookEventName = EmitterWebhookEventName> = { | ||
eventId: string; | ||
eventName: T; | ||
event: EmitterWebhookEvent<T>; | ||
currentPlugin: number; | ||
pluginChain: PluginChain; | ||
inputs: DelegatedComputeInputs[]; | ||
outputs: PluginOutput[]; | ||
}; | ||
|
||
// convert top level properties to string | ||
export function convertToString(obj: Record<string, unknown>): Record<string, string> { | ||
const newObj: Record<string, string> = {}; | ||
for (let i = 0; i < Object.keys(obj).length; i++) { | ||
const key = Object.keys(obj)[i]; | ||
const val = obj[key]; | ||
if (typeof val === "string") { | ||
newObj[key] = val; | ||
} else { | ||
newObj[key] = JSON.stringify(val); | ||
} | ||
} | ||
return newObj; | ||
} |