@ailuracode/alpine-export 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) ailuracode
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # @ailuracode/alpine-export
2
+
3
+ Programmatic file exports (browser downloads) for Alpine.js.
4
+
5
+ **[Full documentation →](../../docs/export.md)**
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @ailuracode/alpine-export alpinejs
11
+ ```
12
+
13
+ ## Quick example
14
+
15
+ ```js
16
+ import Alpine from "alpinejs";
17
+ import exportPlugin from "@ailuracode/alpine-export";
18
+
19
+ Alpine.plugin(exportPlugin);
20
+ Alpine.start();
21
+ ```
22
+
23
+ ```html
24
+ <button @click="await $export('Hello world', 'hello.txt')">
25
+ Export text
26
+ </button>
27
+
28
+ <button @click="await $export('/assets/report.pdf', 'report.pdf')">
29
+ Export file
30
+ </button>
31
+ ```
32
+
33
+ ## API summary
34
+
35
+ | | |
36
+ |-|-|
37
+ | **Magic** | `$export(source, options?)` |
38
+ | **Helpers** | `$export.isSupported()` |
39
+
40
+ Callable like `$clipboard` and `$share`.
41
+
42
+ ## License
43
+
44
+ MIT
@@ -0,0 +1,11 @@
1
+ /// <reference types="@types/alpinejs" />
2
+
3
+ export type { ExportMagic } from "./index.js";
4
+
5
+ declare global {
6
+ namespace Alpine {
7
+ interface Magics<T> {
8
+ $export: import("./index.js").ExportMagic;
9
+ }
10
+ }
11
+ }
@@ -0,0 +1,27 @@
1
+ import AlpineType from 'alpinejs';
2
+
3
+ type ExportOptions = {
4
+ filename?: string;
5
+ mimeType?: string;
6
+ };
7
+ type ExportSource = string | Blob | File;
8
+ type ExportMagic = ((source: ExportSource, options?: ExportOptions | string) => Promise<boolean>) & {
9
+ isSupported(): boolean;
10
+ };
11
+ /** Returns whether programmatic file exports are available in this environment. */
12
+ declare function isExportSupported(): boolean;
13
+ /** Exports a URL, blob, file, or text payload as a download. Resolves to `true` on success. Never throws. */
14
+ declare function exportData(source: ExportSource, options?: ExportOptions | string): Promise<boolean>;
15
+ /** Builds callable `$export` magic with `isSupported` helper. */
16
+ declare function createExportMagic(): ExportMagic;
17
+ /** Alpine.js export plugin. Registers callable magic `$export`. */
18
+ declare function exportPlugin(Alpine: AlpineType.Alpine): void;
19
+ declare global {
20
+ namespace Alpine {
21
+ interface Magics<T> {
22
+ $export: ExportMagic;
23
+ }
24
+ }
25
+ }
26
+
27
+ export { type ExportMagic, type ExportOptions, type ExportSource, createExportMagic, exportPlugin as default, exportData, isExportSupported };
package/dist/index.js ADDED
@@ -0,0 +1,84 @@
1
+ // src/index.ts
2
+ var URL_LIKE = /^(https?:|data:|blob:|\/|\.\/|\.\.\/)/i;
3
+ function normalizeOptions(options) {
4
+ if (typeof options === "string") {
5
+ return { filename: options };
6
+ }
7
+ return options ?? {};
8
+ }
9
+ function isExportEnvironment() {
10
+ return typeof document !== "undefined" && typeof document.createElement === "function" && typeof URL !== "undefined" && typeof URL.createObjectURL === "function";
11
+ }
12
+ function isExportSupported() {
13
+ return isExportEnvironment();
14
+ }
15
+ function triggerAnchorExport(href, filename) {
16
+ const anchor = document.createElement("a");
17
+ anchor.href = href;
18
+ anchor.rel = "noopener";
19
+ if (filename) {
20
+ anchor.download = filename;
21
+ }
22
+ anchor.style.display = "none";
23
+ document.body.appendChild(anchor);
24
+ anchor.click();
25
+ document.body.removeChild(anchor);
26
+ }
27
+ function revokeObjectUrlLater(url) {
28
+ setTimeout(() => {
29
+ URL.revokeObjectURL(url);
30
+ }, 0);
31
+ }
32
+ function exportBlob(blob, filename) {
33
+ const url = URL.createObjectURL(blob);
34
+ try {
35
+ triggerAnchorExport(url, filename);
36
+ return true;
37
+ } catch {
38
+ return false;
39
+ } finally {
40
+ revokeObjectUrlLater(url);
41
+ }
42
+ }
43
+ function isUrlLike(value) {
44
+ return URL_LIKE.test(value);
45
+ }
46
+ function exportData(source, options) {
47
+ if (!isExportSupported()) {
48
+ return Promise.resolve(false);
49
+ }
50
+ const { filename, mimeType } = normalizeOptions(options);
51
+ try {
52
+ if (source instanceof Blob) {
53
+ const name = filename ?? (source instanceof File ? source.name : "export");
54
+ return Promise.resolve(exportBlob(source, name));
55
+ }
56
+ const value = String(source);
57
+ if (isUrlLike(value)) {
58
+ triggerAnchorExport(value, filename);
59
+ return Promise.resolve(true);
60
+ }
61
+ if (!filename) {
62
+ return Promise.resolve(false);
63
+ }
64
+ const blob = new Blob([value], { type: mimeType ?? "text/plain;charset=utf-8" });
65
+ return Promise.resolve(exportBlob(blob, filename));
66
+ } catch {
67
+ return Promise.resolve(false);
68
+ }
69
+ }
70
+ function createExportMagic() {
71
+ const exportFile = (source, options) => exportData(source, options);
72
+ exportFile.isSupported = isExportSupported;
73
+ return exportFile;
74
+ }
75
+ function exportPlugin(Alpine) {
76
+ Alpine.magic("export", () => createExportMagic());
77
+ }
78
+ export {
79
+ createExportMagic,
80
+ exportPlugin as default,
81
+ exportData,
82
+ isExportSupported
83
+ };
84
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type AlpineType from \"alpinejs\";\n\nexport type ExportOptions = {\n filename?: string;\n mimeType?: string;\n};\n\nexport type ExportSource = string | Blob | File;\n\nexport type ExportMagic = ((\n source: ExportSource,\n options?: ExportOptions | string\n) => Promise<boolean>) & {\n isSupported(): boolean;\n};\n\nconst URL_LIKE = /^(https?:|data:|blob:|\\/|\\.\\/|\\.\\.\\/)/i;\n\nfunction normalizeOptions(options?: ExportOptions | string): ExportOptions {\n if (typeof options === \"string\") {\n return { filename: options };\n }\n\n return options ?? {};\n}\n\nfunction isExportEnvironment(): boolean {\n return (\n typeof document !== \"undefined\" &&\n typeof document.createElement === \"function\" &&\n typeof URL !== \"undefined\" &&\n typeof URL.createObjectURL === \"function\"\n );\n}\n\n/** Returns whether programmatic file exports are available in this environment. */\nexport function isExportSupported(): boolean {\n return isExportEnvironment();\n}\n\nfunction triggerAnchorExport(href: string, filename?: string): void {\n const anchor = document.createElement(\"a\");\n anchor.href = href;\n anchor.rel = \"noopener\";\n\n if (filename) {\n anchor.download = filename;\n }\n\n anchor.style.display = \"none\";\n document.body.appendChild(anchor);\n anchor.click();\n document.body.removeChild(anchor);\n}\n\nfunction revokeObjectUrlLater(url: string): void {\n setTimeout(() => {\n URL.revokeObjectURL(url);\n }, 0);\n}\n\nfunction exportBlob(blob: Blob, filename: string): boolean {\n const url = URL.createObjectURL(blob);\n\n try {\n triggerAnchorExport(url, filename);\n return true;\n } catch {\n return false;\n } finally {\n revokeObjectUrlLater(url);\n }\n}\n\nfunction isUrlLike(value: string): boolean {\n return URL_LIKE.test(value);\n}\n\n/** Exports a URL, blob, file, or text payload as a download. Resolves to `true` on success. Never throws. */\nexport function exportData(\n source: ExportSource,\n options?: ExportOptions | string\n): Promise<boolean> {\n if (!isExportSupported()) {\n return Promise.resolve(false);\n }\n\n const { filename, mimeType } = normalizeOptions(options);\n\n try {\n if (source instanceof Blob) {\n const name = filename ?? (source instanceof File ? source.name : \"export\");\n return Promise.resolve(exportBlob(source, name));\n }\n\n const value = String(source);\n\n if (isUrlLike(value)) {\n triggerAnchorExport(value, filename);\n return Promise.resolve(true);\n }\n\n if (!filename) {\n return Promise.resolve(false);\n }\n\n const blob = new Blob([value], { type: mimeType ?? \"text/plain;charset=utf-8\" });\n return Promise.resolve(exportBlob(blob, filename));\n } catch {\n return Promise.resolve(false);\n }\n}\n\n/** Builds callable `$export` magic with `isSupported` helper. */\nexport function createExportMagic(): ExportMagic {\n const exportFile = (source: ExportSource, options?: ExportOptions | string) =>\n exportData(source, options);\n exportFile.isSupported = isExportSupported;\n return exportFile;\n}\n\n/** Alpine.js export plugin. Registers callable magic `$export`. */\nexport default function exportPlugin(Alpine: AlpineType.Alpine): void {\n Alpine.magic(\"export\", () => createExportMagic());\n}\n\ndeclare global {\n namespace Alpine {\n interface Magics<T> {\n $export: ExportMagic;\n }\n }\n}\n"],"mappings":";AAgBA,IAAM,WAAW;AAEjB,SAAS,iBAAiB,SAAiD;AACzE,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO,EAAE,UAAU,QAAQ;AAAA,EAC7B;AAEA,SAAO,WAAW,CAAC;AACrB;AAEA,SAAS,sBAA+B;AACtC,SACE,OAAO,aAAa,eACpB,OAAO,SAAS,kBAAkB,cAClC,OAAO,QAAQ,eACf,OAAO,IAAI,oBAAoB;AAEnC;AAGO,SAAS,oBAA6B;AAC3C,SAAO,oBAAoB;AAC7B;AAEA,SAAS,oBAAoB,MAAc,UAAyB;AAClE,QAAM,SAAS,SAAS,cAAc,GAAG;AACzC,SAAO,OAAO;AACd,SAAO,MAAM;AAEb,MAAI,UAAU;AACZ,WAAO,WAAW;AAAA,EACpB;AAEA,SAAO,MAAM,UAAU;AACvB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Q,OAAO;AAC5B,aAAW,cAAc;AACzB,SAAO;AACT;AAGe,SAAR,aAA8B,QAAiC;AACpE,SAAO,MAAM,UAAU,MAAM,kBAAkB,CAAC;AAClD;","names":[]}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@ailuracode/alpine-export",
3
+ "version": "0.1.0",
4
+ "description": "Alpine.js file export magic",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "ailuracode",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/ailuracode/alpine.git",
14
+ "directory": "packages/export"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/ailuracode/alpine/issues"
18
+ },
19
+ "homepage": "https://github.com/ailuracode/alpine/tree/master/packages/export#readme",
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./global": {
30
+ "types": "./dist/global.d.ts"
31
+ }
32
+ },
33
+ "types": "./dist/global.d.ts",
34
+ "peerDependencies": {
35
+ "alpinejs": "^3.0.0",
36
+ "@types/alpinejs": "^3.13.11"
37
+ },
38
+ "peerDependenciesMeta": {
39
+ "@types/alpinejs": {
40
+ "optional": true
41
+ }
42
+ },
43
+ "keywords": [
44
+ "alpinejs",
45
+ "alpine",
46
+ "plugin",
47
+ "export",
48
+ "file",
49
+ "download"
50
+ ],
51
+ "scripts": {
52
+ "build": "tsup src/index.ts --format esm --dts --clean --sourcemap --out-dir dist && cp src/global.d.ts dist/global.d.ts",
53
+ "test": "vitest run --config ../../vitest.config.js test"
54
+ }
55
+ }