Skip to content

Commit d11c774

Browse files
committed
fix: bind types
1 parent c59b76c commit d11c774

File tree

15 files changed

+111
-116
lines changed

15 files changed

+111
-116
lines changed

@alchemy.run/effect-aws/src/lambda/consume.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Policy } from "@alchemy.run/effect";
1+
import type { From } from "@alchemy.run/effect";
2+
import { declare } from "@alchemy.run/effect";
23
import type {
34
Context as LambdaContext,
45
SQSBatchResponse,
@@ -30,13 +31,13 @@ export const consume =
3031
}: Props) =>
3132
Lambda.Function(id, {
3233
handle: Effect.fn(function* (event: SQSEvent, context: LambdaContext) {
33-
yield* Policy.declare<SQS.Consume<Q>>();
34+
yield* declare<SQS.Consume<From<Q>>>();
3435
const records = yield* Effect.all(
3536
event.Records.map(
3637
Effect.fn(function* (record) {
3738
return {
3839
...record,
39-
body: yield* S.validate(queue.input.schema)(record.body).pipe(
40+
body: yield* S.validate(queue.props.schema)(record.body).pipe(
4041
Effect.catchAll(() => Effect.void),
4142
),
4243
};
@@ -60,4 +61,4 @@ export const consume =
6061
],
6162
} satisfies SQSBatchResponse;
6263
}),
63-
})({ ...props, bindings: bindings.and(SQS.Consume(queue)) });
64+
})({ ...props, bindings: bindings.and(SQS.QueueEventSource(queue)) });

@alchemy.run/effect-aws/src/lambda/function.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import {
2+
Capability,
3+
Policy,
24
Provider,
35
Runtime,
46
type RuntimeHandler,
5-
type RuntimeProps,
67
} from "@alchemy.run/effect";
7-
import type { Context as LambdaContext } from "aws-lambda";
88

9-
export interface FunctionProps<Req = any> extends RuntimeProps<Function, Req> {
9+
import type { Context } from "aws-lambda";
10+
export type { Context } from "aws-lambda";
11+
12+
export interface FunctionProps<Req = any> {
1013
main: string;
1114
handler?: string;
1215
memory?: number;
1316
runtime?: "nodejs20x" | "nodejs22x";
1417
architecture?: "x86_64" | "arm64";
1518
url?: boolean;
19+
bindings: Policy<Function, Extract<Req, Capability>>;
1620
}
1721

1822
export type FunctionAttr<Props extends FunctionProps = FunctionProps> = {
@@ -28,11 +32,10 @@ export type FunctionAttr<Props extends FunctionProps = FunctionProps> = {
2832

2933
export interface Function<
3034
Handler extends
31-
| RuntimeHandler<[event: any, context: LambdaContext]>
35+
| RuntimeHandler<[event: any, context: Context]>
3236
| unknown = unknown,
3337
Props extends FunctionProps<RuntimeHandler.Caps<Handler>> | unknown = unknown,
3438
> extends Runtime<"AWS.Lambda.Function", Handler, Props> {
35-
readonly Constructor: Function;
3639
readonly Provider: FunctionProvider;
3740
readonly Instance: Function<this["handler"], this["props"]>;
3841

@alchemy.run/effect-aws/src/lambda/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export * from "./serve.ts";
99
export type * from "../account.ts";
1010
export type * from "../region.ts";
1111

12-
export type * as lambda from "aws-lambda";
12+
// export type * as lambda from "aws-lambda";

@alchemy.run/effect-aws/src/lambda/serve.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import type {
2-
Context as LambdaContext,
3-
LambdaFunctionURLEvent,
4-
LambdaFunctionURLResult,
5-
} from "aws-lambda";
61
import * as Effect from "effect/Effect";
72
import * as Lambda from "./function.ts";
83

4+
import type {
5+
LambdaFunctionURLEvent as FunctionURLEvent,
6+
LambdaFunctionURLResult as FunctionURLResult,
7+
} from "aws-lambda";
8+
export type {
9+
LambdaFunctionURLEvent as FunctionURLEvent,
10+
LambdaFunctionURLResult as FunctionURLResult,
11+
} from "aws-lambda";
12+
913
export const serve =
1014
<const ID extends string, Req>(
1115
id: ID,
1216
{
1317
fetch,
1418
}: {
1519
fetch: (
16-
event: LambdaFunctionURLEvent,
17-
context: LambdaContext,
18-
) => Effect.Effect<LambdaFunctionURLResult, never, Req>;
20+
event: FunctionURLEvent,
21+
context: Lambda.Context,
22+
) => Effect.Effect<FunctionURLResult, never, Req>;
1923
},
2024
) =>
2125
<const Props extends Lambda.FunctionProps<Req>>(props: Props) =>

@alchemy.run/effect-aws/src/sqs/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@ export * from "./queue.consume.ts";
33
export * from "./queue.provider.ts";
44
export * from "./queue.send-message.ts";
55
export * from "./queue.ts";
6-
7-
export type * as lambda from "aws-lambda";
8-
export type * as sqs from "itty-aws/sqs";

@alchemy.run/effect-aws/src/sqs/queue.consume.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { Capability, Declared } from "@alchemy.run/effect";
1+
import { Binding, Capability, type From } from "@alchemy.run/effect";
22
import type * as lambda from "aws-lambda";
33
import * as Effect from "effect/Effect";
4-
import { Function, type FunctionBinding } from "../lambda/index.ts";
4+
import { Function } from "../lambda/index.ts";
55
import { Queue } from "./queue.ts";
66

77
export type QueueRecord<Data> = Omit<lambda.SQSRecord, "body"> & {
@@ -12,22 +12,22 @@ export type QueueEvent<Data> = Omit<lambda.SQSEvent, "Records"> & {
1212
Records: QueueRecord<Data>[];
1313
};
1414

15-
export interface Consume<Q> extends Capability<"AWS.SQS.Consume", Q> {}
15+
export interface Consume<Q = Queue> extends Capability<"AWS.SQS.Consume", Q> {}
1616

17-
export const Consume = Function.binding<
17+
export const QueueEventSource = Binding<
1818
<Q extends Queue>(
19-
queue: Declared<Q>,
19+
queue: Q,
2020
props?: {
2121
batchSize?: number;
2222
maxBatchingWindow?: number;
2323
maxConcurrency?: number;
2424
reportBatchItemFailures?: boolean;
2525
},
26-
) => FunctionBinding<Consume<Q>>
27-
>("AWS.SQS.Consume", Queue);
26+
) => Binding<Function, Consume<From<Q>>>
27+
>(Function, Queue, "AWS.SQS.Consume");
2828

2929
export const consumeFromLambdaFunction = () =>
30-
Consume.layer.succeed({
30+
QueueEventSource.layer.succeed({
3131
// oxlint-disable-next-line require-yield
3232
attach: Effect.fn(function* (queue, _props, _target) {
3333
return {

@alchemy.run/effect-aws/src/sqs/queue.send-message.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ import * as Effect from "effect/Effect";
22

33
import {
44
Binding,
5-
Policy,
5+
declare,
66
type Capability,
7-
type Declared,
7+
type To,
88
} from "@alchemy.run/effect";
99
import { Function } from "../lambda/index.ts";
1010
import { QueueClient } from "./queue.client.ts";
1111
import { Queue } from "./queue.ts";
1212

13-
export interface SendMessage<Q> extends Capability<"AWS.SQS.SendMessage", Q> {}
13+
export interface SendMessage<Q = Queue>
14+
extends Capability<"AWS.SQS.SendMessage", Q> {}
15+
16+
export const SendMessage = Binding<
17+
<Q extends Queue>(queue: Q) => Binding<Function, SendMessage<To<Q>>>
18+
>(Function, Queue, "AWS.SQS.SendMessage");
1419

1520
export const sendMessage = <Q extends Queue>(
16-
queue: Declared<Q>,
21+
queue: Q,
1722
message: Q["props"]["schema"]["Type"],
1823
) =>
1924
Effect.gen(function* () {
20-
yield* Policy.declare<SendMessage<Q>>();
25+
yield* declare<SendMessage<To<Q>>>();
2126
const sqs = yield* QueueClient;
2227
const url =
2328
process.env[`${queue.id.toUpperCase().replace(/-/g, "_")}_QUEUE_URL`]!;
@@ -27,17 +32,6 @@ export const sendMessage = <Q extends Queue>(
2732
});
2833
});
2934

30-
// provide a custom tag to uniquely identify your binding implementation of Function<SendMessage<Q>>
31-
export const SendMessage2 = Binding<
32-
<Q extends Queue>(
33-
queue: Declared<Q>,
34-
) => Binding<Function, SendMessage<Q>, "Hyperdrive">
35-
>(Function, Queue, "Hyperdrive");
36-
37-
export const SendMessage = Binding<
38-
<Q extends Queue>(queue: Declared<Q>) => Binding<Function, SendMessage<Q>>
39-
>(Function, Queue, "AWS.SQS.SendMessage");
40-
4135
export const sendMessageFromLambdaFunction = () =>
4236
SendMessage.layer.succeed({
4337
// oxlint-disable-next-line require-yield

@alchemy.run/effect-example/src/api.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ import * as Effect from "effect/Effect";
55
import * as S from "effect/Schema";
66
import { Message, Messages } from "./messages.ts";
77

8-
// SQS.SendMessage<Messages>
9-
// -> FunctionBinding<SQS.SendMessage<Messages>>
10-
// -> FunctionBinding<SQS.SendMessage<Queue>>
11-
// -> "AWS.Lambda.Function(SQS.SendMessage(AWS.SQS.Queue))"
12-
13-
const ____ = $(SQS.SendMessage(Messages));
14-
const _____ = $(SQS.SendMessage2(Messages));
15-
168
const ___ = Lambda.serve("Api", {
179
fetch: Effect.fn(function* (event) {
1810
const msg = yield* S.validate(Message)(event.body).pipe(

@alchemy.run/effect/src/bind.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Effect from "effect/Effect";
22
import type { Capability } from "./capability.ts";
3-
import type { Runtime, RuntimeHandler } from "./runtime.ts";
3+
import type { Runtime, RuntimeHandler, RuntimeProps } from "./runtime.ts";
44

55
export const isBound = (value: any): value is Bound =>
66
value && typeof value === "object" && value.type === "bound";
@@ -28,7 +28,8 @@ export const bind = <
2828
handler: Handler,
2929
props: Props,
3030
) => {
31-
type Cap = Extract<Effect.Effect.Context<ReturnType<Handler>>, Capability>;
31+
type Req = Effect.Effect.Context<ReturnType<Handler>>;
32+
type Cap = Extract<Req, Capability>;
3233

3334
type Plan = {
3435
[id in ID]: Bound<
@@ -64,15 +65,14 @@ export const bind = <
6465
};
6566
return {
6667
...(Object.fromEntries(
67-
service.policy?.capabilities.map((cap: any) => [
68-
cap.resource.id,
69-
cap.resource,
70-
]) ?? [],
68+
(props as RuntimeProps<Run, Req>).bindings.capabilities.map(
69+
(cap: any) => [cap.resource.id, cap.resource],
70+
) ?? [],
7171
) as {
7272
// @ts-expect-error
7373
[id in Cap["resource"]["id"]]: Extract<Cap["resource"], { id: id }>;
7474
}),
75-
[service.id]: {
75+
[id]: {
7676
runtime: self,
7777
type: "bound",
7878
toString() {

@alchemy.run/effect/src/binding.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ export interface BindingTag<
3737
>
3838
> {
3939
/** @internal phantom */
40-
tagName: Tag;
40+
name: Tag;
4141
}
4242

4343
export const Binding = <F extends (resource: any, props?: any) => AnyBinding>(
4444
runtime: ReturnType<F>["runtime"],
4545
resource: new () => ReturnType<F>["capability"]["resource"],
46-
tag: ReturnType<F>["tag"]["tagName"],
46+
tag: ReturnType<F>["tag"]["name"],
4747
// _tag: ReturnType<F>,
4848
): F & BindingDeclaration<ReturnType<F>["runtime"], F> => {
4949
type Runtime = ReturnType<F>["runtime"];

0 commit comments

Comments
 (0)