Skip to content

Commit

Permalink
types: Export and parameterize HyperStub
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Frank committed Jun 14, 2022
1 parent 7c9b888 commit f0e4c62
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/HyperDurable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
15 changes: 8 additions & 7 deletions src/HyperNamespaceProxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HyperDurable } from './HyperDurable';
import { HyperError } from './HyperError';

export class HyperNamespaceProxy<DO extends HyperDurable<any, ENV>, ENV> implements DurableObjectNamespace {
export class HyperNamespaceProxy<DO extends HyperDurable<any, Env>, Env> implements DurableObjectNamespace {
namespace: DurableObjectNamespace;
ref: DO;

Expand All @@ -11,7 +11,7 @@ export class HyperNamespaceProxy<DO extends HyperDurable<any, ENV>, 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
Expand Down Expand Up @@ -40,21 +40,22 @@ export class HyperNamespaceProxy<DO extends HyperDurable<any, ENV>, ENV> impleme
get(id: DurableObjectId) {
// All of our prop getters & methods return Promises, since everything uses the
// fetch interface.
type PromisedGetStub = {
type PromisedGetStub<DO extends HyperDurable<any, Env>, Env> = {
[Prop in keyof DO]?:
DO[Prop] extends (...args: any) => any
? (...args: Parameters<DO[Prop]>) => Promise<ReturnType<DO[Prop]>>
: Promise<DO[Prop]>;
};
// All of our props have setters formatted as: setProperty()
type SetStub = {
type SetStub<DO extends HyperDurable<any, Env>, Env> = {
[Prop in keyof DO as DO[Prop] extends Function ? never : `set${Capitalize<string & Prop>}`]?:
(newValue: DO[Prop]) => Promise<DO[Prop]>
}
type HyperStub = DurableObjectStub & PromisedGetStub & SetStub;
type HyperStub<DO extends HyperDurable<any, Env>, Env> =
DurableObjectStub & PromisedGetStub<DO, Env> & SetStub<DO, Env>;

const stub = this.namespace.get(id);
const hyperStub: HyperStub = Object.assign<DurableObjectStub, object>(stub, {});
const hyperStub: HyperStub<DO, Env> = Object.assign<DurableObjectStub, object>(stub, {});

function createHyperRequest(action: string, key: string, payload?: object) {
return new Request(`https://hd.io/${action}/${key}`, {
Expand Down Expand Up @@ -111,7 +112,7 @@ export class HyperNamespaceProxy<DO extends HyperDurable<any, ENV>, ENV> impleme
}
}

return new Proxy<HyperStub>(hyperStub, handler);
return new Proxy<HyperStub<DO, Env>>(hyperStub, handler);
}
}

Expand Down
25 changes: 16 additions & 9 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ export class HyperDurable<T extends object, Env = unknown> {
fetch(request: Request): Promise<Response>;
}

export type PromisedGetStub<DO extends HyperDurable<any, Env>, Env> = {
[Prop in keyof DO]?:
DO[Prop] extends (...args: any) => any
? (...args: Parameters<DO[Prop]>) => Promise<ReturnType<DO[Prop]>>
: Promise<DO[Prop]>;
}

export type SetStub<DO extends HyperDurable<any, Env>, Env> = {
[Prop in keyof DO as DO[Prop] extends Function ? never : `set${Capitalize<string & Prop>}`]?:
(newValue: DO[Prop]) => Promise<DO[Prop]>
}

export type HyperStub<DO extends HyperDurable<any, Env>, Env> =
DurableObjectStub & PromisedGetStub<DO, Env> & SetStub<DO, Env>

export class HyperNamespaceProxy<DO extends HyperDurable<any, Env>, Env> {
namespace: DurableObjectNamespace;
ref: DO;
Expand All @@ -38,15 +53,7 @@ export class HyperNamespaceProxy<DO extends HyperDurable<any, Env>, 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<DO[Prop]>) => Promise<ReturnType<DO[Prop]>>
: Promise<DO[Prop]>;
} & {
[Prop in keyof DO as DO[Prop] extends Function ? never : `set${Capitalize<string & Prop>}`]:
(newValue: DO[Prop]) => Promise<DO[Prop]>
};
get(id: DurableObjectId): HyperStub<DO, Env>;
}

export function proxyHyperDurables<DO extends HyperDurable<any, Env>, Env>(
Expand Down

0 comments on commit f0e4c62

Please sign in to comment.