-
-
Notifications
You must be signed in to change notification settings - Fork 258
/
HttpApiClient.ts
492 lines (471 loc) · 16.6 KB
/
HttpApiClient.ts
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
/**
* @since 1.0.0
*/
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import { identity } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
import * as Option from "effect/Option"
import * as ParseResult from "effect/ParseResult"
import type * as Predicate from "effect/Predicate"
import * as Schema from "effect/Schema"
import type * as AST from "effect/SchemaAST"
import type { Scope } from "effect/Scope"
import type { Simplify } from "effect/Types"
import * as HttpApi from "./HttpApi.js"
import type { HttpApiEndpoint } from "./HttpApiEndpoint.js"
import type { HttpApiGroup } from "./HttpApiGroup.js"
import * as HttpApiSchema from "./HttpApiSchema.js"
import * as HttpBody from "./HttpBody.js"
import * as HttpClient from "./HttpClient.js"
import * as HttpClientError from "./HttpClientError.js"
import * as HttpClientRequest from "./HttpClientRequest.js"
import * as HttpClientResponse from "./HttpClientResponse.js"
import * as HttpMethod from "./HttpMethod.js"
import type { HttpApiMiddleware } from "./index.js"
import * as UrlParams from "./UrlParams.js"
/**
* @since 1.0.0
* @category models
*/
export type Client<Groups extends HttpApiGroup.Any, ApiError> = Simplify<
& {
readonly [Group in Extract<Groups, { readonly topLevel: false }> as HttpApiGroup.Name<Group>]: Client.Group<
Group,
Group["identifier"],
ApiError
>
}
& {
readonly [Method in Client.TopLevelMethods<Groups, ApiError> as Method[0]]: Method[1]
}
>
/**
* @since 1.0.0
* @category models
*/
export declare namespace Client {
/**
* @since 1.0.0
* @category models
*/
export type Group<Groups extends HttpApiGroup.Any, GroupName extends Groups["identifier"], ApiError> =
[HttpApiGroup.WithName<Groups, GroupName>] extends
[HttpApiGroup<infer _GroupName, infer _Endpoints, infer _GroupError, infer _GroupErrorR>] ? {
readonly [Endpoint in _Endpoints as HttpApiEndpoint.Name<Endpoint>]: Method<
Endpoint,
ApiError,
_GroupError
>
} :
never
/**
* @since 1.0.0
* @category models
*/
export type Method<Endpoint, ApiError, GroupError> = [Endpoint] extends [
HttpApiEndpoint<
infer _Name,
infer _Method,
infer _Path,
infer _UrlParams,
infer _Payload,
infer _Headers,
infer _Success,
infer _Error,
infer _R,
infer _RE
>
] ? <WithResponse extends boolean = false>(
request: Simplify<HttpApiEndpoint.ClientRequest<_Path, _UrlParams, _Payload, _Headers, WithResponse>>
) => Effect.Effect<
WithResponse extends true ? [_Success, HttpClientResponse.HttpClientResponse] : _Success,
_Error | GroupError | ApiError | HttpClientError.HttpClientError
> :
never
/**
* @since 1.0.0
* @category models
*/
export type TopLevelMethods<Groups extends HttpApiGroup.Any, ApiError> =
Extract<Groups, { readonly topLevel: true }> extends
HttpApiGroup<infer _Id, infer _Endpoints, infer _Error, infer _ErrorR, infer _TopLevel> ?
_Endpoints extends infer Endpoint ? [HttpApiEndpoint.Name<Endpoint>, Method<Endpoint, ApiError, _Error>]
: never :
never
}
/**
* @internal
*/
const makeClient = <ApiId extends string, Groups extends HttpApiGroup.Any, ApiError, ApiR>(
api: HttpApi.HttpApi<ApiId, Groups, ApiError, ApiR>,
options: {
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<string, HttpMethod.HttpMethod>
readonly mergedAnnotations: Context.Context<never>
readonly middleware: ReadonlySet<HttpApiMiddleware.TagClassAny>
readonly successes: ReadonlyMap<number, {
readonly ast: Option.Option<AST.AST>
readonly description: Option.Option<string>
}>
readonly errors: ReadonlyMap<number, {
readonly ast: Option.Option<AST.AST>
readonly description: Option.Option<string>
}>
readonly endpointFn: Function
}) => void
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, Scope>) => Effect.Effect<unknown, unknown, Scope>)
| undefined
readonly baseUrl?: string | undefined
}
): Effect.Effect<
void,
never,
HttpApiMiddleware.HttpApiMiddleware.Without<ApiR | HttpApiGroup.ClientContext<Groups>> | HttpClient.HttpClient
> =>
Effect.gen(function*() {
const context = yield* Effect.context<any>()
const httpClient = (yield* HttpClient.HttpClient).pipe(
options?.baseUrl === undefined ? identity : HttpClient.mapRequest(HttpClientRequest.prependUrl(options.baseUrl)),
options?.transformClient === undefined ? identity : options.transformClient
)
HttpApi.reflect(api as any, {
predicate: options?.predicate,
onGroup(onGroupOptions) {
options.onGroup?.(onGroupOptions)
},
onEndpoint(onEndpointOptions) {
const { endpoint, errors, successes } = onEndpointOptions
const makeUrl = compilePath(endpoint.path)
const decodeMap: Record<
number | "orElse",
(response: HttpClientResponse.HttpClientResponse) => Effect.Effect<any, any>
> = { orElse: statusOrElse }
const decodeResponse = HttpClientResponse.matchStatus(decodeMap)
errors.forEach(({ ast }, status) => {
if (ast._tag === "None") {
decodeMap[status] = statusCodeError
return
}
const decode = schemaToResponse(ast.value)
decodeMap[status] = (response) => Effect.flatMap(decode(response), Effect.fail)
})
successes.forEach(({ ast }, status) => {
decodeMap[status] = ast._tag === "None" ? responseAsVoid : schemaToResponse(ast.value)
})
const encodePayloadBody = endpoint.payloadSchema.pipe(
Option.map((schema) => {
if (HttpMethod.hasBody(endpoint.method)) {
return Schema.encodeUnknown(payloadSchemaBody(schema as any))
}
return Schema.encodeUnknown(schema)
})
)
const encodeHeaders = endpoint.headersSchema.pipe(
Option.map(Schema.encodeUnknown)
)
const encodeUrlParams = endpoint.urlParamsSchema.pipe(
Option.map(Schema.encodeUnknown)
)
const endpointFn = (request: {
readonly path: any
readonly urlParams: any
readonly payload: any
readonly headers: any
readonly withResponse?: boolean
}) =>
Effect.gen(function*() {
let httpRequest = HttpClientRequest.make(endpoint.method)(
request && request.path ? makeUrl(request.path) : endpoint.path
)
if (request && request.payload instanceof FormData) {
httpRequest = HttpClientRequest.bodyFormData(httpRequest, request.payload)
} else if (encodePayloadBody._tag === "Some") {
if (HttpMethod.hasBody(endpoint.method)) {
const body = (yield* encodePayloadBody.value(request.payload)) as HttpBody.HttpBody
httpRequest = HttpClientRequest.setBody(httpRequest, body)
} else {
const urlParams = (yield* encodePayloadBody.value(request.payload)) as Record<string, string>
httpRequest = HttpClientRequest.setUrlParams(httpRequest, urlParams)
}
}
if (encodeHeaders._tag === "Some") {
httpRequest = HttpClientRequest.setHeaders(
httpRequest,
(yield* encodeHeaders.value(request.headers)) as any
)
}
if (encodeUrlParams._tag === "Some") {
httpRequest = HttpClientRequest.appendUrlParams(
httpRequest,
(yield* encodeUrlParams.value(request.urlParams)) as any
)
}
const response = yield* httpClient.execute(httpRequest)
const value = yield* (options.transformResponse === undefined
? decodeResponse(response)
: options.transformResponse(decodeResponse(response)))
return request?.withResponse === true ? [value, response] : value
}).pipe(
Effect.scoped,
Effect.catchIf(ParseResult.isParseError, Effect.die),
Effect.mapInputContext((input) => Context.merge(context, input))
)
options.onEndpoint({
...onEndpointOptions,
endpointFn
})
}
})
})
/**
* @since 1.0.0
* @category constructors
*/
export const make = <ApiId extends string, Groups extends HttpApiGroup.Any, ApiError, ApiR>(
api: HttpApi.HttpApi<ApiId, Groups, ApiError, ApiR>,
options?: {
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, Scope>) => Effect.Effect<unknown, unknown, Scope>)
| undefined
readonly baseUrl?: string | undefined
}
): Effect.Effect<
Simplify<Client<Groups, ApiError>>,
never,
HttpApiMiddleware.HttpApiMiddleware.Without<ApiR | HttpApiGroup.ClientContext<Groups>> | HttpClient.HttpClient
> => {
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.map(() => client)) as any
}
/**
* @since 1.0.0
* @category constructors
*/
export const group = <
ApiId extends string,
Groups extends HttpApiGroup.Any,
ApiError,
ApiR,
const GroupName extends Groups["identifier"]
>(
api: HttpApi.HttpApi<ApiId, Groups, ApiError, ApiR>,
groupId: GroupName,
options?: {
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, Scope>) => Effect.Effect<unknown, unknown, Scope>)
| undefined
readonly baseUrl?: string | undefined
}
): Effect.Effect<
Client.Group<Groups, GroupName, ApiError>,
never,
HttpApiMiddleware.HttpApiMiddleware.Without<
| ApiR
| HttpApiGroup.ClientContext<
HttpApiGroup.WithName<Groups, GroupName>
>
> | HttpClient.HttpClient
> => {
const client: Record<string, any> = {}
return makeClient(api, {
...options,
predicate: ({ group }) => group.identifier === groupId,
onEndpoint({ endpoint, endpointFn }) {
client[endpoint.name] = endpointFn
}
}).pipe(Effect.map(() => client)) as any
}
/**
* @since 1.0.0
* @category constructors
*/
export const endpoint = <
ApiId extends string,
Groups extends HttpApiGroup.Any,
ApiError,
ApiR,
const GroupName extends HttpApiGroup.Name<Groups>,
const EndpointName extends HttpApiEndpoint.Name<HttpApiGroup.EndpointsWithName<Groups, GroupName>>
>(
api: HttpApi.HttpApi<ApiId, Groups, ApiError, ApiR>,
groupName: GroupName,
endpointName: EndpointName,
options?: {
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
readonly transformResponse?:
| ((effect: Effect.Effect<unknown, unknown, Scope>) => Effect.Effect<unknown, unknown, Scope>)
| undefined
readonly baseUrl?: string | undefined
}
): Effect.Effect<
Client.Method<
HttpApiEndpoint.WithName<HttpApiGroup.Endpoints<HttpApiGroup.WithName<Groups, GroupName>>, EndpointName>,
HttpApiGroup.Error<HttpApiGroup.WithName<Groups, GroupName>>,
ApiError
>,
never,
| HttpApiMiddleware.HttpApiMiddleware.Without<
| ApiR
| HttpApiGroup.Context<HttpApiGroup.WithName<Groups, GroupName>>
| HttpApiEndpoint.ContextWithName<HttpApiGroup.EndpointsWithName<Groups, GroupName>, EndpointName>
| HttpApiEndpoint.ErrorContextWithName<HttpApiGroup.EndpointsWithName<Groups, GroupName>, EndpointName>
>
| HttpClient.HttpClient
> => {
let client: any = undefined
return makeClient(api, {
...options,
predicate: ({ endpoint, group }) => group.identifier === groupName && endpoint.name === endpointName,
onEndpoint({ endpointFn }) {
client = endpointFn
}
}).pipe(Effect.map(() => client)) as any
}
// ----------------------------------------------------------------------------
const paramsRegex = /:([^/:.]+)/g
const compilePath = (path: string) => {
const segments = path.split(paramsRegex)
const len = segments.length
if (len === 1) {
return (_: any) => path
}
return (params: Record<string, string>) => {
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
}
}
const schemaToResponse = (
ast: AST.AST
): (response: HttpClientResponse.HttpClientResponse) => Effect.Effect<any, any> => {
const schema = Schema.make(ast)
const encoding = HttpApiSchema.getEncoding(ast)
const decode = Schema.decodeUnknown(schema)
switch (encoding.kind) {
case "Json": {
return (response) => Effect.flatMap(responseJson(response), decode)
}
case "UrlParams": {
return HttpClientResponse.schemaBodyUrlParams(schema as any)
}
case "Uint8Array": {
return (response: HttpClientResponse.HttpClientResponse) =>
response.arrayBuffer.pipe(
Effect.map((buffer) => new Uint8Array(buffer)),
Effect.flatMap(decode)
)
}
case "Text": {
return (response) => Effect.flatMap(response.text, decode)
}
}
}
const responseJson = (response: HttpClientResponse.HttpClientResponse) =>
Effect.flatMap(response.text, (text) =>
text === "" ? Effect.void : Effect.try({
try: () => JSON.parse(text),
catch: (cause) =>
new HttpClientError.ResponseError({
reason: "Decode",
request: response.request,
response,
cause
})
}))
const statusOrElse = (response: HttpClientResponse.HttpClientResponse) =>
Effect.fail(
new HttpClientError.ResponseError({
reason: "Decode",
request: response.request,
response
})
)
const statusCodeError = (response: HttpClientResponse.HttpClientResponse) =>
Effect.fail(
new HttpClientError.ResponseError({
reason: "StatusCode",
request: response.request,
response
})
)
const responseAsVoid = (_response: HttpClientResponse.HttpClientResponse) => Effect.void
const HttpBodyFromSelf = Schema.declare(HttpBody.isHttpBody)
const payloadSchemaBody = (schema: Schema.Schema.All): Schema.Schema<any, HttpBody.HttpBody> => {
const members = schema.ast._tag === "Union" ? schema.ast.types : [schema.ast]
return Schema.Union(...members.map(bodyFromPayload)) as any
}
const bodyFromPayloadCache = globalValue(
"@effect/platform/HttpApiClient/bodyFromPayloadCache",
() => new WeakMap<AST.AST, Schema.Schema.Any>()
)
const bodyFromPayload = (ast: AST.AST) => {
if (bodyFromPayloadCache.has(ast)) {
return bodyFromPayloadCache.get(ast)!
}
const schema = Schema.make(ast)
const encoding = HttpApiSchema.getEncoding(ast)
const transform = Schema.transformOrFail(
HttpBodyFromSelf,
schema,
{
decode(fromA, _, ast) {
return ParseResult.fail(new ParseResult.Forbidden(ast, fromA, "encode only schema"))
},
encode(toI, _, ast) {
switch (encoding.kind) {
case "Json": {
return HttpBody.json(toI).pipe(
ParseResult.mapError((error) => new ParseResult.Type(ast, toI, `Could not encode as JSON: ${error}`))
)
}
case "Text": {
if (typeof toI !== "string") {
return ParseResult.fail(new ParseResult.Type(ast, toI, "Expected a string"))
}
return ParseResult.succeed(HttpBody.text(toI))
}
case "UrlParams": {
return ParseResult.succeed(HttpBody.urlParams(UrlParams.fromInput(toI as any)))
}
case "Uint8Array": {
if (!(toI instanceof Uint8Array)) {
return ParseResult.fail(new ParseResult.Type(ast, toI, "Expected a Uint8Array"))
}
return ParseResult.succeed(HttpBody.uint8Array(toI))
}
}
}
}
)
bodyFromPayloadCache.set(ast, transform)
return transform
}