client.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var client_exports = {};
  19. __export(client_exports, {
  20. hc: () => hc
  21. });
  22. module.exports = __toCommonJS(client_exports);
  23. var import_cookie = require("../utils/cookie");
  24. var import_utils = require("./utils");
  25. const createProxy = (callback, path) => {
  26. const proxy = new Proxy(() => {
  27. }, {
  28. get(_obj, key) {
  29. if (typeof key !== "string" || key === "then") {
  30. return void 0;
  31. }
  32. return createProxy(callback, [...path, key]);
  33. },
  34. apply(_1, _2, args) {
  35. return callback({
  36. path,
  37. args
  38. });
  39. }
  40. });
  41. return proxy;
  42. };
  43. class ClientRequestImpl {
  44. url;
  45. method;
  46. buildSearchParams;
  47. queryParams = void 0;
  48. pathParams = {};
  49. rBody;
  50. cType = void 0;
  51. constructor(url, method, options) {
  52. this.url = url;
  53. this.method = method;
  54. this.buildSearchParams = options.buildSearchParams;
  55. }
  56. fetch = async (args, opt) => {
  57. if (args) {
  58. if (args.query) {
  59. this.queryParams = this.buildSearchParams(args.query);
  60. }
  61. if (args.form) {
  62. const form = new FormData();
  63. for (const [k, v] of Object.entries(args.form)) {
  64. if (v === void 0) {
  65. continue;
  66. }
  67. if (Array.isArray(v)) {
  68. for (const v2 of v) {
  69. form.append(k, v2);
  70. }
  71. } else {
  72. form.append(k, v);
  73. }
  74. }
  75. this.rBody = form;
  76. }
  77. if (args.json) {
  78. this.rBody = JSON.stringify(args.json);
  79. this.cType = "application/json";
  80. }
  81. if (args.param) {
  82. this.pathParams = args.param;
  83. }
  84. }
  85. let methodUpperCase = this.method.toUpperCase();
  86. const headerValues = {
  87. ...args?.header,
  88. ...typeof opt?.headers === "function" ? await opt.headers() : opt?.headers
  89. };
  90. if (args?.cookie) {
  91. const cookies = [];
  92. for (const [key, value] of Object.entries(args.cookie)) {
  93. cookies.push((0, import_cookie.serialize)(key, value, { path: "/" }));
  94. }
  95. headerValues["Cookie"] = cookies.join(",");
  96. }
  97. if (this.cType) {
  98. headerValues["Content-Type"] = this.cType;
  99. }
  100. const headers = new Headers(headerValues ?? void 0);
  101. let url = this.url;
  102. url = (0, import_utils.removeIndexString)(url);
  103. url = (0, import_utils.replaceUrlParam)(url, this.pathParams);
  104. if (this.queryParams) {
  105. url = url + "?" + this.queryParams.toString();
  106. }
  107. methodUpperCase = this.method.toUpperCase();
  108. const setBody = !(methodUpperCase === "GET" || methodUpperCase === "HEAD");
  109. return (opt?.fetch || fetch)(url, {
  110. body: setBody ? this.rBody : void 0,
  111. method: methodUpperCase,
  112. headers,
  113. ...opt?.init
  114. });
  115. };
  116. }
  117. const hc = (baseUrl, options) => createProxy(function proxyCallback(opts) {
  118. const buildSearchParamsOption = options?.buildSearchParams ?? import_utils.buildSearchParams;
  119. const parts = [...opts.path];
  120. const lastParts = parts.slice(-3).reverse();
  121. if (lastParts[0] === "toString") {
  122. if (lastParts[1] === "name") {
  123. return lastParts[2] || "";
  124. }
  125. return proxyCallback.toString();
  126. }
  127. if (lastParts[0] === "valueOf") {
  128. if (lastParts[1] === "name") {
  129. return lastParts[2] || "";
  130. }
  131. return proxyCallback;
  132. }
  133. let method = "";
  134. if (/^\$/.test(lastParts[0])) {
  135. const last = parts.pop();
  136. if (last) {
  137. method = last.replace(/^\$/, "");
  138. }
  139. }
  140. const path = parts.join("/");
  141. const url = (0, import_utils.mergePath)(baseUrl, path);
  142. if (method === "url" || method === "path") {
  143. let result = url;
  144. if (opts.args[0]) {
  145. if (opts.args[0].param) {
  146. result = (0, import_utils.replaceUrlParam)(url, opts.args[0].param);
  147. }
  148. if (opts.args[0].query) {
  149. result = result + "?" + buildSearchParamsOption(opts.args[0].query).toString();
  150. }
  151. }
  152. result = (0, import_utils.removeIndexString)(result);
  153. if (method === "url") {
  154. return new URL(result);
  155. }
  156. return result.slice(baseUrl.replace(/\/+$/, "").length).replace(/^\/?/, "/");
  157. }
  158. if (method === "ws") {
  159. const webSocketUrl = (0, import_utils.replaceUrlProtocol)(
  160. opts.args[0] && opts.args[0].param ? (0, import_utils.replaceUrlParam)(url, opts.args[0].param) : url,
  161. "ws"
  162. );
  163. const targetUrl = new URL(webSocketUrl);
  164. const queryParams = opts.args[0]?.query;
  165. if (queryParams) {
  166. Object.entries(queryParams).forEach(([key, value]) => {
  167. if (Array.isArray(value)) {
  168. value.forEach((item) => targetUrl.searchParams.append(key, item));
  169. } else {
  170. targetUrl.searchParams.set(key, value);
  171. }
  172. });
  173. }
  174. const establishWebSocket = (...args) => {
  175. if (options?.webSocket !== void 0 && typeof options.webSocket === "function") {
  176. return options.webSocket(...args);
  177. }
  178. return new WebSocket(...args);
  179. };
  180. return establishWebSocket(targetUrl.toString());
  181. }
  182. const req = new ClientRequestImpl(url, method, {
  183. buildSearchParams: buildSearchParamsOption
  184. });
  185. if (method) {
  186. options ??= {};
  187. const args = (0, import_utils.deepMerge)(options, { ...opts.args[1] });
  188. return req.fetch(opts.args[0], args);
  189. }
  190. return req;
  191. }, []);
  192. // Annotate the CommonJS export names for ESM import in node:
  193. 0 && (module.exports = {
  194. hc
  195. });