From f0e4c62745c5d1c2bec01ff3f95bdcd8cbe3937d Mon Sep 17 00:00:00 2001 From: Travis Frank Date: Wed, 15 Jun 2022 00:46:12 +0200 Subject: [PATCH] types: Export and parameterize HyperStub --- package.json | 4 ++-- src/HyperDurable.spec.ts | 2 +- src/HyperNamespaceProxy.ts | 15 ++++++++------- src/index.d.ts | 25 ++++++++++++++++--------- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index f289757..c452d6d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ticketbridge/hyper-durable", - "version": "0.1.12", + "version": "0.1.13", "description": "Object-like access to Durable Object properties and methods", "repository": { "type": "git", @@ -20,7 +20,7 @@ "prebuild": "rm -rf dist && mkdir dist && cp src/index.d.ts dist/index.d.ts", "build": "export ENV=prod && node build.js", "build-test": "export ENV=test && node build.js", - "test": "yarn build-test && node --experimental-vm-modules node_modules/jest/bin/jest.js" + "test": "yarn build-test && NODE_OPTIONS=--experimental-vm-modules npx jest" }, "devDependencies": { "@babel/core": "^7.17.10", diff --git a/src/HyperDurable.spec.ts b/src/HyperDurable.spec.ts index 949253c..6cd3c13 100644 --- a/src/HyperDurable.spec.ts +++ b/src/HyperDurable.spec.ts @@ -12,7 +12,7 @@ import { Counter } from '../test/index'; describe('HyperDurable', () => { const bindings = getMiniflareBindings(); - let counter: Counter | undefined; + let counter: Counter; beforeEach(() => { const id = new DurableObjectId('testName', 'testHexId'); diff --git a/src/HyperNamespaceProxy.ts b/src/HyperNamespaceProxy.ts index 31ec782..88e53ac 100644 --- a/src/HyperNamespaceProxy.ts +++ b/src/HyperNamespaceProxy.ts @@ -1,7 +1,7 @@ import { HyperDurable } from './HyperDurable'; import { HyperError } from './HyperError'; -export class HyperNamespaceProxy, ENV> implements DurableObjectNamespace { +export class HyperNamespaceProxy, Env> implements DurableObjectNamespace { namespace: DurableObjectNamespace; ref: DO; @@ -11,7 +11,7 @@ export class HyperNamespaceProxy, ENV> impleme constructor( namespace: DurableObjectNamespace, - ref: new (state: DurableObjectState, env: ENV) => DO + ref: new (state: DurableObjectState, env: Env) => DO ) { this.namespace = namespace; // Create a reference of the DO to check for methods / properties @@ -40,21 +40,22 @@ export class HyperNamespaceProxy, ENV> impleme get(id: DurableObjectId) { // All of our prop getters & methods return Promises, since everything uses the // fetch interface. - type PromisedGetStub = { + type PromisedGetStub, Env> = { [Prop in keyof DO]?: DO[Prop] extends (...args: any) => any ? (...args: Parameters) => Promise> : Promise; }; // All of our props have setters formatted as: setProperty() - type SetStub = { + type SetStub, Env> = { [Prop in keyof DO as DO[Prop] extends Function ? never : `set${Capitalize}`]?: (newValue: DO[Prop]) => Promise } - type HyperStub = DurableObjectStub & PromisedGetStub & SetStub; + type HyperStub, Env> = + DurableObjectStub & PromisedGetStub & SetStub; const stub = this.namespace.get(id); - const hyperStub: HyperStub = Object.assign(stub, {}); + const hyperStub: HyperStub = Object.assign(stub, {}); function createHyperRequest(action: string, key: string, payload?: object) { return new Request(`https://hd.io/${action}/${key}`, { @@ -111,7 +112,7 @@ export class HyperNamespaceProxy, ENV> impleme } } - return new Proxy(hyperStub, handler); + return new Proxy>(hyperStub, handler); } } diff --git a/src/index.d.ts b/src/index.d.ts index 555bd44..008d604 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -27,6 +27,21 @@ export class HyperDurable { fetch(request: Request): Promise; } +export type PromisedGetStub, Env> = { + [Prop in keyof DO]?: + DO[Prop] extends (...args: any) => any + ? (...args: Parameters) => Promise> + : Promise; +} + +export type SetStub, Env> = { + [Prop in keyof DO as DO[Prop] extends Function ? never : `set${Capitalize}`]?: + (newValue: DO[Prop]) => Promise +} + +export type HyperStub, Env> = + DurableObjectStub & PromisedGetStub & SetStub + export class HyperNamespaceProxy, Env> { namespace: DurableObjectNamespace; ref: DO; @@ -38,15 +53,7 @@ export class HyperNamespaceProxy, Env> { namespace: DurableObjectNamespace, ref: new (state: DurableObjectState, env: Env) => DO ); - get(id: DurableObjectId): DurableObjectStub & { - [Prop in keyof DO]: - DO[Prop] extends (...args: any) => any - ? (...args: Parameters) => Promise> - : Promise; - } & { - [Prop in keyof DO as DO[Prop] extends Function ? never : `set${Capitalize}`]: - (newValue: DO[Prop]) => Promise - }; + get(id: DurableObjectId): HyperStub; } export function proxyHyperDurables, Env>(