Skip to content

Commit

Permalink
Automatically substitute Workers types for Node equivalents
Browse files Browse the repository at this point in the history
Declares a `ReplaceWorkersTypes` type instead of manually declaring
types for each of the Workers APIs.
  • Loading branch information
mrbbot committed Aug 16, 2023
1 parent 920cf61 commit 6498024
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 247 deletions.
23 changes: 14 additions & 9 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import { Duplex, Transform, Writable } from "stream";
import { ReadableStream } from "stream/web";
import zlib from "zlib";
import type {
CacheStorage,
D1Database,
DurableObjectNamespace,
KVNamespace,
Queue,
R2Bucket,
RequestInitCfProperties,
} from "@cloudflare/workers-types/experimental";
import exitHook from "exit-hook";
Expand All @@ -30,16 +34,13 @@ import {
registerAllowUnauthorizedDispatcher,
} from "./http";
import {
CacheStorage,
D1_PLUGIN_NAME,
DURABLE_OBJECTS_PLUGIN_NAME,
DispatchFetch,
DurableObjectClassNames,
DurableObjectNamespace,
GatewayConstructor,
GatewayFactory,
HEADER_CF_BLOB,
KVNamespace,
KV_PLUGIN_NAME,
PLUGIN_ENTRIES,
Persistence,
Expand All @@ -49,8 +50,8 @@ import {
QUEUES_PLUGIN_NAME,
QueueConsumers,
QueuesError,
R2Bucket,
R2_PLUGIN_NAME,
ReplaceWorkersTypes,
SharedOptions,
SourceMapRegistry,
WorkerOptions,
Expand Down Expand Up @@ -1207,23 +1208,24 @@ export class Miniflare {
return proxy as T;
}
// TODO(someday): would be nice to define these in plugins
async getCaches(): Promise<CacheStorage> {
async getCaches(): Promise<ReplaceWorkersTypes<CacheStorage>> {
const proxyClient = await this._getProxyClient();
return proxyClient.global.caches as unknown as CacheStorage;
return proxyClient.global
.caches as unknown as ReplaceWorkersTypes<CacheStorage>;
}
getD1Database(bindingName: string, workerName?: string): Promise<D1Database> {
return this.#getProxy(D1_PLUGIN_NAME, bindingName, workerName);
}
getDurableObjectNamespace(
bindingName: string,
workerName?: string
): Promise<DurableObjectNamespace> {
): Promise<ReplaceWorkersTypes<DurableObjectNamespace>> {
return this.#getProxy(DURABLE_OBJECTS_PLUGIN_NAME, bindingName, workerName);
}
getKVNamespace(
bindingName: string,
workerName?: string
): Promise<KVNamespace> {
): Promise<ReplaceWorkersTypes<KVNamespace>> {
return this.#getProxy(KV_PLUGIN_NAME, bindingName, workerName);
}
getQueueProducer<Body = unknown>(
Expand All @@ -1232,7 +1234,10 @@ export class Miniflare {
): Promise<Queue<Body>> {
return this.#getProxy(QUEUES_PLUGIN_NAME, bindingName, workerName);
}
getR2Bucket(bindingName: string, workerName?: string): Promise<R2Bucket> {
getR2Bucket(
bindingName: string,
workerName?: string
): Promise<ReplaceWorkersTypes<R2Bucket>> {
return this.#getProxy(R2_PLUGIN_NAME, bindingName, workerName);
}

Expand Down
Loading

0 comments on commit 6498024

Please sign in to comment.