@twin.org/api-models 0.9.1-next.3 → 0.9.1-next.5
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/es/helpers/httpHeaderHelper.js +25 -33
- package/dist/es/helpers/httpHeaderHelper.js.map +1 -1
- package/dist/es/helpers/httpUrlHelper.js +19 -7
- package/dist/es/helpers/httpUrlHelper.js.map +1 -1
- package/dist/types/helpers/httpHeaderHelper.d.ts +14 -14
- package/dist/types/helpers/httpUrlHelper.d.ts +8 -5
- package/docs/changelog.md +14 -0
- package/docs/reference/classes/HttpHeaderHelper.md +16 -16
- package/docs/reference/classes/HttpUrlHelper.md +11 -8
- package/locales/en.json +1 -2
- package/package.json +1 -1
|
@@ -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
|
|
28
|
-
* When
|
|
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
|
-
*
|
|
31
|
-
*
|
|
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
|
|
35
|
+
* @param urlTemplate The optional URL template (absolute or relative).
|
|
35
36
|
*/
|
|
36
|
-
static buildId(headers, id,
|
|
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(
|
|
40
|
-
if (
|
|
41
|
-
headers[HeaderTypes.Location] =
|
|
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] =
|
|
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
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
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
|
|
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
|
|
94
|
+
* @throws GeneralError If the Location header is missing or the ID cannot be extracted.
|
|
94
95
|
*/
|
|
95
|
-
static extractId(headers,
|
|
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(
|
|
102
|
-
const
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
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(/:[a-zA-Z][a-zA-Z0-9]*/g, m => (m === ":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(
|
|
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;;;;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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(\n\t\t\t\tescaped.replace(/:[a-zA-Z][a-zA-Z0-9]*/g, m => (m === \":id\" ? \"([^/?#]+)\" : \"[^/?#]+\"))\n\t\t\t);\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
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
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
|
|
66
|
-
|
|
67
|
-
|
|
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,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;;;;OAIG;IACI,MAAM,CAAC,oBAAoB,CAAC,GAAW;QAC7C,IAAI,CAAC;YACJ,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;YAClC,OAAO,GAAG,YAAY,CAAC,QAAQ,GA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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
|
|
22
|
-
* When
|
|
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
|
-
*
|
|
25
|
-
*
|
|
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
|
|
29
|
+
* @param urlTemplate The optional URL template (absolute or relative).
|
|
29
30
|
*/
|
|
30
|
-
static buildId(headers: IHttpHeaders, id: string,
|
|
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
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
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
|
|
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
|
|
59
|
+
* @throws GeneralError If the Location header is missing or the ID cannot be extracted.
|
|
60
60
|
*/
|
|
61
|
-
static extractId(headers?: IHttpHeaders,
|
|
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
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
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
|
|
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,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.9.1-next.5](https://github.com/iotaledger/twin-api/compare/api-models-v0.9.1-next.4...api-models-v0.9.1-next.5) (2026-06-30)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* allow dynamic segments in extractId ([2377c98](https://github.com/iotaledger/twin-api/commit/2377c9846f85b1cc05869c4d73fbf5de26e14f44))
|
|
9
|
+
|
|
10
|
+
## [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)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* improved rest handling ([#211](https://github.com/iotaledger/twin-api/issues/211)) ([12c50ea](https://github.com/iotaledger/twin-api/commit/12c50ea7655276eed7a73d311ac5286476c98201))
|
|
16
|
+
|
|
3
17
|
## [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
18
|
|
|
5
19
|
|
|
@@ -62,13 +62,14 @@ asserts headers is IHttpHeaders & \{ link: string \| undefined \}
|
|
|
62
62
|
|
|
63
63
|
### buildId() {#buildid}
|
|
64
64
|
|
|
65
|
-
> `static` **buildId**(`headers`, `id`, `
|
|
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
|
|
68
|
-
When
|
|
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
|
-
|
|
71
|
-
|
|
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
|
-
#####
|
|
88
|
+
##### urlTemplate?
|
|
88
89
|
|
|
89
90
|
`string`
|
|
90
91
|
|
|
91
|
-
The optional
|
|
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?`, `
|
|
152
|
+
> `static` **extractId**(`headers?`, `templateUrl?`): `string`
|
|
152
153
|
|
|
153
154
|
Extract the resource ID from the Location response header.
|
|
154
|
-
Handles absolute URLs
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
#####
|
|
168
|
+
##### templateUrl?
|
|
169
169
|
|
|
170
170
|
`string`
|
|
171
171
|
|
|
172
|
-
Optional URL template
|
|
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
|
|
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
|
-
###
|
|
117
|
+
### combineOriginPath() {#combineoriginpath}
|
|
118
118
|
|
|
119
|
-
> `static` **
|
|
119
|
+
> `static` **combineOriginPath**(`origin?`, `path?`): `string` \| `undefined`
|
|
120
120
|
|
|
121
|
-
Combine
|
|
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
|
|
132
|
+
The optional origin (e.g. "https://example.com").
|
|
130
133
|
|
|
131
|
-
#####
|
|
134
|
+
##### path?
|
|
132
135
|
|
|
133
136
|
`string`
|
|
134
137
|
|
|
135
|
-
The path
|
|
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
|
|
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
|
}
|