Skip to content

Commit ecbdf85

Browse files
committed
fix: resolve cyclic dependencies
1 parent 7c4ca64 commit ecbdf85

4 files changed

Lines changed: 67 additions & 65 deletions

File tree

src/BotApi.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type * as Layer from 'effect/Layer'
21
import type { BotApiTransport } from './BotApiTransport.ts'
32
import type { MethodParams, MethodResults } from './internal/botApiMethods.gen.ts'
43
import type { BotApiShape } from './internal/botApiShape.gen.ts'
54
import type * as Types from './internal/botApiTypes.gen.ts'
65
import * as Context from 'effect/Context'
76
import * as Data from 'effect/Data'
7+
import * as Layer from 'effect/Layer'
88
import * as internal from './internal/botApi.ts'
99

1010
export type { MethodParams, MethodResults, Types }
@@ -27,4 +27,8 @@ export class BotApiError extends Data.TaggedError('@grom.js/effect-tg/BotApiErro
2727
}
2828
}
2929

30-
export const layer: Layer.Layer<BotApi, never, BotApiTransport> = internal.layer
30+
export const layer: Layer.Layer<
31+
BotApi,
32+
never,
33+
BotApiTransport
34+
> = Layer.effect(BotApi, internal.make)

src/BotApiTransport.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type * as Effect from 'effect/Effect'
22
import type * as Types from './internal/botApiTypes.gen.ts'
33
import * as Context from 'effect/Context'
44
import * as Data from 'effect/Data'
5+
import * as Layer from 'effect/Layer'
56
import * as internal from './internal/botApiTransport.ts'
67

78
export class BotApiTransport extends Context.Tag('@grom.js/effect-tg/BotApiTransport')<
@@ -37,7 +38,9 @@ export class BotApiTransportError extends Data.TaggedError('@grom.js/effect-tg/B
3738
cause: unknown
3839
}> {}
3940

40-
export const layerWith = internal.layerWith
41+
export const layerWith = (options: {
42+
makeUrl: (method: string) => URL
43+
}) => Layer.effect(BotApiTransport, internal.makeWith(options))
4144

4245
export const layerProd = (token: string) => (
4346
layerWith({

src/internal/botApi.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,32 @@
11
import type { BotApiShape } from './botApiShape.gen.ts'
22
import * as Effect from 'effect/Effect'
3-
import * as Layer from 'effect/Layer'
4-
import { BotApi, BotApiError } from '../BotApi.ts'
3+
import { BotApiError } from '../BotApi.ts'
54
import { BotApiTransport } from '../BotApiTransport.ts'
65

7-
export const layer = Layer.effect(
8-
BotApi,
9-
Effect.gen(function* () {
10-
const transport = yield* BotApiTransport
11-
const botApi = new Proxy({}, {
12-
get: (_target, prop) => {
13-
if (typeof prop !== 'string') {
14-
return
15-
}
16-
const method = prop
17-
return (params: void | Record<string, unknown> = {}) => (
18-
Effect.gen(function* () {
19-
const response = yield* transport.sendRequest(method, params)
20-
if (response.ok) {
21-
return response.result
22-
}
23-
yield* Effect.fail(
24-
new BotApiError({
25-
code: response.error_code,
26-
description: response.description,
27-
parameters: response.parameters,
28-
}),
29-
)
30-
})
31-
)
32-
},
33-
})
34-
return botApi as BotApiShape
35-
}),
36-
)
6+
export const make = Effect.gen(function* () {
7+
const transport = yield* BotApiTransport
8+
const botApi = new Proxy({}, {
9+
get: (_target, prop) => {
10+
if (typeof prop !== 'string') {
11+
return
12+
}
13+
const method = prop
14+
return (params: void | Record<string, unknown> = {}) => (
15+
Effect.gen(function* () {
16+
const response = yield* transport.sendRequest(method, params)
17+
if (response.ok) {
18+
return response.result
19+
}
20+
yield* Effect.fail(
21+
new BotApiError({
22+
code: response.error_code,
23+
description: response.description,
24+
parameters: response.parameters,
25+
}),
26+
)
27+
})
28+
)
29+
},
30+
})
31+
return botApi as BotApiShape
32+
})

src/internal/botApiTransport.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
import type { BotApiResponse } from '../BotApiTransport.ts'
1+
import type { BotApiResponse, BotApiTransport } from '../BotApiTransport.ts'
22
import * as HttpBody from '@effect/platform/HttpBody'
33
import * as HttpClient from '@effect/platform/HttpClient'
44
import * as Effect from 'effect/Effect'
5-
import * as Layer from 'effect/Layer'
6-
import { BotApiTransport, BotApiTransportError } from '../BotApiTransport.ts'
5+
import { BotApiTransportError } from '../BotApiTransport.ts'
76

8-
export const layerWith = (options: {
7+
export const makeWith = (options: {
98
makeUrl: (method: string) => URL
10-
}) => Layer.effect(
11-
BotApiTransport,
12-
Effect.gen(function* () {
13-
const { makeUrl } = options
14-
const client = yield* HttpClient.HttpClient
15-
const transport: typeof BotApiTransport.Service = {
16-
sendRequest: (method, params) => (
17-
Effect
18-
.gen(function* () {
19-
const url = makeUrl(method)
20-
// TODO: Serialize necessary parameters and handle file uploads.
21-
const body = yield* HttpBody.json(params)
22-
const response = yield* client.post(url, { body })
23-
const responseJson = yield* response.json
24-
return responseJson as BotApiResponse
25-
})
26-
.pipe(
27-
Effect.catchAll(error => (
28-
Effect.fail(new BotApiTransportError({ cause: error }))
29-
)),
30-
)
31-
),
32-
}
33-
return transport
34-
}),
35-
)
9+
}): Effect.Effect<
10+
typeof BotApiTransport.Service,
11+
never,
12+
HttpClient.HttpClient
13+
> => Effect.gen(function* () {
14+
const { makeUrl } = options
15+
const client = yield* HttpClient.HttpClient
16+
return {
17+
sendRequest: (method, params) => (
18+
Effect
19+
.gen(function* () {
20+
const url = makeUrl(method)
21+
// TODO: Serialize necessary parameters and handle file uploads.
22+
const body = yield* HttpBody.json(params)
23+
const response = yield* client.post(url, { body })
24+
const responseJson = yield* response.json
25+
return responseJson as BotApiResponse
26+
})
27+
.pipe(
28+
Effect.catchAll(error => (
29+
Effect.fail(new BotApiTransportError({ cause: error }))
30+
)),
31+
)
32+
),
33+
}
34+
})

0 commit comments

Comments
 (0)