File size: 26,154 Bytes
31e8457 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 | /**
* @since 4.0.0
*/
import * as Arr from "../../Array.ts"
import * as Cause from "../../Cause.ts"
import type * as Context from "../../Context.ts"
import * as Effect from "../../Effect.ts"
import { identity } from "../../Function.ts"
import * as Option from "../../Option.ts"
import * as Predicate from "../../Predicate.ts"
import * as Schema from "../../Schema.ts"
import * as AST from "../../SchemaAST.ts"
import * as Issue from "../../SchemaIssue.ts"
import * as Transformation from "../../SchemaTransformation.ts"
import type { Simplify } from "../../Types.ts"
import * as UndefinedOr from "../../UndefinedOr.ts"
import * as HttpBody from "../http/HttpBody.ts"
import * as HttpClient from "../http/HttpClient.ts"
import * as HttpClientError from "../http/HttpClientError.ts"
import * as HttpClientRequest from "../http/HttpClientRequest.ts"
import * as HttpClientResponse from "../http/HttpClientResponse.ts"
import * as HttpMethod from "../http/HttpMethod.ts"
import * as UrlParams from "../http/UrlParams.ts"
import * as HttpApi from "./HttpApi.ts"
import * as HttpApiEndpoint from "./HttpApiEndpoint.ts"
import type * as HttpApiGroup from "./HttpApiGroup.ts"
import type * as HttpApiMiddleware from "./HttpApiMiddleware.ts"
import * as HttpApiSchema from "./HttpApiSchema.ts"
/**
* @since 4.0.0
* @category models
*/
export type Client<Groups extends HttpApiGroup.Any, E = never, R = never> = Simplify<
& {
readonly [Group in Extract<Groups, { readonly topLevel: false }> as HttpApiGroup.Name<Group>]: Client.Group<
Group,
Group["identifier"],
E,
R
>
}
& {
readonly [Method in Client.TopLevelMethods<Groups, E, R> as Method[0]]: Method[1]
}
>
/**
* @since 4.0.0
* @category models
*/
export type ForApi<Api extends HttpApi.Any, E = never, R = never> = Api extends
HttpApi.HttpApi<infer _Id, infer Groups> ? Client<Groups, E, R> :
never
/**
* @since 4.0.0
* @category models
*/
export declare namespace Client {
/**
* @since 4.0.0
* @category models
*/
export type ResponseMode = HttpApiEndpoint.ClientResponseMode
/**
* @since 4.0.0
* @category models
*/
export type Response<Success, Mode extends ResponseMode> = [Mode] extends ["decoded-and-response"]
? [Success, HttpClientResponse.HttpClientResponse]
: [Mode] extends ["response-only"] ? HttpClientResponse.HttpClientResponse
: Success
/**
* @since 4.0.0
* @category models
*/
export type Group<Groups extends HttpApiGroup.Any, GroupName extends Groups["identifier"], E, R> =
[HttpApiGroup.WithName<Groups, GroupName>] extends [HttpApiGroup.HttpApiGroup<infer _GroupName, infer _Endpoints>] ?
{
readonly [Endpoint in _Endpoints as HttpApiEndpoint.Name<Endpoint>]: Method<Endpoint, E, R>
} :
never
/**
* @since 4.0.0
* @category models
*/
export type Method<Endpoint, E, R> = [Endpoint] extends [
HttpApiEndpoint.HttpApiEndpoint<
infer _Name,
infer _Method,
infer _Path,
infer _Params,
infer _Query,
infer _Payload,
infer _Headers,
infer _Success,
infer _Error,
infer _Middleware,
infer _MR
>
] ? <Mode extends ResponseMode = ResponseMode>(
request: Simplify<HttpApiEndpoint.ClientRequest<_Params, _Query, _Payload, _Headers, Mode>>
) => Effect.Effect<
Response<_Success["Type"], Mode>,
| HttpApiMiddleware.Error<_Middleware>
| HttpApiMiddleware.ClientError<_Middleware>
| E
| HttpClientError.HttpClientError
| ([Mode] extends ["response-only"] ? never : _Error["Type"] | Schema.SchemaError),
| R
| _Params["EncodingServices"]
| _Query["EncodingServices"]
| _Payload["EncodingServices"]
| _Headers["EncodingServices"]
| ([Mode] extends ["response-only"] ? never : _Success["DecodingServices"] | _Error["DecodingServices"])
> :
never
/**
* @since 4.0.0
* @category models
*/
export type TopLevelMethods<Groups extends HttpApiGroup.Any, E, R> =
Extract<Groups, { readonly topLevel: true }> extends
HttpApiGroup.HttpApiGroup<infer _Id, infer _Endpoints, infer _TopLevel> ?
_Endpoints extends infer Endpoint ? [HttpApiEndpoint.Name<Endpoint>, Method<Endpoint, E, R>]
: never :
never
}
type UrlBuilderRequest<Endpoint extends HttpApiEndpoint.Any> = (
& ([HttpApiEndpoint.Params<Endpoint>["Type"]] extends [never] ? {}
: { readonly params: HttpApiEndpoint.Params<Endpoint>["Type"] })
& ([HttpApiEndpoint.Query<Endpoint>["Type"]] extends [never] ? {}
: { readonly query: HttpApiEndpoint.Query<Endpoint>["Type"] })
) extends infer Request ? keyof Request extends never ? void | undefined : Request
: never
type UrlBuilderArgs<Endpoint extends HttpApiEndpoint.Any> = [UrlBuilderRequest<Endpoint>] extends [void | undefined] ?
[request?: UrlBuilderRequest<Endpoint>]
: [request: UrlBuilderRequest<Endpoint>]
/**
* @since 4.0.0
* @category models
*/
export type UrlBuilder<Api extends HttpApi.Any> = Api extends HttpApi.HttpApi<infer _ApiId, infer Groups> ? Simplify<
& {
readonly [Group in Extract<Groups, { readonly topLevel: false }> as HttpApiGroup.Name<Group>]: UrlBuilderGroup<
HttpApiGroup.Endpoints<Group>
>
}
& {
readonly [Method in UrlBuilderTopLevelMethods<Groups> as Method[0]]: Method[1]
}
>
: never
type UrlBuilderGroup<Endpoints extends HttpApiEndpoint.Any> = {
readonly [Endpoint in Endpoints as HttpApiEndpoint.Name<Endpoint>]: UrlBuilderMethod<Endpoint>
}
type UrlBuilderMethod<Endpoint extends HttpApiEndpoint.Any> = (
...args: UrlBuilderArgs<Endpoint>
) => string
type UrlBuilderTopLevelMethods<Groups extends HttpApiGroup.Any> = Extract<Groups, { readonly topLevel: true }> extends
HttpApiGroup.HttpApiGroup<infer _Id, infer _Endpoints, infer _TopLevel> ?
_Endpoints extends infer Endpoint extends HttpApiEndpoint.Any ?
[HttpApiEndpoint.Name<Endpoint>, UrlBuilderMethod<Endpoint>]
: never :
never
/** @internal */
export const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Any, E, R>(
api: HttpApi.HttpApi<ApiId, Groups>,
options: {
readonly httpClient: HttpClient.HttpClient.With<E, R>
readonly predicate?: Predicate.Predicate<{
readonly endpoint: HttpApiEndpoint.AnyWithProps
readonly group: HttpApiGroup.AnyWithProps
}>
readonly onGroup?: (options: {
readonly group: HttpApiGroup.AnyWithProps
readonly mergedAnnotations: Context.Context<never>
}) => void
readonly onEndpoint: (options: {
readonly group: HttpApiGroup.AnyWithProps
readonly endpoint: HttpApiEndpoint.AnyWithProps
readonly mergedAnnotations: Context.Context<never>
readonly middleware: ReadonlySet<HttpApiMiddleware.AnyService>
readonly successes: ReadonlyMap<number, readonly [Schema.Top, ...Array<Schema.Top>]>
readonly errors: ReadonlyMap<number, readonly [Schema.Top, ...Array<Schema.Top>]>
readonly endpointFn: Function
}) => void
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, unknown>) => Effect.Effect<unknown, unknown, unknown>)
| undefined
readonly baseUrl?: URL | string | undefined
}
): Effect.Effect<void> =>
Effect.gen(function*() {
const services = yield* Effect.context()
const httpClient = options.httpClient.pipe(
options?.baseUrl === undefined
? identity
: HttpClient.mapRequest(
HttpClientRequest.prependUrl(options.baseUrl.toString())
)
)
function executeMiddleware(
group: HttpApiGroup.AnyWithProps,
endpoint: HttpApiEndpoint.AnyWithProps,
request: HttpClientRequest.HttpClientRequest,
middlewareKeys: ReadonlyArray<string>,
index: number
): Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError> {
if (index === -1) {
return httpClient.execute(request) as unknown as Effect.Effect<
HttpClientResponse.HttpClientResponse,
HttpClientError.HttpClientError
>
}
const middleware = services.mapUnsafe.get(middlewareKeys[index]) as
| HttpApiMiddleware.HttpApiMiddlewareClient<any, any, any>
| undefined
if (middleware === undefined) {
return executeMiddleware(group, endpoint, request, middlewareKeys, index - 1)
}
return middleware({
endpoint,
group,
request,
next(request) {
return executeMiddleware(group, endpoint, request, middlewareKeys, index - 1)
}
}) as Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError>
}
HttpApi.reflect(api, {
predicate: options?.predicate,
onGroup(onGroupOptions) {
options.onGroup?.(onGroupOptions)
},
onEndpoint(onEndpointOptions) {
const { group, endpoint, errors, successes } = onEndpointOptions
const makeUrl = compilePath(endpoint.path)
const decodeMap: Record<
number | "orElse",
(response: HttpClientResponse.HttpClientResponse) => Effect.Effect<unknown, unknown, unknown>
> = { orElse: statusOrElse }
const decodeResponse = HttpClientResponse.matchStatus(decodeMap)
errors.forEach((schemas, status) => {
// decoders
const decode = schemasToResponse(schemas)
decodeMap[status] = (response) =>
Effect.flatMap(
Effect.catchCause(decode(response), (cause) =>
Effect.failCause(Cause.combine(
Cause.fail(
new HttpClientError.HttpClientError({
reason: new HttpClientError.StatusCodeError({
request: response.request,
response
})
})
),
cause
))),
Effect.fail
)
})
successes.forEach((schemas, status) => {
decodeMap[status] = schemasToResponse(schemas)
})
// encoders
const encodeParams = UndefinedOr.map(endpoint.params, Schema.encodeUnknownEffect)
const payloadSchemas = HttpApiEndpoint.getPayloadSchemas(endpoint)
const encodePayload = Arr.isArrayNonEmpty(payloadSchemas) ?
HttpMethod.hasBody(endpoint.method)
? Schema.encodeUnknownEffect(getEncodePayloadSchema(payloadSchemas, endpoint.method))
: Schema.encodeUnknownEffect(Schema.Union(payloadSchemas)) :
undefined
const encodeHeaders = UndefinedOr.map(endpoint.headers, Schema.encodeUnknownEffect)
const encodeQuery = UndefinedOr.map(endpoint.query, Schema.encodeUnknownEffect)
const middlewareKeys = Array.from(onEndpointOptions.middleware, (tag) => `${tag.key}/Client`)
const endpointFn = Effect.fnUntraced(function*(
request: {
readonly params: Record<string, string> | undefined
readonly query: unknown
readonly payload: unknown
readonly headers: Record<string, string> | undefined
readonly responseMode?: HttpApiEndpoint.ClientResponseMode
} | undefined
) {
let httpRequest = HttpClientRequest.make(endpoint.method)(endpoint.path)
if (request !== undefined) {
// params
if (encodeParams !== undefined) {
const params = (yield* encodeParams(request.params)) as Record<string, string>
httpRequest = HttpClientRequest.setUrl(httpRequest, makeUrl(params))
}
// payload
if (encodePayload !== undefined) {
if (HttpMethod.hasBody(endpoint.method)) {
if (request.payload instanceof FormData) {
httpRequest = HttpClientRequest.bodyFormData(httpRequest, request.payload)
} else {
const body = (yield* encodePayload(request.payload)) as HttpBody.HttpBody
httpRequest = HttpClientRequest.setBody(httpRequest, body)
}
} else {
const urlParams = (yield* encodePayload(request.payload)) as Record<string, string>
httpRequest = HttpClientRequest.appendUrlParams(httpRequest, urlParams)
}
}
// headers
if (encodeHeaders !== undefined) {
const headers = (yield* encodeHeaders(request.headers)) as Record<string, string>
httpRequest = HttpClientRequest.setHeaders(httpRequest, headers)
}
// query
if (encodeQuery !== undefined) {
const query = (yield* encodeQuery(request.query)) as Record<string, string>
httpRequest = HttpClientRequest.appendUrlParams(httpRequest, query)
}
}
const response = yield* executeMiddleware(
group,
endpoint,
httpRequest,
middlewareKeys,
middlewareKeys.length - 1
)
if (request?.responseMode === "response-only") {
return response
}
const value = yield* (options.transformResponse === undefined
? decodeResponse(response)
: options.transformResponse(decodeResponse(response)))
return request?.responseMode === "decoded-and-response" ? [value, response] : value
})
options.onEndpoint({
...onEndpointOptions,
endpointFn
})
}
})
})
/**
* @since 4.0.0
* @category constructors
*/
export const make = <ApiId extends string, Groups extends HttpApiGroup.Any>(
api: HttpApi.HttpApi<ApiId, Groups>,
options?: {
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, unknown>) => Effect.Effect<unknown, unknown, unknown>)
| undefined
readonly baseUrl?: URL | string | undefined
}
): Effect.Effect<
Client<Groups>,
never,
HttpClient.HttpClient | HttpApiGroup.MiddlewareClient<Groups>
> =>
Effect.flatMap(HttpClient.HttpClient.asEffect(), (httpClient) =>
makeWith(api, {
...options,
httpClient: options?.transformClient ? options.transformClient(httpClient) : httpClient
}))
/**
* @since 4.0.0
* @category constructors
*/
export const makeWith = <ApiId extends string, Groups extends HttpApiGroup.Any, E, R>(
api: HttpApi.HttpApi<ApiId, Groups>,
options: {
readonly httpClient: HttpClient.HttpClient.With<E, R>
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, unknown>) => Effect.Effect<unknown, unknown, unknown>)
| undefined
readonly baseUrl?: URL | string | undefined
}
): Effect.Effect<Client<Groups, E, R>, never, HttpApiGroup.MiddlewareClient<Groups>> => {
const client: Record<string, Record<string, any>> = {}
return makeClient(api, {
...options,
onGroup({ group }) {
if (group.topLevel) return
client[group.identifier] = {}
},
onEndpoint({ endpoint, endpointFn, group }) {
;(group.topLevel ? client : client[group.identifier])[endpoint.name] = endpointFn
}
}).pipe(Effect.as(client)) as any
}
/**
* @since 4.0.0
* @category constructors
*/
export const group = <
ApiId extends string,
Groups extends HttpApiGroup.Any,
const GroupName extends HttpApiGroup.Name<Groups>,
E,
R
>(
api: HttpApi.HttpApi<ApiId, Groups>,
options: {
readonly group: GroupName
readonly httpClient: HttpClient.HttpClient.With<E, R>
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, unknown>) => Effect.Effect<unknown, unknown, unknown>)
| undefined
readonly baseUrl?: URL | string | undefined
}
): Effect.Effect<
Client.Group<Groups, GroupName, E, R>,
never,
HttpApiGroup.MiddlewareClient<HttpApiGroup.WithName<Groups, GroupName>>
> => {
const client: Record<string, any> = {}
return makeClient(api, {
...options,
predicate: ({ group }) => group.identifier === options.group,
onEndpoint({ endpoint, endpointFn }) {
client[endpoint.name] = endpointFn
}
}).pipe(Effect.map(() => client)) as any
}
/**
* @since 4.0.0
* @category constructors
*/
export const endpoint = <
ApiId extends string,
Groups extends HttpApiGroup.Any,
const GroupName extends HttpApiGroup.Name<Groups>,
const EndpointName extends HttpApiEndpoint.Name<HttpApiGroup.EndpointsWithName<Groups, GroupName>>,
E,
R
>(
api: HttpApi.HttpApi<ApiId, Groups>,
options: {
readonly group: GroupName
readonly endpoint: EndpointName
readonly httpClient: HttpClient.HttpClient.With<E, R>
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, unknown>) => Effect.Effect<unknown, unknown, unknown>)
| undefined
readonly baseUrl?: URL | string | undefined
}
): Effect.Effect<
Client.Method<
HttpApiEndpoint.WithName<HttpApiGroup.Endpoints<HttpApiGroup.WithName<Groups, GroupName>>, EndpointName>,
E,
R
>,
never,
HttpApiEndpoint.MiddlewareClient<
HttpApiEndpoint.WithName<HttpApiGroup.Endpoints<HttpApiGroup.WithName<Groups, GroupName>>, EndpointName>
>
> => {
let client: any = undefined
return makeClient(api, {
...options,
predicate: ({ endpoint, group }) => group.identifier === options.group && endpoint.name === options.endpoint,
onEndpoint({ endpointFn }) {
client = endpointFn
}
}).pipe(Effect.map(() => client)) as any
}
/**
* Creates a type-safe URL builder that mirrors `HttpApiClient.make`.
*
* @example
* ```ts
* import { Schema } from "effect"
* import { HttpApi, HttpApiClient, HttpApiEndpoint, HttpApiGroup } from "effect/unstable/httpapi"
*
* const Api = HttpApi.make("Api").add(
* HttpApiGroup.make("users").add(
* HttpApiEndpoint.get("getUser", "/users/:id", {
* params: { id: Schema.String }
* })
* )
* )
*
* const buildUrl = HttpApiClient.urlBuilder(Api, {
* baseUrl: "https://api.example.com"
* })
*
* buildUrl.users.getUser({
* params: { id: "123" }
* })
* //=> "https://api.example.com/users/123"
* ```
* @since 4.0.0
* @category constructors
*/
export const urlBuilder = <Api extends HttpApi.Any>(api: Api, options?: {
readonly baseUrl?: URL | string | undefined
}): UrlBuilder<Api> => {
const builder: Record<string, any> = {}
HttpApi.reflect(api as unknown as HttpApi.AnyWithProps, {
onGroup({ group }) {
if (group.topLevel) return
builder[group.identifier] = {}
},
onEndpoint({ group, endpoint }) {
const makeUrl = compilePath(endpoint.path)
const encodeParams = endpoint.params === undefined
? undefined
: Schema.encodeSync(endpoint.params as Schema.Encoder<unknown>)
const encodeQuery = endpoint.query === undefined
? undefined
: Schema.encodeSync(endpoint.query as Schema.Encoder<unknown>)
const endpointBuilder = (request?: {
readonly params?: unknown
readonly query?: unknown
}) => {
const params = request?.params
const path = params === undefined
? endpoint.path
: makeUrl((encodeParams === undefined ? params : encodeParams(params)) as Record<string, string | undefined>)
const queryInput = request?.query === undefined
? undefined
: (encodeQuery === undefined ? request.query : encodeQuery(request.query)) as UrlParams.Input
const query = queryInput === undefined ? "" : UrlParams.toString(UrlParams.fromInput(queryInput))
const url = query === "" ? path : `${path}?${query}`
return options?.baseUrl === undefined ? url : new URL(url, options.baseUrl.toString()).toString()
}
;(group.topLevel ? builder : builder[group.identifier])[endpoint.name] = endpointBuilder
}
})
return builder as UrlBuilder<Api>
}
// ----------------------------------------------------------------------------
const paramsRegExp = /:(\w+)\??/g
const compilePath = (path: string) => {
const segments = path.split(paramsRegExp)
const len = segments.length
if (len === 1) {
return (_: any) => path
}
return (params: Record<string, string | undefined>) => {
let url = segments[0]
for (let i = 1; i < len; i++) {
if (i % 2 === 0) {
url += segments[i]
} else {
url += params[segments[i]]
}
}
return url
}
}
function schemasToResponse(schemas: readonly [Schema.Top, ...Array<Schema.Top>]) {
const codec = toCodecArrayBuffer(schemas)
const decode = Schema.decodeEffect(codec)
return (response: HttpClientResponse.HttpClientResponse) => Effect.flatMap(response.arrayBuffer, decode)
}
const ArrayBuffer = Schema.instanceOf(globalThis.ArrayBuffer, {
expected: "ArrayBuffer"
})
// _tag: Uint8Array
const Uint8ArrayFromArrayBuffer = ArrayBuffer.pipe(
Schema.decodeTo(
Schema.Uint8Array as Schema.instanceOf<Uint8Array<ArrayBuffer>>,
Transformation.transform({
decode(fromA) {
return new Uint8Array(fromA)
},
encode(arr) {
return arr.byteLength === arr.buffer.byteLength ?
arr.buffer :
arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength)
}
})
)
)
// _tag: Text
const StringFromArrayBuffer = ArrayBuffer.pipe(
Schema.decodeTo(
Schema.String,
Transformation.transform({
decode(fromA) {
return new TextDecoder().decode(fromA)
},
encode(toI) {
const arr = new TextEncoder().encode(toI) as Uint8Array<ArrayBuffer>
return arr.byteLength === arr.buffer.byteLength ?
arr.buffer :
arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength)
}
})
)
)
// _tag: Json
const UnknownFromArrayBuffer = StringFromArrayBuffer.pipe(Schema.decodeTo(
Schema.Union([
// Handle No Content
Schema.Literal("").pipe(Schema.decodeTo(
Schema.Undefined,
Transformation.transform({
decode: () => undefined,
encode: () => ""
})
)),
Schema.UnknownFromJsonString
])
))
function toCodecArrayBuffer(schemas: readonly [Schema.Top, ...Array<Schema.Top>]): Schema.Top {
return Schema.Union(schemas.map(onSchema))
function onSchema(schema: Schema.Top) {
const encoding = HttpApiSchema.getResponseEncoding(schema.ast)
switch (encoding._tag) {
case "Json": {
// handle json codecs that transform void schemas to null
const encodedIsNull = AST.isNull(AST.toEncoded(schema.ast))
return UnknownFromArrayBuffer.pipe(Schema.decodeTo(
schema,
encodedIsNull ?
Transformation.transform({
decode: (a) => a === undefined ? null : a,
encode: (a) => a === null ? undefined : a
}) as any :
undefined
))
}
case "FormUrlEncoded":
return StringFromArrayBuffer.pipe(
Schema.decodeTo(UrlParams.schemaRecord),
Schema.decodeTo(schema)
)
case "Uint8Array":
return Uint8ArrayFromArrayBuffer.pipe(Schema.decodeTo(schema))
case "Text":
return StringFromArrayBuffer.pipe(Schema.decodeTo(schema))
}
}
}
const statusOrElse = (response: HttpClientResponse.HttpClientResponse) =>
Effect.fail(
new HttpClientError.HttpClientError({
reason: new HttpClientError.DecodeError({
request: response.request,
response
})
})
)
const $HttpBody = Schema.declare(HttpBody.isHttpBody)
function getEncodePayloadSchema(
schemas: readonly [Schema.Top, ...Array<Schema.Top>],
method: HttpMethod.HttpMethod
): Schema.Top {
return Schema.Union(schemas.map((s) => getEncodePayloadSchemaFromBody(s, method)))
}
const bodyFromPayloadCache = new WeakMap<AST.AST, Schema.Top>()
function getEncodePayloadSchemaFromBody(
schema: Schema.Top,
method: HttpMethod.HttpMethod
): Schema.Top {
const ast = schema.ast
const cached = bodyFromPayloadCache.get(ast)
if (cached !== undefined) {
return cached
}
const encoding = HttpApiSchema.getPayloadEncoding(ast, method)
const out = $HttpBody.pipe(Schema.decodeTo(
schema,
Transformation.transformOrFail<unknown, HttpBody.HttpBody>({
decode(httpBody) {
return Effect.fail(new Issue.Forbidden(Option.some(httpBody), { message: "Encode only schema" }))
},
encode(t) {
switch (encoding._tag) {
case "Multipart":
return Effect.fail(new Issue.Forbidden(Option.some(t), { message: "Payload must be a FormData" }))
case "Json": {
try {
const body = JSON.stringify(t)
return Effect.succeed(HttpBody.text(body, encoding.contentType))
} catch (error) {
return Effect.fail(new Issue.InvalidValue(Option.some(t), { message: globalThis.String(error) }))
}
}
case "Text": {
if (typeof t !== "string") {
return Effect.fail(
new Issue.InvalidValue(Option.some(t), { message: "Expected a string" })
)
}
return Effect.succeed(HttpBody.text(t, encoding.contentType))
}
case "FormUrlEncoded": {
if (!Predicate.isObject(t)) {
return Effect.fail(new Issue.InvalidValue(Option.some(t), { message: "Expected a record" }))
}
return Effect.succeed(HttpBody.urlParams(UrlParams.fromInput(t as any)))
}
case "Uint8Array": {
if (!(t instanceof Uint8Array)) {
return Effect.fail(
new Issue.InvalidValue(Option.some(t), { message: "Expected a Uint8Array" })
)
}
return Effect.succeed(HttpBody.uint8Array(t, encoding.contentType))
}
}
}
})
))
bodyFromPayloadCache.set(ast, out)
return out
}
|