Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { type ActorContext, actor } from "rivetkit";

export const rawHttpRequestPropertiesActor = actor({
actions: {},
onFetch(ctx: ActorContext<any, any, any, any, any, any>, request: Request) {
onRequest(
ctx: ActorContext<any, any, any, any, any, any>,
request: Request,
) {
// Extract all relevant Request properties
const url = new URL(request.url);
const method = request.method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ export const rawHttpActor = actor({
state: {
requestCount: 0,
},
onFetch(ctx: ActorContext<any, any, any, any, any, any>, request: Request) {
onRequest(
ctx: ActorContext<any, any, any, any, any, any>,
request: Request,
) {
const url = new URL(request.url);
const method = request.method;

Expand Down Expand Up @@ -57,7 +60,7 @@ export const rawHttpNoHandlerActor = actor({
});

export const rawHttpVoidReturnActor = actor({
onFetch(ctx, request) {
onRequest(ctx, request) {
// Intentionally return void to test error handling
return undefined as any;
},
Expand Down Expand Up @@ -107,7 +110,10 @@ export const rawHttpHonoActor = actor({
// Return the router as a var
return { router };
},
onFetch(ctx: ActorContext<any, any, any, any, any, any>, request: Request) {
onRequest(
ctx: ActorContext<any, any, any, any, any, any>,
request: Request,
) {
// Use the Hono router from vars
return ctx.vars.router.fetch(request);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const requestAccessActor = actor({
requestMethod: null as string | null,
requestHeaders: {} as Record<string, string>,
},
onFetchRequest: {
onRequestRequest: {
hasRequest: false,
requestUrl: null as string | null,
requestMethod: null as string | null,
Expand Down Expand Up @@ -74,18 +74,18 @@ export const requestAccessActor = actor({
}
}
},
onFetch: (c, request) => {
onRequest: (c, request) => {
// Store request info
c.state.onFetchRequest.hasRequest = true;
c.state.onFetchRequest.requestUrl = request.url;
c.state.onFetchRequest.requestMethod = request.method;
c.state.onRequestRequest.hasRequest = true;
c.state.onRequestRequest.requestUrl = request.url;
c.state.onRequestRequest.requestMethod = request.method;

// Store select headers
const headers: Record<string, string> = {};
request.headers.forEach((value, key) => {
headers[key] = value;
});
c.state.onFetchRequest.requestHeaders = headers;
c.state.onRequestRequest.requestHeaders = headers;

// Return response with request info
return new Response(
Expand Down Expand Up @@ -134,7 +134,7 @@ export const requestAccessActor = actor({
return {
onBeforeConnect: c.state.onBeforeConnectRequest,
createConnState: c.state.createConnStateRequest,
onFetch: c.state.onFetchRequest,
onRequest: c.state.onRequestRequest,
onWebSocket: c.state.onWebSocketRequest,
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const sleepWithRawHttp = actor({
onSleep: (c) => {
c.state.sleepCount += 1;
},
onFetch: async (c, request) => {
onRequest: async (c, request) => {
c.state.requestCount += 1;
const url = new URL(request.url);

Expand Down
8 changes: 4 additions & 4 deletions rivetkit-typescript/packages/rivetkit/src/actor/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const ActorConfigSchema = z
onConnect: z.function().optional(),
onDisconnect: z.function().optional(),
onBeforeActionResponse: z.function().optional(),
onFetch: z.function().optional(),
onRequest: z.function().optional(),
onWebSocket: z.function().optional(),
actions: z.record(z.function()).default({}),
state: z.any().optional(),
Expand Down Expand Up @@ -410,7 +410,7 @@ interface BaseActorConfig<
* @param request The raw HTTP request object
* @returns A Response object to send back, or void to continue with default routing
*/
onFetch?: (
onRequest?: (
c: ActorContext<
TState,
TConnParams,
Expand Down Expand Up @@ -477,7 +477,7 @@ export type ActorConfig<
| "onConnect"
| "onDisconnect"
| "onBeforeActionResponse"
| "onFetch"
| "onRequest"
| "onWebSocket"
| "state"
| "createState"
Expand Down Expand Up @@ -537,7 +537,7 @@ export type ActorConfigInput<
| "onConnect"
| "onDisconnect"
| "onBeforeActionResponse"
| "onFetch"
| "onRequest"
| "onWebSocket"
| "state"
| "createState"
Expand Down
12 changes: 6 additions & 6 deletions rivetkit-typescript/packages/rivetkit/src/actor/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,12 @@ export class DatabaseNotEnabled extends ActorError {
}
}

export class FetchHandlerNotDefined extends ActorError {
export class RequestHandlerNotDfeined extends ActorError {
constructor() {
super(
"handler",
"fetch_not_defined",
"Raw HTTP handler not defined. Actor must implement `onFetch` to handle raw HTTP requests. (https://www.rivet.dev/docs/actors/fetch-and-websocket-handler/)",
"request_not_defined",
"Raw request handler not defined. Actor must implement `onRequest` to handle raw HTTP requests. (https://www.rivet.dev/docs/actors/fetch-and-websocket-handler/)",
{ public: true },
);
this.statusCode = 404;
Expand All @@ -341,12 +341,12 @@ export class WebSocketHandlerNotDefined extends ActorError {
}
}

export class InvalidFetchResponse extends ActorError {
export class InvalidRequestHandlerResponse extends ActorError {
constructor() {
super(
"handler",
"invalid_fetch_response",
"Actor's onFetch handler must return a Response object. Returning void/undefined is not allowed. (https://www.rivet.dev/docs/actors/fetch-and-websocket-handler/)",
"invalid_request_handler_response",
"Actor's onRequest handler must return a Response object. Returning void/undefined is not allowed. (https://www.rivet.dev/docs/actors/fetch-and-websocket-handler/)",
{ public: true },
);
this.statusCode = 500;
Expand Down
14 changes: 7 additions & 7 deletions rivetkit-typescript/packages/rivetkit/src/actor/instance/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,29 +643,29 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
}

// MARK: - HTTP/WebSocket Handlers
async handleFetch(
async handleRawRequest(
request: Request,
opts: Record<never, never>,
): Promise<Response> {
this.#assertReady();

if (!this.#config.onFetch) {
throw new errors.FetchHandlerNotDefined();
if (!this.#config.onRequest) {
throw new errors.RequestHandlerNotDfeined();
}

try {
const response = await this.#config.onFetch(
const response = await this.#config.onRequest(
this.actorContext,
request,
opts,
);
if (!response) {
throw new errors.InvalidFetchResponse();
throw new errors.InvalidRequestHandlerResponse();
}
return response;
} catch (error) {
this.#rLog.error({
msg: "onFetch error",
msg: "onRequest error",
error: stringifyError(error),
});
throw error;
Expand All @@ -674,7 +674,7 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
}
}

async handleWebSocket(
async handleRawWebSocket(
websocket: UniversalWebSocket,
opts: { request: Request },
): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export async function handleRawWebSocketHandler(
createdConn = conn;

// Call the actor's onWebSocket handler with the adapted WebSocket
actor.handleWebSocket(adapter, {
actor.handleRawWebSocket(adapter, {
request: newRequest,
});
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions rivetkit-typescript/packages/rivetkit/src/actor/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ export function createActorRouter(
to: correctedRequest.url,
});

// Call the actor's onFetch handler - it will throw appropriate errors
const response = await actor.handleFetch(correctedRequest, {});
// Call the actor's onRequest handler - it will throw appropriate errors
const response = await actor.handleRawRequest(correctedRequest, {});

// This should never happen now since handleFetch throws errors
if (!response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function createTestInlineClientDriver(
if (errorData.error) {
// Handle both error formats:
// 1. { error: { code, message, metadata } } - structured format
// 2. { error: "message" } - simple string format (from custom onFetch handlers)
// 2. { error: "message" } - simple string format (from custom onRequest handlers)
if (typeof errorData.error === "object") {
throw new ClientActorError(
errorData.error.code,
Expand All @@ -138,7 +138,7 @@ export function createTestInlineClientDriver(
);
}
// For simple string errors, just return the response as-is
// This allows custom onFetch handlers to return their own error formats
// This allows custom onRequest handlers to return their own error formats
}
} catch (e) {
// If it's not our error format, just return the response as-is
Expand Down Expand Up @@ -452,7 +452,7 @@ export function createTestInlineClientDriver(
// if (errorData.error) {
// // Handle both error formats:
// // 1. { error: { code, message, metadata } } - structured format
// // 2. { error: "message" } - simple string format (from custom onFetch handlers)
// // 2. { error: "message" } - simple string format (from custom onRequest handlers)
// if (typeof errorData.error === "object") {
// throw new ClientActorError(
// errorData.error.code,
Expand All @@ -461,7 +461,7 @@ export function createTestInlineClientDriver(
// );
// }
// // For simple string errors, just return the response as-is
// // This allows custom onFetch handlers to return their own error formats
// // This allows custom onRequest handlers to return their own error formats
// }
// } catch (e) {
// // If it's not our error format, just return the response as-is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
// expect(data).toEqual({ message: "Hello from actor!" });
// });
//
// test("should return 404 for actors without onFetch handler", async (c) => {
// test("should return 404 for actors without onRequest handler", async (c) => {
// const { endpoint } = await setupDriverTest(c, driverTestConfig);
//
// const actorQuery: ActorQuery = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function runRawHttpRequestPropertiesTests(
driverTestConfig: DriverTestConfig,
) {
describe("raw http request properties", () => {
test("should pass all Request properties correctly to onFetch", async (c) => {
test("should pass all Request properties correctly to onRequest", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);
const actor = client.rawHttpRequestPropertiesActor.getOrCreate([
"test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function runRawHttpTests(driverTestConfig: DriverTestConfig) {
expect(response.status).toBe(404);
});

test("should return 404 when no onFetch handler defined", async (c) => {
test("should return 404 when no onRequest handler defined", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);
const actor = client.rawHttpNoHandlerActor.getOrCreate([
"no-handler",
Expand All @@ -89,10 +89,10 @@ export function runRawHttpTests(driverTestConfig: DriverTestConfig) {
expect(response.ok).toBe(false);
expect(response.status).toBe(404);

// No actions available without onFetch handler
// No actions available without onRequest handler
});

test("should return 500 error when onFetch returns void", async (c) => {
test("should return 500 error when onRequest returns void", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);
const actor = client.rawHttpVoidReturnActor.getOrCreate([
"void-return",
Expand All @@ -108,14 +108,14 @@ export function runRawHttpTests(driverTestConfig: DriverTestConfig) {
message: string;
};
expect(errorData.message).toContain(
"onFetch handler must return a Response",
"onRequest handler must return a Response",
);
} catch {
// If JSON parsing fails, just check that we got a 500 error
// The error details are already validated by the status code
}

// No actions available when onFetch returns void
// No actions available when onRequest returns void
});

test("should handle different HTTP methods", async (c) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function runRequestAccessTests(driverTestConfig: DriverTestConfig) {
});

// TODO: re-expose this once we can have actor queries on the gateway
// test("should have access to request object in onFetch", async (c) => {
// test("should have access to request object in onRequest", async (c) => {
// const { client, endpoint } = await setupDriverTest(c, driverTestConfig);
//
// // Create actor
Expand Down Expand Up @@ -163,7 +163,7 @@ export function runRequestAccessTests(driverTestConfig: DriverTestConfig) {
// expect(response.ok).toBe(true);
// const data = await response.json();
//
// // Verify request info from onFetch
// // Verify request info from onRequest
// expect((data as any).hasRequest).toBe(true);
// expect((data as any).requestUrl).toContain("/test-path");
// expect((data as any).requestMethod).toBe("POST");
Expand Down
Loading