UNPKG

@httpc/client

Version:

httpc client for building function-based API with minimal code and end-to-end type safety

38 lines 1.25 kB
import { HttpCClient } from "./client"; import { isCall } from "./utils"; export class HttpCTypedClient { constructor(options) { this.$client = new HttpCClient(options); this.$metadata = options?.metadata; if (this.$metadata) { this.$client.use(CallMetadataMiddleware(this.$metadata)); } } } function CallMetadataMiddleware(metadata) { return (request, next) => { if (request.path && metadata) { const parts = request.path.split("/"); if (!parts[0]) parts.shift(); // if empty entry, means path starts with /, remove it let call; do { if (parts.length === 1 && isCall(metadata?.[parts[0]])) { call = metadata[parts[0]]; break; } else if (parts.length > 1) { metadata = metadata[parts.shift()]; } else { break; } } while (metadata); if (call) { request.metadata = call.metadata; } } return next(request); }; } //# sourceMappingURL=typed.js.map