Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudflare-module): use correct types for email and queue events/hooks #2711

Merged
merged 1 commit into from
Sep 29, 2024
Merged
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
16 changes: 7 additions & 9 deletions src/presets/cloudflare/runtime/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
getAssetFromKV,
mapRequestToAsset,
} from "@cloudflare/kv-asset-handler";
import type { ExecutionContext } from "@cloudflare/workers-types";
import type {
ExecutionContext,
ForwardableEmailMessage,
MessageBatch,
} from "@cloudflare/workers-types";
import wsAdapter from "crossws/adapters/cloudflare";
import { withoutBase } from "ufo";

import type { CloudflareEmailContext, CloudflareMessageBatch } from "../types";

// @ts-ignore Bundled by Wrangler
// See https://github.com/cloudflare/kv-asset-handler#asset_manifest-required-for-es-modules
import manifest from "__STATIC_CONTENT_MANIFEST";
Expand Down Expand Up @@ -101,7 +103,7 @@ export default {
},

email(
event: CloudflareEmailContext,
event: ForwardableEmailMessage,
env: CFModuleEnv,
context: ExecutionContext
) {
Expand All @@ -111,11 +113,7 @@ export default {
);
},

queue(
event: CloudflareEmailContext,
env: CFModuleEnv,
context: ExecutionContext
) {
queue(event: MessageBatch, env: CFModuleEnv, context: ExecutionContext) {
(globalThis as any).__env__ = env;
context.waitUntil(
nitroApp.hooks.callHook("cloudflare:queue", { event, env, context })
Expand Down
42 changes: 7 additions & 35 deletions src/presets/cloudflare/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { ExecutionContext } from "@cloudflare/workers-types";
import type {
ExecutionContext,
ForwardableEmailMessage,
MessageBatch,
} from "@cloudflare/workers-types";
import type { Config as WranglerConfig } from "./types.wrangler";

/**
Expand Down Expand Up @@ -52,47 +56,15 @@ export interface CloudflareOptions {
};
}

/** @experimental */
export interface CloudflareEmailContext {
readonly from?: string;
readonly to?: string;
readonly headers?: Headers;
readonly raw?: ReadableStream;
readonly rawSize?: number;

setReject?(reason: string): void;
forward?(rcptTo: string, headers?: Headers): Promise<void>;
reply?(message: CloudflareEmailContext): Promise<void>;
}

export interface CloudflareQueueRetryOptions {
delaySeconds?: number;
}

export interface CloudflareMessageBody<Body = unknown> {
readonly id: string;
readonly timestamp: Date;
readonly body: Body;
adk(): void;
retry(options?: CloudflareQueueRetryOptions): void;
}

export interface CloudflareMessageBatch<Body = unknown> {
readonly queue: string;
readonly messages: CloudflareMessageBody<Body>[];
ackAll(): void;
retryAll(options?: CloudflareQueueRetryOptions): void;
}

declare module "nitropack/types" {
export interface NitroRuntimeHooks {
"cloudflare:email": (_: {
event: CloudflareEmailContext;
event: ForwardableEmailMessage;
env: any;
context: ExecutionContext;
}) => void;
"cloudflare:queue": (_: {
event: CloudflareEmailContext;
event: MessageBatch;
env: any;
context: ExecutionContext;
}) => void;
Expand Down