@uniformdev/redirect 19.7.0 → 19.9.2-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -83,6 +83,9 @@ declare class RedirectClient extends ApiClient<RedirectClientOptions> {
83
83
  * @param options - Different options available to the redirect engine
84
84
  */
85
85
  private static processDefinitionToResults;
86
+ static getTargetVariableExpandedUrl(url: string, redirectDefinition: RedirectDefinition['redirect'], isVariable?: (pathSegment: string) => boolean): string;
87
+ private static mergeQueryStrings;
88
+ private static getSourceVariables;
86
89
  private static processUrl;
87
90
  }
88
91
  declare class UncachedRedirectClient extends RedirectClient {
@@ -105,6 +108,8 @@ interface paths {
105
108
  id?: string;
106
109
  /** Ids of the redirects to retrieve */
107
110
  ids?: string[];
111
+ /** Source url to attempt to find redirects for */
112
+ sourceUrl?: string;
108
113
  /** Id of the project map the source or target belongs to. */
109
114
  projectMapId?: string;
110
115
  /** Id of the project map node to find redirects for. */
package/dist/index.esm.js CHANGED
@@ -405,9 +405,56 @@ var _RedirectClient = class extends ApiClient {
405
405
  }, finalUrl) : void 0
406
406
  };
407
407
  }
408
+ static getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
409
+ const processedTarget = this.processUrl(redirectDefinition.targetUrl);
410
+ const processedSource = this.processUrl(redirectDefinition.sourceUrl);
411
+ let finalUrlPath = processedTarget.path;
412
+ const processedUrl = this.processUrl(url);
413
+ const variables = this.getSourceVariables(processedUrl.path, processedSource.path, isVariable);
414
+ for (const variable in variables) {
415
+ finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
416
+ }
417
+ const protocol = redirectDefinition.targetPreserveIncomingProtocol ? processedUrl.protocol : processedTarget.protocol;
418
+ const domain = redirectDefinition.targetPreserveIncomingDomain ? processedUrl.domain : processedTarget.domain;
419
+ const port = processedTarget.port;
420
+ const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
421
+ const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
422
+ return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
423
+ }
424
+ static mergeQueryStrings(qs1, qs2) {
425
+ let fragment = false;
426
+ if (qs1.startsWith("#")) {
427
+ fragment = true;
428
+ qs1 = qs1.substring(1);
429
+ }
430
+ if (qs2.startsWith("#")) {
431
+ fragment = true;
432
+ qs2 = qs2.substring(1);
433
+ }
434
+ const params1 = new URLSearchParams(qs1);
435
+ const params2 = new URLSearchParams(qs2);
436
+ const merged = new URLSearchParams([...params1, ...params2]).toString();
437
+ if (merged.length > 0)
438
+ return (fragment ? "#" : "?") + merged;
439
+ return "";
440
+ }
441
+ static getSourceVariables(path, source, isVariable = (pathSegment) => pathSegment.startsWith(":")) {
442
+ const variables = {};
443
+ const pathSegments = path.split("/");
444
+ const sourceSegments = source.split("/");
445
+ if (pathSegments.length !== sourceSegments.length) {
446
+ throw new Error("Path and source have different numbers of path segments, must be the same");
447
+ }
448
+ sourceSegments.forEach((sourceSegment, i) => {
449
+ if (isVariable(sourceSegment)) {
450
+ variables[sourceSegment] = pathSegments[i];
451
+ }
452
+ });
453
+ return variables;
454
+ }
408
455
  static processUrl(url) {
409
456
  var _a, _b, _c, _d, _e, _f;
410
- const matches = url.match(/^(https?:\/\/)?(([^:/?#]*)(?::([0-9]+))?)?([/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
457
+ const matches = url.match(/^(https?:\/\/)?(([^:/?#]*)(?:(:[0-9]+))?)?([/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
411
458
  return {
412
459
  url,
413
460
  protocol: (_a = matches == null ? void 0 : matches[1]) != null ? _a : "",
@@ -415,7 +462,7 @@ var _RedirectClient = class extends ApiClient {
415
462
  port: (_c = matches == null ? void 0 : matches[4]) != null ? _c : "",
416
463
  path: (_d = matches == null ? void 0 : matches[5]) != null ? _d : "",
417
464
  query: (_e = matches == null ? void 0 : matches[6]) != null ? _e : "",
418
- hash: (_f = matches == null ? void 0 : matches[7]) != null ? _f : ""
465
+ fragment: (_f = matches == null ? void 0 : matches[7]) != null ? _f : ""
419
466
  };
420
467
  }
421
468
  };
package/dist/index.js CHANGED
@@ -435,9 +435,56 @@ var _RedirectClient = class extends import_api.ApiClient {
435
435
  }, finalUrl) : void 0
436
436
  };
437
437
  }
438
+ static getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
439
+ const processedTarget = this.processUrl(redirectDefinition.targetUrl);
440
+ const processedSource = this.processUrl(redirectDefinition.sourceUrl);
441
+ let finalUrlPath = processedTarget.path;
442
+ const processedUrl = this.processUrl(url);
443
+ const variables = this.getSourceVariables(processedUrl.path, processedSource.path, isVariable);
444
+ for (const variable in variables) {
445
+ finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
446
+ }
447
+ const protocol = redirectDefinition.targetPreserveIncomingProtocol ? processedUrl.protocol : processedTarget.protocol;
448
+ const domain = redirectDefinition.targetPreserveIncomingDomain ? processedUrl.domain : processedTarget.domain;
449
+ const port = processedTarget.port;
450
+ const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
451
+ const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
452
+ return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
453
+ }
454
+ static mergeQueryStrings(qs1, qs2) {
455
+ let fragment = false;
456
+ if (qs1.startsWith("#")) {
457
+ fragment = true;
458
+ qs1 = qs1.substring(1);
459
+ }
460
+ if (qs2.startsWith("#")) {
461
+ fragment = true;
462
+ qs2 = qs2.substring(1);
463
+ }
464
+ const params1 = new URLSearchParams(qs1);
465
+ const params2 = new URLSearchParams(qs2);
466
+ const merged = new URLSearchParams([...params1, ...params2]).toString();
467
+ if (merged.length > 0)
468
+ return (fragment ? "#" : "?") + merged;
469
+ return "";
470
+ }
471
+ static getSourceVariables(path, source, isVariable = (pathSegment) => pathSegment.startsWith(":")) {
472
+ const variables = {};
473
+ const pathSegments = path.split("/");
474
+ const sourceSegments = source.split("/");
475
+ if (pathSegments.length !== sourceSegments.length) {
476
+ throw new Error("Path and source have different numbers of path segments, must be the same");
477
+ }
478
+ sourceSegments.forEach((sourceSegment, i) => {
479
+ if (isVariable(sourceSegment)) {
480
+ variables[sourceSegment] = pathSegments[i];
481
+ }
482
+ });
483
+ return variables;
484
+ }
438
485
  static processUrl(url) {
439
486
  var _a, _b, _c, _d, _e, _f;
440
- const matches = url.match(/^(https?:\/\/)?(([^:/?#]*)(?::([0-9]+))?)?([/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
487
+ const matches = url.match(/^(https?:\/\/)?(([^:/?#]*)(?:(:[0-9]+))?)?([/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
441
488
  return {
442
489
  url,
443
490
  protocol: (_a = matches == null ? void 0 : matches[1]) != null ? _a : "",
@@ -445,7 +492,7 @@ var _RedirectClient = class extends import_api.ApiClient {
445
492
  port: (_c = matches == null ? void 0 : matches[4]) != null ? _c : "",
446
493
  path: (_d = matches == null ? void 0 : matches[5]) != null ? _d : "",
447
494
  query: (_e = matches == null ? void 0 : matches[6]) != null ? _e : "",
448
- hash: (_f = matches == null ? void 0 : matches[7]) != null ? _f : ""
495
+ fragment: (_f = matches == null ? void 0 : matches[7]) != null ? _f : ""
449
496
  };
450
497
  }
451
498
  };
package/dist/index.mjs CHANGED
@@ -405,9 +405,56 @@ var _RedirectClient = class extends ApiClient {
405
405
  }, finalUrl) : void 0
406
406
  };
407
407
  }
408
+ static getTargetVariableExpandedUrl(url, redirectDefinition, isVariable) {
409
+ const processedTarget = this.processUrl(redirectDefinition.targetUrl);
410
+ const processedSource = this.processUrl(redirectDefinition.sourceUrl);
411
+ let finalUrlPath = processedTarget.path;
412
+ const processedUrl = this.processUrl(url);
413
+ const variables = this.getSourceVariables(processedUrl.path, processedSource.path, isVariable);
414
+ for (const variable in variables) {
415
+ finalUrlPath = finalUrlPath.replace(variable, variables[variable]);
416
+ }
417
+ const protocol = redirectDefinition.targetPreserveIncomingProtocol ? processedUrl.protocol : processedTarget.protocol;
418
+ const domain = redirectDefinition.targetPreserveIncomingDomain ? processedUrl.domain : processedTarget.domain;
419
+ const port = processedTarget.port;
420
+ const query = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.query, processedTarget.query) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.query : processedTarget.query;
421
+ const fragment = redirectDefinition.sourceRetainQuerystring && redirectDefinition.targetMergeQuerystring ? this.mergeQueryStrings(processedUrl.fragment, processedTarget.fragment) : !redirectDefinition.targetMergeQuerystring && redirectDefinition.sourceRetainQuerystring ? processedUrl.fragment : processedTarget.fragment;
422
+ return `${protocol}${domain}${port}${finalUrlPath}${query}${fragment}`;
423
+ }
424
+ static mergeQueryStrings(qs1, qs2) {
425
+ let fragment = false;
426
+ if (qs1.startsWith("#")) {
427
+ fragment = true;
428
+ qs1 = qs1.substring(1);
429
+ }
430
+ if (qs2.startsWith("#")) {
431
+ fragment = true;
432
+ qs2 = qs2.substring(1);
433
+ }
434
+ const params1 = new URLSearchParams(qs1);
435
+ const params2 = new URLSearchParams(qs2);
436
+ const merged = new URLSearchParams([...params1, ...params2]).toString();
437
+ if (merged.length > 0)
438
+ return (fragment ? "#" : "?") + merged;
439
+ return "";
440
+ }
441
+ static getSourceVariables(path, source, isVariable = (pathSegment) => pathSegment.startsWith(":")) {
442
+ const variables = {};
443
+ const pathSegments = path.split("/");
444
+ const sourceSegments = source.split("/");
445
+ if (pathSegments.length !== sourceSegments.length) {
446
+ throw new Error("Path and source have different numbers of path segments, must be the same");
447
+ }
448
+ sourceSegments.forEach((sourceSegment, i) => {
449
+ if (isVariable(sourceSegment)) {
450
+ variables[sourceSegment] = pathSegments[i];
451
+ }
452
+ });
453
+ return variables;
454
+ }
408
455
  static processUrl(url) {
409
456
  var _a, _b, _c, _d, _e, _f;
410
- const matches = url.match(/^(https?:\/\/)?(([^:/?#]*)(?::([0-9]+))?)?([/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
457
+ const matches = url.match(/^(https?:\/\/)?(([^:/?#]*)(?:(:[0-9]+))?)?([/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
411
458
  return {
412
459
  url,
413
460
  protocol: (_a = matches == null ? void 0 : matches[1]) != null ? _a : "",
@@ -415,7 +462,7 @@ var _RedirectClient = class extends ApiClient {
415
462
  port: (_c = matches == null ? void 0 : matches[4]) != null ? _c : "",
416
463
  path: (_d = matches == null ? void 0 : matches[5]) != null ? _d : "",
417
464
  query: (_e = matches == null ? void 0 : matches[6]) != null ? _e : "",
418
- hash: (_f = matches == null ? void 0 : matches[7]) != null ? _f : ""
465
+ fragment: (_f = matches == null ? void 0 : matches[7]) != null ? _f : ""
419
466
  };
420
467
  }
421
468
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/redirect",
3
- "version": "19.7.0",
3
+ "version": "19.9.2-alpha.3+547c8b11d",
4
4
  "description": "Uniform redirect client",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "main": "./dist/index.js",
@@ -32,11 +32,11 @@
32
32
  "/dist"
33
33
  ],
34
34
  "dependencies": {
35
- "@uniformdev/context": "19.7.0",
35
+ "@uniformdev/context": "19.9.2-alpha.3+547c8b11d",
36
36
  "p-limit": "^3.1.0"
37
37
  },
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  },
41
- "gitHead": "bbcd8d36ccb082d87f1f434a94d6d9402ddb5424"
41
+ "gitHead": "547c8b11d4655b34250c2fbe3f016c9bc12c3905"
42
42
  }