/* eslint-disable @typescript-eslint/no-unsafe-argument */ /* eslint-disable @typescript-eslint/no-explicit-any */ import { REST } from "@effect-app/prelude/schema" import * as utils from "@effect-app/prelude/utils" import { Path } from "path-parser" import type { ApiConfig } from "./config.js" import type { SupportedErrors } from "./errors.js" import type { FetchError, FetchResponse } from "./fetch.js" import { fetchApi, fetchApi3S, fetchApi3SE, makePathWithBody, makePathWithQuery, mapResponseM, ResponseError } from "./fetch.js" export * from "./config.js" type Requests = Record type AnyRequest = & Omit< REST.QueryRequest, "method" > & REST.RequestSchemed const cache = new Map>() export type Client = & RequestHandlers< ApiConfig | HttpClient.Default, SupportedErrors | FetchError | ResponseError, M > & RequestHandlersE< ApiConfig | HttpClient.Default, SupportedErrors | FetchError | ResponseError, M > export function clientFor( models: M ): Client> { const found = cache.get(models) if (found) { return found } const m = clientFor_(models) cache.set(models, m) return m } function clientFor_(models: M) { return (models .$$ .keys // ignore module interop with automatic default exports.. .filter((x) => x !== "default" && x !== "meta") .reduce((prev, cur) => { const h = models[cur] const Request_ = REST.extractRequest(h) as AnyRequest const Response = REST.extractResponse(h) // @ts-expect-error doc const actionName = utils.uncapitalize(cur) const m = (models as any).meta as { moduleName: string } if (!m) throw new Error("No meta defined in Resource!") const requestName = `${m.moduleName}.${cur as string}` .replaceAll(".js", "") const Request = class extends (Request_ as any) { static path = "/" + requestName + (Request_.path === "/" ? "" : Request_.path) static method = Request_.method as REST.SupportedMethods === "AUTO" ? REST.determineMethod(cur as string, Request_) : Request_.method } as unknown as AnyRequest if ((Request_ as any).method === "AUTO") { Object.assign(Request, { [Request.method === "GET" || Request.method === "DELETE" ? "Query" : "Body"]: (Request_ as any).Auto }) } const b = Object.assign({}, h, { Request, Response }) const meta = { Request, Response, mapPath: Request.path } const res = Response as Schema const parseResponse = flow(res.decodeUnknown, (_) => _.mapError((err) => new ResponseError(err))) const parseResponseE = flow(parseResponse, (x) => x.andThen(res.encode)) const path = new Path(Request.path) // TODO: look into ast, look for propertySignatures, etc. // TODO: and fix type wise // if we don't need fields, then also dont require an argument. const fields = [Request.Body, Request.Query, Request.Path] .filter((x) => x) // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access .flatMap((x) => x.ast.propertySignatures) // @ts-expect-error doc prev[actionName] = Request.method === "GET" ? fields.length === 0 ? { handler: fetchApi(Request.method, Request.path) .flatMap(mapResponseM(parseResponse)) .withSpan("client.request", { attributes: { "request.name": requestName } }), ...meta } : { handler: (req: any) => fetchApi(Request.method, makePathWithQuery(path, Request.encodeSync(req))) .flatMap(mapResponseM(parseResponse)) .withSpan("client.request", { attributes: { "request.name": requestName } }), ...meta, mapPath: (req: any) => req ? makePathWithQuery(path, Request.encodeSync(req)) : Request.path } : fields.length === 0 ? { handler: fetchApi3S(b)({}).withSpan("client.request", { attributes: { "request.name": requestName } }), ...meta } : { handler: (req: any) => fetchApi3S(b)(req).withSpan("client.request", { attributes: { "request.name": requestName } }), ...meta, mapPath: (req: any) => req ? Request.method === "DELETE" ? makePathWithQuery(path, Request.encodeSync(req)) : makePathWithBody(path, Request.encodeSync(req)) : Request.path } // generate handler // @ts-expect-error doc prev[`${actionName}E`] = Request.method === "GET" ? fields.length === 0 ? { handler: fetchApi(Request.method, Request.path) .flatMap(mapResponseM(parseResponseE)) .withSpan("client.request", { attributes: { "request.name": requestName } }), ...meta } : { handler: (req: any) => fetchApi(Request.method, makePathWithQuery(path, Request.encodeSync(req))) .flatMap(mapResponseM(parseResponseE)) .withSpan("client.request", { attributes: { "request.name": requestName } }), ...meta, mapPath: (req: any) => req ? makePathWithQuery(path, Request.encodeSync(req)) : Request.path } : fields.length === 0 ? { handler: fetchApi3SE(b)({}).withSpan("client.request", { attributes: { "request.name": requestName } }), ...meta } : { handler: (req: any) => fetchApi3SE(b)(req).withSpan("client.request", { attributes: { "request.name": requestName } }), ...meta, mapPath: (req: any) => req ? Request.method === "DELETE" ? makePathWithQuery(path, Request.encodeSync(req)) : makePathWithBody(path, Request.encodeSync(req)) : Request.path } // generate handler return prev }, {} as Client)) } export type ExtractResponse = T extends Schema ? Schema.To : T extends unknown ? void : never export type ExtractEResponse = T extends Schema ? Schema.From : T extends unknown ? void : never type HasEmptyTo> = T extends { struct: Schema } ? Schema.To extends Record ? true : Schema.To extends Record ? true : false : false type RequestHandlers = { [K in keyof M & string as Uncapitalize]: HasEmptyTo> extends true ? { handler: Effect>>, E, R> Request: REST.GetRequest Reponse: ExtractResponse> mapPath: string } : { handler: ( req: InstanceType> ) => Effect< FetchResponse>>, E, R > Request: REST.GetRequest Reponse: ExtractResponse> mapPath: (req: InstanceType>) => string } } type RequestHandlersE = { [K in keyof M & string as `${Uncapitalize}E`]: HasEmptyTo> extends true ? { handler: Effect< FetchResponse>>, E, R > Request: REST.GetRequest Reponse: ExtractResponse> mapPath: string } : { handler: ( req: InstanceType> ) => Effect< FetchResponse>>, E, R > Request: REST.GetRequest Reponse: ExtractResponse> mapPath: (req: InstanceType>) => string } }