@companieshouse/ch-node-utils 1.2.0 → 1.3.1

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/README.md CHANGED
@@ -1,2 +1,78 @@
1
1
  # ch-node-utils
2
- set of node utils which can be used across repos/projects
2
+
3
+ set of node utils which can be used across repos/projects.
4
+ Available as a normal npm dependency([here on npmjs](https://www.npmjs.com/package/@companieshouse/ch-node-utils))
5
+
6
+ ## Areas for the different utilities stored in this repo:
7
+
8
+ ### i18n (Internationalisation)
9
+
10
+ Internationalisation has been added to CHS, after already being available (at least for Welsh) on legacy systems (EWF/SCRS/CHD/...)
11
+
12
+ The implementation is described in [Confluence](https://companieshouse.atlassian.net/wiki/spaces/OLI/pages/4235231301/Implementing+Internationalisation+on+CH+web+services) for both Java and Node.js services.
13
+
14
+ Some common utilities for Node.js are here provided. The chosen Node.js i18n library is [i18next](https://www.i18next.com/), which, at the time of writing, can be considered the standard:
15
+
16
+ ![Node.js i18n libs](https://github.com/companieshouse/ch-node-utils/blob/81a066b38086e7040a089882bd5e750a11393c2a/images/Node.js__i18n.libraries.png)
17
+
18
+ ![Node.js i18n libs trend](https://github.com/companieshouse/ch-node-utils/blob/81a066b38086e7040a089882bd5e750a11393c2a/images/Node.js__i18n.libraries.trend.png)
19
+
20
+ - `i18nCh.ts`: wrapper to [i18next](https://www.i18next.com/)
21
+
22
+ - `languageNames.ts`: to manage Language names and their associated ISO codes (iso-639). It mainly provides:
23
+ - a function to get, from an array of submitted iso-codes, a structured array of iso-codes & native names.
24
+
25
+ Ex:
26
+ ```
27
+ [... "en", "cy", ...] ==> [ ... {"IsoCode": "en", "Name": "English" }, {"IsoCode": "cy", "Name": "Cymraeg" } ...]
28
+ ```
29
+ - a custom function to sort the supported locales (apart from leaving 'en' always at 1st position, it sorts alphabetically)
30
+
31
+ - `subDirs.ts`: to manage files and dirs, so that i18n locales' folders can be sourced automatically.
32
+ This allows for example to have an array of iso-codes if the `./locales` dir follows the normal convention.
33
+ Ex, this array of iso-codes `[ "ar", "bn", "cy", "de", "en", "gd", "ja", "uk"]` is retrieved from the following tree:
34
+ ```
35
+ │   ├── locales
36
+ │   │   ├── ar
37
+ │   │   │   ├── errors.json
38
+ │   │   │   ├── global.json
39
+ │   │   │   ├── landing.json
40
+ │   │   │   ├── layout.json
41
+ │   │   │   ├── search-company.json
42
+ │   │   │   ├── view-company-information.json
43
+ │   │   │   └── who-to-tell.json
44
+ │   │   ├── bn
45
+ │   │   │   └── *.json
46
+ │   │   ├── cy
47
+ │   │   │   └── *.json
48
+ │   │   ├── de
49
+ │   │   │   └── *.json
50
+ │   │   ├── en
51
+ │   │   │   └── *.json
52
+ │   │   ├── gd
53
+ │   │   │   └── *.json
54
+ │   │   ├── ja
55
+ │   │   │   └── *.json
56
+ │   │   ├── uk
57
+ │   │   │   └── *.json
58
+ ```
59
+
60
+ - `manageLocales.middleware.ts`: middleware that can be reused to manage locales while dealing with http requests.
61
+
62
+ - `add-lang-to-url.njk`: to add the `lang=xx` query param to urls:
63
+ ```
64
+ https://.....<self>....../?lang=cy
65
+ ```
66
+
67
+ ![Auto add lang=xx](https://github.com/companieshouse/ch-node-utils/blob/81a066b38086e7040a089882bd5e750a11393c2a/images/Node.js__i18n.add.lang.png)
68
+
69
+ - `locales-banner.njk`: to add the locales banner
70
+
71
+ ![locales banner](https://github.com/companieshouse/ch-node-utils/blob/81a066b38086e7040a089882bd5e750a11393c2a/images/Node.js__i18n.locales.banner.png)
72
+
73
+ - `ENV VARS`: the following ENV vars are used:
74
+
75
+ | ENV VAR | Description |
76
+ | ------- | ----------- |
77
+ |`CH_NODE_UTILS_DROP_LANG_QUERY_PARAM`| It could be set to [drop the lang="xx" query param](https://github.com/companieshouse/ch-node-utils/blob/f9e5c47a86206f0b12e4e536c4c459db16747631/src/middleware/manageLocales.middleware.ts#L25) from the current URL ([see Example](https://github.com/companieshouse/docker-chs-development/blob/842c61245adcbba02a6316847fc4f9d94c52410d/services/modules/dissolution/dissolution-web.docker-compose.yaml#L50)) |
78
+ |`CH_NODE_UTILS_LOG_LVL`| It could be set to ["TRACE" or "DEBUG" (case insensitive)](https://github.com/companieshouse/ch-node-utils/blob/24bc717477d21082439d1b460108cb0d60465f0f/src/utils/log.ts#L2) to dump internal info while inside ch-node-utils ([see Example](https://github.com/companieshouse/docker-chs-development/blob/842c61245adcbba02a6316847fc4f9d94c52410d/services/modules/dissolution/dissolution-web.docker-compose.yaml#L49))|
@@ -22,10 +22,15 @@ function LocalesMiddleware() {
22
22
  req.session.setExtraData(constants_1.QUERY_PAR_LANG, lang);
23
23
  }
24
24
  req.lang = lang;
25
- const [pathWithoutQuery] = req.url.split("?");
26
- req.url = pathWithoutQuery;
27
- const currentUrl = `${req.protocol}://${req.get("host")}${pathWithoutQuery}`;
28
- res.locals.currentUrl = currentUrl;
25
+ let currentUrl = req.url;
26
+ if (process.env.CH_NODE_UTILS_DROP_LANG_QUERY_PARAM) {
27
+ let cUrl = new URL(`http://a${req.url}`);
28
+ cUrl.searchParams.delete(constants_1.QUERY_PAR_LANG);
29
+ currentUrl = `${cUrl.pathname}${cUrl.search}`;
30
+ (0, log_1.default)(`LocalesMiddleware: removing '${constants_1.QUERY_PAR_LANG}'= query param (req.url=${currentUrl})`);
31
+ }
32
+ req.url = currentUrl;
33
+ res.locals.currentUrl = `${req.protocol}://${req.get("host")}${currentUrl}`;
29
34
  res.locals.htmlLang = lang;
30
35
  res.locals.pageTitleLang = lang;
31
36
  res.locals.mainLang = lang;
@@ -1 +1 @@
1
- {"version":3,"file":"manageLocales.middleware.js","sourceRoot":"","sources":["../../src/middleware/manageLocales.middleware.ts"],"names":[],"mappings":";;;;AACA,sDAAuD;AACvD,kGAAiE;AACjE,mFAAoD;AACpD,+DAA+B;AAE/B,SAAgB,iBAAiB;IAC9B,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;;QAExD,IAAA,aAAG,EAAC,2CAA2C,CAAC,CAAA;QA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BC"}
1
+ {"version":3,"file":"manageLocales.middleware.js","sourceRoot":"","sources":["../../src/middleware/manageLocales.middleware.ts"],"names":[],"mappings":";;;;AACA,sDAAuD;AACvD,kGAAiE;AACjE,mFAAoD;AACpD,+DAA+B;AAE/B,SAAgB,iBAAiB;IAC9B,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;;QAExD,IAAA,aAAG,EAAC,2CAA2C,CAAC,CAAA;QAChD,IAAI,IAAI,GAAuB,CAAS,GAAG,CAAC,KAAK,CAAC,0BAAc,CAAC;YAChC,GAAG,CAAC,IAAI,CAAC,IAAI;aACb,MAAA,GAAG,CAAC,OAAO,0CAAE,YAAY,CAAS,0BAAc,CAAC,CAAA,CAAC,CAAA;QACnF,IAAA,aAAG,EAAC,0DAA0D,IAAI,YAAY,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAA;QAC/F,IAAI,IAAI,KAAK,SAAS,IAAI,CAAE,uBAAa,CAAC,iBAAiB,CAAE,yBAAc,CAAC,WAAW,EAAE,CAAC,aAAa,EAAE,IAAI,CAAC,EAAE,CAAC;YAC3G,IAAI,GAAG,IAAI,CAAA;QACjB,CAAC;QACD,IAAA,aAAG,EAAC,uCAAuC,IAAI,GAAG,CAAC,CAAA;QACnD,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;YACf,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,0BAAc,EAAE,IAAI,CAAC,CAAA;QACjD,CAAC;QACD,GAAG,CAAC,IAAI,GAAG,IAAI,CAAA;QAEf,IAAI,UAAU,GAAG,GAAG,CAAC,GAAG,CAAA;QACxB,IAAI,OAAO,CAAC,GAAG,CAAC,mCAAmC,EAAE,CAAC;YAInD,IAAI,IAAI,GAAG,IAAI,GAAG,CAAC,WAAW,GAAG,CAAC,GAAG,EAAE,CAAC,CAAA;YACxC,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,0BAAc,CAAC,CAAA;YACxC,UAAU,GAAG,GAAG,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,MAAM,EAAE,CAAA;YAC7C,IAAA,aAAG,EAAC,gCAAgC,0BAAc,2BAA2B,UAAU,GAAG,CAAC,CAAA;QAC9F,CAAC;QAED,GAAG,CAAC,GAAG,GAAG,UAAU,CAAA;QACpB,GAAG,CAAC,MAAM,CAAC,UAAU,GAAG,GAAG,GAAG,CAAC,QAAQ,MAAM,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,UAAU,EAAE,CAAA;QAG3E,GAAG,CAAC,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAA;QAC1B,GAAG,CAAC,MAAM,CAAC,aAAa,GAAG,IAAI,CAAA;QAC/B,GAAG,CAAC,MAAM,CAAC,QAAQ,GAAG,IAAI,CAAA;QAE1B,OAAO,IAAI,EAAE,CAAA;IAChB,CAAC,CAAA;AACJ,CAAC;AAtCD,8CAsCC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@companieshouse/ch-node-utils",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "author": "companieshouse",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -36,16 +36,16 @@
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
38
  "@companieshouse/node-session-handler": "^5.0.1",
39
- "express": "^4.18.2",
40
- "i18next": "^23.4.6",
41
- "i18next-fs-backend": "^2.1.5",
42
- "iso-639-1": "^3.0.0"
39
+ "express": "^4.18.3",
40
+ "i18next": "^23.10.1",
41
+ "i18next-fs-backend": "^2.3.1",
42
+ "iso-639-1": "^3.1.2"
43
43
  },
44
44
  "devDependencies": {
45
- "@types/express": "^4.17.17",
45
+ "@types/express": "^4.17.21",
46
46
  "@types/i18next": "^13.0.0",
47
- "@types/node": "^20.5.7",
48
- "typescript": "^5.2.2"
47
+ "@types/node": "^20.11.28",
48
+ "typescript": "^5.4.2"
49
49
  },
50
50
  "bugs": {
51
51
  "url": "https://github.com/companieshouse/ch-node-utils/issues"
@@ -16,7 +16,7 @@ It receives:
16
16
  {% for l in languages %}
17
17
  <li class="govuk-language-select__list-item">
18
18
  <a href="{{lang2url.addLangToUrl(currentUrl, l.IsoCode, true)}}" lang={{l.IsoCode}} rel="alternate" class="govuk-link" >
19
- <span aria-hidden="true">{{l.Name}}</span>
19
+ <span>{{l.Name}}</span>
20
20
  </a>
21
21
  </li>
22
22
  {% endfor %}