@twin.org/api-models 0.9.1-next.4 → 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.
|
@@ -101,7 +101,7 @@ export class HttpHeaderHelper {
|
|
|
101
101
|
const withoutQuery = location.split("?")[0];
|
|
102
102
|
if (Is.stringValue(templateUrl) && templateUrl.includes(":id")) {
|
|
103
103
|
const escaped = templateUrl.replace(/[.+^${}()|[\]\\?]/g, "\\$&");
|
|
104
|
-
const pattern = new RegExp(escaped.replace(":id"
|
|
104
|
+
const pattern = new RegExp(escaped.replace(/:[a-zA-Z][a-zA-Z0-9]*/g, m => (m === ":id" ? "([^/?#]+)" : "[^/?#]+")));
|
|
105
105
|
const match = pattern.exec(location);
|
|
106
106
|
if (!Is.stringValue(match?.[1])) {
|
|
107
107
|
throw new GeneralError(HttpHeaderHelper.CLASS_NAME, "idNotFound");
|
|
@@ -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,gBAA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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"]}
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
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
|
+
|
|
3
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)
|
|
4
11
|
|
|
5
12
|
|