Skip to content

Commit

Permalink
feat: introduce tagged clients options for better narrowing
Browse files Browse the repository at this point in the history
issue #7
  • Loading branch information
floydspace committed Sep 25, 2023
1 parent d43c957 commit d7d6c50
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 48 deletions.
7 changes: 7 additions & 0 deletions .changeset/tricky-buttons-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect-aws/client-sns": minor
"@effect-aws/client-sqs": minor
"@effect-aws/client-s3": minor
---

introduce tagged clients options for better narrowing
33 changes: 20 additions & 13 deletions packages/client-s3/src/Context.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { S3Client, S3ClientConfig } from "@aws-sdk/client-s3";
import * as Context from "@effect/data/Context";
import * as Data from "@effect/data/Data";
import { flow } from "@effect/data/Function";
import * as Effect from "@effect/io/Effect";
import * as Layer from "@effect/io/Layer";
import * as Runtime from "@effect/io/Runtime";

export const S3ClientConfigTag = Context.Tag<S3ClientConfig>(
export class S3ClientOptions extends Data.TaggedClass(
"S3ClientOptions",
)<S3ClientConfig> {}

export const S3ClientConfigTag = Context.Tag<S3ClientOptions>(
"@effect-aws/S3Client/Config",
);

export const DefaultS3ClientConfigLayer = Layer.effect(
S3ClientConfigTag,
Effect.runtime<never>().pipe(
Effect.map((runtime) => ({
logger: {
info: flow(Effect.logInfo, Runtime.runSync(runtime)),
warn: flow(Effect.logWarning, Runtime.runSync(runtime)),
error: flow(Effect.logError, Runtime.runSync(runtime)),
debug: flow(Effect.logDebug, Runtime.runSync(runtime)),
trace: flow(Effect.logTrace, Runtime.runSync(runtime)),
},
})),
Effect.map(
(runtime) =>
new S3ClientOptions({
logger: {
info: flow(Effect.logInfo, Runtime.runSync(runtime)),
warn: flow(Effect.logWarning, Runtime.runSync(runtime)),
error: flow(Effect.logError, Runtime.runSync(runtime)),
debug: flow(Effect.logDebug, Runtime.runSync(runtime)),
trace: flow(Effect.logTrace, Runtime.runSync(runtime)),
},
}),
),
),
);

Expand All @@ -33,7 +41,6 @@ export const S3ClientInstanceLayer = Layer.effect(
S3ClientConfigTag.pipe(Effect.map((config) => new S3Client(config))),
);

export const DefaultS3ClientInstanceLayer = Layer.provide(
DefaultS3ClientConfigLayer,
S3ClientInstanceLayer,
export const DefaultS3ClientInstanceLayer = S3ClientInstanceLayer.pipe(
Layer.use(DefaultS3ClientConfigLayer),
);
8 changes: 5 additions & 3 deletions packages/client-s3/test/S3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DefaultS3ServiceEffect,
S3ClientConfigTag,
S3ClientInstanceTag,
S3ClientOptions,
S3ServiceEffect,
} from "../src";

Expand Down Expand Up @@ -43,9 +44,10 @@ describe("S3ClientImpl", () => {
s3.headObject(args),
);

const S3ClientConfigLayer = Layer.succeed(S3ClientConfigTag, {
region: "eu-central-1",
});
const S3ClientConfigLayer = Layer.succeed(
S3ClientConfigTag,
new S3ClientOptions({ region: "eu-central-1" }),
);

const result = await pipe(
program,
Expand Down
33 changes: 20 additions & 13 deletions packages/client-sns/src/Context.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { SNSClient, SNSClientConfig } from "@aws-sdk/client-sns";
import * as Context from "@effect/data/Context";
import * as Data from "@effect/data/Data";
import { flow } from "@effect/data/Function";
import * as Effect from "@effect/io/Effect";
import * as Layer from "@effect/io/Layer";
import * as Runtime from "@effect/io/Runtime";

export const SNSClientConfigTag = Context.Tag<SNSClientConfig>(
export class SNSClientOptions extends Data.TaggedClass(
"SNSClientOptions",
)<SNSClientConfig> {}

export const SNSClientConfigTag = Context.Tag<SNSClientOptions>(
"@effect-aws/SNSClient/Config",
);

export const DefaultSNSClientConfigLayer = Layer.effect(
SNSClientConfigTag,
Effect.runtime<never>().pipe(
Effect.map((runtime) => ({
logger: {
info: flow(Effect.logInfo, Runtime.runSync(runtime)),
warn: flow(Effect.logWarning, Runtime.runSync(runtime)),
error: flow(Effect.logError, Runtime.runSync(runtime)),
debug: flow(Effect.logDebug, Runtime.runSync(runtime)),
trace: flow(Effect.logTrace, Runtime.runSync(runtime)),
},
})),
Effect.map(
(runtime) =>
new SNSClientOptions({
logger: {
info: flow(Effect.logInfo, Runtime.runSync(runtime)),
warn: flow(Effect.logWarning, Runtime.runSync(runtime)),
error: flow(Effect.logError, Runtime.runSync(runtime)),
debug: flow(Effect.logDebug, Runtime.runSync(runtime)),
trace: flow(Effect.logTrace, Runtime.runSync(runtime)),
},
}),
),
),
);

Expand All @@ -33,7 +41,6 @@ export const SNSClientInstanceLayer = Layer.effect(
SNSClientConfigTag.pipe(Effect.map((config) => new SNSClient(config))),
);

export const DefaultSNSClientInstanceLayer = Layer.provide(
DefaultSNSClientConfigLayer,
SNSClientInstanceLayer,
export const DefaultSNSClientInstanceLayer = SNSClientInstanceLayer.pipe(
Layer.use(DefaultSNSClientConfigLayer),
);
8 changes: 5 additions & 3 deletions packages/client-sns/test/SNS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DefaultSNSServiceEffect,
SNSClientConfigTag,
SNSClientInstanceTag,
SNSClientOptions,
SNSServiceEffect,
} from "../src";

Expand Down Expand Up @@ -47,9 +48,10 @@ describe("SNSClientImpl", () => {
sns.publish(args),
);

const SNSClientConfigLayer = Layer.succeed(SNSClientConfigTag, {
region: "eu-central-1",
});
const SNSClientConfigLayer = Layer.succeed(
SNSClientConfigTag,
new SNSClientOptions({ region: "eu-central-1" }),
);

const result = await pipe(
program,
Expand Down
33 changes: 20 additions & 13 deletions packages/client-sqs/src/Context.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { SQSClient, SQSClientConfig } from "@aws-sdk/client-sqs";
import * as Context from "@effect/data/Context";
import * as Data from "@effect/data/Data";
import { flow } from "@effect/data/Function";
import * as Effect from "@effect/io/Effect";
import * as Layer from "@effect/io/Layer";
import * as Runtime from "@effect/io/Runtime";

export const SQSClientConfigTag = Context.Tag<SQSClientConfig>(
export class SQSClientOptions extends Data.TaggedClass(
"SQSClientOptions",
)<SQSClientConfig> {}

export const SQSClientConfigTag = Context.Tag<SQSClientOptions>(
"@effect-aws/SQSClient/Config",
);

export const DefaultSQSClientConfigLayer = Layer.effect(
SQSClientConfigTag,
Effect.runtime<never>().pipe(
Effect.map((runtime) => ({
logger: {
info: flow(Effect.logInfo, Runtime.runSync(runtime)),
warn: flow(Effect.logWarning, Runtime.runSync(runtime)),
error: flow(Effect.logError, Runtime.runSync(runtime)),
debug: flow(Effect.logDebug, Runtime.runSync(runtime)),
trace: flow(Effect.logTrace, Runtime.runSync(runtime)),
},
})),
Effect.map(
(runtime) =>
new SQSClientOptions({
logger: {
info: flow(Effect.logInfo, Runtime.runSync(runtime)),
warn: flow(Effect.logWarning, Runtime.runSync(runtime)),
error: flow(Effect.logError, Runtime.runSync(runtime)),
debug: flow(Effect.logDebug, Runtime.runSync(runtime)),
trace: flow(Effect.logTrace, Runtime.runSync(runtime)),
},
}),
),
),
);

Expand All @@ -33,7 +41,6 @@ export const SQSClientInstanceLayer = Layer.effect(
SQSClientConfigTag.pipe(Effect.map((config) => new SQSClient(config))),
);

export const DefaultSQSClientInstanceLayer = Layer.provide(
DefaultSQSClientConfigLayer,
SQSClientInstanceLayer,
export const DefaultSQSClientInstanceLayer = SQSClientInstanceLayer.pipe(
Layer.use(DefaultSQSClientConfigLayer),
);
8 changes: 5 additions & 3 deletions packages/client-sqs/test/SQS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DefaultSQSServiceEffect,
SQSClientConfigTag,
SQSClientInstanceTag,
SQSClientOptions,
SQSServiceEffect,
} from "../src";

Expand Down Expand Up @@ -53,9 +54,10 @@ describe("SQSClientImpl", () => {
sqs.sendMessage(args),
);

const SQSClientConfigLayer = Layer.succeed(SQSClientConfigTag, {
region: "eu-central-1",
});
const SQSClientConfigLayer = Layer.succeed(
SQSClientConfigTag,
new SQSClientOptions({ region: "eu-central-1" }),
);

const result = await pipe(
program,
Expand Down

0 comments on commit d7d6c50

Please sign in to comment.