Skip to content

Commit

Permalink
feat: update handler return type
Browse files Browse the repository at this point in the history
  • Loading branch information
whilefoo committed Nov 18, 2024
1 parent eeae4e6 commit becefc3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { verifySignature } from "./signature";
import { KERNEL_PUBLIC_KEY } from "./constants";
import { jsonType } from "./types/util";
import { commandCallSchema } from "./types/command";
import { HandlerReturn } from "./types/sdk";

config();

Expand All @@ -37,7 +38,7 @@ const inputSchema = T.Object({
});

export async function createActionsPlugin<TConfig = unknown, TEnv = unknown, TCommand = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName>(
handler: (context: Context<TConfig, TEnv, TCommand, TSupportedEvents>) => Promise<Record<string, unknown> | undefined>,
handler: (context: Context<TConfig, TEnv, TCommand, TSupportedEvents>) => HandlerReturn,
options?: Options
) {
const pluginOptions = {
Expand Down Expand Up @@ -160,7 +161,7 @@ function getGithubWorkflowRunUrl() {
return `${github.context.payload.repository?.html_url}/actions/runs/${github.context.runId}`;
}

async function returnDataToKernel(repoToken: string, stateId: string, output: object | undefined) {
async function returnDataToKernel(repoToken: string, stateId: string, output: HandlerReturn) {
const octokit = new customOctokit({ auth: repoToken });
await octokit.rest.repos.createDispatchEvent({
owner: github.context.repo.owner,
Expand Down
13 changes: 7 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Context } from "./context";
import { customOctokit } from "./octokit";
import { verifySignature } from "./signature";
import { Manifest } from "./types/manifest";
import { HandlerReturn } from "./types/sdk";

interface Options {
kernelPublicKey?: string;
Expand All @@ -34,7 +35,7 @@ const inputSchema = T.Object({
});

export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unknown, TSupportedEvents extends WebhookEventName = WebhookEventName>(
handler: (context: Context<TConfig, TEnv, TCommand, TSupportedEvents>) => Promise<Record<string, unknown> | undefined>,
handler: (context: Context<TConfig, TEnv, TCommand, TSupportedEvents>) => HandlerReturn,
manifest: Manifest,
options?: Options
) {
Expand Down Expand Up @@ -62,7 +63,7 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unkno
const body = await ctx.req.json();
const inputSchemaErrors = [...Value.Errors(inputSchema, body)];
if (inputSchemaErrors.length) {
console.dir(inputSchemaErrors, { depth: null });
console.log(inputSchemaErrors, { depth: null });
throw new HTTPException(400, { message: "Invalid body" });
}
const inputs = Value.Decode(inputSchema, body);
Expand All @@ -76,7 +77,7 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unkno
try {
config = Value.Decode(pluginOptions.settingsSchema, Value.Default(pluginOptions.settingsSchema, inputs.settings));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.settingsSchema, inputs.settings), { depth: null });
console.log(...Value.Errors(pluginOptions.settingsSchema, inputs.settings), { depth: null });
throw e;
}
} else {
Expand All @@ -89,7 +90,7 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unkno
try {
env = Value.Decode(pluginOptions.envSchema, Value.Default(pluginOptions.envSchema, honoEnvironment));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.envSchema, honoEnvironment), { depth: null });
console.log(...Value.Errors(pluginOptions.envSchema, honoEnvironment), { depth: null });
throw e;
}
} else {
Expand All @@ -101,7 +102,7 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unkno
try {
command = Value.Decode(pluginOptions.commandSchema, Value.Default(pluginOptions.commandSchema, inputs.command));
} catch (e) {
console.dir(...Value.Errors(pluginOptions.commandSchema, inputs.command), { depth: null });
console.log(...Value.Errors(pluginOptions.commandSchema, inputs.command), { depth: null });
throw e;
}
} else if (inputs.command) {
Expand All @@ -120,7 +121,7 @@ export function createPlugin<TConfig = unknown, TEnv = unknown, TCommand = unkno

try {
const result = await handler(context);
return ctx.json({ stateId: inputs.stateId, output: result });
return ctx.json({ stateId: inputs.stateId, output: result ? JSON.stringify(result) : null });
} catch (error) {
console.error(error);

Expand Down
2 changes: 2 additions & 0 deletions src/types/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type Return = Record<string, unknown> | undefined | void;
export type HandlerReturn = Promise<Return> | Return;

0 comments on commit becefc3

Please sign in to comment.