@watchtowerobservability/activity-render 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/README.md +32 -2
- package/dist/index.cjs +217 -0
- package/dist/index.d.cts +63 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +196 -0
- package/package.json +48 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @watchtowerobservability/activity-render
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The renderer behind `display=1` of the Watchtower Activity API, client side and dependency free. Give it an event and a theme as the API returns them and it returns the same `display` object the server would, so an app can show an event the moment it creates it, before any round trip.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { render } from '@watchtowerobservability/activity-render';
|
|
7
|
+
|
|
8
|
+
const display = render(event, theme, { locale: 'de', timeZone: 'Europe/Berlin' });
|
|
9
|
+
// { title, subtitle?, fields?, failure_reason?: { code, text, action?: { label, url } } }
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`event` is an Event as `GET .../events` returns it (`action`, `outcome`, `actor`, `target`, `metadata`, ...). `theme` is the effective theme as the feed returns it: only `actions`, `fallbackTemplate`, `failureReasonPath` and `failureReasons` are read (the snake_case spellings are accepted too). Remove what the viewer must not see (metadata, IP) from the event before you call `render`: the server renders after its own hiding.
|
|
13
|
+
|
|
14
|
+
What it does, in this order: the action's template (exact key, else the longest `stem.*` key, else `fallbackTemplate`), the outcome variant (`label_failure`, `label_denied`), the locale copy (`pt-BR`, then `pt`, then the template's own), placeholders and formatters, and for a `failure` outcome the `failureReasons` entry named by the value at `failureReasonPath`.
|
|
15
|
+
|
|
16
|
+
The event's own fields are all available as placeholders, the open-event ones too: `{progress.done}` of `{progress.total}`, `{abandoned}`, `{imported}`, `{label}`, `{detail_ref.bytes|count}`, `{metadata.error_code}`. The `failure_reason` is the advice of the theme's `failureReasons` for the code at `failureReasonPath` (for example `metadata.error_code`), rendered with the same placeholders and locales, `fallbackTemplate` reads a new action as a sentence (`{action.noun}`, `{action.verb}`) until the theme has a template for it.
|
|
17
|
+
|
|
18
|
+
`timeZone` is accepted for symmetry with the API and changes nothing: the server prints `date` and `datetime` as the ISO text was written and does no zone conversion, and so does this package.
|
|
19
|
+
|
|
20
|
+
## Parity
|
|
21
|
+
|
|
22
|
+
`testdata/activity-render-vectors.json` in the repository holds event, theme and options -> expected `display` cases. The Rust renderer (`crates/wt-domain-activity`) and this package both run it; the test here compares `JSON.stringify`, so key order and omitted keys must match too.
|
|
23
|
+
|
|
24
|
+
### Not guaranteed
|
|
25
|
+
|
|
26
|
+
Everything outside the vectors is the same algorithm but is not proven byte for byte. Known differences:
|
|
27
|
+
|
|
28
|
+
- **Floats in event JSON.** Rust prints a JSON number from its parsed form (`5.0` stays `5.0`, `1e16` is `1e16`); JavaScript has lost that text by the time it reaches `render` (`5`, `10000000000000000`). Integers up to 2^53 and plain decimals agree. Send numbers as strings if exact digits matter.
|
|
29
|
+
- **Rounding of exact ties.** Rust rounds half to even on the exact binary value (`2.5|count` is `2`), and this package does it by hand. No shared vector covers a tie, so `0.125|money:USD` is the first thing to check if a mismatch is reported.
|
|
30
|
+
- **Digits.** Rust's `\d` in the ISO date check matches Unicode digits (a date written in Arabic-Indic digits is cut by bytes there and left alone here); this package matches ASCII digits only.
|
|
31
|
+
- **Characters.** Values are cut at 200 Unicode code points in both (a surrogate pair counts once here); control characters are the Unicode `Cc` set in both.
|
|
32
|
+
- **Theme merge.** The server merges the org default theme under the project's. `render` takes the theme you give it: pass the effective theme.
|
|
33
|
+
- **Invalid themes.** The server rejects unknown fields and over-long texts when the theme is saved; `render` does not validate, so a theme that could not be saved renders as best it can.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
render: () => render
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var MAX_VALUE_CHARS = 200;
|
|
27
|
+
var HOLE = /\{([A-Za-z0-9_.\-]+)((?:\|[a-z]+(?::[A-Za-z0-9_ .\-]*)?)*)\}/g;
|
|
28
|
+
var ISO = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2})?/;
|
|
29
|
+
var NUMBER = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
|
|
30
|
+
var CONTROL = /^[\u0000-\u001f\u007f-\u009f]$/;
|
|
31
|
+
var isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
32
|
+
var has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
33
|
+
function actionParts(action) {
|
|
34
|
+
const segs = action.split(".");
|
|
35
|
+
return [segs[0] ?? "", (segs[segs.length - 1] ?? "").split("_").join(" ")];
|
|
36
|
+
}
|
|
37
|
+
function text(v) {
|
|
38
|
+
if (typeof v === "string") return v;
|
|
39
|
+
if (typeof v === "number" || typeof v === "boolean") return String(v);
|
|
40
|
+
return void 0;
|
|
41
|
+
}
|
|
42
|
+
function lookup(e, path) {
|
|
43
|
+
if (path === "action.verb" || path === "action.noun") {
|
|
44
|
+
const a = isObj(e) ? e.action : void 0;
|
|
45
|
+
if (typeof a !== "string") return void 0;
|
|
46
|
+
const [noun, verb] = actionParts(a);
|
|
47
|
+
return path === "action.noun" ? noun : verb;
|
|
48
|
+
}
|
|
49
|
+
if (path.startsWith("changes.")) {
|
|
50
|
+
const rest = path.slice("changes.".length);
|
|
51
|
+
const list = isObj(e) ? e.changes : void 0;
|
|
52
|
+
if (!Array.isArray(list)) return void 0;
|
|
53
|
+
if (rest === "count") return String(list.length);
|
|
54
|
+
const i = rest.lastIndexOf(".");
|
|
55
|
+
const side = rest.slice(i + 1);
|
|
56
|
+
if (i >= 0 && (side === "old" || side === "new")) {
|
|
57
|
+
const field = rest.slice(0, i);
|
|
58
|
+
const c = list.find((c2) => isObj(c2) && c2.field === field);
|
|
59
|
+
return isObj(c) && has(c, side) ? text(c[side]) : void 0;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
let cur = e;
|
|
63
|
+
for (const k of path.split(".")) {
|
|
64
|
+
if (Array.isArray(cur)) {
|
|
65
|
+
if (!/^\d+$/.test(k)) return void 0;
|
|
66
|
+
cur = cur[Number(k)];
|
|
67
|
+
} else if (isObj(cur) && has(cur, k)) {
|
|
68
|
+
cur = cur[k];
|
|
69
|
+
} else {
|
|
70
|
+
return void 0;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return text(cur);
|
|
74
|
+
}
|
|
75
|
+
function clean(v) {
|
|
76
|
+
return Array.from(v).slice(0, MAX_VALUE_CHARS).map((c) => CONTROL.test(c) ? " " : c).join("");
|
|
77
|
+
}
|
|
78
|
+
function parseNumber(s) {
|
|
79
|
+
if (!NUMBER.test(s)) return void 0;
|
|
80
|
+
const n = Number(s);
|
|
81
|
+
return Number.isFinite(n) ? n : void 0;
|
|
82
|
+
}
|
|
83
|
+
function fixed(n, dec) {
|
|
84
|
+
if (n >= 1e21) return BigInt(n).toString() + (dec > 0 ? "." + "0".repeat(dec) : "");
|
|
85
|
+
const full = n.toFixed(100);
|
|
86
|
+
const dot = full.indexOf(".");
|
|
87
|
+
const frac = full.slice(dot + 1);
|
|
88
|
+
const tie = frac[dec] === "5" && /^0*$/.test(frac.slice(dec + 1));
|
|
89
|
+
if (!tie) return n.toFixed(dec);
|
|
90
|
+
let digits = BigInt(full.slice(0, dot) + frac.slice(0, dec));
|
|
91
|
+
if (digits % 2n === 1n) digits += 1n;
|
|
92
|
+
const s = digits.toString().padStart(dec + 1, "0");
|
|
93
|
+
return dec > 0 ? `${s.slice(0, s.length - dec)}.${s.slice(s.length - dec)}` : s;
|
|
94
|
+
}
|
|
95
|
+
function group(s) {
|
|
96
|
+
const neg = s.startsWith("-");
|
|
97
|
+
const body = neg ? s.slice(1) : s;
|
|
98
|
+
const dot = body.indexOf(".");
|
|
99
|
+
const int = dot >= 0 ? body.slice(0, dot) : body;
|
|
100
|
+
const frac = dot >= 0 ? body.slice(dot + 1) : "";
|
|
101
|
+
let out = "";
|
|
102
|
+
for (let i = 0; i < int.length; i++) {
|
|
103
|
+
if (i > 0 && (int.length - i) % 3 === 0) out += ",";
|
|
104
|
+
out += int[i];
|
|
105
|
+
}
|
|
106
|
+
return `${neg ? "-" : ""}${out}${frac === "" ? "" : "."}${frac}`;
|
|
107
|
+
}
|
|
108
|
+
function money(v, cur) {
|
|
109
|
+
const n = parseNumber(v);
|
|
110
|
+
if (n === void 0) return v;
|
|
111
|
+
const code = cur.trim().toUpperCase();
|
|
112
|
+
const [sym, dec] = code === "USD" ? ["$", 2] : code === "EUR" ? ["\u20AC", 2] : code === "GBP" ? ["\xA3", 2] : code === "JPY" ? ["\xA5", 0] : ["", 2];
|
|
113
|
+
const g = group(fixed(Math.abs(n), dec));
|
|
114
|
+
const body = sym !== "" ? `${sym}${g}` : code === "" ? g : `${g} ${code}`;
|
|
115
|
+
return n < 0 ? `-${body}` : body;
|
|
116
|
+
}
|
|
117
|
+
function count(v) {
|
|
118
|
+
const n = parseNumber(v);
|
|
119
|
+
if (n === void 0) return v;
|
|
120
|
+
return group(`${n < 0 || Object.is(n, -0) ? "-" : ""}${fixed(Math.abs(n), 0)}`);
|
|
121
|
+
}
|
|
122
|
+
function date(v, n) {
|
|
123
|
+
const m = ISO.exec(v);
|
|
124
|
+
return m && m[0].length >= n ? v.slice(0, n) : v;
|
|
125
|
+
}
|
|
126
|
+
function interpolate(tpl, event) {
|
|
127
|
+
return tpl.replace(HOLE, (_m, path, fmts) => {
|
|
128
|
+
let v = lookup(event, path) ?? "";
|
|
129
|
+
for (const f of fmts.split("|")) {
|
|
130
|
+
if (f === "") continue;
|
|
131
|
+
const c = f.indexOf(":");
|
|
132
|
+
const name = c >= 0 ? f.slice(0, c) : f;
|
|
133
|
+
const arg = c >= 0 ? f.slice(c + 1) : "";
|
|
134
|
+
if (name === "money") v = money(v, arg);
|
|
135
|
+
else if (name === "count") v = count(v);
|
|
136
|
+
else if (name === "date") v = date(v, 10);
|
|
137
|
+
else if (name === "datetime") v = date(v, 16).split("T").join(" ");
|
|
138
|
+
}
|
|
139
|
+
return clean(v);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
function templateFor(actions, action) {
|
|
143
|
+
if (has(actions, action)) return actions[action];
|
|
144
|
+
let best;
|
|
145
|
+
let bestLen = -1;
|
|
146
|
+
for (const k of Object.keys(actions)) {
|
|
147
|
+
if (!k.endsWith("*")) continue;
|
|
148
|
+
const stem = k.slice(0, -1);
|
|
149
|
+
if (action.startsWith(stem) && stem.length > bestLen) {
|
|
150
|
+
best = actions[k];
|
|
151
|
+
bestLen = stem.length;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return best;
|
|
155
|
+
}
|
|
156
|
+
function fallbackTitle(e) {
|
|
157
|
+
const s = (p) => {
|
|
158
|
+
const v = lookup(e, p);
|
|
159
|
+
return v === void 0 || v === "" ? void 0 : v;
|
|
160
|
+
};
|
|
161
|
+
return [s("actor.name") ?? s("actor.id"), s("action"), s("target.name") ?? s("target.id")].filter((x) => x !== void 0).join(" ");
|
|
162
|
+
}
|
|
163
|
+
function tags(locale) {
|
|
164
|
+
return locale == null ? [] : [locale, locale.split("-")[0] ?? locale];
|
|
165
|
+
}
|
|
166
|
+
function renderTemplate(tpl, event, locale) {
|
|
167
|
+
if (!tpl) return { title: fallbackTitle(event) };
|
|
168
|
+
const chain = [];
|
|
169
|
+
for (const k of tags(locale)) if (tpl.locales && has(tpl.locales, k)) chain.push(tpl.locales[k]);
|
|
170
|
+
chain.push(tpl);
|
|
171
|
+
const outcome = isObj(event) && typeof event.outcome === "string" ? event.outcome : void 0;
|
|
172
|
+
let label;
|
|
173
|
+
for (const l of chain) {
|
|
174
|
+
const v = (outcome === "failure" ? l.label_failure : outcome === "denied" ? l.label_denied : void 0) ?? l.label;
|
|
175
|
+
if (v != null) {
|
|
176
|
+
label = v;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const out = { title: label === void 0 ? fallbackTitle(event) : interpolate(label, event) };
|
|
181
|
+
const subtitle = chain.find((l) => l.subtitle != null)?.subtitle;
|
|
182
|
+
if (subtitle != null) out.subtitle = interpolate(subtitle, event);
|
|
183
|
+
const fields = (chain.find((l) => l.fields != null)?.fields ?? []).map((f) => ({
|
|
184
|
+
label: interpolate(f.label, event),
|
|
185
|
+
value: interpolate(f.value, event)
|
|
186
|
+
}));
|
|
187
|
+
if (fields.length > 0) out.fields = fields;
|
|
188
|
+
return out;
|
|
189
|
+
}
|
|
190
|
+
function failureReason(theme, event, locale) {
|
|
191
|
+
if (!isObj(event) || event.outcome !== "failure") return void 0;
|
|
192
|
+
const path = theme.failureReasonPath ?? theme.failure_reason_path;
|
|
193
|
+
const reasons = theme.failureReasons ?? theme.failure_reasons;
|
|
194
|
+
if (reasons == null) return void 0;
|
|
195
|
+
const flag = (v) => v === true;
|
|
196
|
+
const abandoned = flag(event.abandoned) || isObj(event.ext) && flag(event.ext.abandoned);
|
|
197
|
+
let code;
|
|
198
|
+
if (abandoned && has(reasons, "abandoned")) code = "abandoned";
|
|
199
|
+
else if (path != null) code = lookup(event, path);
|
|
200
|
+
if (code === void 0 || code === "" || !has(reasons, code)) return void 0;
|
|
201
|
+
const r = reasons[code];
|
|
202
|
+
const chain = [];
|
|
203
|
+
for (const k of tags(locale)) if (r.locales && has(r.locales, k)) chain.push(r.locales[k]);
|
|
204
|
+
const t = chain.find((l) => l.text != null)?.text ?? r.text;
|
|
205
|
+
const a = chain.find((l) => l.action != null)?.action ?? r.action;
|
|
206
|
+
const out = { code, text: interpolate(t, event) };
|
|
207
|
+
if (a != null) out.action = { label: interpolate(a.label, event), url: a.url };
|
|
208
|
+
return out;
|
|
209
|
+
}
|
|
210
|
+
function render(event, theme, options = {}) {
|
|
211
|
+
const action = isObj(event) && typeof event.action === "string" ? event.action : void 0;
|
|
212
|
+
const found = action !== void 0 && theme.actions ? templateFor(theme.actions, action) : void 0;
|
|
213
|
+
const out = renderTemplate(found ?? theme.fallbackTemplate ?? theme.fallback_template ?? void 0, event, options.locale);
|
|
214
|
+
const reason = failureReason(theme, event, options.locale);
|
|
215
|
+
if (reason) out.failure_reason = reason;
|
|
216
|
+
return out;
|
|
217
|
+
}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export interface DisplayField {
|
|
2
|
+
label: string;
|
|
3
|
+
value: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ReasonAction {
|
|
6
|
+
label: string;
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ActionLocale {
|
|
10
|
+
label?: string | null;
|
|
11
|
+
label_failure?: string | null;
|
|
12
|
+
label_denied?: string | null;
|
|
13
|
+
subtitle?: string | null;
|
|
14
|
+
fields?: DisplayField[] | null;
|
|
15
|
+
}
|
|
16
|
+
export interface ActionTemplate extends ActionLocale {
|
|
17
|
+
locales?: Record<string, ActionLocale> | null;
|
|
18
|
+
icon?: string | null;
|
|
19
|
+
tone?: string | null;
|
|
20
|
+
}
|
|
21
|
+
export interface ReasonLocale {
|
|
22
|
+
text?: string | null;
|
|
23
|
+
action?: ReasonAction | null;
|
|
24
|
+
}
|
|
25
|
+
export interface FailureReason {
|
|
26
|
+
text: string;
|
|
27
|
+
action?: ReasonAction | null;
|
|
28
|
+
locales?: Record<string, ReasonLocale> | null;
|
|
29
|
+
}
|
|
30
|
+
/** The parts of a theme the renderer reads (the rest of the theme is ignored). The snake_case spellings are accepted like the API does. */
|
|
31
|
+
export interface RenderTheme {
|
|
32
|
+
actions?: Record<string, ActionTemplate> | null;
|
|
33
|
+
fallbackTemplate?: ActionTemplate | null;
|
|
34
|
+
fallback_template?: ActionTemplate | null;
|
|
35
|
+
failureReasonPath?: string | null;
|
|
36
|
+
failure_reason_path?: string | null;
|
|
37
|
+
failureReasons?: Record<string, FailureReason> | null;
|
|
38
|
+
failure_reasons?: Record<string, FailureReason> | null;
|
|
39
|
+
}
|
|
40
|
+
export interface FailureReasonDisplay {
|
|
41
|
+
code: string;
|
|
42
|
+
text: string;
|
|
43
|
+
action?: ReasonAction;
|
|
44
|
+
}
|
|
45
|
+
/** The `display` object of the API. */
|
|
46
|
+
export interface Display {
|
|
47
|
+
title: string;
|
|
48
|
+
subtitle?: string;
|
|
49
|
+
fields?: DisplayField[];
|
|
50
|
+
failure_reason?: FailureReasonDisplay;
|
|
51
|
+
}
|
|
52
|
+
export interface RenderOptions {
|
|
53
|
+
/** Tag such as `pt-BR`; `pt-BR`, then `pt`, then the template's own copy. */
|
|
54
|
+
locale?: string | null;
|
|
55
|
+
/** Accepted for API symmetry; the server does no zone conversion, so neither does this. */
|
|
56
|
+
timeZone?: string | null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Render `event` (as the API returns it) with `theme` (as the API returns it): the template of its
|
|
60
|
+
* action (exact key, longest prefix key, then `fallbackTemplate`), outcome and locale variants,
|
|
61
|
+
* and `failure_reason`. Pure; the result serializes like the API's `display`.
|
|
62
|
+
*/
|
|
63
|
+
export declare function render(event: unknown, theme: RenderTheme, options?: RenderOptions): Display;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export interface DisplayField {
|
|
2
|
+
label: string;
|
|
3
|
+
value: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ReasonAction {
|
|
6
|
+
label: string;
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ActionLocale {
|
|
10
|
+
label?: string | null;
|
|
11
|
+
label_failure?: string | null;
|
|
12
|
+
label_denied?: string | null;
|
|
13
|
+
subtitle?: string | null;
|
|
14
|
+
fields?: DisplayField[] | null;
|
|
15
|
+
}
|
|
16
|
+
export interface ActionTemplate extends ActionLocale {
|
|
17
|
+
locales?: Record<string, ActionLocale> | null;
|
|
18
|
+
icon?: string | null;
|
|
19
|
+
tone?: string | null;
|
|
20
|
+
}
|
|
21
|
+
export interface ReasonLocale {
|
|
22
|
+
text?: string | null;
|
|
23
|
+
action?: ReasonAction | null;
|
|
24
|
+
}
|
|
25
|
+
export interface FailureReason {
|
|
26
|
+
text: string;
|
|
27
|
+
action?: ReasonAction | null;
|
|
28
|
+
locales?: Record<string, ReasonLocale> | null;
|
|
29
|
+
}
|
|
30
|
+
/** The parts of a theme the renderer reads (the rest of the theme is ignored). The snake_case spellings are accepted like the API does. */
|
|
31
|
+
export interface RenderTheme {
|
|
32
|
+
actions?: Record<string, ActionTemplate> | null;
|
|
33
|
+
fallbackTemplate?: ActionTemplate | null;
|
|
34
|
+
fallback_template?: ActionTemplate | null;
|
|
35
|
+
failureReasonPath?: string | null;
|
|
36
|
+
failure_reason_path?: string | null;
|
|
37
|
+
failureReasons?: Record<string, FailureReason> | null;
|
|
38
|
+
failure_reasons?: Record<string, FailureReason> | null;
|
|
39
|
+
}
|
|
40
|
+
export interface FailureReasonDisplay {
|
|
41
|
+
code: string;
|
|
42
|
+
text: string;
|
|
43
|
+
action?: ReasonAction;
|
|
44
|
+
}
|
|
45
|
+
/** The `display` object of the API. */
|
|
46
|
+
export interface Display {
|
|
47
|
+
title: string;
|
|
48
|
+
subtitle?: string;
|
|
49
|
+
fields?: DisplayField[];
|
|
50
|
+
failure_reason?: FailureReasonDisplay;
|
|
51
|
+
}
|
|
52
|
+
export interface RenderOptions {
|
|
53
|
+
/** Tag such as `pt-BR`; `pt-BR`, then `pt`, then the template's own copy. */
|
|
54
|
+
locale?: string | null;
|
|
55
|
+
/** Accepted for API symmetry; the server does no zone conversion, so neither does this. */
|
|
56
|
+
timeZone?: string | null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Render `event` (as the API returns it) with `theme` (as the API returns it): the template of its
|
|
60
|
+
* action (exact key, longest prefix key, then `fallbackTemplate`), outcome and locale variants,
|
|
61
|
+
* and `failure_reason`. Pure; the result serializes like the API's `display`.
|
|
62
|
+
*/
|
|
63
|
+
export declare function render(event: unknown, theme: RenderTheme, options?: RenderOptions): Display;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var MAX_VALUE_CHARS = 200;
|
|
3
|
+
var HOLE = /\{([A-Za-z0-9_.\-]+)((?:\|[a-z]+(?::[A-Za-z0-9_ .\-]*)?)*)\}/g;
|
|
4
|
+
var ISO = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2})?/;
|
|
5
|
+
var NUMBER = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
|
|
6
|
+
var CONTROL = /^[\u0000-\u001f\u007f-\u009f]$/;
|
|
7
|
+
var isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
8
|
+
var has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
9
|
+
function actionParts(action) {
|
|
10
|
+
const segs = action.split(".");
|
|
11
|
+
return [segs[0] ?? "", (segs[segs.length - 1] ?? "").split("_").join(" ")];
|
|
12
|
+
}
|
|
13
|
+
function text(v) {
|
|
14
|
+
if (typeof v === "string") return v;
|
|
15
|
+
if (typeof v === "number" || typeof v === "boolean") return String(v);
|
|
16
|
+
return void 0;
|
|
17
|
+
}
|
|
18
|
+
function lookup(e, path) {
|
|
19
|
+
if (path === "action.verb" || path === "action.noun") {
|
|
20
|
+
const a = isObj(e) ? e.action : void 0;
|
|
21
|
+
if (typeof a !== "string") return void 0;
|
|
22
|
+
const [noun, verb] = actionParts(a);
|
|
23
|
+
return path === "action.noun" ? noun : verb;
|
|
24
|
+
}
|
|
25
|
+
if (path.startsWith("changes.")) {
|
|
26
|
+
const rest = path.slice("changes.".length);
|
|
27
|
+
const list = isObj(e) ? e.changes : void 0;
|
|
28
|
+
if (!Array.isArray(list)) return void 0;
|
|
29
|
+
if (rest === "count") return String(list.length);
|
|
30
|
+
const i = rest.lastIndexOf(".");
|
|
31
|
+
const side = rest.slice(i + 1);
|
|
32
|
+
if (i >= 0 && (side === "old" || side === "new")) {
|
|
33
|
+
const field = rest.slice(0, i);
|
|
34
|
+
const c = list.find((c2) => isObj(c2) && c2.field === field);
|
|
35
|
+
return isObj(c) && has(c, side) ? text(c[side]) : void 0;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
let cur = e;
|
|
39
|
+
for (const k of path.split(".")) {
|
|
40
|
+
if (Array.isArray(cur)) {
|
|
41
|
+
if (!/^\d+$/.test(k)) return void 0;
|
|
42
|
+
cur = cur[Number(k)];
|
|
43
|
+
} else if (isObj(cur) && has(cur, k)) {
|
|
44
|
+
cur = cur[k];
|
|
45
|
+
} else {
|
|
46
|
+
return void 0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return text(cur);
|
|
50
|
+
}
|
|
51
|
+
function clean(v) {
|
|
52
|
+
return Array.from(v).slice(0, MAX_VALUE_CHARS).map((c) => CONTROL.test(c) ? " " : c).join("");
|
|
53
|
+
}
|
|
54
|
+
function parseNumber(s) {
|
|
55
|
+
if (!NUMBER.test(s)) return void 0;
|
|
56
|
+
const n = Number(s);
|
|
57
|
+
return Number.isFinite(n) ? n : void 0;
|
|
58
|
+
}
|
|
59
|
+
function fixed(n, dec) {
|
|
60
|
+
if (n >= 1e21) return BigInt(n).toString() + (dec > 0 ? "." + "0".repeat(dec) : "");
|
|
61
|
+
const full = n.toFixed(100);
|
|
62
|
+
const dot = full.indexOf(".");
|
|
63
|
+
const frac = full.slice(dot + 1);
|
|
64
|
+
const tie = frac[dec] === "5" && /^0*$/.test(frac.slice(dec + 1));
|
|
65
|
+
if (!tie) return n.toFixed(dec);
|
|
66
|
+
let digits = BigInt(full.slice(0, dot) + frac.slice(0, dec));
|
|
67
|
+
if (digits % 2n === 1n) digits += 1n;
|
|
68
|
+
const s = digits.toString().padStart(dec + 1, "0");
|
|
69
|
+
return dec > 0 ? `${s.slice(0, s.length - dec)}.${s.slice(s.length - dec)}` : s;
|
|
70
|
+
}
|
|
71
|
+
function group(s) {
|
|
72
|
+
const neg = s.startsWith("-");
|
|
73
|
+
const body = neg ? s.slice(1) : s;
|
|
74
|
+
const dot = body.indexOf(".");
|
|
75
|
+
const int = dot >= 0 ? body.slice(0, dot) : body;
|
|
76
|
+
const frac = dot >= 0 ? body.slice(dot + 1) : "";
|
|
77
|
+
let out = "";
|
|
78
|
+
for (let i = 0; i < int.length; i++) {
|
|
79
|
+
if (i > 0 && (int.length - i) % 3 === 0) out += ",";
|
|
80
|
+
out += int[i];
|
|
81
|
+
}
|
|
82
|
+
return `${neg ? "-" : ""}${out}${frac === "" ? "" : "."}${frac}`;
|
|
83
|
+
}
|
|
84
|
+
function money(v, cur) {
|
|
85
|
+
const n = parseNumber(v);
|
|
86
|
+
if (n === void 0) return v;
|
|
87
|
+
const code = cur.trim().toUpperCase();
|
|
88
|
+
const [sym, dec] = code === "USD" ? ["$", 2] : code === "EUR" ? ["\u20AC", 2] : code === "GBP" ? ["\xA3", 2] : code === "JPY" ? ["\xA5", 0] : ["", 2];
|
|
89
|
+
const g = group(fixed(Math.abs(n), dec));
|
|
90
|
+
const body = sym !== "" ? `${sym}${g}` : code === "" ? g : `${g} ${code}`;
|
|
91
|
+
return n < 0 ? `-${body}` : body;
|
|
92
|
+
}
|
|
93
|
+
function count(v) {
|
|
94
|
+
const n = parseNumber(v);
|
|
95
|
+
if (n === void 0) return v;
|
|
96
|
+
return group(`${n < 0 || Object.is(n, -0) ? "-" : ""}${fixed(Math.abs(n), 0)}`);
|
|
97
|
+
}
|
|
98
|
+
function date(v, n) {
|
|
99
|
+
const m = ISO.exec(v);
|
|
100
|
+
return m && m[0].length >= n ? v.slice(0, n) : v;
|
|
101
|
+
}
|
|
102
|
+
function interpolate(tpl, event) {
|
|
103
|
+
return tpl.replace(HOLE, (_m, path, fmts) => {
|
|
104
|
+
let v = lookup(event, path) ?? "";
|
|
105
|
+
for (const f of fmts.split("|")) {
|
|
106
|
+
if (f === "") continue;
|
|
107
|
+
const c = f.indexOf(":");
|
|
108
|
+
const name = c >= 0 ? f.slice(0, c) : f;
|
|
109
|
+
const arg = c >= 0 ? f.slice(c + 1) : "";
|
|
110
|
+
if (name === "money") v = money(v, arg);
|
|
111
|
+
else if (name === "count") v = count(v);
|
|
112
|
+
else if (name === "date") v = date(v, 10);
|
|
113
|
+
else if (name === "datetime") v = date(v, 16).split("T").join(" ");
|
|
114
|
+
}
|
|
115
|
+
return clean(v);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
function templateFor(actions, action) {
|
|
119
|
+
if (has(actions, action)) return actions[action];
|
|
120
|
+
let best;
|
|
121
|
+
let bestLen = -1;
|
|
122
|
+
for (const k of Object.keys(actions)) {
|
|
123
|
+
if (!k.endsWith("*")) continue;
|
|
124
|
+
const stem = k.slice(0, -1);
|
|
125
|
+
if (action.startsWith(stem) && stem.length > bestLen) {
|
|
126
|
+
best = actions[k];
|
|
127
|
+
bestLen = stem.length;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return best;
|
|
131
|
+
}
|
|
132
|
+
function fallbackTitle(e) {
|
|
133
|
+
const s = (p) => {
|
|
134
|
+
const v = lookup(e, p);
|
|
135
|
+
return v === void 0 || v === "" ? void 0 : v;
|
|
136
|
+
};
|
|
137
|
+
return [s("actor.name") ?? s("actor.id"), s("action"), s("target.name") ?? s("target.id")].filter((x) => x !== void 0).join(" ");
|
|
138
|
+
}
|
|
139
|
+
function tags(locale) {
|
|
140
|
+
return locale == null ? [] : [locale, locale.split("-")[0] ?? locale];
|
|
141
|
+
}
|
|
142
|
+
function renderTemplate(tpl, event, locale) {
|
|
143
|
+
if (!tpl) return { title: fallbackTitle(event) };
|
|
144
|
+
const chain = [];
|
|
145
|
+
for (const k of tags(locale)) if (tpl.locales && has(tpl.locales, k)) chain.push(tpl.locales[k]);
|
|
146
|
+
chain.push(tpl);
|
|
147
|
+
const outcome = isObj(event) && typeof event.outcome === "string" ? event.outcome : void 0;
|
|
148
|
+
let label;
|
|
149
|
+
for (const l of chain) {
|
|
150
|
+
const v = (outcome === "failure" ? l.label_failure : outcome === "denied" ? l.label_denied : void 0) ?? l.label;
|
|
151
|
+
if (v != null) {
|
|
152
|
+
label = v;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const out = { title: label === void 0 ? fallbackTitle(event) : interpolate(label, event) };
|
|
157
|
+
const subtitle = chain.find((l) => l.subtitle != null)?.subtitle;
|
|
158
|
+
if (subtitle != null) out.subtitle = interpolate(subtitle, event);
|
|
159
|
+
const fields = (chain.find((l) => l.fields != null)?.fields ?? []).map((f) => ({
|
|
160
|
+
label: interpolate(f.label, event),
|
|
161
|
+
value: interpolate(f.value, event)
|
|
162
|
+
}));
|
|
163
|
+
if (fields.length > 0) out.fields = fields;
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
function failureReason(theme, event, locale) {
|
|
167
|
+
if (!isObj(event) || event.outcome !== "failure") return void 0;
|
|
168
|
+
const path = theme.failureReasonPath ?? theme.failure_reason_path;
|
|
169
|
+
const reasons = theme.failureReasons ?? theme.failure_reasons;
|
|
170
|
+
if (reasons == null) return void 0;
|
|
171
|
+
const flag = (v) => v === true;
|
|
172
|
+
const abandoned = flag(event.abandoned) || isObj(event.ext) && flag(event.ext.abandoned);
|
|
173
|
+
let code;
|
|
174
|
+
if (abandoned && has(reasons, "abandoned")) code = "abandoned";
|
|
175
|
+
else if (path != null) code = lookup(event, path);
|
|
176
|
+
if (code === void 0 || code === "" || !has(reasons, code)) return void 0;
|
|
177
|
+
const r = reasons[code];
|
|
178
|
+
const chain = [];
|
|
179
|
+
for (const k of tags(locale)) if (r.locales && has(r.locales, k)) chain.push(r.locales[k]);
|
|
180
|
+
const t = chain.find((l) => l.text != null)?.text ?? r.text;
|
|
181
|
+
const a = chain.find((l) => l.action != null)?.action ?? r.action;
|
|
182
|
+
const out = { code, text: interpolate(t, event) };
|
|
183
|
+
if (a != null) out.action = { label: interpolate(a.label, event), url: a.url };
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
function render(event, theme, options = {}) {
|
|
187
|
+
const action = isObj(event) && typeof event.action === "string" ? event.action : void 0;
|
|
188
|
+
const found = action !== void 0 && theme.actions ? templateFor(theme.actions, action) : void 0;
|
|
189
|
+
const out = renderTemplate(found ?? theme.fallbackTemplate ?? theme.fallback_template ?? void 0, event, options.locale);
|
|
190
|
+
const reason = failureReason(theme, event, options.locale);
|
|
191
|
+
if (reason) out.failure_reason = reason;
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
export {
|
|
195
|
+
render
|
|
196
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@watchtowerobservability/activity-render",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Watchtower activity renderer: the same display the API returns (templates, formatters, locales, failure reasons), client side, zero dependencies",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "node scripts/build.mjs",
|
|
31
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"prepack": "npm run build"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "22.15.0",
|
|
37
|
+
"esbuild": "0.28.2",
|
|
38
|
+
"typescript": "5.7.2",
|
|
39
|
+
"vitest": "5.0.5"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/watchtowerobservability/watchtower-activity-render.git"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/watchtowerobservability/watchtower-activity-render#readme",
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"registry": "https://registry.npmjs.org/"
|
|
49
|
+
}
|
|
50
|
+
}
|