Skip to content

Commit

Permalink
Rename runParameters to args; rename runParameters schema to params
Browse files Browse the repository at this point in the history
  • Loading branch information
3mcd committed May 13, 2024
1 parent 8ca0152 commit 8633b4e
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 73 deletions.
69 changes: 24 additions & 45 deletions core/actions/_lib/runActionInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,7 @@ type ActionInstanceRunResult = ClientException | ClientExceptionOptions | Action
type ActionInstanceArgs = {
pubId: PubsId;
actionInstanceId: ActionInstancesId;
runParameters?: Record<string, unknown>;
};

const recordActionRun = async (
actionInstanceId: ActionInstancesId,
pubId: PubsId,
result: ActionInstanceRunResult,
config?: unknown,
params?: unknown,
event?: Event
) => {
const actionRun = await db
.insertInto("action_runs")
.values({
action_instance_id: actionInstanceId,
pub_id: pubId,
status: "error" in result ? ActionRunStatus.failure : ActionRunStatus.success,
config,
params,
event,
})
.execute();

return actionRun;
args?: Record<string, unknown>;
};

const _runActionInstance = async (
Expand All @@ -63,7 +40,7 @@ const _runActionInstance = async (
stageId: StagesId;
},
pub: GetPubResponseBody,
runParameters = {}
args = {}
): Promise<ActionInstanceRunResult> => {
if (!actionInstance.action) {
return {
Expand All @@ -89,11 +66,11 @@ const _runActionInstance = async (
};
}

const parsedrunParameters = action.runParameters.safeParse(runParameters ?? {});
if (!parsedrunParameters.success) {
const parsedArgs = action.params.safeParse(args ?? {});
if (!parsedArgs.success) {
return {
title: "Invalid pub config",
cause: parsedrunParameters.error,
cause: parsedArgs.error,
error: "The action was run with invalid parameters",
};
}
Expand All @@ -116,7 +93,7 @@ const _runActionInstance = async (
id: pub.id,
values: pub.values as any,
},
runParameters: runParameters,
args: args,
stageId: actionInstance.stageId,
});

Expand All @@ -134,7 +111,7 @@ const _runActionInstance = async (
};

const runAndRecordActionInstance = async (
{ pubId, actionInstanceId, runParameters = {} }: ActionInstanceArgs,
{ pubId, actionInstanceId, args = {} }: ActionInstanceArgs,
event?: Event
) => {
let result: ActionInstanceRunResult;
Expand Down Expand Up @@ -171,31 +148,33 @@ const runAndRecordActionInstance = async (
cause: actionInstanceResult.reason,
};
} else {
result = await _runActionInstance(
actionInstanceResult.value,
pubResult.value,
runParameters
);
result = await _runActionInstance(actionInstanceResult.value, pubResult.value, args);
}

await recordActionRun(
actionInstanceId,
pubId,
result,
actionInstanceResult.status === "fulfilled" ? actionInstanceResult.value.config : undefined,
runParameters,
event
);
await db
.insertInto("action_runs")
.values({
action_instance_id: actionInstanceId,
pub_id: pubId,
status: "error" in result ? ActionRunStatus.failure : ActionRunStatus.success,
config:
actionInstanceResult.status === "fulfilled"
? actionInstanceResult.value.config
: undefined,
params: args,
event,
})
.execute();

return result;
};

export const runActionInstance = defineServerAction(async function runActionInstance({
pubId,
actionInstanceId,
runParameters = {},
args = {},
}: ActionInstanceArgs) {
return runAndRecordActionInstance({ pubId, actionInstanceId, runParameters });
return runAndRecordActionInstance({ pubId, actionInstanceId, args });
});

export const runInstancesForEvent = async (pubId: PubsId, stageId: StagesId, event: Event) => {
Expand Down
2 changes: 1 addition & 1 deletion core/actions/email/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const action = defineAction({
body: z.string().min(0).max(1_000).describe("Email body||textarea"),
}),
description: "Send an email to one or more users",
runParameters: z
params: z
.object({
email: z
.string()
Expand Down
4 changes: 2 additions & 2 deletions core/actions/email/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { action } from "./action";
import { smtpclient } from "~/lib/server/mailgun";
import { defineRun } from "../types";

export const run = defineRun<typeof action>(async ({ pub, config, runParameters }) => {
export const run = defineRun<typeof action>(async ({ pub, config, args }) => {
try {
await smtpclient.sendMail({
from: "[email protected]",
Expand All @@ -24,7 +24,7 @@ export const run = defineRun<typeof action>(async ({ pub, config, runParameters
};
}

logger.info({ msg: "email", pub, config, runParameters });
logger.info({ msg: "email", pub, config, args });

return {
success: true,
Expand Down
2 changes: 1 addition & 1 deletion core/actions/log/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const action = defineAction({
debounce: z.number().optional().describe("Debounce time in milliseconds."),
}),
description: "Log a pub to the console",
runParameters: z
params: z
.object({
debounce: z.number().optional().describe("Debounce time in milliseconds."),
text: z
Expand Down
8 changes: 4 additions & 4 deletions core/actions/log/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { logger } from "logger";
import type { action } from "./action";
import { defineRun } from "../types";

export const run = defineRun<typeof action>(async ({ pub, config, runParameters }) => {
export const run = defineRun<typeof action>(async ({ pub, config, args }) => {
logger.info({
msg: `Logging${runParameters?.text ? ` ${runParameters.text}` : ""}`,
msg: `Logging${args?.text ? ` ${args.text}` : ""}`,
pub,
config,
runParameters,
args,
});

return {
success: true,
report: `Logged out ${runParameters?.text || "some data"}, check your console.`,
report: `Logged out ${args?.text || "some data"}, check your console.`,
data: {},
};
});
2 changes: 1 addition & 1 deletion core/actions/move/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const action = defineAction({
stage: z.string().describe("Destination stage"),
}),
description: "Move a pub to a different stage",
runParameters: z.object({}).optional(),
params: z.object({}).optional(),
pubFields: [],
icon: MoveHorizontal,
});
2 changes: 1 addition & 1 deletion core/actions/pdf/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const action = defineAction({
margin: z.number().optional().describe("Page margin in pixels"),
}),
description: "Generate a PDF from a pub",
runParameters: z
params: z
.object({
margin: z.number().optional().describe("Page margin in pixels"),
})
Expand Down
4 changes: 2 additions & 2 deletions core/actions/pdf/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { logger } from "logger";
import type { action } from "./action";
import { defineRun } from "../types";

export const run = defineRun<typeof action>(async ({ pub, config, runParameters }) => {
export const run = defineRun<typeof action>(async ({ pub, config, args }) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
logger.info({ msg: "pdf generated", pub, config, runParameters });
logger.info({ msg: "pdf generated", pub, config, args });
return {
error: "Wow, an error",
};
Expand Down
2 changes: 1 addition & 1 deletion core/actions/pushToV6/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const action = defineAction({
authToken: z.string().describe("PubPub v6 API auth token"),
}),
description: "Sync a PubPub v7 pub to v6",
runParameters: z.object({}).optional(),
params: z.object({}).optional(),
pubFields: [corePubFields.title, corePubFields.content],
icon: FileText,
});
4 changes: 2 additions & 2 deletions core/actions/pushToV6/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const updateV6PubId = async (pubId: string, v6PubId: string) => {
.execute();
};

export const run = defineRun<typeof action>(async ({ pub, config, runParameters }) => {
export const run = defineRun<typeof action>(async ({ pub, config, args }) => {
try {
const v6Community = await getV6Community(config.communitySlug, config.authToken);

Expand Down Expand Up @@ -235,7 +235,7 @@ export const run = defineRun<typeof action>(async ({ pub, config, runParameters
};
}

logger.info({ msg: "pub pushed to v6", pub, config, runParameters });
logger.info({ msg: "pub pushed to v6", pub, config, args });

return {
success: true,
Expand Down
22 changes: 11 additions & 11 deletions core/actions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ export type ActionPub<T extends ActionPubType> = {
};

export type RunProps<T extends Action> =
T extends Action<infer PT, infer AC, infer RP>
? { config: AC; pub: ActionPub<PT>; runParameters: RP; stageId: StagesId }
T extends Action<infer P, infer C, infer A>
? { config: C; pub: ActionPub<P>; args: A; stageId: StagesId }
: never;

export type ConfigProps<C> = {
config: C;
};

export type Action<
PT extends ActionPubType = ActionPubType,
AC extends object = {},
RP extends object | undefined = {} | undefined,
P extends ActionPubType = ActionPubType,
C extends object = {},
A extends object | undefined = {} | undefined,
N extends string = string,
> = {
id?: string;
Expand All @@ -39,7 +39,7 @@ export type Action<
*
* These are the "statically known" parameters for this action.
*/
config: z.ZodType<AC>;
config: z.ZodType<C>;
/**
* The run parameters for this action
*
Expand All @@ -48,11 +48,11 @@ export type Action<
* Defining this as an optional Zod schema (e.g. `z.object({/*...*\/}).optional()`) means that the action can be automatically run
* through a rule.
*/
runParameters: z.ZodType<RP>;
params: z.ZodType<A>;
/**
* The core pub fields that this action requires in order to run.
*/
pubFields: PT;
pubFields: P;
/**
* The icon to display for this action. Used in the UI.
*/
Expand All @@ -61,11 +61,11 @@ export type Action<

export const defineAction = <
T extends ActionPubType,
AC extends object,
RP extends object | undefined,
C extends object,
A extends object | undefined,
N extends string,
>(
action: Action<T, AC, RP, N>
action: Action<T, C, A, N>
) => action;

export type ActionSuccess = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const StagePanelPubsRunActionButton = ({
const result = await runAction({
actionInstanceId: actionInstance.id as ActionInstancesId,
pubId: pub.id as PubsId,
runParameters: values,
args: values,
});

if ("success" in result) {
Expand Down Expand Up @@ -73,7 +73,7 @@ export const StagePanelPubsRunActionButton = ({
<DialogHeader>
<DialogTitle>{actionInstance.name || action.name}</DialogTitle>
</DialogHeader>
<AutoForm formSchema={action.runParameters as ZodObject<{}>} onSubmit={onSubmit}>
<AutoForm formSchema={action.params as ZodObject<{}>} onSubmit={onSubmit}>
<AutoFormSubmit disabled={isPending}>Run</AutoFormSubmit>
</AutoForm>
</DialogContent>
Expand Down

0 comments on commit 8633b4e

Please sign in to comment.