UNPKG

@nekofar/warpcast

Version:

TypeScript client for interacting with Warpcast APIs

116 lines (114 loc) 3.19 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/client/core/params.ts var params_exports = {}; __export(params_exports, { buildClientParams: () => buildClientParams }); module.exports = __toCommonJS(params_exports); var extraPrefixesMap = { $body_: "body", $headers_: "headers", $path_: "path", $query_: "query" }; var extraPrefixes = Object.entries(extraPrefixesMap); var buildKeyMap = (fields, map) => { if (!map) { map = /* @__PURE__ */ new Map(); } for (const config of fields) { if ("in" in config) { if (config.key) { map.set(config.key, { in: config.in, map: config.map }); } } else if (config.args) { buildKeyMap(config.args, map); } } return map; }; var stripEmptySlots = (params) => { for (const [slot, value] of Object.entries(params)) { if (value && typeof value === "object" && !Object.keys(value).length) { delete params[slot]; } } }; var buildClientParams = (args, fields) => { const params = { body: {}, headers: {}, path: {}, query: {} }; const map = buildKeyMap(fields); let config; for (const [index, arg] of args.entries()) { if (fields[index]) { config = fields[index]; } if (!config) { continue; } if ("in" in config) { if (config.key) { const field = map.get(config.key); const name = field.map || config.key; params[field.in][name] = arg; } else { params.body = arg; } } else { for (const [key, value] of Object.entries(arg ?? {})) { const field = map.get(key); if (field) { const name = field.map || key; params[field.in][name] = value; } else { const extra = extraPrefixes.find( ([prefix]) => key.startsWith(prefix) ); if (extra) { const [prefix, slot] = extra; params[slot][key.slice(prefix.length)] = value; } else { for (const [slot, allowed] of Object.entries( config.allowExtra ?? {} )) { if (allowed) { params[slot][key] = value; break; } } } } } } } stripEmptySlots(params); return params; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { buildClientParams });