Skip to content

Commit

Permalink
fix: all resources have name type param (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
thantos authored May 21, 2023
1 parent 7bf3d62 commit 97bcdb9
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 90 deletions.
1 change: 1 addition & 0 deletions packages/@eventual/aws-runtime/src/stores/entity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface AWSEntityStoreProps {
}

export type EntityAttributesFromEntity<E extends Entity> = E extends Entity<
any,
infer Attributes,
any,
any
Expand Down
2 changes: 1 addition & 1 deletion packages/@eventual/client/src/http-eventual-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class HttpEventualClient implements EventualServiceClient {
return this.serviceClient.listWorkflows();
}

public async startExecution<W extends Workflow<any, any>>(
public async startExecution<W extends Workflow>(
request: StartExecutionRequest<W>
): Promise<ExecutionHandle<W>> {
// serialize the workflow object to a string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export async function orchestrateExecution(
* Retrieves the previously started executor or creates a new one and starts it.
*/
async function getExecutor(
workflow: Workflow<any, any>,
workflow: Workflow,
executionId: string,
logAgent?: LogAgent
): Promise<WorkflowExecutor<any, any, ExecutorRunContext>> {
Expand Down
4 changes: 2 additions & 2 deletions packages/@eventual/core-runtime/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export function entityStreamMatchesItem<
const Partition extends CompositeKeyPart<Attr>,
const Sort extends CompositeKeyPart<Attr> | undefined
>(
entity: Entity<Attr, Partition, Sort>,
entity: Entity<any, Attr, Partition, Sort>,
item: EntityStreamItem<Attr, Partition, Sort>,
streamSpec: EntityStreamSpec<Attr, Partition, Sort>
streamSpec: EntityStreamSpec<any, Attr, Partition, Sort>
) {
const { partition, sort } = normalizeCompositeKey(entity, item.key);
const normalizedQueryKeys =
Expand Down
2 changes: 1 addition & 1 deletion packages/@eventual/core-runtime/src/workflow-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class WorkflowExecutor<Input, Output, Context = undefined> {
} = {};

constructor(
private workflow: Workflow<Input, Output>,
private workflow: Workflow<any, Input, Output>,
public history: HistoryStateEvent[]
) {
this.nextSeq = 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/@eventual/core-runtime/src/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { WorkflowContext } from "@eventual/core";

declare module "@eventual/core" {
export interface Workflow<Input, Output> {
export interface Workflow<Name extends string, Input, Output> {
definition: (
input: Input,
context: WorkflowContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const entity = (() => {
) => {
// eslint-disable-next-line no-empty
while (entities().has(`ent${++n}`)) {}
return _entity<Attr, Partition, Sort>(`ent${n}`, options);
return _entity<string, Attr, Partition, Sort>(`ent${n}`, options);
};
})();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const workflow = (() => {
return <Input = any, Output = any>(
handler: WorkflowHandler<Input, Output>
) => {
return _workflow<Input, Output>(`wf${n++}`, handler);
return _workflow<any, Input, Output>(`wf${n++}`, handler);
};
})();

Expand Down
39 changes: 20 additions & 19 deletions packages/@eventual/core/src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { DurationSchedule } from "./schedule.js";

export type PresignedUrlOperation = "put" | "get" | "head" | "delete";

export interface Bucket extends Omit<BucketSpec, "handlers"> {
export interface Bucket<Name extends string = string>
extends Omit<BucketSpec<Name>, "handlers"> {
kind: "Bucket";
handlers: BucketNotificationHandler[];
/**
Expand Down Expand Up @@ -106,26 +107,26 @@ export interface Bucket extends Omit<BucketSpec, "handlers"> {
* });
* ```
*/
on(
on<Name extends string = string>(
events: BucketNotificationHandlerEventInput,
name: string,
name: Name,
options: Omit<BucketNotificationHandlerOptions, "eventTypes">,
handler: BucketNotificationHandlerFunction
): BucketNotificationHandler;
on(
): BucketNotificationHandler<Name>;
on<Name extends string = string>(
events: BucketNotificationHandlerEventInput,
name: string,
name: Name,
handler: BucketNotificationHandlerFunction
): BucketNotificationHandler;
): BucketNotificationHandler<Name>;
}

export function bucket(name: string): Bucket {
export function bucket<Name extends string = string>(name: Name): Bucket<Name> {
if (buckets().has(name)) {
throw new Error(`bucket with name '${name}' already exists`);
}

const handlers: BucketNotificationHandler[] = [];
const bucket: Bucket = {
const bucket: Bucket<Name> = {
name,
handlers,
kind: "Bucket",
Expand Down Expand Up @@ -217,29 +218,29 @@ export function bucket(name: string): Bucket {
// should be constant, can be used directly in a workflow.
return getBucketHook().physicalName(name);
},
on: (
on: <Name extends string = string>(
...args:
| [
events: BucketNotificationHandlerEventInput,
name: string,
name: Name,
handler: BucketNotificationHandlerFunction
]
| [
events: BucketNotificationHandlerEventInput,
name: string,
name: Name,
options: Omit<BucketNotificationHandlerOptions, "eventTypes">,
handler: BucketNotificationHandlerFunction
]
| [
sourceLocation: SourceLocation,
events: BucketNotificationHandlerEventInput,
name: string,
name: Name,
handler: BucketNotificationHandlerFunction
]
| [
sourceLocation: SourceLocation,
events: BucketNotificationHandlerEventInput,
name: string,
name: Name,
options: Omit<BucketNotificationHandlerOptions, "eventTypes">,
handler: BucketNotificationHandlerFunction
]
Expand All @@ -253,19 +254,19 @@ export function bucket(name: string): Bucket {
? [
args[0],
args[1] as BucketNotificationHandlerEventInput,
args[2] as string,
args[2] as Name,
,
args[3],
]
: [
,
args[0] as BucketNotificationHandlerEventInput,
args[1] as string,
args[1] as Name,
args[2] as Omit<BucketNotificationHandlerOptions, "eventTypes">,
args[3],
];

const bucketHandler: BucketNotificationHandler = {
const bucketHandler: BucketNotificationHandler<Name> = {
kind: "BucketNotificationHandler",
handler,
name: streamName,
Expand Down Expand Up @@ -349,8 +350,8 @@ export interface CopyBucketObjectResponse {
etag?: string;
}

export interface BucketNotificationHandler
extends BucketNotificationHandlerSpec {
export interface BucketNotificationHandler<Name extends string = string>
extends BucketNotificationHandlerSpec<Name> {
kind: "BucketNotificationHandler";
handler: BucketNotificationHandlerFunction;
sourceLocation?: SourceLocation;
Expand Down
41 changes: 23 additions & 18 deletions packages/@eventual/core/src/entity/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ export type EntityZodShape<Attr extends Attributes> = {
* @see entity
*/
export interface Entity<
Name extends string = string,
Attr extends Attributes = any,
Partition extends CompositeKeyPart<Attr> = CompositeKeyPart<Attr>,
Sort extends CompositeKeyPart<Attr> | undefined =
| CompositeKeyPart<Attr>
| undefined
> extends Omit<
EntitySpec,
EntitySpec<Name>,
"attributes" | "streams" | "partition" | "sort" | "indices"
> {
kind: "Entity";
key: KeyDefinition;
attributes: ZodAttributesObject<Attr>;
indices: EntityIndex[];
streams: EntityStream<Attr, Partition, Sort>[];
streams: EntityStream<any, Attr, Partition, Sort>[];
/**
* Get a value.
* If your values use composite keys, the namespace must be provided.
Expand Down Expand Up @@ -130,21 +131,22 @@ export interface Entity<
*/
scan(request?: EntityQueryOptions): Promise<EntityQueryResult<Attr>>;
index<
Name extends string = string,
const IndexPartition extends CompositeKeyPart<Attr> | undefined = undefined,
const IndexSort extends CompositeKeyPart<Attr> | undefined = undefined
>(
name: string,
name: Name,
options: EntityIndexOptions<Attr, IndexPartition, IndexSort>
): EntityIndexMapper<Attr, Partition, IndexPartition, IndexSort>;
stream(
name: string,
): EntityIndexMapper<Name, Attr, Partition, IndexPartition, IndexSort>;
stream<Name extends string = string>(
name: Name,
options: EntityStreamOptions<Attr, Partition, Sort>,
handler: EntityStreamHandler<Attr, Partition, Sort>
): EntityStream<Attr, Partition, Sort>;
stream(
): EntityStream<Name, Attr, Partition, Sort>;
stream<Name extends string = string>(
name: string,
handler: EntityStreamHandler<Attr, Partition, Sort>
): EntityStream<Attr, Partition, Sort>;
): EntityStream<Name, Attr, Partition, Sort>;
}

export const Entity = {
Expand Down Expand Up @@ -245,13 +247,14 @@ export interface EntityOptions<
* ```
*/
export function entity<
Name extends string,
Attr extends Attributes,
const Partition extends CompositeKeyPart<Attr>,
const Sort extends CompositeKeyPart<Attr> | undefined = undefined
>(
name: string,
name: Name,
options: EntityOptions<Attr, Partition, Sort>
): Entity<Attr, Partition, Sort> {
): Entity<Name, Attr, Partition, Sort> {
if (entities().has(name)) {
throw new Error(`entity with name '${name}' already exists`);
}
Expand All @@ -261,14 +264,14 @@ export function entity<
/**
* Used to maintain a limited number of streams on the entity.
*/
const streams: EntityStream<Attr, Partition, Sort>[] = [];
const streams: EntityStream<any, Attr, Partition, Sort>[] = [];

const attributes =
options.attributes instanceof z.ZodObject
? options.attributes
: (z.object(options.attributes) as unknown as ZodAttributesObject<Attr>);

const entity: Entity<Attr, Partition, Sort> = {
const entity: Entity<Name, Attr, Partition, Sort> = {
// @ts-ignore
__entityBrand: undefined,
kind: "Entity",
Expand Down Expand Up @@ -444,7 +447,7 @@ export function entity<
throw new Error("Only two streams are allowed per entity.");
}

const entityStream: EntityStream<Attr, Partition, Sort> = {
const entityStream: EntityStream<any, Attr, Partition, Sort> = {
kind: "EntityStream",
handler,
name: streamName,
Expand Down Expand Up @@ -478,21 +481,23 @@ export type EntityIndexOptions<
};

export type EntityIndexMapper<
Name extends string,
Attr extends Attributes,
EntityPartition extends CompositeKeyPart<Attr> = CompositeKeyPart<Attr>,
IndexPartition extends CompositeKeyPart<Attr> | undefined = undefined,
Sort extends CompositeKeyPart<Attr> | undefined = undefined
> = IndexPartition extends undefined
? EntityIndex<Attr, EntityPartition, Sort>
: EntityIndex<Attr, Exclude<IndexPartition, undefined>, Sort>;
? EntityIndex<Name, Attr, EntityPartition, Sort>
: EntityIndex<Name, Attr, Exclude<IndexPartition, undefined>, Sort>;

export interface EntityIndex<
Name extends string = string,
Attr extends Attributes = any,
Partition extends CompositeKeyPart<Attr> = CompositeKeyPart<Attr>,
Sort extends CompositeKeyPart<Attr> | undefined =
| CompositeKeyPart<Attr>
| undefined
> extends EntityIndexSpec {
> extends EntityIndexSpec<Name> {
kind: "EntityIndex";
query(
queryKey: QueryKey<Attr, Partition, Sort>,
Expand Down Expand Up @@ -562,7 +567,7 @@ interface EntityTransactItemBase<
Partition extends CompositeKeyPart<Attr>,
Sort extends CompositeKeyPart<Attr> | undefined
> {
entity: Entity<Attr, Partition, Sort> | string;
entity: Entity<any, Attr, Partition, Sort> | string;
}

export type EntityTransactItem<
Expand Down
15 changes: 8 additions & 7 deletions packages/@eventual/core/src/entity/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ export interface EntityStreamRemoveItem<
}

export interface EntityStream<
Name extends string,
Attr extends Attributes,
Partition extends CompositeKeyPart<Attr>,
Sort extends CompositeKeyPart<Attr> | undefined
> extends EntityStreamSpec<Attr, Partition, Sort> {
> extends EntityStreamSpec<Name, Attr, Partition, Sort> {
kind: "EntityStream";
handler: EntityStreamHandler<Attr, Partition, Sort>;
sourceLocation?: SourceLocation;
Expand All @@ -110,25 +111,25 @@ export function entityStream<
...args:
| [
name: string,
entity: Entity<Attr, Partition, Sort>,
entity: Entity<any, Attr, Partition, Sort>,
handler: EntityStreamHandler<Attr, Partition, Sort>
]
| [
name: string,
entity: Entity<Attr, Partition, Sort>,
entity: Entity<any, Attr, Partition, Sort>,
options: EntityStreamOptions<Attr, Partition, Sort>,
handler: EntityStreamHandler<Attr, Partition, Sort>
]
| [
sourceLocation: SourceLocation,
name: string,
entity: Entity<Attr, Partition, Sort>,
entity: Entity<any, Attr, Partition, Sort>,
handler: EntityStreamHandler<Attr, Partition, Sort>
]
| [
sourceLocation: SourceLocation,
name: string,
entity: Entity<Attr, Partition, Sort>,
entity: Entity<any, Attr, Partition, Sort>,
options: EntityStreamOptions<Attr, Partition, Sort>,
handler: EntityStreamHandler<Attr, Partition, Sort>
]
Expand All @@ -142,14 +143,14 @@ export function entityStream<
? [
args[0],
args[1] as string,
args[2] as Entity<Attr, Partition, Sort>,
args[2] as Entity<any, Attr, Partition, Sort>,
,
args[3],
]
: [
,
args[0] as string,
args[1] as Entity<Attr, Partition, Sort>,
args[1] as Entity<any, Attr, Partition, Sort>,
args[2] as EntityStreamOptions<Attr, Partition, Sort>,
args[3],
];
Expand Down
Loading

0 comments on commit 97bcdb9

Please sign in to comment.