Skip to content

Commit 6dbb3d7

Browse files
committed
services - MongoClientService: add explicit return type on public code + type tests (#44)
1 parent 1681943 commit 6dbb3d7

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as MongoClientService from "@effect-mongodb/services/MongoClientService"
2+
import type * as MongoClient from "effect-mongodb/MongoClient"
3+
import * as Config from "effect/Config"
4+
import type * as Context from "effect/Context"
5+
import * as Effect from "effect/Effect"
6+
7+
declare const mongoClient: MongoClientService.Tag<"MyMongoClient">
8+
9+
type SomeService = { url: string }
10+
declare const SomeService: Context.Tag<SomeService, SomeService>
11+
12+
// -------------------------------------------------------------------------------------
13+
// Tag
14+
// -------------------------------------------------------------------------------------
15+
16+
// $ExpectType Tag<"MyMongoClient">
17+
MongoClientService.Tag("MyMongoClient")
18+
19+
// -------------------------------------------------------------------------------------
20+
// layerEffect
21+
// -------------------------------------------------------------------------------------
22+
23+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, MongoError, never>
24+
MongoClientService.layerEffect(mongoClient, Effect.succeed("mongodb://localhost:27017"))
25+
26+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, ConfigError | MongoError, never>
27+
MongoClientService.layerEffect(mongoClient, Config.string("DATABASE_NAME"))
28+
29+
const withRequirements = SomeService.pipe(Effect.flatMap(({ url }) => Config.string(url)))
30+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, ConfigError | MongoError, SomeService>
31+
MongoClientService.layerEffect(mongoClient, withRequirements)
32+
33+
// -------------------------------------------------------------------------------------
34+
// layer
35+
// -------------------------------------------------------------------------------------
36+
37+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, MongoError, never>
38+
MongoClientService.layer(mongoClient, "mongodb://localhost:27017")
39+
40+
// -------------------------------------------------------------------------------------
41+
// fromMongoClient
42+
// -------------------------------------------------------------------------------------
43+
44+
declare const legacyClient: Effect.Effect<MongoClient.MongoClient>
45+
46+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, never, never>
47+
MongoClientService.fromMongoClient(mongoClient, legacyClient)
48+
49+
declare const legacyClientWithError: Effect.Effect<MongoClient.MongoClient, Error>
50+
51+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, Error, never>
52+
MongoClientService.fromMongoClient(mongoClient, legacyClientWithError)
53+
54+
declare const legacyClientWithRequirements: Effect.Effect<MongoClient.MongoClient, Error, SomeService>
55+
56+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, Error, SomeService>
57+
MongoClientService.fromMongoClient(mongoClient, legacyClientWithRequirements)

packages/services/src/MongoClientService.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @since 0.0.1
33
*/
44
import * as MongoClient from "effect-mongodb/MongoClient"
5+
import type * as MongoError from "effect-mongodb/MongoError"
56
import type * as Brand from "effect/Brand"
67
import * as Context from "effect/Context"
78
import * as Effect from "effect/Effect"
@@ -16,7 +17,7 @@ export const layerEffect = <MongoClientK extends string, E = never, R = never>(
1617
clientTag: Tag<MongoClientK>,
1718
url: Effect.Effect<string, E, R>,
1819
options?: MongoClient.MongoClientScopedOptions
19-
) =>
20+
): Layer.Layer<MongoClientService<MongoClientK>, MongoError.MongoError | E, R> =>
2021
Effect.gen(function*() {
2122
const url_ = yield* url
2223
return layer(clientTag, url_, options)
@@ -26,7 +27,7 @@ export const layer = <MongoClientK extends string>(
2627
clientTag: Tag<MongoClientK>,
2728
url: string,
2829
options?: MongoClient.MongoClientScopedOptions
29-
) =>
30+
): Layer.Layer<MongoClientService<MongoClientK>, MongoError.MongoError> =>
3031
Layer.scopedContext(Effect.gen(function*() {
3132
const client = yield* MongoClient.connectScoped(url, options)
3233
return Context.make(clientTag, client as MongoClientService<MongoClientK>)
@@ -35,7 +36,7 @@ export const layer = <MongoClientK extends string>(
3536
export const fromMongoClient = <MongoClientK extends string, E = never, R = never>(
3637
clientTag: Tag<MongoClientK>,
3738
mongoClient: Effect.Effect<MongoClient.MongoClient, E, R>
38-
) =>
39+
): Layer.Layer<MongoClientService<MongoClientK>, E, R> =>
3940
Layer.effect(
4041
clientTag,
4142
Effect.map(mongoClient, (client) => client as MongoClientService<MongoClientK>)

0 commit comments

Comments
 (0)