effect-app 4.0.0-beta.19 → 4.0.0-beta.190
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +796 -0
- package/dist/Array.d.ts +1 -1
- package/dist/Chunk.d.ts +1 -1
- package/dist/Chunk.d.ts.map +1 -1
- package/dist/Config/SecretURL.d.ts +1 -1
- package/dist/Config/SecretURL.d.ts.map +1 -1
- package/dist/Config/SecretURL.js +2 -2
- package/dist/Config/internal/configSecretURL.d.ts +1 -1
- package/dist/Config/internal/configSecretURL.d.ts.map +1 -1
- package/dist/Config.d.ts +7 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +6 -0
- package/dist/ConfigProvider.d.ts +39 -0
- package/dist/ConfigProvider.d.ts.map +1 -0
- package/dist/ConfigProvider.js +42 -0
- package/dist/Context.d.ts +40 -0
- package/dist/Context.d.ts.map +1 -0
- package/dist/Context.js +67 -0
- package/dist/Effect.d.ts +9 -10
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js +3 -6
- package/dist/Function.d.ts +1 -1
- package/dist/Function.d.ts.map +1 -1
- package/dist/Inputify.type.d.ts +1 -1
- package/dist/Layer.d.ts +6 -5
- package/dist/Layer.d.ts.map +1 -1
- package/dist/Layer.js +1 -1
- package/dist/NonEmptySet.d.ts +1 -1
- package/dist/NonEmptySet.d.ts.map +1 -1
- package/dist/Operations.d.ts +369 -47
- package/dist/Operations.d.ts.map +1 -1
- package/dist/Operations.js +10 -10
- package/dist/Option.d.ts +1 -1
- package/dist/Option.d.ts.map +1 -1
- package/dist/Pure.d.ts +5 -5
- package/dist/Pure.d.ts.map +1 -1
- package/dist/Pure.js +13 -13
- package/dist/Schema/Class.d.ts +69 -20
- package/dist/Schema/Class.d.ts.map +1 -1
- package/dist/Schema/Class.js +190 -22
- package/dist/Schema/FastCheck.d.ts +1 -1
- package/dist/Schema/FastCheck.d.ts.map +1 -1
- package/dist/Schema/Methods.d.ts +1 -1
- package/dist/Schema/SchemaParser.d.ts +5 -0
- package/dist/Schema/SchemaParser.d.ts.map +1 -0
- package/dist/Schema/SchemaParser.js +6 -0
- package/dist/Schema/SpecialJsonSchema.d.ts +33 -0
- package/dist/Schema/SpecialJsonSchema.d.ts.map +1 -0
- package/dist/Schema/SpecialJsonSchema.js +122 -0
- package/dist/Schema/SpecialOpenApi.d.ts +32 -0
- package/dist/Schema/SpecialOpenApi.d.ts.map +1 -0
- package/dist/Schema/SpecialOpenApi.js +123 -0
- package/dist/Schema/brand.d.ts +7 -2
- package/dist/Schema/brand.d.ts.map +1 -1
- package/dist/Schema/brand.js +1 -1
- package/dist/Schema/email.d.ts +1 -1
- package/dist/Schema/email.d.ts.map +1 -1
- package/dist/Schema/email.js +7 -4
- package/dist/Schema/ext.d.ts +117 -45
- package/dist/Schema/ext.d.ts.map +1 -1
- package/dist/Schema/ext.js +126 -43
- package/dist/Schema/moreStrings.d.ts +111 -11
- package/dist/Schema/moreStrings.d.ts.map +1 -1
- package/dist/Schema/moreStrings.js +14 -15
- package/dist/Schema/numbers.d.ts +127 -15
- package/dist/Schema/numbers.d.ts.map +1 -1
- package/dist/Schema/numbers.js +10 -12
- package/dist/Schema/phoneNumber.d.ts +1 -1
- package/dist/Schema/phoneNumber.d.ts.map +1 -1
- package/dist/Schema/phoneNumber.js +6 -3
- package/dist/Schema/schema.d.ts +1 -1
- package/dist/Schema/strings.d.ts +37 -5
- package/dist/Schema/strings.d.ts.map +1 -1
- package/dist/Schema/strings.js +1 -5
- package/dist/Schema.d.ts +102 -56
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +128 -64
- package/dist/Set.d.ts +1 -1
- package/dist/Set.d.ts.map +1 -1
- package/dist/TypeTest.d.ts +1 -1
- package/dist/Types.d.ts +1 -1
- package/dist/Widen.type.d.ts +1 -1
- package/dist/_ext/Array.d.ts +1 -1
- package/dist/_ext/Array.d.ts.map +1 -1
- package/dist/_ext/date.d.ts +1 -1
- package/dist/_ext/misc.d.ts +1 -1
- package/dist/_ext/ord.ext.d.ts +1 -1
- package/dist/_ext/ord.ext.d.ts.map +1 -1
- package/dist/builtin.d.ts +1 -1
- package/dist/builtin.d.ts.map +1 -1
- package/dist/client/InvalidationKeys.d.ts +29 -0
- package/dist/client/InvalidationKeys.d.ts.map +1 -0
- package/dist/client/InvalidationKeys.js +33 -0
- package/dist/client/apiClientFactory.d.ts +17 -31
- package/dist/client/apiClientFactory.d.ts.map +1 -1
- package/dist/client/apiClientFactory.js +78 -26
- package/dist/client/clientFor.d.ts +62 -10
- package/dist/client/clientFor.d.ts.map +1 -1
- package/dist/client/clientFor.js +9 -1
- package/dist/client/errors.d.ts +49 -25
- package/dist/client/errors.d.ts.map +1 -1
- package/dist/client/errors.js +43 -17
- package/dist/client/makeClient.d.ts +360 -30
- package/dist/client/makeClient.d.ts.map +1 -1
- package/dist/client/makeClient.js +63 -23
- package/dist/client.d.ts +2 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -1
- package/dist/faker.d.ts +1 -1
- package/dist/faker.d.ts.map +1 -1
- package/dist/http/Request.d.ts +2 -2
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +5 -5
- package/dist/http/internal/lib.d.ts +1 -1
- package/dist/http.d.ts +1 -1
- package/dist/ids.d.ts +3 -3
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js +3 -2
- package/dist/index.d.ts +5 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -8
- package/dist/logger.d.ts +1 -1
- package/dist/middleware.d.ts +16 -9
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +13 -9
- package/dist/rpc/Invalidation.d.ts +845 -0
- package/dist/rpc/Invalidation.d.ts.map +1 -0
- package/dist/rpc/Invalidation.js +150 -0
- package/dist/rpc/MiddlewareMaker.d.ts +5 -4
- package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
- package/dist/rpc/MiddlewareMaker.js +26 -27
- package/dist/rpc/RpcContextMap.d.ts +3 -3
- package/dist/rpc/RpcContextMap.d.ts.map +1 -1
- package/dist/rpc/RpcContextMap.js +4 -4
- package/dist/rpc/RpcMiddleware.d.ts +5 -4
- package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
- package/dist/rpc/RpcMiddleware.js +1 -1
- package/dist/rpc.d.ts +2 -2
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +2 -2
- package/dist/transform.d.ts +1 -1
- package/dist/transform.d.ts.map +1 -1
- package/dist/transform.js +3 -3
- package/dist/utils/effectify.d.ts +1 -1
- package/dist/utils/extend.d.ts +1 -1
- package/dist/utils/extend.d.ts.map +1 -1
- package/dist/utils/gen.d.ts +2 -2
- package/dist/utils/gen.d.ts.map +1 -1
- package/dist/utils/logLevel.d.ts +2 -2
- package/dist/utils/logLevel.d.ts.map +1 -1
- package/dist/utils/logger.d.ts +3 -3
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +3 -3
- package/dist/utils.d.ts +30 -10
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +12 -6
- package/dist/validation/validators.d.ts +1 -1
- package/dist/validation/validators.d.ts.map +1 -1
- package/dist/validation.d.ts +1 -1
- package/dist/validation.d.ts.map +1 -1
- package/eslint.config.mjs +2 -2
- package/package.json +47 -23
- package/src/Config/SecretURL.ts +2 -1
- package/src/Config.ts +14 -0
- package/src/ConfigProvider.ts +48 -0
- package/src/{ServiceMap.ts → Context.ts} +52 -59
- package/src/Effect.ts +12 -14
- package/src/Layer.ts +5 -4
- package/src/Pure.ts +17 -18
- package/src/Schema/Class.ts +279 -62
- package/src/Schema/SchemaParser.ts +12 -0
- package/src/Schema/SpecialJsonSchema.ts +137 -0
- package/src/Schema/SpecialOpenApi.ts +130 -0
- package/src/Schema/brand.ts +9 -1
- package/src/Schema/email.ts +7 -2
- package/src/Schema/ext.ts +195 -73
- package/src/Schema/moreStrings.ts +22 -20
- package/src/Schema/numbers.ts +14 -16
- package/src/Schema/phoneNumber.ts +5 -1
- package/src/Schema/strings.ts +4 -8
- package/src/Schema.ts +265 -105
- package/src/client/InvalidationKeys.ts +50 -0
- package/src/client/apiClientFactory.ts +200 -119
- package/src/client/clientFor.ts +105 -23
- package/src/client/errors.ts +52 -26
- package/src/client/makeClient.ts +339 -63
- package/src/client.ts +1 -0
- package/src/http/Request.ts +7 -4
- package/src/ids.ts +2 -1
- package/src/index.ts +5 -10
- package/src/middleware.ts +12 -10
- package/src/rpc/Invalidation.ts +221 -0
- package/src/rpc/MiddlewareMaker.ts +36 -47
- package/src/rpc/README.md +2 -2
- package/src/rpc/RpcContextMap.ts +6 -5
- package/src/rpc/RpcMiddleware.ts +5 -4
- package/src/rpc.ts +1 -1
- package/src/transform.ts +2 -2
- package/src/utils/gen.ts +1 -1
- package/src/utils/logger.ts +2 -2
- package/src/utils.ts +49 -13
- package/test/dist/rpc.test.d.ts.map +1 -1
- package/test/dist/secretURL.test.d.ts.map +1 -0
- package/test/dist/special.test.d.ts.map +1 -0
- package/test/rpc.test.ts +38 -6
- package/test/schema.test.ts +591 -17
- package/test/secretURL.test.ts +157 -0
- package/test/special.test.ts +1023 -0
- package/test/utils.test.ts +6 -6
- package/tsconfig.base.json +3 -4
- package/tsconfig.json +0 -1
- package/tsconfig.json.bak +2 -2
- package/tsconfig.src.json +29 -29
- package/tsconfig.test.json +2 -2
- package/dist/ServiceMap.d.ts +0 -44
- package/dist/ServiceMap.d.ts.map +0 -1
- package/dist/ServiceMap.js +0 -91
- package/src/Operations.ts +0 -55
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import * as Config from "effect/Config"
|
|
3
2
|
import { flow } from "effect/Function"
|
|
4
3
|
import * as Layer from "effect/Layer"
|
|
5
4
|
import * as ManagedRuntime from "effect/ManagedRuntime"
|
|
5
|
+
import * as Option from "effect/Option"
|
|
6
6
|
import * as Predicate from "effect/Predicate"
|
|
7
7
|
import * as Schema from "effect/Schema"
|
|
8
|
+
import * as Stream from "effect/Stream"
|
|
8
9
|
import * as Struct from "effect/Struct"
|
|
9
10
|
import { Rpc, RpcClient, RpcGroup, RpcSerialization } from "effect/unstable/rpc"
|
|
11
|
+
import * as Config from "../Config.js"
|
|
12
|
+
import * as Context from "../Context.js"
|
|
10
13
|
import * as Effect from "../Effect.js"
|
|
11
14
|
import { HttpClient, HttpClientRequest } from "../http.js"
|
|
12
|
-
import
|
|
15
|
+
import { Invalidation } from "../rpc.js"
|
|
13
16
|
import type * as S from "../Schema.js"
|
|
14
|
-
import * as ServiceMap from "../ServiceMap.js"
|
|
15
17
|
import { typedKeysOf, typedValuesOf } from "../utils.js"
|
|
16
|
-
import type { Client, ClientForOptions,
|
|
18
|
+
import type { Client, ClientForOptions, ExtractModuleName, RequestsAny } from "./clientFor.js"
|
|
19
|
+
import { InvalidationKeysFromServer } from "./InvalidationKeys.js"
|
|
17
20
|
|
|
18
21
|
export interface ApiConfig {
|
|
19
22
|
url: string
|
|
@@ -31,16 +34,21 @@ export const DefaultApiConfig = Config.all({
|
|
|
31
34
|
})
|
|
32
35
|
|
|
33
36
|
export type Req = S.Top & {
|
|
34
|
-
|
|
37
|
+
readonly make: (...args: any[]) => any
|
|
35
38
|
_tag: string
|
|
36
39
|
fields: S.Struct.Fields
|
|
37
40
|
success: S.Top
|
|
38
41
|
error: S.Top
|
|
42
|
+
/** Optional final-value schema for stream requests. When set, the execute effect resolves with the last stream value decoded to this type. */
|
|
43
|
+
final?: S.Top
|
|
39
44
|
config?: Record<string, any>
|
|
45
|
+
readonly id: string
|
|
46
|
+
readonly moduleName: string
|
|
47
|
+
readonly type: "command" | "query" | "stream"
|
|
40
48
|
readonly "~decodingServices"?: unknown
|
|
41
49
|
}
|
|
42
50
|
|
|
43
|
-
class RequestName extends
|
|
51
|
+
class RequestName extends Context.Reference("RequestName", {
|
|
44
52
|
defaultValue: () => ({ requestName: "Unspecified", moduleName: "Error" })
|
|
45
53
|
}) {}
|
|
46
54
|
|
|
@@ -84,7 +92,7 @@ type RpcHandlers<M extends RequestsAny> = {
|
|
|
84
92
|
[K in keyof M]: Rpc.Rpc<M[K]["_tag"], M[K], M[K]["success"], M[K]["error"]>
|
|
85
93
|
}
|
|
86
94
|
|
|
87
|
-
const getFiltered = <M extends
|
|
95
|
+
const getFiltered = <M extends RequestsAny>(resource: M) => {
|
|
88
96
|
type Filtered = {
|
|
89
97
|
[K in keyof M as M[K] extends Req ? K : never]: M[K] extends Req ? M[K] : never
|
|
90
98
|
}
|
|
@@ -102,13 +110,13 @@ const getFiltered = <M extends Requests>(resource: M) => {
|
|
|
102
110
|
return filtered as unknown as Filtered
|
|
103
111
|
}
|
|
104
112
|
|
|
105
|
-
export const getMeta = <M extends
|
|
106
|
-
const
|
|
107
|
-
if (
|
|
108
|
-
|
|
113
|
+
export const getMeta = <M extends RequestsAny>(resource: M): { moduleName: ExtractModuleName<M> } => {
|
|
114
|
+
const first = typedValuesOf(getFiltered(resource))[0]
|
|
115
|
+
if (first && "moduleName" in first) return { moduleName: first.moduleName }
|
|
116
|
+
throw new Error("No moduleName on requests!")
|
|
109
117
|
}
|
|
110
118
|
|
|
111
|
-
export const makeRpcGroupFromRequestsAndModuleName = <M extends
|
|
119
|
+
export const makeRpcGroupFromRequestsAndModuleName = <M extends RequestsAny, const ModuleName extends string>(
|
|
112
120
|
resource: M,
|
|
113
121
|
moduleName: ModuleName
|
|
114
122
|
) => {
|
|
@@ -117,7 +125,19 @@ export const makeRpcGroupFromRequestsAndModuleName = <M extends Requests, const
|
|
|
117
125
|
const rpcs = RpcGroup
|
|
118
126
|
.make(
|
|
119
127
|
...typedValuesOf(filtered).map((_) => {
|
|
120
|
-
|
|
128
|
+
const r = _ as any
|
|
129
|
+
const isStream = r.type === "stream"
|
|
130
|
+
const isCommand = r.type === "command"
|
|
131
|
+
return (isCommand
|
|
132
|
+
? Invalidation.makeCommandRpc(r._tag, { payload: r, success: r.success, error: r.error })
|
|
133
|
+
: isStream
|
|
134
|
+
? Invalidation.makeStreamRpc(r._tag, {
|
|
135
|
+
payload: r,
|
|
136
|
+
success: r.success,
|
|
137
|
+
error: r.error,
|
|
138
|
+
stream: true as const
|
|
139
|
+
})
|
|
140
|
+
: Rpc.make(r._tag, { payload: r, success: r.success, error: r.error })) as any
|
|
121
141
|
})
|
|
122
142
|
)
|
|
123
143
|
.prefix(`${moduleName}.`) as unknown as RpcGroup.RpcGroup<
|
|
@@ -126,20 +146,13 @@ export const makeRpcGroupFromRequestsAndModuleName = <M extends Requests, const
|
|
|
126
146
|
return rpcs
|
|
127
147
|
}
|
|
128
148
|
|
|
129
|
-
|
|
130
|
-
M extends Requests,
|
|
131
|
-
const ModuleName extends string
|
|
132
|
-
>(
|
|
133
|
-
resource: M & { meta: { moduleName: ModuleName } }
|
|
134
|
-
) => makeRpcGroupFromRequestsAndModuleName(resource, resource.meta.moduleName)
|
|
135
|
-
|
|
136
|
-
const makeRpcTag = <M extends Requests>(resource: M) => {
|
|
149
|
+
const makeRpcTag = <M extends RequestsAny>(resource: M) => {
|
|
137
150
|
const meta = getMeta(resource)
|
|
138
151
|
const rpcs = makeRpcGroupFromRequestsAndModuleName(resource, meta.moduleName)
|
|
139
152
|
|
|
140
153
|
// Use Object.assign instead of class extension to avoid TS2509 with complex generic return types.
|
|
141
154
|
// The first type arg is `any` because this is a dynamically created tag — its identity is the string key.
|
|
142
|
-
const TheClient =
|
|
155
|
+
const TheClient = Context.Opaque<
|
|
143
156
|
any,
|
|
144
157
|
RpcClient.RpcClient<RpcGroup.Rpcs<typeof rpcs>>
|
|
145
158
|
>()(`RpcClient.${meta.moduleName}`)
|
|
@@ -153,99 +166,170 @@ const makeRpcTag = <M extends Requests>(resource: M) => {
|
|
|
153
166
|
|
|
154
167
|
const makeApiClientFactory = Effect
|
|
155
168
|
.gen(function*() {
|
|
156
|
-
const ctx = yield* Effect.
|
|
157
|
-
const makeClientFor =
|
|
169
|
+
const ctx = yield* Effect.context<RpcSerialization.RpcSerialization | HttpClient.HttpClient>()
|
|
170
|
+
const makeClientFor = Effect.fnUntraced(function*<M extends RequestsAny>(
|
|
158
171
|
resource: M,
|
|
159
172
|
requestLevelLayers = Layer.empty,
|
|
160
173
|
options?: ClientForOptions
|
|
161
|
-
)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
174
|
+
) {
|
|
175
|
+
const TheClient = makeRpcTag(resource)
|
|
176
|
+
|
|
177
|
+
const meta = getMeta(resource)
|
|
178
|
+
|
|
179
|
+
// TODO: somehow we need a protocol per REQUEST kind of it seems ...
|
|
180
|
+
// otherwise it locks up on the client, navigation remains empty...
|
|
181
|
+
const clientLayer = TheClient.layer.pipe(
|
|
182
|
+
// add ApiClientFactory for nested schemas
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
184
|
+
Layer.provide(Layer.succeed(ApiClientFactory, makeClientForCached as any)),
|
|
185
|
+
Layer.provide(
|
|
186
|
+
RpcClient
|
|
187
|
+
.layerProtocolHttp({ url: "" }) // why not here set meta.moduleName as root?
|
|
188
|
+
.pipe(
|
|
189
|
+
Layer.provideMerge(Layer.succeedContext(ctx))
|
|
190
|
+
)
|
|
191
|
+
)
|
|
192
|
+
)
|
|
193
|
+
const mr = ManagedRuntime.make(clientLayer)
|
|
194
|
+
|
|
195
|
+
const filtered = getFiltered(resource)
|
|
196
|
+
|
|
197
|
+
const unwrapCommand = (eff: Effect.Effect<any, any, any>): Effect.Effect<any, any, any> =>
|
|
198
|
+
eff.pipe(
|
|
199
|
+
Effect.flatMap((result: any) =>
|
|
200
|
+
Effect.gen(function*() {
|
|
201
|
+
const keys: ReadonlyArray<Invalidation.InvalidationKey> = result?.metadata?.invalidateQueries ?? []
|
|
202
|
+
const invalidationKeys = yield* InvalidationKeysFromServer
|
|
203
|
+
yield* Effect.forEach(keys, (key) => invalidationKeys.add(key), { discard: true })
|
|
204
|
+
return result.payload
|
|
205
|
+
})
|
|
206
|
+
),
|
|
207
|
+
// V2: unwrap CommandFailureWithMetaData failures — forward keys, re-fail with the
|
|
208
|
+
// original error so callers see the unmodified error type.
|
|
209
|
+
Effect.catch((result: any) =>
|
|
210
|
+
result?._tag === "CommandFailureWithMetaData"
|
|
211
|
+
? Effect.gen(function*() {
|
|
212
|
+
const keys: ReadonlyArray<Invalidation.InvalidationKey> = result.metadata?.invalidateQueries ?? []
|
|
213
|
+
const invalidationKeys = yield* InvalidationKeysFromServer
|
|
214
|
+
yield* Effect.forEach(keys, (key) => invalidationKeys.add(key), { discard: true })
|
|
215
|
+
return yield* Effect.fail(result.error)
|
|
177
216
|
})
|
|
178
|
-
.
|
|
179
|
-
Layer.provideMerge(Layer.succeedServices(ctx))
|
|
180
|
-
)
|
|
217
|
+
: Effect.fail(result)
|
|
181
218
|
)
|
|
182
219
|
)
|
|
183
|
-
const mr = ManagedRuntime.make(clientLayer)
|
|
184
220
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
221
|
+
return {
|
|
222
|
+
mr,
|
|
223
|
+
client: typedKeysOf(filtered)
|
|
224
|
+
.reduce((prev, cur) => {
|
|
225
|
+
const h = filtered[cur]!
|
|
226
|
+
|
|
227
|
+
const Request = h
|
|
228
|
+
|
|
229
|
+
const id = `${meta.moduleName}.${cur as string}`
|
|
230
|
+
.replaceAll(".js", "")
|
|
231
|
+
|
|
232
|
+
const requestMeta = {
|
|
233
|
+
Request,
|
|
234
|
+
id,
|
|
235
|
+
options
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const requestNameLayer = Layer.succeed(RequestName, {
|
|
239
|
+
requestName: cur as string,
|
|
240
|
+
moduleName: meta.moduleName
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
const layers = requestLevelLayers.pipe(Layer.provideMerge(requestNameLayer))
|
|
244
|
+
|
|
245
|
+
const fields = Struct.omit(Request.fields, ["_tag"] as const)
|
|
246
|
+
const requestAttr = `${meta.moduleName}.${h._tag}`
|
|
247
|
+
const isCommand = h.type === "command"
|
|
248
|
+
const isStream = h.type === "stream"
|
|
249
|
+
|
|
250
|
+
const buildEffect = (input: any) =>
|
|
251
|
+
mr.contextEffect.pipe(
|
|
252
|
+
Effect.flatMap((svcs) => {
|
|
253
|
+
const rpcEffect = TheClient
|
|
254
|
+
.use((client) =>
|
|
255
|
+
(client as any)[requestAttr]!(Request.make(input)) as Effect.Effect<any, any, never>
|
|
256
|
+
)
|
|
257
|
+
.pipe(
|
|
258
|
+
Effect.provide(layers),
|
|
259
|
+
Effect.provide(svcs)
|
|
260
|
+
)
|
|
261
|
+
return isCommand ? unwrapCommand(rpcEffect) : rpcEffect
|
|
262
|
+
})
|
|
263
|
+
)
|
|
193
264
|
|
|
194
|
-
|
|
195
|
-
|
|
265
|
+
const buildStream = (input: any) =>
|
|
266
|
+
Stream.unwrap(
|
|
267
|
+
mr.contextEffect.pipe(
|
|
268
|
+
Effect.flatMap((svcs) =>
|
|
269
|
+
TheClient
|
|
270
|
+
.useSync((client) => {
|
|
271
|
+
const rpcStream = (client as any)[requestAttr]!(
|
|
272
|
+
Request.make(input)
|
|
273
|
+
) as Stream.Stream<any, any, any>
|
|
274
|
+
return rpcStream.pipe(
|
|
275
|
+
// Collect server invalidation keys from the "done" chunk, then discard it.
|
|
276
|
+
Stream.tap((item: any) =>
|
|
277
|
+
item._tag === "done" || item._tag === "metadata"
|
|
278
|
+
? InvalidationKeysFromServer.use((svc) =>
|
|
279
|
+
Effect.forEach(
|
|
280
|
+
(item.metadata as Invalidation.CommandMetaData).invalidateQueries,
|
|
281
|
+
svc.add,
|
|
282
|
+
{ discard: true }
|
|
283
|
+
)
|
|
284
|
+
)
|
|
285
|
+
: Effect.void
|
|
286
|
+
),
|
|
287
|
+
Stream.filter((item: any) => item._tag === "value"),
|
|
288
|
+
Stream.map((item: any) => item.value),
|
|
289
|
+
// V2: unwrap StreamFailureChunk — forward keys from failures too,
|
|
290
|
+
// then re-fail with the original error so callers see the unmodified
|
|
291
|
+
// error type.
|
|
292
|
+
Stream.catch((err: any) =>
|
|
293
|
+
err?._tag === "error" && err?.metadata
|
|
294
|
+
? Stream.fromEffect(
|
|
295
|
+
InvalidationKeysFromServer.use((svc) =>
|
|
296
|
+
Effect
|
|
297
|
+
.forEach(
|
|
298
|
+
(err.metadata as Invalidation.CommandMetaData).invalidateQueries,
|
|
299
|
+
svc.add,
|
|
300
|
+
{ discard: true }
|
|
301
|
+
)
|
|
302
|
+
.pipe(Effect.flatMap(() => Effect.fail(err.error)))
|
|
303
|
+
)
|
|
304
|
+
)
|
|
305
|
+
: Stream.fail(err)
|
|
306
|
+
),
|
|
307
|
+
Stream.provide(layers),
|
|
308
|
+
Stream.provide(svcs)
|
|
309
|
+
)
|
|
310
|
+
})
|
|
311
|
+
.pipe(Effect.provide(svcs))
|
|
312
|
+
)
|
|
313
|
+
)
|
|
314
|
+
)
|
|
196
315
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
316
|
+
// @ts-expect-error doc
|
|
317
|
+
prev[cur] = Object.keys(fields).length === 0
|
|
318
|
+
? {
|
|
319
|
+
handler: isStream ? buildStream({}) : buildEffect({}),
|
|
320
|
+
...requestMeta
|
|
321
|
+
}
|
|
322
|
+
: {
|
|
323
|
+
handler: isStream
|
|
324
|
+
? (req: any) => buildStream(req)
|
|
325
|
+
: (req: any) => buildEffect(req),
|
|
326
|
+
...requestMeta
|
|
201
327
|
}
|
|
202
328
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const layers = requestLevelLayers.pipe(Layer.provideMerge(requestNameLayer))
|
|
209
|
-
|
|
210
|
-
const fields = Struct.omit(Request.fields, ["_tag"] as const)
|
|
211
|
-
const requestAttr = `${meta.moduleName}.${h._tag}`
|
|
212
|
-
// @ts-expect-error doc
|
|
213
|
-
prev[cur] = Object.keys(fields).length === 0
|
|
214
|
-
? {
|
|
215
|
-
handler: mr.servicesEffect.pipe(
|
|
216
|
-
Effect.flatMap((svcs) =>
|
|
217
|
-
TheClient
|
|
218
|
-
.use((client) => (client as any)[requestAttr]!(new Request()) as Effect.Effect<any, any, never>)
|
|
219
|
-
.pipe(
|
|
220
|
-
Effect.provide(layers),
|
|
221
|
-
Effect.provide(svcs)
|
|
222
|
-
)
|
|
223
|
-
)
|
|
224
|
-
),
|
|
225
|
-
...requestMeta
|
|
226
|
-
}
|
|
227
|
-
: {
|
|
228
|
-
handler: (req: any) =>
|
|
229
|
-
mr.servicesEffect.pipe(
|
|
230
|
-
Effect.flatMap((svcs) =>
|
|
231
|
-
TheClient
|
|
232
|
-
.use((client) =>
|
|
233
|
-
(client as any)[requestAttr]!(new Request(req)) as Effect.Effect<any, any, never>
|
|
234
|
-
)
|
|
235
|
-
.pipe(
|
|
236
|
-
Effect.provide(layers),
|
|
237
|
-
Effect.provide(svcs)
|
|
238
|
-
)
|
|
239
|
-
)
|
|
240
|
-
),
|
|
241
|
-
|
|
242
|
-
...requestMeta
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return prev
|
|
246
|
-
}, {} as Client<M, M["meta"]["moduleName"]>)
|
|
247
|
-
}
|
|
248
|
-
})
|
|
329
|
+
return prev
|
|
330
|
+
}, {} as Client<M, ExtractModuleName<M>>)
|
|
331
|
+
}
|
|
332
|
+
})
|
|
249
333
|
|
|
250
334
|
const register: ManagedRuntime.ManagedRuntime<any, any>[] = []
|
|
251
335
|
yield* Effect.addFinalizer(() => Effect.forEach(register, (mr) => mr.disposeEffect))
|
|
@@ -259,19 +343,16 @@ const makeApiClientFactory = Effect
|
|
|
259
343
|
cacheL.set(requestLevelLayers, cache)
|
|
260
344
|
}
|
|
261
345
|
|
|
262
|
-
return
|
|
263
|
-
models
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
register.push(m.mr)
|
|
273
|
-
return m.client
|
|
274
|
-
})
|
|
346
|
+
return Effect.fnUntraced(function*<M extends RequestsAny>(models: M) {
|
|
347
|
+
const found = cache.get(models) as Client<M, ExtractModuleName<M>> | undefined
|
|
348
|
+
if (found) {
|
|
349
|
+
return found
|
|
350
|
+
}
|
|
351
|
+
const m = yield* makeClientFor(models, requestLevelLayers, options)
|
|
352
|
+
cache.set(models, m.client)
|
|
353
|
+
register.push(m.mr)
|
|
354
|
+
return m.client
|
|
355
|
+
})
|
|
275
356
|
}
|
|
276
357
|
|
|
277
358
|
return makeClientForCached
|
|
@@ -281,7 +362,7 @@ const makeApiClientFactory = Effect
|
|
|
281
362
|
* Used to create clients for resource modules.
|
|
282
363
|
*/
|
|
283
364
|
export class ApiClientFactory
|
|
284
|
-
extends
|
|
365
|
+
extends Context.Opaque<ApiClientFactory, Effect.Success<typeof makeApiClientFactory>>()("ApiClientFactory")
|
|
285
366
|
{
|
|
286
367
|
static readonly layer = (config: ApiConfig) =>
|
|
287
368
|
ApiClientFactory.toLayer(makeApiClientFactory).pipe(Layer.provide(RpcSerializationLayer(config)))
|
|
@@ -294,7 +375,7 @@ export class ApiClientFactory
|
|
|
294
375
|
|
|
295
376
|
static readonly makeFor =
|
|
296
377
|
(requestLevelLayers: Layer.Layer<never, never, never>, options?: ClientForOptions) =>
|
|
297
|
-
<M extends
|
|
378
|
+
<M extends RequestsAny>(
|
|
298
379
|
resource: M
|
|
299
380
|
) =>
|
|
300
381
|
ApiClientFactory.use((apiClientFactory) => {
|
package/src/client/clientFor.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
3
|
|
|
4
4
|
import * as Record from "effect/Record"
|
|
5
|
-
import type * as
|
|
5
|
+
import type * as Stream from "effect/Stream"
|
|
6
6
|
import type { Path } from "path-parser"
|
|
7
7
|
import qs from "query-string"
|
|
8
8
|
import type * as Effect from "../Effect.js"
|
|
@@ -48,9 +48,14 @@ export function makePathWithBody(
|
|
|
48
48
|
return path.build(pars, { ignoreSearch: true, ignoreConstraints: true })
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
export type Requests
|
|
51
|
+
export type Requests = RequestsAny
|
|
52
52
|
export type RequestsAny = Record<string, any>
|
|
53
53
|
|
|
54
|
+
export type ExtractModuleName<M extends RequestsAny> =
|
|
55
|
+
{ [K in keyof M]: M[K] extends { moduleName: infer N extends string } ? N : never }[keyof M] extends
|
|
56
|
+
infer R extends string ? R
|
|
57
|
+
: string
|
|
58
|
+
|
|
54
59
|
export type Client<M extends RequestsAny, ModuleName extends string> = RequestHandlers<
|
|
55
60
|
never,
|
|
56
61
|
never,
|
|
@@ -66,16 +71,20 @@ export type ExtractEResponse<T> = T extends S.Codec<any> ? S.Codec.Encoded<T>
|
|
|
66
71
|
: T extends unknown ? void
|
|
67
72
|
: never
|
|
68
73
|
|
|
69
|
-
type IsEmpty<T> = keyof T extends never ? true
|
|
70
|
-
: false
|
|
71
|
-
|
|
72
|
-
// v4: Request.RequestTypeId, S.symbolSerializable, S.symbolWithResult removed — use keyof Request to filter internal props
|
|
73
|
-
type Cruft = "_tag" | keyof Request.Request<any, any, any>
|
|
74
|
-
|
|
75
74
|
export interface ClientForOptions {
|
|
76
75
|
readonly skipQueryKey?: readonly string[]
|
|
77
76
|
}
|
|
78
77
|
|
|
78
|
+
// $Project/$Configuration.Index
|
|
79
|
+
// -> "$Project", "$Configuration", "Index"
|
|
80
|
+
export const makeQueryKey = ({ id, options }: { id: string; options?: ClientForOptions }) =>
|
|
81
|
+
id
|
|
82
|
+
.split("/")
|
|
83
|
+
.filter((segment: string) => !options || !options.skipQueryKey?.includes(segment))
|
|
84
|
+
.map((segment: string) => "$" + segment)
|
|
85
|
+
.join(".")
|
|
86
|
+
.split(".")
|
|
87
|
+
|
|
79
88
|
export interface RequestHandler<A, E, R, Request extends Req, Id extends string> {
|
|
80
89
|
handler: Effect.Effect<A, E, R>
|
|
81
90
|
id: Id
|
|
@@ -90,24 +99,97 @@ export interface RequestHandlerWithInput<I, A, E, R, Request extends Req, Id ext
|
|
|
90
99
|
Request: Request
|
|
91
100
|
}
|
|
92
101
|
|
|
102
|
+
export interface RequestStreamHandler<A, E, R, Request extends Req, Id extends string, Final = A> {
|
|
103
|
+
handler: Stream.Stream<A, E, R>
|
|
104
|
+
id: Id
|
|
105
|
+
options?: ClientForOptions
|
|
106
|
+
Request: Request
|
|
107
|
+
/**
|
|
108
|
+
* Phantom type property (never set at runtime) that carries the `Final` type to
|
|
109
|
+
* `StreamMutationWithExtensions`. The tilde prefix follows the Effect convention for
|
|
110
|
+
* phantom/virtual properties and prevents accidental runtime access.
|
|
111
|
+
* Stream failures bubble through the execute effect's typed error channel `E`;
|
|
112
|
+
* the reactive `AsyncResult` ref also mirrors the failure for live progress UI.
|
|
113
|
+
*/
|
|
114
|
+
readonly "~final"?: Final
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface RequestStreamHandlerWithInput<I, A, E, R, Request extends Req, Id extends string, Final = A> {
|
|
118
|
+
handler: (i: I) => Stream.Stream<A, E, R>
|
|
119
|
+
id: Id
|
|
120
|
+
options?: ClientForOptions
|
|
121
|
+
Request: Request
|
|
122
|
+
/**
|
|
123
|
+
* Phantom type property (never set at runtime) that carries the `Final` type to
|
|
124
|
+
* `StreamMutationWithExtensions`. The tilde prefix follows the Effect convention for
|
|
125
|
+
* phantom/virtual properties and prevents accidental runtime access.
|
|
126
|
+
* Stream failures bubble through the execute effect's typed error channel `E`;
|
|
127
|
+
* the reactive `AsyncResult` ref also mirrors the failure for live progress UI.
|
|
128
|
+
*/
|
|
129
|
+
readonly "~final"?: Final
|
|
130
|
+
}
|
|
131
|
+
|
|
93
132
|
// make sure this is exported or d.ts of apiClientFactory breaks?!
|
|
94
133
|
type ReqDecodingServices<M> = M extends { readonly "~decodingServices": infer DS } ? DS : never
|
|
95
134
|
|
|
96
|
-
export type
|
|
97
|
-
[
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
135
|
+
export type RequestInputFromMake<I extends { readonly make: (...args: any[]) => any }> =
|
|
136
|
+
Parameters<I["make"]> extends [] ? void : Parameters<I["make"]>[0]
|
|
137
|
+
|
|
138
|
+
// Has no input only when the request schema declares no payload fields (the auto-added
|
|
139
|
+
// `_tag` field is ignored). Any payload fields (even all-optional) produce a function handler.
|
|
140
|
+
type HasNoFields<I> = I extends { readonly fields: infer F extends S.Struct.Fields }
|
|
141
|
+
? [Exclude<keyof F, "_tag">] extends [never] ? true : false
|
|
142
|
+
: false
|
|
143
|
+
|
|
144
|
+
type RequestInput<I extends { readonly make: (...args: any[]) => any }> = Parameters<I["make"]>[0]
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Caller-facing input type for a request. `void` when the request schema has no fields;
|
|
148
|
+
* otherwise `make`'s first param type.
|
|
149
|
+
*/
|
|
150
|
+
export type HandlerInput<I extends { readonly make: (...args: any[]) => any }> = HasNoFields<I> extends true ? void
|
|
151
|
+
: RequestInput<I>
|
|
152
|
+
|
|
153
|
+
/** Extracts the final-value type from a stream request. Defaults to the success type when no `final` schema is set. */
|
|
154
|
+
type FinalTypeOf<T extends Req> = T extends { readonly final: infer F extends S.Top } ? S.Schema.Type<F>
|
|
155
|
+
: S.Schema.Type<T["success"]>
|
|
156
|
+
|
|
157
|
+
type RequestHandlerFor<R, E, T extends Req, Id extends string> = T["type"] extends "stream"
|
|
158
|
+
? HasNoFields<T> extends true ? RequestStreamHandler<
|
|
159
|
+
S.Schema.Type<T["success"]>,
|
|
160
|
+
S.Schema.Type<T["error"]> | E,
|
|
161
|
+
R | ReqDecodingServices<T>,
|
|
162
|
+
T,
|
|
163
|
+
Id,
|
|
164
|
+
FinalTypeOf<T>
|
|
104
165
|
>
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
166
|
+
: RequestStreamHandlerWithInput<
|
|
167
|
+
RequestInput<T>,
|
|
168
|
+
S.Schema.Type<T["success"]>,
|
|
169
|
+
S.Schema.Type<T["error"]> | E,
|
|
170
|
+
R | ReqDecodingServices<T>,
|
|
171
|
+
T,
|
|
172
|
+
Id,
|
|
173
|
+
FinalTypeOf<T>
|
|
174
|
+
>
|
|
175
|
+
: HasNoFields<T> extends true ? RequestHandler<
|
|
176
|
+
S.Schema.Type<T["success"]>,
|
|
177
|
+
S.Schema.Type<T["error"]> | E,
|
|
178
|
+
R | ReqDecodingServices<T>,
|
|
179
|
+
T,
|
|
180
|
+
Id
|
|
112
181
|
>
|
|
182
|
+
: RequestHandlerWithInput<
|
|
183
|
+
RequestInput<T>,
|
|
184
|
+
S.Schema.Type<T["success"]>,
|
|
185
|
+
S.Schema.Type<T["error"]> | E,
|
|
186
|
+
R | ReqDecodingServices<T>,
|
|
187
|
+
T,
|
|
188
|
+
Id
|
|
189
|
+
>
|
|
190
|
+
|
|
191
|
+
export type RequestHandlers<R, E, M extends RequestsAny, ModuleName extends string> = {
|
|
192
|
+
[K in keyof M as M[K] extends Req ? K : never]: Extract<M[K], Req> extends infer T extends Req
|
|
193
|
+
? RequestHandlerFor<R, E, T, `${ModuleName}.${K & string}`>
|
|
194
|
+
: never
|
|
113
195
|
}
|