This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_custom.ts
140 lines (130 loc) · 3.94 KB
/
_custom.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
/* eslint-disable @typescript-eslint/no-explicit-any */
import * as T from "@effect-ts/core/Effect"
import { flow, pipe } from "@effect-ts/core/Function"
import { Has } from "@effect-ts/core/Has"
import * as H from "@effect-ts-app/core/http/http-client"
import * as MO from "@effect-ts-app/core/Schema"
import {
Parser,
ReqRes,
ReqResSchemed,
RequestSchemed,
} from "@effect-ts-app/core/Schema"
import { Path } from "path-parser"
import { ApiConfig, ExtractResponse } from "../clientFor"
import { ComputeUnlessClass, fetchApi, ResponseError } from "../fetch"
import * as Ts from "./_index"
import { TaskView } from "./views"
/*
TODOS:
- express this somehow to openapi...
- make portable
FUTURE:
- select child props
- translate selections to data source?
- select from single item query, instead of just multi items.
*/
const mksearchWithFields = () => {
const h = Ts.Search
const res = h.Response ?? MO.Void
const Request = MO.extractRequest(h)
const b = Object.assign({}, h, { Request })
//export function makeAdapter<Props extends MO.PropertyRecord>(props: Props) {
type Props = typeof TaskView.Model.Api.props
function a<Key extends keyof Props>(
req: Ts.Search.SearchTasksRequest & {
$select: readonly Key[] // TODO:
}
): T.Effect<
H.RequestEnv & Has<ApiConfig>,
ResponseError | H.HttpError<string>,
MO.ParsedShapeOf<MO.Adapted<Props, Key>>
>
function a(
req: Ts.Search.SearchTasksRequest
): T.Effect<
H.RequestEnv & Has<ApiConfig>,
ResponseError | H.HttpError<string>,
ExtractResponse<typeof res>
> // todo
function a(req: any): any {
return pipe(
fetchApi3S(b, Ts.Search.adapt)(req)
// GET
// fetchApi(Request.method, new Path(Request.path).build(req)),
// T.chain(
// // @ts-expect-error doc
// flow(
// (res.Parser ?? MO.Parser.for(res))["|>"](MO.condemnFail),
// // @ts-expect-error doc
// mapResponseErrorS
// )
// )
)
}
return a
}
export const searchWithFields = mksearchWithFields()
function fetchApi2S<RequestA, RequestE, ResponseA>(
encodeRequest: (a: RequestA) => RequestE,
decodeResponse: (u: unknown, req: RequestA) => T.IO<unknown, ResponseA>
) {
const decodeRes = flow(
decodeResponse,
T.mapError((err) => new ResponseError(err))
)
return (method: H.Method, path: string) => (req: RequestA) =>
pipe(
encodeRequest(req),
(r) => fetchApi(method, new Path(path).build(req), r),
T.chain((x) => decodeRes(x, req)),
// TODO: as long as we don't use classes for Responses..
T.map((i) => i as ComputeUnlessClass<ResponseA>)
)
}
// TODO: validate headers vs path vs body vs query?
// export function fetchApi3S<
// RequestA,
// RequestE,
// ResponseE = unknown,
// ResponseA = void
// >(): (
// req: RequestA & { fields: readonly (keyof Response["items"][number])[] }
// ) => T.Effect<
// H.RequestEnv & Has<ApiConfig>,
// ResponseError | H.HttpError<string>,
// ResponseA
// >
// export function fetchApi3S<
// RequestA,
// RequestE,
// ResponseE = unknown,
// ResponseA = void
// >(): (
// req: RequestA
// ) => T.Effect<
// H.RequestEnv & Has<ApiConfig>,
// ResponseError | H.HttpError<string>,
// ResponseA
// >
function fetchApi3S<RequestA, RequestE, ResponseE = unknown, ResponseA = void>(
{
Request,
Response,
}: {
// eslint-disable-next-line @typescript-eslint/ban-types
Request: RequestSchemed<RequestE, RequestA>
// eslint-disable-next-line @typescript-eslint/ban-types
Response?: ReqRes<ResponseE, ResponseA> | ReqResSchemed<ResponseE, ResponseA>
},
adapt?: any
) {
const Res = (Response ? MO.extractSchema(Response) : MO.Void) as ReqRes<
ResponseE,
ResponseA
>
const encodeRequest = Request.Encoder
const decodeResponse = (u: any, res: any) =>
Parser.for(adapt ? adapt(res) : Res)["|>"](MO.condemn)(u)
return fetchApi2S(encodeRequest, decodeResponse)(Request.method, Request.path)
}