effect 4.0.0-beta.36 → 4.0.0-beta.38

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.
Files changed (125) hide show
  1. package/dist/Cause.d.ts +31 -0
  2. package/dist/Cause.d.ts.map +1 -1
  3. package/dist/Cause.js +19 -0
  4. package/dist/Cause.js.map +1 -1
  5. package/dist/Channel.d.ts +2 -2
  6. package/dist/Channel.d.ts.map +1 -1
  7. package/dist/Effect.d.ts +1 -16
  8. package/dist/Effect.d.ts.map +1 -1
  9. package/dist/Effect.js.map +1 -1
  10. package/dist/Schedule.d.ts.map +1 -1
  11. package/dist/Schedule.js +19 -1
  12. package/dist/Schedule.js.map +1 -1
  13. package/dist/Schema.d.ts +20 -48
  14. package/dist/Schema.d.ts.map +1 -1
  15. package/dist/Schema.js +9 -13
  16. package/dist/Schema.js.map +1 -1
  17. package/dist/SchemaAST.d.ts +5 -0
  18. package/dist/SchemaAST.d.ts.map +1 -1
  19. package/dist/SchemaAST.js.map +1 -1
  20. package/dist/SchemaParser.d.ts +22 -66
  21. package/dist/SchemaParser.d.ts.map +1 -1
  22. package/dist/SchemaParser.js +7 -2
  23. package/dist/SchemaParser.js.map +1 -1
  24. package/dist/Sink.d.ts +5 -2
  25. package/dist/Sink.d.ts.map +1 -1
  26. package/dist/Sink.js.map +1 -1
  27. package/dist/Stream.d.ts +1 -1
  28. package/dist/Stream.d.ts.map +1 -1
  29. package/dist/internal/effect.js +15 -1
  30. package/dist/internal/effect.js.map +1 -1
  31. package/dist/unstable/cli/Prompt.js +16 -6
  32. package/dist/unstable/cli/Prompt.js.map +1 -1
  33. package/dist/unstable/cluster/EntityAddress.d.ts.map +1 -1
  34. package/dist/unstable/cluster/EntityAddress.js +1 -1
  35. package/dist/unstable/cluster/EntityAddress.js.map +1 -1
  36. package/dist/unstable/cluster/Runner.d.ts.map +1 -1
  37. package/dist/unstable/cluster/Runner.js +1 -1
  38. package/dist/unstable/cluster/Runner.js.map +1 -1
  39. package/dist/unstable/cluster/RunnerAddress.d.ts.map +1 -1
  40. package/dist/unstable/cluster/RunnerAddress.js +1 -1
  41. package/dist/unstable/cluster/RunnerAddress.js.map +1 -1
  42. package/dist/unstable/cluster/ShardId.js +3 -3
  43. package/dist/unstable/cluster/ShardId.js.map +1 -1
  44. package/dist/unstable/devtools/DevToolsClient.d.ts.map +1 -1
  45. package/dist/unstable/devtools/DevToolsClient.js +4 -3
  46. package/dist/unstable/devtools/DevToolsClient.js.map +1 -1
  47. package/dist/unstable/devtools/DevToolsSchema.d.ts +2 -3
  48. package/dist/unstable/devtools/DevToolsSchema.d.ts.map +1 -1
  49. package/dist/unstable/devtools/DevToolsSchema.js +11 -1
  50. package/dist/unstable/devtools/DevToolsSchema.js.map +1 -1
  51. package/dist/unstable/eventlog/EventJournal.js +2 -2
  52. package/dist/unstable/eventlog/EventJournal.js.map +1 -1
  53. package/dist/unstable/eventlog/EventLog.js +1 -1
  54. package/dist/unstable/eventlog/EventLog.js.map +1 -1
  55. package/dist/unstable/eventlog/SqlEventLogJournal.js +2 -2
  56. package/dist/unstable/eventlog/SqlEventLogJournal.js.map +1 -1
  57. package/dist/unstable/http/HttpClientRequest.d.ts +24 -2
  58. package/dist/unstable/http/HttpClientRequest.d.ts.map +1 -1
  59. package/dist/unstable/http/HttpClientRequest.js +97 -0
  60. package/dist/unstable/http/HttpClientRequest.js.map +1 -1
  61. package/dist/unstable/http/HttpServerResponse.d.ts.map +1 -1
  62. package/dist/unstable/http/HttpServerResponse.js +2 -1
  63. package/dist/unstable/http/HttpServerResponse.js.map +1 -1
  64. package/dist/unstable/httpapi/HttpApiBuilder.js +9 -8
  65. package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
  66. package/dist/unstable/httpapi/HttpApiClient.d.ts +36 -20
  67. package/dist/unstable/httpapi/HttpApiClient.d.ts.map +1 -1
  68. package/dist/unstable/httpapi/HttpApiClient.js +49 -18
  69. package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
  70. package/dist/unstable/httpapi/HttpApiEndpoint.d.ts +186 -67
  71. package/dist/unstable/httpapi/HttpApiEndpoint.d.ts.map +1 -1
  72. package/dist/unstable/httpapi/HttpApiEndpoint.js +44 -29
  73. package/dist/unstable/httpapi/HttpApiEndpoint.js.map +1 -1
  74. package/dist/unstable/httpapi/HttpApiSchema.d.ts +5 -0
  75. package/dist/unstable/httpapi/HttpApiSchema.d.ts.map +1 -1
  76. package/dist/unstable/httpapi/HttpApiSchema.js +20 -2
  77. package/dist/unstable/httpapi/HttpApiSchema.js.map +1 -1
  78. package/dist/unstable/httpapi/OpenApi.d.ts.map +1 -1
  79. package/dist/unstable/httpapi/OpenApi.js +2 -5
  80. package/dist/unstable/httpapi/OpenApi.js.map +1 -1
  81. package/dist/unstable/reactivity/AtomHttpApi.d.ts +11 -7
  82. package/dist/unstable/reactivity/AtomHttpApi.d.ts.map +1 -1
  83. package/dist/unstable/reactivity/AtomHttpApi.js +6 -6
  84. package/dist/unstable/reactivity/AtomHttpApi.js.map +1 -1
  85. package/dist/unstable/schema/VariantSchema.d.ts +1 -1
  86. package/dist/unstable/schema/VariantSchema.d.ts.map +1 -1
  87. package/dist/unstable/sql/Migrator.d.ts.map +1 -1
  88. package/dist/unstable/sql/Migrator.js +2 -2
  89. package/dist/unstable/sql/Migrator.js.map +1 -1
  90. package/dist/unstable/sql/SqlError.d.ts +229 -9
  91. package/dist/unstable/sql/SqlError.d.ts.map +1 -1
  92. package/dist/unstable/sql/SqlError.js +256 -6
  93. package/dist/unstable/sql/SqlError.js.map +1 -1
  94. package/package.json +1 -1
  95. package/src/Cause.ts +35 -0
  96. package/src/Channel.ts +2 -2
  97. package/src/Effect.ts +1 -16
  98. package/src/Schedule.ts +20 -8
  99. package/src/Schema.ts +14 -22
  100. package/src/SchemaAST.ts +6 -0
  101. package/src/SchemaParser.ts +32 -31
  102. package/src/Sink.ts +5 -2
  103. package/src/Stream.ts +1 -1
  104. package/src/internal/effect.ts +27 -2
  105. package/src/unstable/cli/Prompt.ts +15 -6
  106. package/src/unstable/cluster/EntityAddress.ts +1 -1
  107. package/src/unstable/cluster/Runner.ts +1 -1
  108. package/src/unstable/cluster/RunnerAddress.ts +1 -1
  109. package/src/unstable/cluster/ShardId.ts +2 -2
  110. package/src/unstable/devtools/DevToolsClient.ts +23 -18
  111. package/src/unstable/devtools/DevToolsSchema.ts +13 -1
  112. package/src/unstable/eventlog/EventJournal.ts +2 -2
  113. package/src/unstable/eventlog/EventLog.ts +1 -1
  114. package/src/unstable/eventlog/SqlEventLogJournal.ts +2 -2
  115. package/src/unstable/http/HttpClientRequest.ts +104 -2
  116. package/src/unstable/http/HttpServerResponse.ts +8 -4
  117. package/src/unstable/httpapi/HttpApiBuilder.ts +15 -9
  118. package/src/unstable/httpapi/HttpApiClient.ts +118 -55
  119. package/src/unstable/httpapi/HttpApiEndpoint.ts +164 -36
  120. package/src/unstable/httpapi/HttpApiSchema.ts +20 -2
  121. package/src/unstable/httpapi/OpenApi.ts +2 -6
  122. package/src/unstable/reactivity/AtomHttpApi.ts +22 -17
  123. package/src/unstable/schema/VariantSchema.ts +1 -1
  124. package/src/unstable/sql/Migrator.ts +7 -5
  125. package/src/unstable/sql/SqlError.ts +360 -8
@@ -1,8 +1,11 @@
1
1
  /**
2
2
  * @since 4.0.0
3
3
  */
4
+ import * as Exit from "../../Exit.ts"
5
+ import { identity } from "../../Function.ts"
4
6
  import type * as Option from "../../Option.ts"
5
7
  import * as Schema from "../../Schema.ts"
8
+ import * as SchemaTransformation from "../../SchemaTransformation.ts"
6
9
 
7
10
  /**
8
11
  * @since 4.0.0
@@ -26,7 +29,16 @@ export type SpanStatusStarted = Schema.Schema.Type<typeof SpanStatusStarted>
26
29
  export const SpanStatusEnded = Schema.Struct({
27
30
  _tag: Schema.tag("Ended"),
28
31
  startTime: Schema.BigInt,
29
- endTime: Schema.BigInt
32
+ endTime: Schema.BigInt,
33
+ exit: Schema.Exit(Schema.Void, Schema.DefectWithStack, Schema.DefectWithStack).pipe(
34
+ Schema.decodeTo(
35
+ Schema.Exit(Schema.Unknown, Schema.Unknown, Schema.Unknown),
36
+ SchemaTransformation.transform({
37
+ decode: identity,
38
+ encode: Exit.asVoid
39
+ })
40
+ )
41
+ )
30
42
  })
31
43
 
32
44
  /**
@@ -252,7 +252,7 @@ export const makeMemory: Effect.Effect<EventJournal["Service"]> = Effect.gen(fun
252
252
  event,
253
253
  primaryKey,
254
254
  payload
255
- }, { disableValidation: true })
255
+ }, { disableChecks: true })
256
256
  ),
257
257
  effect,
258
258
  (entry, exit) =>
@@ -399,7 +399,7 @@ export const makeIndexedDb = (options?: {
399
399
  event,
400
400
  primaryKey,
401
401
  payload
402
- }, { disableValidation: true })
402
+ }, { disableChecks: true })
403
403
  return restore(effect(entry)).pipe(
404
404
  Effect.tap(
405
405
  idbReq("write", () =>
@@ -367,7 +367,7 @@ export const groupCompaction = <Events extends Event.Any, R>(
367
367
  Effect.orDie
368
368
  ) as any,
369
369
  primaryKey: event.primaryKey(payload)
370
- }, { disableValidation: true })
370
+ }, { disableChecks: true })
371
371
  yield* write(entry)
372
372
  })
373
373
 
@@ -199,7 +199,7 @@ export const make = (options?: {
199
199
  event,
200
200
  primaryKey,
201
201
  payload
202
- }, { disableValidation: true })
202
+ }, { disableChecks: true })
203
203
  yield* insertEntry(toEntryRow(entry))
204
204
  const value = yield* effect(entry)
205
205
  yield* PubSub.publish(pubsub, entry)
@@ -280,7 +280,7 @@ const toEntry = (row: EntryRow): EventJournal.Entry =>
280
280
  event: row.event,
281
281
  primaryKey: row.primary_key,
282
282
  payload: row.payload
283
- }, { disableValidation: true })
283
+ }, { disableChecks: true })
284
284
 
285
285
  const toEntryRow = (entry: EventJournal.Entry): EntryRow => ({
286
286
  id: entry.id,
@@ -15,10 +15,11 @@ import type * as Redacted from "../../Redacted.ts"
15
15
  import * as Result from "../../Result.ts"
16
16
  import type * as Schema from "../../Schema.ts"
17
17
  import type { ParseOptions } from "../../SchemaAST.ts"
18
- import type * as Stream from "../../Stream.ts"
18
+ import * as ServiceMap from "../../ServiceMap.ts"
19
+ import * as Stream from "../../Stream.ts"
19
20
  import * as Headers from "./Headers.ts"
20
21
  import * as HttpBody from "./HttpBody.ts"
21
- import type { HttpMethod } from "./HttpMethod.ts"
22
+ import { hasBody, type HttpMethod } from "./HttpMethod.ts"
22
23
  import * as UrlParams from "./UrlParams.ts"
23
24
 
24
25
  const TypeId = "~effect/http/HttpClientRequest"
@@ -954,3 +955,104 @@ export function toUrl(self: HttpClientRequest): Option.Option<URL> {
954
955
  }
955
956
  return Option.none()
956
957
  }
958
+
959
+ /**
960
+ * @since 4.0.0
961
+ * @category conversions
962
+ */
963
+ export const fromWeb = (request: globalThis.Request): HttpClientRequest => {
964
+ const method = request.method.toUpperCase() as HttpMethod
965
+ return modify(empty, {
966
+ method,
967
+ url: new URL(request.url),
968
+ headers: request.headers,
969
+ body: fromWebBody(request, method)
970
+ })
971
+ }
972
+
973
+ const fromWebBody = (request: globalThis.Request, method: HttpMethod): HttpBody.HttpBody => {
974
+ if (!hasBody(method) || request.body === null) {
975
+ return HttpBody.empty
976
+ }
977
+ return HttpBody.raw(request.body, {
978
+ contentType: request.headers.get("content-type") ?? undefined,
979
+ contentLength: parseContentLength(request.headers.get("content-length"))
980
+ })
981
+ }
982
+
983
+ const parseContentLength = (contentLength: string | null): number | undefined => {
984
+ if (contentLength === null) {
985
+ return undefined
986
+ }
987
+ const parsed = Number.parseInt(contentLength, 10)
988
+ return Number.isNaN(parsed) ? undefined : parsed
989
+ }
990
+
991
+ /**
992
+ * @since 4.0.0
993
+ * @category conversions
994
+ */
995
+ export const toWebResult = (self: HttpClientRequest, options?: {
996
+ readonly signal?: AbortSignal | undefined
997
+ readonly services?: ServiceMap.ServiceMap<never> | undefined
998
+ }): Result.Result<Request, UrlParams.UrlParamsError> => {
999
+ const url = UrlParams.makeUrl(self.url, self.urlParams, Option.getOrUndefined(self.hash))
1000
+ if (Result.isFailure(url)) {
1001
+ return Result.fail(url.failure)
1002
+ }
1003
+ const requestInit: RequestInit = {
1004
+ method: self.method,
1005
+ headers: self.headers
1006
+ }
1007
+ if (options?.signal) {
1008
+ requestInit.signal = options.signal
1009
+ }
1010
+ if (hasBody(self.method)) {
1011
+ switch (self.body._tag) {
1012
+ case "Empty": {
1013
+ break
1014
+ }
1015
+ case "Raw": {
1016
+ requestInit.body = self.body.body as any
1017
+ if (isReadableStream(self.body.body)) {
1018
+ ;(requestInit as any).duplex = "half"
1019
+ }
1020
+ break
1021
+ }
1022
+ case "Uint8Array": {
1023
+ requestInit.body = self.body.body as any
1024
+ break
1025
+ }
1026
+ case "FormData": {
1027
+ requestInit.body = self.body.formData
1028
+ break
1029
+ }
1030
+ case "Stream": {
1031
+ requestInit.body = Stream.toReadableStreamWith(self.body.stream, options?.services ?? ServiceMap.empty())
1032
+ ;(requestInit as any).duplex = "half"
1033
+ break
1034
+ }
1035
+ }
1036
+ }
1037
+ return Result.try({
1038
+ try: () => new Request(url.success, requestInit),
1039
+ catch: (cause) => new UrlParams.UrlParamsError({ cause })
1040
+ })
1041
+ }
1042
+
1043
+ const isReadableStream = (u: unknown): u is ReadableStream<Uint8Array> =>
1044
+ typeof ReadableStream !== "undefined" && u instanceof ReadableStream
1045
+
1046
+ /**
1047
+ * @since 4.0.0
1048
+ * @category conversions
1049
+ */
1050
+ export const toWeb = (self: HttpClientRequest, options?: {
1051
+ readonly signal?: AbortSignal | undefined
1052
+ }): Effect.Effect<Request, UrlParams.UrlParamsError> =>
1053
+ Effect.servicesWith((services) =>
1054
+ toWebResult(self, {
1055
+ services,
1056
+ signal: options?.signal
1057
+ }).asEffect()
1058
+ )
@@ -1225,12 +1225,16 @@ export const fromWeb = (response: Response): HttpServerResponse => {
1225
1225
  cookies: Cookies.fromSetCookie(setCookieHeaders)
1226
1226
  })
1227
1227
  if (response.body) {
1228
+ const contentType = response.headers.get("content-type")
1228
1229
  self = setBody(
1229
1230
  self,
1230
- Body.stream(Stream.fromReadableStream({
1231
- evaluate: () => response.body!,
1232
- onError: (e) => e
1233
- }))
1231
+ Body.stream(
1232
+ Stream.fromReadableStream({
1233
+ evaluate: () => response.body!,
1234
+ onError: (e) => e
1235
+ }),
1236
+ contentType ?? undefined
1237
+ )
1234
1238
  )
1235
1239
  }
1236
1240
  return self
@@ -652,7 +652,7 @@ const applyMiddleware = <A extends Effect.Effect<any, any, any>>(
652
652
  }
653
653
 
654
654
  const securityMiddlewareCache = new WeakMap<
655
- any,
655
+ object,
656
656
  (effect: Effect.Effect<any, any, any>, options: any) => Effect.Effect<any, any, any>
657
657
  >()
658
658
 
@@ -660,13 +660,14 @@ const makeSecurityMiddleware = (
660
660
  key: HttpApiMiddleware.AnyServiceSecurity,
661
661
  service: HttpApiMiddleware.HttpApiMiddlewareSecurity<any, any, any, any>
662
662
  ): (effect: Effect.Effect<any, any, any>, options: any) => Effect.Effect<any, any, any> => {
663
- if (securityMiddlewareCache.has(key)) {
664
- return securityMiddlewareCache.get(key)!
663
+ const cached = securityMiddlewareCache.get(service)
664
+ if (cached !== undefined) {
665
+ return cached
665
666
  }
666
667
 
667
- const entries = Object.entries(key.security).map(([key, security]) => ({
668
+ const entries = Object.entries(key.security).map(([securityKey, security]) => ({
668
669
  decode: securityDecode(security),
669
- middleware: service[key]
670
+ middleware: service[securityKey]
670
671
  }))
671
672
  if (entries.length === 0) {
672
673
  return identity
@@ -694,7 +695,7 @@ const makeSecurityMiddleware = (
694
695
  return yield* lastResult!.asEffect()
695
696
  })
696
697
 
697
- securityMiddlewareCache.set(key, middleware)
698
+ securityMiddlewareCache.set(service, middleware)
698
699
  return middleware
699
700
  }
700
701
 
@@ -734,7 +735,11 @@ function getResponseTransformation(
734
735
  schema: Schema.Top
735
736
  ): Transformation.Transformation<unknown, Response.HttpServerResponse> {
736
737
  const ast = schema.ast
737
- const encode = getResponseEncode(getStatus(ast), HttpApiSchema.getResponseEncoding(ast))
738
+ const encode = getResponseEncode(
739
+ getStatus(ast),
740
+ HttpApiSchema.getResponseEncoding(ast),
741
+ HttpApiSchema.isNoContent(ast)
742
+ )
738
743
 
739
744
  return Transformation.transformOrFail({
740
745
  decode: (res) => Effect.fail(new Issue.Forbidden(Option.some(res), { message: "Encode only schema" })),
@@ -744,12 +749,13 @@ function getResponseTransformation(
744
749
 
745
750
  function getResponseEncode<E>(
746
751
  status: number,
747
- encoding: HttpApiSchema.ResponseEncoding
752
+ encoding: HttpApiSchema.ResponseEncoding,
753
+ isNoContent: boolean
748
754
  ): (e: E) => Effect.Effect<Response.HttpServerResponse, Issue.InvalidValue, never> {
749
755
  switch (encoding._tag) {
750
756
  case "Json": {
751
757
  return ((e) => {
752
- if (e === undefined) {
758
+ if (e === undefined || isNoContent) {
753
759
  return Effect.succeed(Response.empty({ status }))
754
760
  }
755
761
  try {
@@ -8,7 +8,7 @@ import { identity } from "../../Function.ts"
8
8
  import * as Option from "../../Option.ts"
9
9
  import * as Predicate from "../../Predicate.ts"
10
10
  import * as Schema from "../../Schema.ts"
11
- import type * as AST from "../../SchemaAST.ts"
11
+ import * as AST from "../../SchemaAST.ts"
12
12
  import * as Issue from "../../SchemaIssue.ts"
13
13
  import * as Transformation from "../../SchemaTransformation.ts"
14
14
  import type * as ServiceMap from "../../ServiceMap.ts"
@@ -59,6 +59,21 @@ export type ForApi<Api extends HttpApi.Any, E = BadRequest, R = never> = Api ext
59
59
  * @category models
60
60
  */
61
61
  export declare namespace Client {
62
+ /**
63
+ * @since 4.0.0
64
+ * @category models
65
+ */
66
+ export type ResponseMode = HttpApiEndpoint.ClientResponseMode
67
+
68
+ /**
69
+ * @since 4.0.0
70
+ * @category models
71
+ */
72
+ export type Response<Success, Mode extends ResponseMode> = [Mode] extends ["decoded-and-response"]
73
+ ? [Success, HttpClientResponse.HttpClientResponse]
74
+ : [Mode] extends ["response-only"] ? HttpClientResponse.HttpClientResponse
75
+ : Success
76
+
62
77
  /**
63
78
  * @since 4.0.0
64
79
  * @category models
@@ -88,23 +103,21 @@ export declare namespace Client {
88
103
  infer _Middleware,
89
104
  infer _MR
90
105
  >
91
- ] ? <WithResponse extends boolean = false>(
92
- request: Simplify<HttpApiEndpoint.ClientRequest<_Params, _Query, _Payload, _Headers, WithResponse>>
106
+ ] ? <Mode extends ResponseMode = ResponseMode>(
107
+ request: Simplify<HttpApiEndpoint.ClientRequest<_Params, _Query, _Payload, _Headers, Mode>>
93
108
  ) => Effect.Effect<
94
- WithResponse extends true ? [_Success["Type"], HttpClientResponse.HttpClientResponse] : _Success["Type"],
95
- | _Error["Type"]
109
+ Response<_Success["Type"], Mode>,
96
110
  | HttpApiMiddleware.Error<_Middleware>
97
111
  | HttpApiMiddleware.ClientError<_Middleware>
98
112
  | E
99
113
  | HttpClientError.HttpClientError
100
- | Schema.SchemaError,
114
+ | ([Mode] extends ["response-only"] ? never : _Error["Type"] | Schema.SchemaError),
101
115
  | R
102
116
  | _Params["EncodingServices"]
103
117
  | _Query["EncodingServices"]
104
118
  | _Payload["EncodingServices"]
105
119
  | _Headers["EncodingServices"]
106
- | _Success["DecodingServices"]
107
- | _Error["DecodingServices"]
120
+ | ([Mode] extends ["response-only"] ? never : _Success["DecodingServices"] | _Error["DecodingServices"])
108
121
  > :
109
122
  never
110
123
 
@@ -120,24 +133,11 @@ export declare namespace Client {
120
133
  never
121
134
  }
122
135
 
123
- type ApiGroups<Api extends HttpApi.Any> = Api extends HttpApi.HttpApi<infer _ApiId, infer Groups> ? Groups : never
124
-
125
- type EndpointId<Endpoint extends HttpApiEndpoint.Any> = Endpoint extends {
126
- readonly method: infer Method extends HttpMethod.HttpMethod
127
- readonly path: infer Path extends string
128
- } ? `${Method} ${Path}`
129
- : never
130
-
131
- type EndpointWithId<Endpoints extends HttpApiEndpoint.Any, Id extends string> = Id extends
132
- `${infer Method extends HttpMethod.HttpMethod} ${infer Path extends string}` ?
133
- Extract<Endpoints, { readonly method: Method; readonly path: Path }> :
134
- never
135
-
136
136
  type UrlBuilderRequest<Endpoint extends HttpApiEndpoint.Any> = (
137
- & ([HttpApiEndpoint.Params<Endpoint>["Encoded"]] extends [never] ? {}
138
- : { readonly params: HttpApiEndpoint.Params<Endpoint>["Encoded"] })
139
- & ([HttpApiEndpoint.Query<Endpoint>["Encoded"]] extends [never] ? {}
140
- : { readonly query: HttpApiEndpoint.Query<Endpoint>["Encoded"] })
137
+ & ([HttpApiEndpoint.Params<Endpoint>["Type"]] extends [never] ? {}
138
+ : { readonly params: HttpApiEndpoint.Params<Endpoint>["Type"] })
139
+ & ([HttpApiEndpoint.Query<Endpoint>["Type"]] extends [never] ? {}
140
+ : { readonly query: HttpApiEndpoint.Query<Endpoint>["Type"] })
141
141
  ) extends infer Request ? keyof Request extends never ? void | undefined : Request
142
142
  : never
143
143
 
@@ -149,15 +149,33 @@ type UrlBuilderArgs<Endpoint extends HttpApiEndpoint.Any> = [UrlBuilderRequest<E
149
149
  * @since 4.0.0
150
150
  * @category models
151
151
  */
152
- export type UrlBuilder<Api extends HttpApi.Any> = <
153
- const GroupName extends HttpApiGroup.Name<ApiGroups<Api>>,
154
- const Id extends EndpointId<HttpApiGroup.EndpointsWithName<ApiGroups<Api>, GroupName>>
155
- >(
156
- group: GroupName,
157
- endpoint: Id,
158
- ...args: UrlBuilderArgs<EndpointWithId<HttpApiGroup.EndpointsWithName<ApiGroups<Api>, GroupName>, Id>>
152
+ export type UrlBuilder<Api extends HttpApi.Any> = Api extends HttpApi.HttpApi<infer _ApiId, infer Groups> ? Simplify<
153
+ & {
154
+ readonly [Group in Extract<Groups, { readonly topLevel: false }> as HttpApiGroup.Name<Group>]: UrlBuilderGroup<
155
+ HttpApiGroup.Endpoints<Group>
156
+ >
157
+ }
158
+ & {
159
+ readonly [Method in UrlBuilderTopLevelMethods<Groups> as Method[0]]: Method[1]
160
+ }
161
+ >
162
+ : never
163
+
164
+ type UrlBuilderGroup<Endpoints extends HttpApiEndpoint.Any> = {
165
+ readonly [Endpoint in Endpoints as HttpApiEndpoint.Name<Endpoint>]: UrlBuilderMethod<Endpoint>
166
+ }
167
+
168
+ type UrlBuilderMethod<Endpoint extends HttpApiEndpoint.Any> = (
169
+ ...args: UrlBuilderArgs<Endpoint>
159
170
  ) => string
160
171
 
172
+ type UrlBuilderTopLevelMethods<Groups extends HttpApiGroup.Any> = Extract<Groups, { readonly topLevel: true }> extends
173
+ HttpApiGroup.HttpApiGroup<infer _Id, infer _Endpoints, infer _TopLevel> ?
174
+ _Endpoints extends infer Endpoint extends HttpApiEndpoint.Any ?
175
+ [HttpApiEndpoint.Name<Endpoint>, UrlBuilderMethod<Endpoint>]
176
+ : never :
177
+ never
178
+
161
179
  const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Any, E, R>(
162
180
  api: HttpApi.HttpApi<ApiId, Groups>,
163
181
  options: {
@@ -268,7 +286,7 @@ const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Any, E, R>
268
286
  const payloadSchemas = HttpApiEndpoint.getPayloadSchemas(endpoint)
269
287
  const encodePayload = Arr.isArrayNonEmpty(payloadSchemas) ?
270
288
  HttpMethod.hasBody(endpoint.method)
271
- ? Schema.encodeUnknownEffect(getEncodePayloadSchema(payloadSchemas))
289
+ ? Schema.encodeUnknownEffect(getEncodePayloadSchema(payloadSchemas, endpoint.method))
272
290
  : Schema.encodeUnknownEffect(Schema.Union(payloadSchemas)) :
273
291
  undefined
274
292
 
@@ -283,7 +301,7 @@ const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Any, E, R>
283
301
  readonly query: unknown
284
302
  readonly payload: unknown
285
303
  readonly headers: Record<string, string> | undefined
286
- readonly withResponse?: boolean
304
+ readonly responseMode?: HttpApiEndpoint.ClientResponseMode
287
305
  } | undefined
288
306
  ) {
289
307
  let httpRequest = HttpClientRequest.make(endpoint.method)(endpoint.path)
@@ -331,11 +349,15 @@ const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Any, E, R>
331
349
  middlewareKeys.length - 1
332
350
  )
333
351
 
352
+ if (request?.responseMode === "response-only") {
353
+ return response
354
+ }
355
+
334
356
  const value = yield* (options.transformResponse === undefined
335
357
  ? decodeResponse(response)
336
358
  : options.transformResponse(decodeResponse(response)))
337
359
 
338
- return request?.withResponse === true ? [value, response] : value
360
+ return request?.responseMode === "decoded-and-response" ? [value, response] : value
339
361
  })
340
362
 
341
363
  options.onEndpoint({
@@ -477,7 +499,7 @@ export const endpoint = <
477
499
  }
478
500
 
479
501
  /**
480
- * Creates a type-safe URL builder keyed by `${method} ${path}`.
502
+ * Creates a type-safe URL builder that mirrors `HttpApiClient.make`.
481
503
  *
482
504
  * @example
483
505
  * ```ts
@@ -492,11 +514,11 @@ export const endpoint = <
492
514
  * )
493
515
  * )
494
516
  *
495
- * const buildUrl = HttpApiClient.urlBuilder<typeof Api>({
517
+ * const buildUrl = HttpApiClient.urlBuilder(Api, {
496
518
  * baseUrl: "https://api.example.com"
497
519
  * })
498
520
  *
499
- * buildUrl("users", "GET /users/:id", {
521
+ * buildUrl.users.getUser({
500
522
  * params: { id: "123" }
501
523
  * })
502
524
  * //=> "https://api.example.com/users/123"
@@ -504,19 +526,45 @@ export const endpoint = <
504
526
  * @since 4.0.0
505
527
  * @category constructors
506
528
  */
507
- export const urlBuilder = <Api extends HttpApi.Any>(options?: {
529
+ export const urlBuilder = <Api extends HttpApi.AnyWithProps>(api: Api, options?: {
508
530
  readonly baseUrl?: URL | string | undefined
509
531
  }): UrlBuilder<Api> => {
510
- return ((_: string, endpoint: string, request?: {
511
- readonly params?: Record<string, string | undefined> | undefined
512
- readonly query?: UrlParams.Input | undefined
513
- }) => {
514
- const path = endpoint.slice(endpoint.indexOf(" ") + 1)
515
- const withParams = request?.params === undefined ? path : compilePath(path)(request.params)
516
- const query = request?.query === undefined ? "" : UrlParams.toString(UrlParams.fromInput(request.query))
517
- const url = query === "" ? withParams : `${withParams}?${query}`
518
- return options?.baseUrl === undefined ? url : new URL(url, options.baseUrl.toString()).toString()
519
- }) as UrlBuilder<Api>
532
+ const builder: Record<string, any> = {}
533
+
534
+ HttpApi.reflect(api, {
535
+ onGroup({ group }) {
536
+ if (group.topLevel) return
537
+ builder[group.identifier] = {}
538
+ },
539
+ onEndpoint({ group, endpoint }) {
540
+ const makeUrl = compilePath(endpoint.path)
541
+ const encodeParams = endpoint.params === undefined
542
+ ? undefined
543
+ : Schema.encodeSync(endpoint.params as Schema.Encoder<unknown>)
544
+ const encodeQuery = endpoint.query === undefined
545
+ ? undefined
546
+ : Schema.encodeSync(endpoint.query as Schema.Encoder<unknown>)
547
+
548
+ const endpointBuilder = (request?: {
549
+ readonly params?: unknown
550
+ readonly query?: unknown
551
+ }) => {
552
+ const params = request?.params
553
+ const path = params === undefined
554
+ ? endpoint.path
555
+ : makeUrl((encodeParams === undefined ? params : encodeParams(params)) as Record<string, string | undefined>)
556
+ const queryInput = request?.query === undefined
557
+ ? undefined
558
+ : (encodeQuery === undefined ? request.query : encodeQuery(request.query)) as UrlParams.Input
559
+ const query = queryInput === undefined ? "" : UrlParams.toString(UrlParams.fromInput(queryInput))
560
+ const url = query === "" ? path : `${path}?${query}`
561
+ return options?.baseUrl === undefined ? url : new URL(url, options.baseUrl.toString()).toString()
562
+ }
563
+ ;(group.topLevel ? builder : builder[group.identifier])[endpoint.name] = endpointBuilder
564
+ }
565
+ })
566
+
567
+ return builder as UrlBuilder<Api>
520
568
  }
521
569
 
522
570
  // ----------------------------------------------------------------------------
@@ -608,8 +656,19 @@ function toCodecArrayBuffer(schemas: readonly [Schema.Top, ...Array<Schema.Top>]
608
656
  function onSchema(schema: Schema.Top) {
609
657
  const encoding = HttpApiSchema.getResponseEncoding(schema.ast)
610
658
  switch (encoding._tag) {
611
- case "Json":
612
- return UnknownFromArrayBuffer.pipe(Schema.decodeTo(schema))
659
+ case "Json": {
660
+ // handle json codecs that transform void schemas to null
661
+ const encodedIsNull = AST.isNull(AST.toEncoded(schema.ast))
662
+ return UnknownFromArrayBuffer.pipe(Schema.decodeTo(
663
+ schema,
664
+ encodedIsNull ?
665
+ Transformation.transform({
666
+ decode: (a) => a === undefined ? null : a,
667
+ encode: (a) => a === null ? undefined : a
668
+ }) as any :
669
+ undefined
670
+ ))
671
+ }
613
672
  case "FormUrlEncoded":
614
673
  return StringFromArrayBuffer.pipe(
615
674
  Schema.decodeTo(UrlParams.schemaRecord),
@@ -635,21 +694,25 @@ const statusOrElse = (response: HttpClientResponse.HttpClientResponse) =>
635
694
 
636
695
  const $HttpBody = Schema.declare(HttpBody.isHttpBody)
637
696
 
638
- function getEncodePayloadSchema(schemas: readonly [Schema.Top, ...Array<Schema.Top>]): Schema.Top {
639
- return Schema.Union(schemas.map(getEncodePayloadSchemaFromBody))
697
+ function getEncodePayloadSchema(
698
+ schemas: readonly [Schema.Top, ...Array<Schema.Top>],
699
+ method: HttpMethod.HttpMethod
700
+ ): Schema.Top {
701
+ return Schema.Union(schemas.map((s) => getEncodePayloadSchemaFromBody(s, method)))
640
702
  }
641
703
 
642
704
  const bodyFromPayloadCache = new WeakMap<AST.AST, Schema.Top>()
643
705
 
644
706
  function getEncodePayloadSchemaFromBody(
645
- schema: Schema.Top
707
+ schema: Schema.Top,
708
+ method: HttpMethod.HttpMethod
646
709
  ): Schema.Top {
647
710
  const ast = schema.ast
648
711
  const cached = bodyFromPayloadCache.get(ast)
649
712
  if (cached !== undefined) {
650
713
  return cached
651
714
  }
652
- const encoding = HttpApiSchema.getPayloadEncoding(ast)
715
+ const encoding = HttpApiSchema.getPayloadEncoding(ast, method)
653
716
  const out = $HttpBody.pipe(Schema.decodeTo(
654
717
  schema,
655
718
  Transformation.transformOrFail({