@twin.org/api-models 0.9.1-next.3 → 0.9.1-next.4

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.
@@ -24,24 +24,26 @@ export class HttpHeaderHelper {
24
24
  }
25
25
  }
26
26
  /**
27
- * Set the Location header to the encoded ID, optionally placed within a base URL or template.
28
- * When baseUrl contains a colon-prefixed placeholder (e.g. "/path1/:id/path2") the
27
+ * Set the Location header to the encoded ID, optionally placed within a URL template.
28
+ * When the template contains `:id` (e.g. "/path1/:id/path2" or "/path?p=:id") the
29
29
  * encoded ID is substituted at that position; otherwise it is appended.
30
- * The base URL may be an absolute URL or a relative path. When omitted the bare
31
- * encoded ID is used.
30
+ * When no template is provided the bare encoded ID is used.
31
+ * Callers that need to combine a public origin with a path should use
32
+ * HttpUrlHelper.combineOriginPath to build the template before calling this method.
32
33
  * @param headers The response headers to mutate.
33
34
  * @param id The resource ID to encode and place.
34
- * @param baseUrl The optional base URL, relative path, or URL template.
35
+ * @param urlTemplate The optional URL template (absolute or relative).
35
36
  */
36
- static buildId(headers, id, baseUrl) {
37
+ static buildId(headers, id, urlTemplate) {
37
38
  Guards.stringValue(HttpHeaderHelper.CLASS_NAME, "id", id);
38
39
  const encodedId = encodeURIComponent(id);
39
- if (Is.stringValue(baseUrl)) {
40
- if (baseUrl.includes("/:")) {
41
- headers[HeaderTypes.Location] = baseUrl.replace(/\/:[^/?#]+/, `/${encodedId}`);
40
+ if (Is.stringValue(urlTemplate)) {
41
+ if (urlTemplate.includes(":id")) {
42
+ headers[HeaderTypes.Location] = urlTemplate.replace(/:id/, encodedId);
42
43
  }
43
44
  else {
44
- headers[HeaderTypes.Location] = `${StringHelper.trimTrailingSlashes(baseUrl)}/${encodedId}`;
45
+ headers[HeaderTypes.Location] =
46
+ `${StringHelper.trimTrailingSlashes(urlTemplate)}/${encodedId}`;
45
47
  }
46
48
  }
47
49
  else {
@@ -82,39 +84,29 @@ export class HttpHeaderHelper {
82
84
  }
83
85
  /**
84
86
  * Extract the resource ID from the Location response header.
85
- * Handles absolute URLs (http://host/path/:id, http://host/path/:id?foo=bar),
86
- * relative paths (/segment/:id, ./segment/:id), and bare ID values.
87
- * When a URL template such as "/path1/:id/path2" is supplied the ID is extracted
88
- * from the segment position marked by the first colon-prefixed placeholder.
89
- * Without a template the last path segment is returned.
87
+ * Handles absolute URLs, relative paths, and bare ID values.
88
+ * When a templateUrl containing ':id' is supplied (e.g. "/path1/:id/path2" or
89
+ * "https://host/path/:id") the ID is extracted via pattern matching at the ':id'
90
+ * position. Without a matching template the last path segment is returned.
90
91
  * @param headers The response headers containing the Location header.
91
- * @param template Optional URL template with a colon-prefixed placeholder marking the ID position, e.g. "/path1/:id/path2".
92
+ * @param templateUrl Optional URL template containing the ':id' placeholder, e.g. "/path1/:id/path2".
92
93
  * @returns The extracted ID string.
93
- * @throws GeneralError If the Location header is missing, the template has no placeholder, or the ID cannot be extracted.
94
+ * @throws GeneralError If the Location header is missing or the ID cannot be extracted.
94
95
  */
95
- static extractId(headers, template) {
96
+ static extractId(headers, templateUrl) {
96
97
  const location = headers?.[HeaderTypes.Location];
97
98
  if (!Is.stringValue(location)) {
98
99
  throw new GeneralError(HttpHeaderHelper.CLASS_NAME, "locationMissing");
99
100
  }
100
101
  const withoutQuery = location.split("?")[0];
101
- if (Is.stringValue(template)) {
102
- const templateSegments = template.split("/").filter(s => s.length > 0);
103
- const placeholderIndex = templateSegments.findIndex(s => s.startsWith(":"));
104
- if (placeholderIndex === -1) {
105
- throw new GeneralError(HttpHeaderHelper.CLASS_NAME, "templateNoPlaceholder");
106
- }
107
- let path = withoutQuery;
108
- try {
109
- path = new URL(withoutQuery).pathname;
110
- }
111
- catch { }
112
- const locationSegments = path.split("/").filter(s => s.length > 0 && s !== ".");
113
- const id = locationSegments[placeholderIndex];
114
- if (!Is.stringValue(id)) {
102
+ if (Is.stringValue(templateUrl) && templateUrl.includes(":id")) {
103
+ const escaped = templateUrl.replace(/[.+^${}()|[\]\\?]/g, "\\$&");
104
+ const pattern = new RegExp(escaped.replace(":id", "([^/?#]+)"));
105
+ const match = pattern.exec(location);
106
+ if (!Is.stringValue(match?.[1])) {
115
107
  throw new GeneralError(HttpHeaderHelper.CLASS_NAME, "idNotFound");
116
108
  }
117
- return decodeURIComponent(id);
109
+ return decodeURIComponent(match[1]);
118
110
  }
119
111
  const id = withoutQuery.includes("/") ? withoutQuery.split("/").pop() : withoutQuery;
120
112
  if (!Is.stringValue(id)) {
@@ -1 +1 @@
1
- {"version":3,"file":"httpHeaderHelper.js","sourceRoot":"","sources":["../../../src/helpers/httpHeaderHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAExE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAqB,MAAM,eAAe,CAAC;AACxF,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC5B;;OAEG;IACI,MAAM,CAAU,UAAU,sBAAsC;IAEvE;;;;;;OAMG;IACI,MAAM,CAAC,WAAW,CACxB,OAAqB,EACrB,GAAW,EACX,YAAgC,EAChC,MAA0B;QAE1B,IAAI,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC;YAC5B,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,GAAG,YAAY,CAAC,gBAAgB,CACxD,aAAa,CAAC,aAAa,CAAC,GAAG,EAAE,YAAY,CAAC,EAC9C,EAAE,MAAM,EAAE,EACV,MAAM,CACN,CAAC;QACH,CAAC;IACF,CAAC;IAED;;;;;;;;;OASG;IACI,MAAM,CAAC,OAAO,CACpB,OAAqB,EACrB,EAAU,EACV,OAAgB;QAEhB,MAAM,CAAC,WAAW,CAAC,gBAAgB,CAAC,UAAU,QAAc,EAAE,CAAC,C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,MAAM,gBAAgB,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC;YAChF,MAAM,EAAE,GAAG,gBAAgB,CAAC,gBAAgB,CAAC,CAAC;YAE9C,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,CAAC;gBACzB,MAAM,IAAI,YAAY,CAAC,gBAAgB,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;YACnE,CAAC;YACD,OAAO,kBAAkB,CAAC,EAAE,CAAC,CAAC;QAC/B,CAAC;QAED,MAAM,EAAE,GAAG,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC;QAErF,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,YAAY,CAAC,gBAAgB,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;QACnE,CAAC;QACD,OAAO,kBAAkB,CAAC,EAAE,CAAC,CAAC;IAC/B,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Guards, Is, StringHelper } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { HeaderHelper, HeaderTypes, MimeTypes, type IHttpHeaders } from \"@twin.org/web\";\nimport { HttpUrlHelper } from \"./httpUrlHelper.js\";\n\n/**\n * Class to help with handling http headers.\n */\nexport class HttpHeaderHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<HttpHeaderHelper>();\n\n\t/**\n\t * Set the Link header with a next cursor relation when a cursor is present.\n\t * @param headers The response headers to mutate.\n\t * @param url The request URL used as the base for the link.\n\t * @param publicOrigin The public origin to substitute into the URL, if any.\n\t * @param cursor The cursor value; when undefined or empty the header is not set.\n\t */\n\tpublic static buildCursor(\n\t\theaders: IHttpHeaders,\n\t\turl: string,\n\t\tpublicOrigin: string | undefined,\n\t\tcursor: string | undefined\n\t): asserts headers is IHttpHeaders & { [HeaderTypes.Link]: string | undefined } {\n\t\tif (Is.stringValue(cursor)) {\n\t\t\theaders[HeaderTypes.Link] = HeaderHelper.createLinkHeader(\n\t\t\t\tHttpUrlHelper.replaceOrigin(url, publicOrigin),\n\t\t\t\t{ cursor },\n\t\t\t\t\"next\"\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Set the Location header to the encoded ID, optionally placed within a base URL or template.\n\t * When baseUrl contains a colon-prefixed placeholder (e.g. \"/path1/:id/path2\") the\n\t * encoded ID is substituted at that position; otherwise it is appended.\n\t * The base URL may be an absolute URL or a relative path. When omitted the bare\n\t * encoded ID is used.\n\t * @param headers The response headers to mutate.\n\t * @param id The resource ID to encode and place.\n\t * @param baseUrl The optional base URL, relative path, or URL template.\n\t */\n\tpublic static buildId(\n\t\theaders: IHttpHeaders,\n\t\tid: string,\n\t\tbaseUrl?: string\n\t): asserts headers is IHttpHeaders & { [HeaderTypes.Location]: string } {\n\t\tGuards.stringValue(HttpHeaderHelper.CLASS_NAME, nameof(id), id);\n\t\tconst encodedId = encodeURIComponent(id);\n\t\tif (Is.stringValue(baseUrl)) {\n\t\t\tif (baseUrl.includes(\"/:\")) {\n\t\t\t\theaders[HeaderTypes.Location] = baseUrl.replace(/\\/:[^/?#]+/, `/${encodedId}`);\n\t\t\t} else {\n\t\t\t\theaders[HeaderTypes.Location] = `${StringHelper.trimTrailingSlashes(baseUrl)}/${encodedId}`;\n\t\t\t}\n\t\t} else {\n\t\t\theaders[HeaderTypes.Location] = encodedId;\n\t\t}\n\t}\n\n\t/**\n\t * Set the Content-Type header to JSON-LD or JSON depending on the request Accept header.\n\t * Uses HeaderHelper.extractAccept which parses the Accept header per RFC 7231 and returns\n\t * entries ordered by quality descending, preserving original order for equal q-values.\n\t * @param headers The response headers to mutate.\n\t * @param requestHeaders The request headers to inspect for the Accept value.\n\t */\n\tpublic static buildJsonContentType(\n\t\theaders: IHttpHeaders,\n\t\trequestHeaders?: IHttpHeaders\n\t): asserts headers is IHttpHeaders & {\n\t\t[HeaderTypes.ContentType]: typeof MimeTypes.JsonLd | typeof MimeTypes.Json;\n\t} {\n\t\tconst accepted = HeaderHelper.extractAccept(requestHeaders);\n\t\tlet contentType: string = MimeTypes.Json;\n\t\tif (Is.arrayValue(accepted)) {\n\t\t\tfor (const { mimeType } of accepted) {\n\t\t\t\tif (mimeType === MimeTypes.JsonLd) {\n\t\t\t\t\tcontentType = MimeTypes.JsonLd;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (mimeType === MimeTypes.Json || mimeType === \"application/*\" || mimeType === \"*/*\") {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\theaders[HeaderTypes.ContentType] = contentType;\n\t}\n\n\t/**\n\t * Extract the cursor from the Link header's next relation.\n\t * @param headers The response headers to extract the cursor from.\n\t * @returns The cursor value or undefined if not present.\n\t */\n\tpublic static extractCursor(headers?: IHttpHeaders): string | undefined {\n\t\treturn HeaderHelper.extractLinkHeaderRelation(headers?.[HeaderTypes.Link], \"next\")\n\t\t\t?.urlQueryParams?.cursor;\n\t}\n\n\t/**\n\t * Extract the resource ID from the Location response header.\n\t * Handles absolute URLs (http://host/path/:id, http://host/path/:id?foo=bar),\n\t * relative paths (/segment/:id, ./segment/:id), and bare ID values.\n\t * When a URL template such as \"/path1/:id/path2\" is supplied the ID is extracted\n\t * from the segment position marked by the first colon-prefixed placeholder.\n\t * Without a template the last path segment is returned.\n\t * @param headers The response headers containing the Location header.\n\t * @param template Optional URL template with a colon-prefixed placeholder marking the ID position, e.g. \"/path1/:id/path2\".\n\t * @returns The extracted ID string.\n\t * @throws GeneralError If the Location header is missing, the template has no placeholder, or the ID cannot be extracted.\n\t */\n\tpublic static extractId(headers?: IHttpHeaders, template?: string): string {\n\t\tconst location = headers?.[HeaderTypes.Location];\n\t\tif (!Is.stringValue(location)) {\n\t\t\tthrow new GeneralError(HttpHeaderHelper.CLASS_NAME, \"locationMissing\");\n\t\t}\n\n\t\tconst withoutQuery = location.split(\"?\")[0];\n\n\t\tif (Is.stringValue(template)) {\n\t\t\tconst templateSegments = template.split(\"/\").filter(s => s.length > 0);\n\t\t\tconst placeholderIndex = templateSegments.findIndex(s => s.startsWith(\":\"));\n\t\t\tif (placeholderIndex === -1) {\n\t\t\t\tthrow new GeneralError(HttpHeaderHelper.CLASS_NAME, \"templateNoPlaceholder\");\n\t\t\t}\n\n\t\t\tlet path = withoutQuery;\n\t\t\ttry {\n\t\t\t\tpath = new URL(withoutQuery).pathname;\n\t\t\t} catch {}\n\n\t\t\tconst locationSegments = path.split(\"/\").filter(s => s.length > 0 && s !== \".\");\n\t\t\tconst id = locationSegments[placeholderIndex];\n\n\t\t\tif (!Is.stringValue(id)) {\n\t\t\t\tthrow new GeneralError(HttpHeaderHelper.CLASS_NAME, \"idNotFound\");\n\t\t\t}\n\t\t\treturn decodeURIComponent(id);\n\t\t}\n\n\t\tconst id = withoutQuery.includes(\"/\") ? withoutQuery.split(\"/\").pop() : withoutQuery;\n\n\t\tif (!Is.stringValue(id)) {\n\t\t\tthrow new GeneralError(HttpHeaderHelper.CLASS_NAME, \"idNotFound\");\n\t\t}\n\t\treturn decodeURIComponent(id);\n\t}\n}\n"]}
1
+ {"version":3,"file":"httpHeaderHelper.js","sourceRoot":"","sources":["../../../src/helpers/httpHeaderHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAExE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAqB,MA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,EAAE,CAAC,WAAW,CAAC,WAAW,CAAC,IAAI,WAAW,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YAChE,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,oBAAoB,EAAE,MAAM,CAAC,CAAC;YAClE,MAAM,OAAO,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC;YAChE,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACrC,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;gBACjC,MAAM,IAAI,YAAY,CAAC,gBAAgB,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;YACnE,CAAC;YACD,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QACrC,CAAC;QAED,MAAM,EAAE,GAAG,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC;QAErF,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,YAAY,CAAC,gBAAgB,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;QACnE,CAAC;QACD,OAAO,kBAAkB,CAAC,EAAE,CAAC,CAAC;IAC/B,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Guards, Is, StringHelper } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { HeaderHelper, HeaderTypes, MimeTypes, type IHttpHeaders } from \"@twin.org/web\";\nimport { HttpUrlHelper } from \"./httpUrlHelper.js\";\n\n/**\n * Class to help with handling http headers.\n */\nexport class HttpHeaderHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<HttpHeaderHelper>();\n\n\t/**\n\t * Set the Link header with a next cursor relation when a cursor is present.\n\t * @param headers The response headers to mutate.\n\t * @param url The request URL used as the base for the link.\n\t * @param publicOrigin The public origin to substitute into the URL, if any.\n\t * @param cursor The cursor value; when undefined or empty the header is not set.\n\t */\n\tpublic static buildCursor(\n\t\theaders: IHttpHeaders,\n\t\turl: string,\n\t\tpublicOrigin: string | undefined,\n\t\tcursor: string | undefined\n\t): asserts headers is IHttpHeaders & { [HeaderTypes.Link]: string | undefined } {\n\t\tif (Is.stringValue(cursor)) {\n\t\t\theaders[HeaderTypes.Link] = HeaderHelper.createLinkHeader(\n\t\t\t\tHttpUrlHelper.replaceOrigin(url, publicOrigin),\n\t\t\t\t{ cursor },\n\t\t\t\t\"next\"\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Set the Location header to the encoded ID, optionally placed within a URL template.\n\t * When the template contains `:id` (e.g. \"/path1/:id/path2\" or \"/path?p=:id\") the\n\t * encoded ID is substituted at that position; otherwise it is appended.\n\t * When no template is provided the bare encoded ID is used.\n\t * Callers that need to combine a public origin with a path should use\n\t * HttpUrlHelper.combineOriginPath to build the template before calling this method.\n\t * @param headers The response headers to mutate.\n\t * @param id The resource ID to encode and place.\n\t * @param urlTemplate The optional URL template (absolute or relative).\n\t */\n\tpublic static buildId(\n\t\theaders: IHttpHeaders,\n\t\tid: string,\n\t\turlTemplate?: string\n\t): asserts headers is IHttpHeaders & { [HeaderTypes.Location]: string } {\n\t\tGuards.stringValue(HttpHeaderHelper.CLASS_NAME, nameof(id), id);\n\t\tconst encodedId = encodeURIComponent(id);\n\n\t\tif (Is.stringValue(urlTemplate)) {\n\t\t\tif (urlTemplate.includes(\":id\")) {\n\t\t\t\theaders[HeaderTypes.Location] = urlTemplate.replace(/:id/, encodedId);\n\t\t\t} else {\n\t\t\t\theaders[HeaderTypes.Location] =\n\t\t\t\t\t`${StringHelper.trimTrailingSlashes(urlTemplate)}/${encodedId}`;\n\t\t\t}\n\t\t} else {\n\t\t\theaders[HeaderTypes.Location] = encodedId;\n\t\t}\n\t}\n\n\t/**\n\t * Set the Content-Type header to JSON-LD or JSON depending on the request Accept header.\n\t * Uses HeaderHelper.extractAccept which parses the Accept header per RFC 7231 and returns\n\t * entries ordered by quality descending, preserving original order for equal q-values.\n\t * @param headers The response headers to mutate.\n\t * @param requestHeaders The request headers to inspect for the Accept value.\n\t */\n\tpublic static buildJsonContentType(\n\t\theaders: IHttpHeaders,\n\t\trequestHeaders?: IHttpHeaders\n\t): asserts headers is IHttpHeaders & {\n\t\t[HeaderTypes.ContentType]: typeof MimeTypes.JsonLd | typeof MimeTypes.Json;\n\t} {\n\t\tconst accepted = HeaderHelper.extractAccept(requestHeaders);\n\t\tlet contentType: string = MimeTypes.Json;\n\t\tif (Is.arrayValue(accepted)) {\n\t\t\tfor (const { mimeType } of accepted) {\n\t\t\t\tif (mimeType === MimeTypes.JsonLd) {\n\t\t\t\t\tcontentType = MimeTypes.JsonLd;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (mimeType === MimeTypes.Json || mimeType === \"application/*\" || mimeType === \"*/*\") {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\theaders[HeaderTypes.ContentType] = contentType;\n\t}\n\n\t/**\n\t * Extract the cursor from the Link header's next relation.\n\t * @param headers The response headers to extract the cursor from.\n\t * @returns The cursor value or undefined if not present.\n\t */\n\tpublic static extractCursor(headers?: IHttpHeaders): string | undefined {\n\t\treturn HeaderHelper.extractLinkHeaderRelation(headers?.[HeaderTypes.Link], \"next\")\n\t\t\t?.urlQueryParams?.cursor;\n\t}\n\n\t/**\n\t * Extract the resource ID from the Location response header.\n\t * Handles absolute URLs, relative paths, and bare ID values.\n\t * When a templateUrl containing ':id' is supplied (e.g. \"/path1/:id/path2\" or\n\t * \"https://host/path/:id\") the ID is extracted via pattern matching at the ':id'\n\t * position. Without a matching template the last path segment is returned.\n\t * @param headers The response headers containing the Location header.\n\t * @param templateUrl Optional URL template containing the ':id' placeholder, e.g. \"/path1/:id/path2\".\n\t * @returns The extracted ID string.\n\t * @throws GeneralError If the Location header is missing or the ID cannot be extracted.\n\t */\n\tpublic static extractId(headers?: IHttpHeaders, templateUrl?: string): string {\n\t\tconst location = headers?.[HeaderTypes.Location];\n\t\tif (!Is.stringValue(location)) {\n\t\t\tthrow new GeneralError(HttpHeaderHelper.CLASS_NAME, \"locationMissing\");\n\t\t}\n\n\t\tconst withoutQuery = location.split(\"?\")[0];\n\n\t\tif (Is.stringValue(templateUrl) && templateUrl.includes(\":id\")) {\n\t\t\tconst escaped = templateUrl.replace(/[.+^${}()|[\\]\\\\?]/g, \"\\\\$&\");\n\t\t\tconst pattern = new RegExp(escaped.replace(\":id\", \"([^/?#]+)\"));\n\t\t\tconst match = pattern.exec(location);\n\t\t\tif (!Is.stringValue(match?.[1])) {\n\t\t\t\tthrow new GeneralError(HttpHeaderHelper.CLASS_NAME, \"idNotFound\");\n\t\t\t}\n\t\t\treturn decodeURIComponent(match[1]);\n\t\t}\n\n\t\tconst id = withoutQuery.includes(\"/\") ? withoutQuery.split(\"/\").pop() : withoutQuery;\n\n\t\tif (!Is.stringValue(id)) {\n\t\t\tthrow new GeneralError(HttpHeaderHelper.CLASS_NAME, \"idNotFound\");\n\t\t}\n\t\treturn decodeURIComponent(id);\n\t}\n}\n"]}
@@ -57,14 +57,26 @@ export class HttpUrlHelper {
57
57
  catch { }
58
58
  }
59
59
  /**
60
- * Combine the urls parts.
61
- * @param origin The origin to combine.
62
- * @param pathAndSearch The path and search to combine.
63
- * @returns The combined parts.
60
+ * Combine an optional origin and an optional path into a single URL string.
61
+ * Relative paths (no protocol, no leading slash) have a leading slash prepended.
62
+ * Trailing slashes are trimmed from the origin before joining.
63
+ * Returns undefined when both arguments are absent or empty.
64
+ * @param origin The optional origin (e.g. "https://example.com").
65
+ * @param path The optional path or URL template (e.g. "/api/items" or "api/items").
66
+ * @returns The combined string, or undefined when both are absent.
64
67
  */
65
- static combineParts(origin, pathAndSearch) {
66
- if (Is.string(origin) && Is.string(pathAndSearch)) {
67
- return `${StringHelper.trimTrailingSlashes(origin)}/${StringHelper.trimLeadingSlashes(pathAndSearch)}`;
68
+ static combineOriginPath(origin, path) {
69
+ const normalizedPath = Is.stringValue(path) && !path.includes("://")
70
+ ? `/${StringHelper.trimLeadingSlashes(path)}`
71
+ : path;
72
+ if (Is.stringValue(origin) && Is.stringValue(normalizedPath)) {
73
+ return `${StringHelper.trimTrailingSlashes(origin)}${normalizedPath}`;
74
+ }
75
+ else if (Is.stringValue(origin)) {
76
+ return origin;
77
+ }
78
+ else if (Is.stringValue(normalizedPath)) {
79
+ return normalizedPath;
68
80
  }
69
81
  }
70
82
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"httpUrlHelper.js","sourceRoot":"","sources":["../../../src/helpers/httpUrlHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAElD;;GAEG;AACH,MAAM,OAAO,aAAa;IACzB;;;;;OAKG;IACI,MAAM,CAAC,aAAa,CAAC,GAAW;QACtC,IAAI,CAAC;YACJ,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YAClC,OAAO,YAAY,CAAC,MAAM,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;IACX,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,WAAW,CAAC,GAAW;QACpC,IAAI,CAAC;YACJ,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YAClC,OAAO,YAAY,CAAC,QAAQ,CAAC;QAC9B,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;IACX,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC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E,CAAC;YAClD,OAAO,SAAS,CAAC;QAClB,CAAC;QAED,IAAI,CAAC;YACJ,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YAC/B,OAAO,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,SAAS,CAAC;QACrD,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QAEV,OAAO,SAAS,CAAC;IAClB,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is, StringHelper } from \"@twin.org/core\";\n\n/**\n * Class to help with handling http URLs.\n */\nexport class HttpUrlHelper {\n\t/**\n\t * Extract the origin from the url which includes protocol,host,port.\n\t * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/origin\n\t * @param url The url to extract the origin from.\n\t * @returns The extracted origin.\n\t */\n\tpublic static extractOrigin(url: string): string | undefined {\n\t\ttry {\n\t\t\tconst convertedUrl = new URL(url);\n\t\t\treturn convertedUrl.origin;\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Extract the path from the url.\n\t * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname\n\t * @param url The url to extract the path from.\n\t * @returns The extracted path.\n\t */\n\tpublic static extractPath(url: string): string | undefined {\n\t\ttry {\n\t\t\tconst convertedUrl = new URL(url);\n\t\t\treturn convertedUrl.pathname;\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Extract the search from the url.\n\t * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/search\n\t * @param url The url to extract the search from.\n\t * @returns The extracted search.\n\t */\n\tpublic static extractSearch(url: string): string | undefined {\n\t\ttry {\n\t\t\tconst convertedUrl = new URL(url);\n\t\t\treturn convertedUrl.search;\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Extract the path and search from the url.\n\t * @param url The url to extract the path and search from.\n\t * @returns The extracted path and search.\n\t */\n\tpublic static extractPathAndSearch(url: string): string | undefined {\n\t\ttry {\n\t\t\tconst convertedUrl = new URL(url);\n\t\t\treturn `${convertedUrl.pathname}${convertedUrl.search}`;\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Combine the urls parts.\n\t * @param origin The origin to combine.\n\t * @param pathAndSearch The path and search to combine.\n\t * @returns The combined parts.\n\t */\n\tpublic static combineParts(origin: string, pathAndSearch: string): string | undefined {\n\t\tif (Is.string(origin) && Is.string(pathAndSearch)) {\n\t\t\treturn `${StringHelper.trimTrailingSlashes(origin)}/${StringHelper.trimLeadingSlashes(pathAndSearch)}`;\n\t\t}\n\t}\n\n\t/**\n\t * Encode a single URL path segment per RFC 3986 §3.3.\n\t * Unlike encodeURIComponent, sub-delimiters ($ & + , ; =) and the colon and\n\t * at-sign characters that are valid unencoded in path segments are preserved.\n\t * @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.3\n\t * @param segment The raw path segment value to encode.\n\t * @returns The percent-encoded path segment.\n\t */\n\tpublic static encodeUriPathSegment(segment: string): string {\n\t\t// RFC 3986 §3.3: only encode characters outside the allowed path segment set.\n\t\t// Allowed: unreserved (A-Za-z0-9 - . _ ~), sub-delimiters (! $ & ' ( ) * + , ; =), and : @\n\t\treturn segment.replace(/[^\\w!$&'()*+,.:;=@~-]/g, ch => encodeURIComponent(ch));\n\t}\n\n\t/**\n\t * Replace the origin in the url.\n\t * @param url The url to replace the origin in.\n\t * @param newOrigin The new origin to use.\n\t * @returns The url with the replaced origin.\n\t */\n\tpublic static replaceOrigin(url: string, newOrigin?: string): string {\n\t\tif (!Is.stringValue(url) || !Is.stringValue(newOrigin) || !newOrigin.startsWith(\"http\")) {\n\t\t\treturn url;\n\t\t}\n\n\t\ttry {\n\t\t\tconst parsedUrl = new URL(url.startsWith(\"/\") ? `http://placeholder${url}` : url);\n\t\t\tconst newParsedUrl = new URL(newOrigin);\n\t\t\tparsedUrl.protocol = newParsedUrl.protocol;\n\t\t\tparsedUrl.host = newParsedUrl.host;\n\t\t\tparsedUrl.port = newParsedUrl.port;\n\t\t\treturn parsedUrl.toString();\n\t\t} catch {}\n\n\t\treturn url;\n\t}\n\n\t/**\n\t * Add a query string parameter to the url.\n\t * @param url The url to add the query string parameter to.\n\t * @param key The key of the query string parameter.\n\t * @param value The value of the query string parameter.\n\t * @returns The url with the added query string parameter.\n\t */\n\tpublic static addQueryStringParam(url: string, key: string, value: string): string {\n\t\tif (!Is.stringValue(url) || !Is.stringValue(key) || !Is.stringValue(value)) {\n\t\t\treturn url;\n\t\t}\n\n\t\ttry {\n\t\t\tconst parsedUrl = new URL(url);\n\t\t\tparsedUrl.searchParams.set(key, value);\n\t\t\treturn parsedUrl.toString();\n\t\t} catch {}\n\n\t\treturn url;\n\t}\n\n\t/**\n\t * Get a query string parameter from the url.\n\t * @param url The url to get the query string parameter from.\n\t * @param key The key of the query string parameter.\n\t * @returns The value of the query string parameter.\n\t */\n\tpublic static getQueryStringParam(url: string, key: string): string | undefined {\n\t\tif (!Is.stringValue(url) || !Is.stringValue(key)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\ttry {\n\t\t\tconst parsedUrl = new URL(url);\n\t\t\treturn parsedUrl.searchParams.get(key) ?? undefined;\n\t\t} catch {}\n\n\t\treturn undefined;\n\t}\n}\n"]}
1
+ {"version":3,"file":"httpUrlHelper.js","sourceRoot":"","sources":["../../../src/helpers/httpUrlHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,EAAE,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAElD;;GAEG;AACH,MAAM,OAAO,aAAa;IACzB;;;;;OAKG;IACI,MAAM,CAAC,aAAa,CAAC,GAAW;QACtC,IAAI,CAAC;YACJ,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YAClC,OAAO,YAAY,CAAC,MAAM,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;IACX,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,WAAW,CAAC,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AAC,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC;YAC5E,OAAO,GAAG,CAAC;QACZ,CAAC;QAED,IAAI,CAAC;YACJ,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YAC/B,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACvC,OAAO,SAAS,CAAC,QAAQ,EAAE,CAAC;QAC7B,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QAEV,OAAO,GAAG,CAAC;IACZ,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,mBAAmB,CAAC,GAAW,EAAE,GAAW;QACzD,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC;YAClD,OAAO,SAAS,CAAC;QAClB,CAAC;QAED,IAAI,CAAC;YACJ,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YAC/B,OAAO,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,SAAS,CAAC;QACrD,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QAEV,OAAO,SAAS,CAAC;IAClB,CAAC;CACD","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { Is, StringHelper } from \"@twin.org/core\";\n\n/**\n * Class to help with handling http URLs.\n */\nexport class HttpUrlHelper {\n\t/**\n\t * Extract the origin from the url which includes protocol,host,port.\n\t * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/origin\n\t * @param url The url to extract the origin from.\n\t * @returns The extracted origin.\n\t */\n\tpublic static extractOrigin(url: string): string | undefined {\n\t\ttry {\n\t\t\tconst convertedUrl = new URL(url);\n\t\t\treturn convertedUrl.origin;\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Extract the path from the url.\n\t * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname\n\t * @param url The url to extract the path from.\n\t * @returns The extracted path.\n\t */\n\tpublic static extractPath(url: string): string | undefined {\n\t\ttry {\n\t\t\tconst convertedUrl = new URL(url);\n\t\t\treturn convertedUrl.pathname;\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Extract the search from the url.\n\t * @see https://developer.mozilla.org/en-US/docs/Web/API/URL/search\n\t * @param url The url to extract the search from.\n\t * @returns The extracted search.\n\t */\n\tpublic static extractSearch(url: string): string | undefined {\n\t\ttry {\n\t\t\tconst convertedUrl = new URL(url);\n\t\t\treturn convertedUrl.search;\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Extract the path and search from the url.\n\t * @param url The url to extract the path and search from.\n\t * @returns The extracted path and search.\n\t */\n\tpublic static extractPathAndSearch(url: string): string | undefined {\n\t\ttry {\n\t\t\tconst convertedUrl = new URL(url);\n\t\t\treturn `${convertedUrl.pathname}${convertedUrl.search}`;\n\t\t} catch {}\n\t}\n\n\t/**\n\t * Combine an optional origin and an optional path into a single URL string.\n\t * Relative paths (no protocol, no leading slash) have a leading slash prepended.\n\t * Trailing slashes are trimmed from the origin before joining.\n\t * Returns undefined when both arguments are absent or empty.\n\t * @param origin The optional origin (e.g. \"https://example.com\").\n\t * @param path The optional path or URL template (e.g. \"/api/items\" or \"api/items\").\n\t * @returns The combined string, or undefined when both are absent.\n\t */\n\tpublic static combineOriginPath(origin?: string, path?: string): string | undefined {\n\t\tconst normalizedPath =\n\t\t\tIs.stringValue(path) && !path.includes(\"://\")\n\t\t\t\t? `/${StringHelper.trimLeadingSlashes(path)}`\n\t\t\t\t: path;\n\n\t\tif (Is.stringValue(origin) && Is.stringValue(normalizedPath)) {\n\t\t\treturn `${StringHelper.trimTrailingSlashes(origin)}${normalizedPath}`;\n\t\t} else if (Is.stringValue(origin)) {\n\t\t\treturn origin;\n\t\t} else if (Is.stringValue(normalizedPath)) {\n\t\t\treturn normalizedPath;\n\t\t}\n\t}\n\n\t/**\n\t * Encode a single URL path segment per RFC 3986 §3.3.\n\t * Unlike encodeURIComponent, sub-delimiters ($ & + , ; =) and the colon and\n\t * at-sign characters that are valid unencoded in path segments are preserved.\n\t * @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.3\n\t * @param segment The raw path segment value to encode.\n\t * @returns The percent-encoded path segment.\n\t */\n\tpublic static encodeUriPathSegment(segment: string): string {\n\t\t// RFC 3986 §3.3: only encode characters outside the allowed path segment set.\n\t\t// Allowed: unreserved (A-Za-z0-9 - . _ ~), sub-delimiters (! $ & ' ( ) * + , ; =), and : @\n\t\treturn segment.replace(/[^\\w!$&'()*+,.:;=@~-]/g, ch => encodeURIComponent(ch));\n\t}\n\n\t/**\n\t * Replace the origin in the url.\n\t * @param url The url to replace the origin in.\n\t * @param newOrigin The new origin to use.\n\t * @returns The url with the replaced origin.\n\t */\n\tpublic static replaceOrigin(url: string, newOrigin?: string): string {\n\t\tif (!Is.stringValue(url) || !Is.stringValue(newOrigin) || !newOrigin.startsWith(\"http\")) {\n\t\t\treturn url;\n\t\t}\n\n\t\ttry {\n\t\t\tconst parsedUrl = new URL(url.startsWith(\"/\") ? `http://placeholder${url}` : url);\n\t\t\tconst newParsedUrl = new URL(newOrigin);\n\t\t\tparsedUrl.protocol = newParsedUrl.protocol;\n\t\t\tparsedUrl.host = newParsedUrl.host;\n\t\t\tparsedUrl.port = newParsedUrl.port;\n\t\t\treturn parsedUrl.toString();\n\t\t} catch {}\n\n\t\treturn url;\n\t}\n\n\t/**\n\t * Add a query string parameter to the url.\n\t * @param url The url to add the query string parameter to.\n\t * @param key The key of the query string parameter.\n\t * @param value The value of the query string parameter.\n\t * @returns The url with the added query string parameter.\n\t */\n\tpublic static addQueryStringParam(url: string, key: string, value: string): string {\n\t\tif (!Is.stringValue(url) || !Is.stringValue(key) || !Is.stringValue(value)) {\n\t\t\treturn url;\n\t\t}\n\n\t\ttry {\n\t\t\tconst parsedUrl = new URL(url);\n\t\t\tparsedUrl.searchParams.set(key, value);\n\t\t\treturn parsedUrl.toString();\n\t\t} catch {}\n\n\t\treturn url;\n\t}\n\n\t/**\n\t * Get a query string parameter from the url.\n\t * @param url The url to get the query string parameter from.\n\t * @param key The key of the query string parameter.\n\t * @returns The value of the query string parameter.\n\t */\n\tpublic static getQueryStringParam(url: string, key: string): string | undefined {\n\t\tif (!Is.stringValue(url) || !Is.stringValue(key)) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\ttry {\n\t\t\tconst parsedUrl = new URL(url);\n\t\t\treturn parsedUrl.searchParams.get(key) ?? undefined;\n\t\t} catch {}\n\n\t\treturn undefined;\n\t}\n}\n"]}
@@ -18,16 +18,17 @@ export declare class HttpHeaderHelper {
18
18
  [HeaderTypes.Link]: string | undefined;
19
19
  };
20
20
  /**
21
- * Set the Location header to the encoded ID, optionally placed within a base URL or template.
22
- * When baseUrl contains a colon-prefixed placeholder (e.g. "/path1/:id/path2") the
21
+ * Set the Location header to the encoded ID, optionally placed within a URL template.
22
+ * When the template contains `:id` (e.g. "/path1/:id/path2" or "/path?p=:id") the
23
23
  * encoded ID is substituted at that position; otherwise it is appended.
24
- * The base URL may be an absolute URL or a relative path. When omitted the bare
25
- * encoded ID is used.
24
+ * When no template is provided the bare encoded ID is used.
25
+ * Callers that need to combine a public origin with a path should use
26
+ * HttpUrlHelper.combineOriginPath to build the template before calling this method.
26
27
  * @param headers The response headers to mutate.
27
28
  * @param id The resource ID to encode and place.
28
- * @param baseUrl The optional base URL, relative path, or URL template.
29
+ * @param urlTemplate The optional URL template (absolute or relative).
29
30
  */
30
- static buildId(headers: IHttpHeaders, id: string, baseUrl?: string): asserts headers is IHttpHeaders & {
31
+ static buildId(headers: IHttpHeaders, id: string, urlTemplate?: string): asserts headers is IHttpHeaders & {
31
32
  [HeaderTypes.Location]: string;
32
33
  };
33
34
  /**
@@ -48,15 +49,14 @@ export declare class HttpHeaderHelper {
48
49
  static extractCursor(headers?: IHttpHeaders): string | undefined;
49
50
  /**
50
51
  * Extract the resource ID from the Location response header.
51
- * Handles absolute URLs (http://host/path/:id, http://host/path/:id?foo=bar),
52
- * relative paths (/segment/:id, ./segment/:id), and bare ID values.
53
- * When a URL template such as "/path1/:id/path2" is supplied the ID is extracted
54
- * from the segment position marked by the first colon-prefixed placeholder.
55
- * Without a template the last path segment is returned.
52
+ * Handles absolute URLs, relative paths, and bare ID values.
53
+ * When a templateUrl containing ':id' is supplied (e.g. "/path1/:id/path2" or
54
+ * "https://host/path/:id") the ID is extracted via pattern matching at the ':id'
55
+ * position. Without a matching template the last path segment is returned.
56
56
  * @param headers The response headers containing the Location header.
57
- * @param template Optional URL template with a colon-prefixed placeholder marking the ID position, e.g. "/path1/:id/path2".
57
+ * @param templateUrl Optional URL template containing the ':id' placeholder, e.g. "/path1/:id/path2".
58
58
  * @returns The extracted ID string.
59
- * @throws GeneralError If the Location header is missing, the template has no placeholder, or the ID cannot be extracted.
59
+ * @throws GeneralError If the Location header is missing or the ID cannot be extracted.
60
60
  */
61
- static extractId(headers?: IHttpHeaders, template?: string): string;
61
+ static extractId(headers?: IHttpHeaders, templateUrl?: string): string;
62
62
  }
@@ -30,12 +30,15 @@ export declare class HttpUrlHelper {
30
30
  */
31
31
  static extractPathAndSearch(url: string): string | undefined;
32
32
  /**
33
- * Combine the urls parts.
34
- * @param origin The origin to combine.
35
- * @param pathAndSearch The path and search to combine.
36
- * @returns The combined parts.
33
+ * Combine an optional origin and an optional path into a single URL string.
34
+ * Relative paths (no protocol, no leading slash) have a leading slash prepended.
35
+ * Trailing slashes are trimmed from the origin before joining.
36
+ * Returns undefined when both arguments are absent or empty.
37
+ * @param origin The optional origin (e.g. "https://example.com").
38
+ * @param path The optional path or URL template (e.g. "/api/items" or "api/items").
39
+ * @returns The combined string, or undefined when both are absent.
37
40
  */
38
- static combineParts(origin: string, pathAndSearch: string): string | undefined;
41
+ static combineOriginPath(origin?: string, path?: string): string | undefined;
39
42
  /**
40
43
  * Encode a single URL path segment per RFC 3986 §3.3.
41
44
  * Unlike encodeURIComponent, sub-delimiters ($ & + , ; =) and the colon and
package/docs/changelog.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.9.1-next.4](https://github.com/iotaledger/twin-api/compare/api-models-v0.9.1-next.3...api-models-v0.9.1-next.4) (2026-06-30)
4
+
5
+
6
+ ### Features
7
+
8
+ * improved rest handling ([#211](https://github.com/iotaledger/twin-api/issues/211)) ([12c50ea](https://github.com/iotaledger/twin-api/commit/12c50ea7655276eed7a73d311ac5286476c98201))
9
+
3
10
  ## [0.9.1-next.3](https://github.com/iotaledger/twin-api/compare/api-models-v0.9.1-next.2...api-models-v0.9.1-next.3) (2026-06-29)
4
11
 
5
12
 
@@ -62,13 +62,14 @@ asserts headers is IHttpHeaders & \{ link: string \| undefined \}
62
62
 
63
63
  ### buildId() {#buildid}
64
64
 
65
- > `static` **buildId**(`headers`, `id`, `baseUrl?`): `asserts headers is IHttpHeaders & { location: string }`
65
+ > `static` **buildId**(`headers`, `id`, `urlTemplate?`): `asserts headers is IHttpHeaders & { location: string }`
66
66
 
67
- Set the Location header to the encoded ID, optionally placed within a base URL or template.
68
- When baseUrl contains a colon-prefixed placeholder (e.g. "/path1/:id/path2") the
67
+ Set the Location header to the encoded ID, optionally placed within a URL template.
68
+ When the template contains `:id` (e.g. "/path1/:id/path2" or "/path?p=:id") the
69
69
  encoded ID is substituted at that position; otherwise it is appended.
70
- The base URL may be an absolute URL or a relative path. When omitted the bare
71
- encoded ID is used.
70
+ When no template is provided the bare encoded ID is used.
71
+ Callers that need to combine a public origin with a path should use
72
+ HttpUrlHelper.combineOriginPath to build the template before calling this method.
72
73
 
73
74
  #### Parameters
74
75
 
@@ -84,11 +85,11 @@ The response headers to mutate.
84
85
 
85
86
  The resource ID to encode and place.
86
87
 
87
- ##### baseUrl?
88
+ ##### urlTemplate?
88
89
 
89
90
  `string`
90
91
 
91
- The optional base URL, relative path, or URL template.
92
+ The optional URL template (absolute or relative).
92
93
 
93
94
  #### Returns
94
95
 
@@ -148,14 +149,13 @@ The cursor value or undefined if not present.
148
149
 
149
150
  ### extractId() {#extractid}
150
151
 
151
- > `static` **extractId**(`headers?`, `template?`): `string`
152
+ > `static` **extractId**(`headers?`, `templateUrl?`): `string`
152
153
 
153
154
  Extract the resource ID from the Location response header.
154
- Handles absolute URLs (http://host/path/:id, http://host/path/:id?foo=bar),
155
- relative paths (/segment/:id, ./segment/:id), and bare ID values.
156
- When a URL template such as "/path1/:id/path2" is supplied the ID is extracted
157
- from the segment position marked by the first colon-prefixed placeholder.
158
- Without a template the last path segment is returned.
155
+ Handles absolute URLs, relative paths, and bare ID values.
156
+ When a templateUrl containing ':id' is supplied (e.g. "/path1/:id/path2" or
157
+ "https://host/path/:id") the ID is extracted via pattern matching at the ':id'
158
+ position. Without a matching template the last path segment is returned.
159
159
 
160
160
  #### Parameters
161
161
 
@@ -165,11 +165,11 @@ Without a template the last path segment is returned.
165
165
 
166
166
  The response headers containing the Location header.
167
167
 
168
- ##### template?
168
+ ##### templateUrl?
169
169
 
170
170
  `string`
171
171
 
172
- Optional URL template with a colon-prefixed placeholder marking the ID position, e.g. "/path1/:id/path2".
172
+ Optional URL template containing the ':id' placeholder, e.g. "/path1/:id/path2".
173
173
 
174
174
  #### Returns
175
175
 
@@ -179,4 +179,4 @@ The extracted ID string.
179
179
 
180
180
  #### Throws
181
181
 
182
- GeneralError If the Location header is missing, the template has no placeholder, or the ID cannot be extracted.
182
+ GeneralError If the Location header is missing or the ID cannot be extracted.
@@ -114,31 +114,34 @@ The extracted path and search.
114
114
 
115
115
  ***
116
116
 
117
- ### combineParts() {#combineparts}
117
+ ### combineOriginPath() {#combineoriginpath}
118
118
 
119
- > `static` **combineParts**(`origin`, `pathAndSearch`): `string` \| `undefined`
119
+ > `static` **combineOriginPath**(`origin?`, `path?`): `string` \| `undefined`
120
120
 
121
- Combine the urls parts.
121
+ Combine an optional origin and an optional path into a single URL string.
122
+ Relative paths (no protocol, no leading slash) have a leading slash prepended.
123
+ Trailing slashes are trimmed from the origin before joining.
124
+ Returns undefined when both arguments are absent or empty.
122
125
 
123
126
  #### Parameters
124
127
 
125
- ##### origin
128
+ ##### origin?
126
129
 
127
130
  `string`
128
131
 
129
- The origin to combine.
132
+ The optional origin (e.g. "https://example.com").
130
133
 
131
- ##### pathAndSearch
134
+ ##### path?
132
135
 
133
136
  `string`
134
137
 
135
- The path and search to combine.
138
+ The optional path or URL template (e.g. "/api/items" or "api/items").
136
139
 
137
140
  #### Returns
138
141
 
139
142
  `string` \| `undefined`
140
143
 
141
- The combined parts.
144
+ The combined string, or undefined when both are absent.
142
145
 
143
146
  ***
144
147
 
package/locales/en.json CHANGED
@@ -2,8 +2,7 @@
2
2
  "error": {
3
3
  "httpHeaderHelper": {
4
4
  "locationMissing": "The Location response header is missing or empty",
5
- "idNotFound": "The resource ID could not be extracted from the Location header",
6
- "templateNoPlaceholder": "The URL template contains no colon-prefixed placeholder segment"
5
+ "idNotFound": "The resource ID could not be extracted from the Location header"
7
6
  }
8
7
  }
9
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/api-models",
3
- "version": "0.9.1-next.3",
3
+ "version": "0.9.1-next.4",
4
4
  "description": "Shared API contracts, route types, and response models used across services and clients.",
5
5
  "repository": {
6
6
  "type": "git",