Skip to content

Commit

Permalink
add types to exported layers
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Oct 7, 2024
1 parent 85f51d9 commit b73c012
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 44 deletions.
43 changes: 31 additions & 12 deletions src/DiscordGateway.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import type { HttpClient } from "@effect/platform/HttpClient"
import type { WebSocketConstructor } from "@effect/platform/Socket"
import type { DiscordConfig } from "dfx/DiscordConfig"
import type { DiscordWSCodec } from "dfx/DiscordGateway/DiscordWS"
import type { Messsaging } from "dfx/DiscordGateway/Messaging"
import { Messaging, MesssagingLive } from "dfx/DiscordGateway/Messaging"
import type { RunningShard } from "dfx/DiscordGateway/Shard"
import type { ShardStateStore } from "dfx/DiscordGateway/Shard/StateStore"
import { Sharder, SharderLive } from "dfx/DiscordGateway/Sharder"
import type { ShardStore } from "dfx/DiscordGateway/ShardStore"
import type { RateLimitStore } from "dfx/RateLimit"
import type * as Discord from "dfx/types"
import { GenericTag } from "effect/Context"
import * as Effect from "effect/Effect"
Expand Down Expand Up @@ -30,21 +38,32 @@ export interface DiscordGateway {

export const DiscordGateway = GenericTag<DiscordGateway>("dfx/DiscordGateway")

export const make = Effect.gen(function* () {
const sharder = yield* Sharder
const messaging = yield* Messaging
export const make: Effect.Effect<DiscordGateway, never, Messsaging | Sharder> =
Effect.gen(function* () {
const sharder = yield* Sharder
const messaging = yield* Messaging

return DiscordGateway.of({
[TypeId]: TypeId,
dispatch: messaging.dispatch,
fromDispatch: messaging.fromDispatch,
handleDispatch: messaging.handleDispatch,
send: messaging.send,
shards: sharder.shards,
return DiscordGateway.of({
[TypeId]: TypeId,
dispatch: messaging.dispatch,
fromDispatch: messaging.fromDispatch,
handleDispatch: messaging.handleDispatch,
send: messaging.send,
shards: sharder.shards,
})
})
})

export const DiscordGatewayLive = Layer.effect(DiscordGateway, make).pipe(
export const DiscordGatewayLive: Layer.Layer<
DiscordGateway,
never,
| ShardStore
| DiscordConfig
| RateLimitStore
| DiscordWSCodec
| ShardStateStore
| WebSocketConstructor
| HttpClient.Service
> = Layer.effect(DiscordGateway, make).pipe(
Layer.provide(MesssagingLive),
Layer.provide(SharderLive),
)
8 changes: 5 additions & 3 deletions src/DiscordREST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ export interface DiscordRESTService
export const DiscordREST = GenericTag<DiscordREST, DiscordRESTService>(
"dfx/DiscordREST",
)
export const DiscordRESTLive = Layer.effect(DiscordREST, make).pipe(
Layer.provide(RateLimiterLive),
)
export const DiscordRESTLive: Layer.Layer<
DiscordREST,
never,
DiscordConfig | HttpClient.HttpClient.Service | RateLimitStore
> = Layer.effect(DiscordREST, make).pipe(Layer.provide(RateLimiterLive))
15 changes: 10 additions & 5 deletions src/Interactions/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Chunk from "effect/Chunk"
import type { Tag } from "effect/Context"
import { GenericTag } from "effect/Context"
import * as Duration from "effect/Duration"
import { pipe } from "effect/Function"
Expand Down Expand Up @@ -165,11 +166,15 @@ export interface InteractionsRegistry {
readonly _: unique symbol
}

export const InteractionsRegistry = GenericTag<
export const InteractionsRegistry: Tag<
InteractionsRegistry,
InteractionsRegistryService
>("dfx/Interactions/InteractionsRegistry")
export const InteractionsRegistryLive = Layer.scoped(
InteractionsRegistry,
makeRegistry,
> = GenericTag<InteractionsRegistry, InteractionsRegistryService>(
"dfx/Interactions/InteractionsRegistry",
)

export const InteractionsRegistryLive: Layer.Layer<
InteractionsRegistry,
never,
DiscordREST | DiscordGateway
> = Layer.scoped(InteractionsRegistry, makeRegistry)
6 changes: 3 additions & 3 deletions src/Interactions/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as Option from "effect/Option"
import type * as Cause from "effect/Cause"
import type * as Config from "effect/Config"
import type * as ConfigError from "effect/ConfigError"
import * as Secret from "effect/Secret"
import * as Redacted from "effect/Redacted"
import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"
import type * as D from "dfx/Interactions/definitions"
Expand Down Expand Up @@ -57,7 +57,7 @@ const checkSignature = (

export interface MakeConfigOpts {
readonly applicationId: string
readonly publicKey: Secret.Secret
readonly publicKey: Redacted.Redacted
readonly crypto: SubtleCrypto
readonly algorithm: keyof typeof Verify.PlatformAlgorithm
}
Expand All @@ -68,7 +68,7 @@ const makeConfig = ({
publicKey,
}: MakeConfigOpts) => ({
applicationId,
publicKey: Secret.value(publicKey),
publicKey: Redacted.value(publicKey),
crypto,
algorithm: Verify.PlatformAlgorithm[algorithm],
})
Expand Down
24 changes: 17 additions & 7 deletions src/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as CachePrelude from "dfx/Cache/prelude"
import type { DiscordGateway } from "dfx/DiscordGateway"
import { DiscordGatewayLive } from "dfx/DiscordGateway"
import * as DiscordWS from "dfx/DiscordGateway/DiscordWS"
import { JsonDiscordWSCodecLive } from "dfx/DiscordGateway/DiscordWS"
Expand All @@ -7,10 +8,16 @@ import { ShardStateStore } from "dfx/DiscordGateway/Shard/StateStore"
import * as SendEvent from "dfx/DiscordGateway/Shard/sendEvents"
import * as ShardStore from "dfx/DiscordGateway/ShardStore"
import { MemoryShardStoreLive } from "dfx/DiscordGateway/ShardStore"
import type { DiscordREST } from "dfx/DiscordREST"
import { DiscordRESTLive } from "dfx/DiscordREST"
import type { InteractionsRegistry } from "dfx/Interactions/gateway"
import { InteractionsRegistryLive } from "dfx/Interactions/gateway"
import type { RateLimiter } from "dfx/RateLimit"
import { MemoryRateLimitStoreLive, RateLimiterLive } from "dfx/RateLimit"
import * as Layer from "effect/Layer"
import type * as HttpClient from "@effect/platform/HttpClient"
import type { DiscordConfig } from "dfx/DiscordConfig"
import type { WebSocketConstructor } from "@effect/platform/Socket"

export { DiscordGateway, DiscordGatewayLive } from "dfx/DiscordGateway"

Expand All @@ -24,17 +31,20 @@ export {

export { CachePrelude, DiscordWS, SendEvent, Shard, ShardStore }

export const DiscordLive = Layer.mergeAll(
RateLimiterLive,
DiscordGatewayLive,
).pipe(
export const DiscordLive: Layer.Layer<
RateLimiter | DiscordGateway | DiscordREST,
never,
DiscordConfig | WebSocketConstructor | HttpClient.HttpClient.Service
> = Layer.mergeAll(RateLimiterLive, DiscordGatewayLive).pipe(
Layer.provideMerge(DiscordRESTLive),
Layer.provide(JsonDiscordWSCodecLive),
Layer.provide(MemoryRateLimitStoreLive),
Layer.provide(MemoryShardStoreLive),
Layer.provide(ShardStateStore.MemoryLive),
)

export const DiscordIxLive = InteractionsRegistryLive.pipe(
Layer.provideMerge(DiscordLive),
)
export const DiscordIxLive: Layer.Layer<
RateLimiter | DiscordGateway | DiscordREST | InteractionsRegistry,
never,
DiscordConfig | WebSocketConstructor | HttpClient.HttpClient.Service
> = InteractionsRegistryLive.pipe(Layer.provideMerge(DiscordLive))
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { HttpClient } from "@effect/platform/HttpClient"
import * as Cache from "dfx/Cache"
import * as DiscordConfig from "dfx/DiscordConfig"
import type { DiscordREST } from "dfx/DiscordREST"
import { DiscordRESTLive } from "dfx/DiscordREST"
import * as Flags from "dfx/Helpers/flags"
import * as Intents from "dfx/Helpers/intents"
Expand Down Expand Up @@ -35,6 +37,8 @@ export {
UI,
}

export const DiscordRESTMemoryLive = DiscordRESTLive.pipe(
Layer.provide(MemoryRateLimitStoreLive),
)
export const DiscordRESTMemoryLive: Layer.Layer<
DiscordREST,
never,
DiscordConfig.DiscordConfig | HttpClient.Service
> = DiscordRESTLive.pipe(Layer.provide(MemoryRateLimitStoreLive))
24 changes: 14 additions & 10 deletions src/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import type { HttpClient } from "@effect/platform/HttpClient"
import type { DiscordConfig } from "dfx/DiscordConfig"
import type { DiscordREST } from "dfx/DiscordREST"
import { DiscordRESTLive } from "dfx/DiscordREST"
import {
MemoryRateLimitStoreLive as MemoryRateLimitStoreLive,
RateLimiterLive as RateLimiterLive,
} from "dfx/RateLimit"
import type { RateLimiter } from "dfx/RateLimit"
import { MemoryRateLimitStoreLive, RateLimiterLive } from "dfx/RateLimit"
import * as Layer from "effect/Layer"

export {
BadWebhookSignature,
WebhookConfig,
WebhookParseError,
makeHandler,
makeSimpleHandler,
WebhookConfig,
layer as webhookLayer,
layerConfig as webhookLayerConfig,
WebhookParseError,
} from "dfx/Interactions/webhook"

export const DiscordLive = Layer.mergeAll(
DiscordRESTLive,
RateLimiterLive,
).pipe(Layer.provide(MemoryRateLimitStoreLive))
export const DiscordLive: Layer.Layer<
DiscordREST | RateLimiter,
never,
DiscordConfig | HttpClient.Service
> = Layer.mergeAll(DiscordRESTLive, RateLimiterLive).pipe(
Layer.provide(MemoryRateLimitStoreLive),
)

0 comments on commit b73c012

Please sign in to comment.