From d8efd72c21559c497e8b16ee93d1aa32b5879afa Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 1 Mar 2023 23:34:36 -0500 Subject: [PATCH] fix(striker-ui-api): extract link from UPS type description --- .../request_handlers/ups/getUPSTemplate.ts | 23 ++++++++++++++++--- striker-ui-api/src/types/APIUPS.d.ts | 11 +++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/striker-ui-api/src/lib/request_handlers/ups/getUPSTemplate.ts b/striker-ui-api/src/lib/request_handlers/ups/getUPSTemplate.ts index ea014049..3c4a8b9c 100644 --- a/striker-ui-api/src/lib/request_handlers/ups/getUPSTemplate.ts +++ b/striker-ui-api/src/lib/request_handlers/ups/getUPSTemplate.ts @@ -21,11 +21,28 @@ export const getUPSTemplate: RequestHandler = (request, response) => { const upsData: AnvilDataUPSHash = Object.entries( rawUPSData, - ).reduce((previous, [upsTypeId, value]) => { - const { brand } = value; + ).reduce((previous, [upsTypeId, value]) => { + const { brand, description: rawDescription, ...rest } = value; + + const matched = rawDescription.match( + /^(.+)\s+[-]\s+[<][^>]+href=[\\"]+([^\s]+)[\\"]+.+[>]([^<]+)[<]/, + ); + const result: UPSTemplate[string] = { + ...rest, + brand, + description: rawDescription, + links: {}, + }; + + if (matched) { + const [, description, linkHref, linkLabel] = matched; + + result.description = description; + result.links[0] = { linkHref, linkLabel }; + } if (/apc/i.test(brand)) { - previous[upsTypeId] = value; + previous[upsTypeId] = result; } return previous; diff --git a/striker-ui-api/src/types/APIUPS.d.ts b/striker-ui-api/src/types/APIUPS.d.ts index 4eb25534..5199e348 100644 --- a/striker-ui-api/src/types/APIUPS.d.ts +++ b/striker-ui-api/src/types/APIUPS.d.ts @@ -4,3 +4,14 @@ type UPSOverview = { upsName: string; upsUUID: string; }; + +type UPSTemplate = { + [upsName: string]: AnvilDataUPSHash[string] & { + links: { + [linkId: string]: { + linkHref: string; + linkLabel: string; + }; + }; + }; +};