UNPKG

@httpc/client

Version:

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

42 lines 1.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpCTypedClient = void 0; const client_1 = require("./client"); const utils_1 = require("./utils"); class HttpCTypedClient { constructor(options) { this.$client = new client_1.HttpCClient(options); this.$metadata = options?.metadata; if (this.$metadata) { this.$client.use(CallMetadataMiddleware(this.$metadata)); } } } exports.HttpCTypedClient = HttpCTypedClient; 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 && (0, utils_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