@allurereport/web-commons 3.0.0-beta.10

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 ADDED
@@ -0,0 +1,25 @@
1
+ # Allure Web Commons
2
+
3
+ [<img src="https://allurereport.org/public/img/allure-report.svg" height="85px" alt="Allure Report logo" align="right" />](https://allurereport.org "Allure Report")
4
+
5
+ - Learn more about Allure Report at https://allurereport.org
6
+ - 📚 [Documentation](https://allurereport.org/docs/) – discover official documentation for Allure Report
7
+ - ❓ [Questions and Support](https://github.com/orgs/allure-framework/discussions/categories/questions-support) – get help from the team and community
8
+ - 📢 [Official announcements](https://github.com/orgs/allure-framework/discussions/categories/announcements) – be in touch with the latest updates
9
+ - 💬 [General Discussion ](https://github.com/orgs/allure-framework/discussions/categories/general-discussion) – engage in casual conversations, share insights and ideas with the community
10
+
11
+ ---
12
+
13
+ ## Overview
14
+
15
+ The package includes utilities which are used in web-implementations of Allure reports.
16
+
17
+ ## Install
18
+
19
+ Use your favorite package manager to install the package:
20
+
21
+ ```shell
22
+ npm add @allurereport/web-commons
23
+ yarn add @allurereport/web-commons
24
+ pnpm add @allurereport/web-commons
25
+ ```
@@ -0,0 +1,20 @@
1
+ export interface Attachments {
2
+ id?: string;
3
+ ext?: string;
4
+ contentType?: string;
5
+ text?: string;
6
+ src?: string;
7
+ img?: string;
8
+ }
9
+ export declare const fetchAttachment: (id: string, ext: string, contentType?: string) => Promise<Attachments | null>;
10
+ export declare const blobAttachment: (id: string, ext: string, contentType: string) => Promise<Blob>;
11
+ export declare const downloadAttachment: (id: string, ext: string, contentType: string) => Promise<void>;
12
+ export declare const openAttachmentInNewTab: (id: string, ext: string, contentType: string) => Promise<void>;
13
+ export declare const attachmentType: (type?: string) => {
14
+ type: string;
15
+ icon: string;
16
+ } | {
17
+ type: null;
18
+ icon: string;
19
+ };
20
+ export declare const restrictedContentTypes: string[];
@@ -0,0 +1,141 @@
1
+ import { fetchReportAttachment } from "./data.js";
2
+ const fetchFromUrl = async ({ id, ext, contentType }) => {
3
+ const fileName = `${id || "-"}${ext || ""}`;
4
+ return fetchReportAttachment(`data/attachments/${fileName}?attachment`, contentType);
5
+ };
6
+ export const fetchAttachment = async (id, ext, contentType) => {
7
+ if (!id && !ext) {
8
+ return null;
9
+ }
10
+ const response = await fetchFromUrl({ id, ext, contentType });
11
+ const fileType = attachmentType(contentType);
12
+ switch (fileType.type) {
13
+ case "svg":
14
+ case "image": {
15
+ const blob = await response.blob();
16
+ const img = URL.createObjectURL(blob);
17
+ return { img, id };
18
+ }
19
+ case "uri":
20
+ case "code":
21
+ case "html":
22
+ case "table":
23
+ case "text": {
24
+ const text = await response.text();
25
+ return { text };
26
+ }
27
+ case "video": {
28
+ const blob = await response.blob();
29
+ const src = URL.createObjectURL(blob);
30
+ return { src, id, contentType };
31
+ }
32
+ default:
33
+ return null;
34
+ }
35
+ };
36
+ export const blobAttachment = async (id, ext, contentType) => {
37
+ const response = await fetchFromUrl({ id, ext, contentType });
38
+ return await response.blob();
39
+ };
40
+ export const downloadAttachment = async (id, ext, contentType) => {
41
+ if (!id && !ext) {
42
+ return;
43
+ }
44
+ const fileName = `${id}${ext}`;
45
+ const blob = await blobAttachment(id, ext, contentType);
46
+ const linkUrl = URL.createObjectURL(blob);
47
+ const link = document.createElement("a");
48
+ link.href = linkUrl;
49
+ link.download = fileName;
50
+ link.click();
51
+ URL.revokeObjectURL(linkUrl);
52
+ };
53
+ export const openAttachmentInNewTab = async (id, ext, contentType) => {
54
+ if (!id && !ext) {
55
+ return;
56
+ }
57
+ const blob = await blobAttachment(id, ext, contentType);
58
+ const linkUrl = URL.createObjectURL(blob);
59
+ globalThis.open(linkUrl, "_blank");
60
+ };
61
+ export const attachmentType = (type) => {
62
+ switch (type) {
63
+ case "image/bmp":
64
+ case "image/gif":
65
+ case "image/tiff":
66
+ case "image/jpeg":
67
+ case "image/jpg":
68
+ case "image/png":
69
+ case "image/*":
70
+ return {
71
+ type: "image",
72
+ icon: "file",
73
+ };
74
+ case "text/xml":
75
+ case "application/xml":
76
+ case "application/json":
77
+ case "text/json":
78
+ case "text/yaml":
79
+ case "application/yaml":
80
+ case "application/x-yaml":
81
+ case "text/x-yaml":
82
+ case "text/css":
83
+ return {
84
+ type: "code",
85
+ icon: "file",
86
+ };
87
+ case "text/plain":
88
+ case "text/*":
89
+ return {
90
+ type: "text",
91
+ icon: "txt",
92
+ };
93
+ case "text/html":
94
+ return {
95
+ type: "html",
96
+ icon: "file",
97
+ };
98
+ case "text/csv":
99
+ return {
100
+ type: "table",
101
+ icon: "csv",
102
+ };
103
+ case "text/tab-separated-values":
104
+ return {
105
+ type: "table",
106
+ icon: "table",
107
+ };
108
+ case "image/svg+xml":
109
+ return {
110
+ type: "svg",
111
+ icon: "file",
112
+ };
113
+ case "video/mp4":
114
+ case "video/ogg":
115
+ case "video/webm":
116
+ return {
117
+ type: "video",
118
+ icon: "file",
119
+ };
120
+ case "text/uri-list":
121
+ return {
122
+ type: "uri",
123
+ icon: "list",
124
+ };
125
+ case "application/x-tar":
126
+ case "application/x-gtar":
127
+ case "application/x-bzip2":
128
+ case "application/gzip":
129
+ case "application/zip":
130
+ return {
131
+ type: "archive",
132
+ icon: "file",
133
+ };
134
+ default:
135
+ return {
136
+ type: null,
137
+ icon: "file",
138
+ };
139
+ }
140
+ };
141
+ export const restrictedContentTypes = ["application/gzip"];
package/dist/data.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export declare const ALLURE_LIVE_RELOAD_HASH_STORAGE_KEY = "__allure_report_live_reload_hash__";
2
+ export declare const createReportDataScript: (reportFiles?: {
3
+ name: string;
4
+ value: string;
5
+ }[]) => string;
6
+ export declare const ensureReportDataReady: () => Promise<unknown>;
7
+ export declare const loadReportData: (name: string) => Promise<string>;
8
+ export declare const reportDataUrl: (path: string, contentType?: string) => Promise<string>;
9
+ export declare const fetchReportJsonData: <T>(path: string) => Promise<T>;
10
+ export declare const fetchReportAttachment: (path: string, contentType?: string) => Promise<Response>;
11
+ export declare const getReportOptions: <T>() => T;
package/dist/data.js ADDED
@@ -0,0 +1,81 @@
1
+ export const ALLURE_LIVE_RELOAD_HASH_STORAGE_KEY = "__allure_report_live_reload_hash__";
2
+ export const createReportDataScript = (reportFiles = []) => {
3
+ if (reportFiles.length === 0) {
4
+ return `
5
+ <script async>
6
+ window.allureReportDataReady = true;
7
+ </script>
8
+ `;
9
+ }
10
+ const reportFilesDeclaration = reportFiles.map(({ name, value }) => `d('${name}','${value}')`).join(",");
11
+ return `
12
+ <script async>
13
+ window.allureReportDataReady = false;
14
+ window.allureReportData = window.allureReportData || {};
15
+
16
+ function d(name, value){
17
+ return new Promise(function (resolve) {
18
+ window.allureReportData[name] = value;
19
+
20
+ return resolve(true);
21
+ });
22
+ }
23
+ </script>
24
+ <script defer>
25
+ Promise.allSettled([${reportFilesDeclaration}])
26
+ .then(function(){
27
+ window.allureReportDataReady = true;
28
+ })
29
+ </script>
30
+ `;
31
+ };
32
+ export const ensureReportDataReady = () => new Promise((resolve) => {
33
+ const waitForReady = () => {
34
+ if (globalThis.allureReportDataReady) {
35
+ return resolve(true);
36
+ }
37
+ setTimeout(waitForReady, 30);
38
+ };
39
+ waitForReady();
40
+ });
41
+ export const loadReportData = async (name) => {
42
+ await ensureReportDataReady();
43
+ return new Promise((resolve, reject) => {
44
+ if (globalThis.allureReportData[name]) {
45
+ return resolve(globalThis.allureReportData[name]);
46
+ }
47
+ else {
48
+ return reject(new Error(`Data "${name}" not found!`));
49
+ }
50
+ });
51
+ };
52
+ export const reportDataUrl = async (path, contentType = "application/octet-stream") => {
53
+ if (globalThis.allureReportData) {
54
+ const dataKey = path.replace(/\?attachment$/, "");
55
+ const value = await loadReportData(dataKey);
56
+ return `data:${contentType};base64,${value}`;
57
+ }
58
+ const baseEl = globalThis.document.head.querySelector("base")?.href ?? "https://localhost";
59
+ const url = new URL(path, baseEl);
60
+ const liveReloadHash = globalThis.localStorage.getItem(ALLURE_LIVE_RELOAD_HASH_STORAGE_KEY);
61
+ if (liveReloadHash) {
62
+ url.searchParams.set("live_reload_hash", liveReloadHash);
63
+ }
64
+ return url.pathname + url.search + url.hash;
65
+ };
66
+ export const fetchReportJsonData = async (path) => {
67
+ const url = await reportDataUrl(path);
68
+ const res = await globalThis.fetch(url);
69
+ if (!res.ok) {
70
+ throw new Error(`Failed to fetch ${url}, response status: ${res.status}`);
71
+ }
72
+ const data = res.json();
73
+ return data;
74
+ };
75
+ export const fetchReportAttachment = async (path, contentType) => {
76
+ const url = await reportDataUrl(path, contentType);
77
+ return globalThis.fetch(url);
78
+ };
79
+ export const getReportOptions = () => {
80
+ return globalThis.allureReportOptions;
81
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./data.js";
2
+ export * from "./static.js";
3
+ export * from "./attachments.js";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./data.js";
2
+ export * from "./static.js";
3
+ export * from "./attachments.js";
@@ -0,0 +1,8 @@
1
+ export declare const createScriptTag: (src: string, options?: {
2
+ async?: false;
3
+ defer?: false;
4
+ }) => string;
5
+ export declare const createStylesLinkTag: (src: string) => string;
6
+ export declare const createFontLinkTag: (src: string) => string;
7
+ export declare const createFaviconLinkTag: (src: string) => string;
8
+ export declare const createBaseUrlScript: () => string;
package/dist/static.js ADDED
@@ -0,0 +1,25 @@
1
+ export const createScriptTag = (src, options) => {
2
+ return `<script ${options?.async ? "async" : ""} ${options?.defer ? "defer" : ""} src="${src}"></script>`;
3
+ };
4
+ export const createStylesLinkTag = (src) => {
5
+ return `<link rel="stylesheet" type="text/css" href="${src}">`;
6
+ };
7
+ export const createFontLinkTag = (src) => {
8
+ return `<link rel="preload" href="${src}" as="font" type="font/woff" crossorigin /> `;
9
+ };
10
+ export const createFaviconLinkTag = (src) => {
11
+ return `<link rel="icon" href="${src}">`;
12
+ };
13
+ export const createBaseUrlScript = () => {
14
+ return `
15
+ <script>
16
+ const { origin, pathname } = window.location;
17
+ const url = new URL(pathname, origin);
18
+ const baseEl = document.createElement("base");
19
+
20
+ baseEl.href = url.toString();
21
+
22
+ window.document.head.appendChild(baseEl);
23
+ </script>
24
+ `;
25
+ };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@allurereport/web-commons",
3
+ "version": "3.0.0-beta.10",
4
+ "description": "Collection of utilities used across the web Allure reports",
5
+ "keywords": [
6
+ "allure",
7
+ "testing"
8
+ ],
9
+ "repository": "https://github.com/allure-framework/allure3",
10
+ "license": "Apache-2.0",
11
+ "author": "Qameta Software",
12
+ "type": "module",
13
+ "exports": {
14
+ ".": "./dist/index.js"
15
+ },
16
+ "module": "dist/index.js",
17
+ "types": "dist/index.d.ts",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "scripts": {
22
+ "build": "run clean && tsc --project ./tsconfig.json",
23
+ "clean": "rimraf ./dist"
24
+ },
25
+ "dependencies": {
26
+ "@allurereport/core-api": "3.0.0-beta.10"
27
+ },
28
+ "devDependencies": {
29
+ "@stylistic/eslint-plugin": "^2.6.1",
30
+ "@types/eslint": "^8.56.11",
31
+ "@typescript-eslint/eslint-plugin": "^8.0.0",
32
+ "@typescript-eslint/parser": "^8.0.0",
33
+ "@vitest/runner": "^2.1.8",
34
+ "allure-js-commons": "^3.0.9",
35
+ "allure-vitest": "^3.0.9",
36
+ "eslint": "^8.57.0",
37
+ "eslint-config-prettier": "^9.1.0",
38
+ "eslint-plugin-import": "^2.29.1",
39
+ "eslint-plugin-jsdoc": "^50.0.0",
40
+ "eslint-plugin-n": "^17.10.1",
41
+ "eslint-plugin-no-null": "^1.0.2",
42
+ "eslint-plugin-prefer-arrow": "^1.2.3",
43
+ "rimraf": "^6.0.1",
44
+ "tslib": "^2.7.0",
45
+ "typescript": "^5.6.3",
46
+ "vitest": "^2.1.8"
47
+ }
48
+ }