urlcat-umd

A UMD version of urlcat

目前为 2024-03-15 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/489919/1343689/urlcat-umd.js

  1. (function (global, factory) {
  2. global = typeof globalThis !== 'undefined' ? globalThis : global || self;
  3. factory(global.urlcat = {}, global.Qs);
  4. })(this, (function (exports, qs) { 'use strict';
  5.  
  6. var __defProp = Object.defineProperty;
  7. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  8. var __hasOwnProp = Object.prototype.hasOwnProperty;
  9. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  10. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  11. var __spreadValues = (a, b) => {
  12. for (var prop in b || (b = {}))
  13. if (__hasOwnProp.call(b, prop))
  14. __defNormalProp(a, prop, b[prop]);
  15. if (__getOwnPropSymbols)
  16. for (var prop of __getOwnPropSymbols(b)) {
  17. if (__propIsEnum.call(b, prop))
  18. __defNormalProp(a, prop, b[prop]);
  19. }
  20. return a;
  21. };
  22. function urlcat(baseUrlOrTemplate, pathTemplateOrParams, maybeParams = {}, config = {}) {
  23. if (typeof pathTemplateOrParams === "string") {
  24. const baseUrl = baseUrlOrTemplate;
  25. const pathTemplate = pathTemplateOrParams;
  26. const params = maybeParams;
  27. return urlcatImpl(pathTemplate, params, baseUrl, config);
  28. } else {
  29. const baseTemplate = baseUrlOrTemplate;
  30. const params = pathTemplateOrParams;
  31. return urlcatImpl(baseTemplate, params, void 0, config);
  32. }
  33. }
  34. function configure(rootConfig) {
  35. return (baseUrlOrTemplate, pathTemplateOrParams, maybeParams = {}, config = {}) => urlcat(baseUrlOrTemplate, pathTemplateOrParams, maybeParams, __spreadValues(__spreadValues({}, rootConfig), config));
  36. }
  37. function joinFullUrl(renderedPath, baseUrl, pathAndQuery) {
  38. if (renderedPath.length) {
  39. return join(baseUrl, "/", pathAndQuery);
  40. } else {
  41. return join(baseUrl, "?", pathAndQuery);
  42. }
  43. }
  44. function urlcatImpl(pathTemplate, params, baseUrl, config) {
  45. const { renderedPath, remainingParams } = path(pathTemplate, params);
  46. const cleanParams = removeNullOrUndef(remainingParams);
  47. const renderedQuery = query(cleanParams, config);
  48. const pathAndQuery = join(renderedPath, "?", renderedQuery);
  49. return baseUrl ? joinFullUrl(renderedPath, baseUrl, pathAndQuery) : pathAndQuery;
  50. }
  51. function query(params, config) {
  52. var _a, _b;
  53. if (Object.keys(params).length < 1) {
  54. return "";
  55. }
  56. const qsConfiguration = {
  57. format: (_b = (_a = config == null ? void 0 : config.objectFormat) == null ? void 0 : _a.format) != null ? _b : "RFC1738",
  58. arrayFormat: config == null ? void 0 : config.arrayFormat
  59. };
  60. return qs.stringify(params, qsConfiguration);
  61. }
  62. function subst(template, params) {
  63. const { renderedPath } = path(template, params);
  64. return renderedPath;
  65. }
  66. function path(template, params) {
  67. const remainingParams = __spreadValues({}, params);
  68. const renderedPath = template.replace(/:[_A-Za-z]+[_A-Za-z0-9]*/g, (p) => {
  69. const key = p.slice(1);
  70. validatePathParam(params, key);
  71. delete remainingParams[key];
  72. return encodeURIComponent(params[key]);
  73. });
  74. return { renderedPath, remainingParams };
  75. }
  76. function validatePathParam(params, key) {
  77. const allowedTypes = ["boolean", "string", "number"];
  78. if (!Object.prototype.hasOwnProperty.call(params, key)) {
  79. throw new Error(`Missing value for path parameter ${key}.`);
  80. }
  81. if (!allowedTypes.includes(typeof params[key])) {
  82. throw new TypeError(
  83. `Path parameter ${key} cannot be of type ${typeof params[key]}. Allowed types are: ${allowedTypes.join(", ")}.`
  84. );
  85. }
  86. if (typeof params[key] === "string" && params[key].trim() === "") {
  87. throw new Error(`Path parameter ${key} cannot be an empty string.`);
  88. }
  89. }
  90. function join(part1, separator, part2) {
  91. const p1 = part1.endsWith(separator) ? part1.slice(0, -separator.length) : part1;
  92. const p2 = part2.startsWith(separator) ? part2.slice(separator.length) : part2;
  93. return p1 === "" || p2 === "" ? p1 + p2 : p1 + separator + p2;
  94. }
  95. function removeNullOrUndef(params) {
  96. return Object.keys(params).filter((k) => notNullOrUndefined(params[k])).reduce((result, k) => {
  97. result[k] = params[k];
  98. return result;
  99. }, {});
  100. }
  101. function notNullOrUndefined(v) {
  102. return v !== void 0 && v !== null;
  103. }
  104.  
  105. exports.configure = configure;
  106. exports.urlcat = urlcat;
  107. exports.join = join;
  108. exports.query = query;
  109. exports.subst = subst;
  110.  
  111. // Object.defineProperty(exports, '__esModule', { value: true });
  112.  
  113. }));

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址