nuxt-toastflow 1.0.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 +21 -0
- package/README.md +70 -0
- package/package.json +43 -0
- package/src/module.ts +195 -0
- package/tsconfig.json +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Adrián Janočko
|
|
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,70 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
# nuxt-toastflow
|
|
4
|
+
|
|
5
|
+
Nuxt module wrapper for `vue-toastflow` so Toastflow works out of the box in Nuxt.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add nuxt-toastflow
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
// nuxt.config.ts
|
|
17
|
+
export default defineNuxtConfig({
|
|
18
|
+
modules: ["nuxt-toastflow"],
|
|
19
|
+
toastflow: {
|
|
20
|
+
config: {
|
|
21
|
+
position: "top-right",
|
|
22
|
+
duration: 5000,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```vue
|
|
29
|
+
<!-- app.vue -->
|
|
30
|
+
<template>
|
|
31
|
+
<ToastContainer />
|
|
32
|
+
<NuxtPage />
|
|
33
|
+
</template>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```vue
|
|
37
|
+
|
|
38
|
+
<script setup lang="ts">
|
|
39
|
+
// Optional:
|
|
40
|
+
// const toast = useToast();
|
|
41
|
+
|
|
42
|
+
toast.success({
|
|
43
|
+
title: "Saved",
|
|
44
|
+
description: "Your changes are live.",
|
|
45
|
+
});
|
|
46
|
+
</script>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Options
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
type NuxtToastflowOptions = {
|
|
53
|
+
config: Partial<ToastConfig>;
|
|
54
|
+
css: boolean;
|
|
55
|
+
componentName: string | false;
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Defaults:
|
|
60
|
+
|
|
61
|
+
- `config: {}`
|
|
62
|
+
- `css: true`
|
|
63
|
+
- `componentName: "ToastContainer"`
|
|
64
|
+
|
|
65
|
+
`ToastContainer` is auto-registered as a client-only component by the module.
|
|
66
|
+
You can use either auto-imported `toast` (Vue-like API) or `useToast()`.
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nuxt-toastflow",
|
|
3
|
+
"author": "adrianjanocko",
|
|
4
|
+
"description": "Nuxt wrapper module for vue-toastflow.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nuxt",
|
|
7
|
+
"toast",
|
|
8
|
+
"toastflow",
|
|
9
|
+
"notification",
|
|
10
|
+
"vue"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"homepage": "https://toastflow.top/",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/adrianjanocko/toastflow.git",
|
|
17
|
+
"directory": "packages/nuxt"
|
|
18
|
+
},
|
|
19
|
+
"version": "1.0.0",
|
|
20
|
+
"main": "src/module.ts",
|
|
21
|
+
"module": "src/module.ts",
|
|
22
|
+
"types": "src/module.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"README.md",
|
|
25
|
+
"package.json",
|
|
26
|
+
"tsconfig.json",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"nuxt": "^3.0.0 || ^4.0.0"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@nuxt/kit": "^3.17.5",
|
|
34
|
+
"vue-toastflow": "^1.1.8"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"tsup": "^8.5.1",
|
|
38
|
+
"typescript": "^5.9.3"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup src/module.ts --format cjs,esm --dts"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/module.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import type { ToastConfig } from "vue-toastflow";
|
|
2
|
+
import {
|
|
3
|
+
addComponent,
|
|
4
|
+
addImports,
|
|
5
|
+
addPluginTemplate,
|
|
6
|
+
addTemplate,
|
|
7
|
+
addTypeTemplate,
|
|
8
|
+
defineNuxtModule,
|
|
9
|
+
} from "@nuxt/kit";
|
|
10
|
+
|
|
11
|
+
export interface NuxtToastflowOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Global Toastflow config passed to createToastflow.
|
|
14
|
+
*/
|
|
15
|
+
config: Partial<ToastConfig>;
|
|
16
|
+
/**
|
|
17
|
+
* Automatically include Toastflow CSS.
|
|
18
|
+
*/
|
|
19
|
+
css: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Auto-register ToastContainer under this name. Set false to disable.
|
|
22
|
+
*/
|
|
23
|
+
componentName: string | false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function containsFunction(
|
|
27
|
+
value: unknown,
|
|
28
|
+
visited: Set<unknown> = new Set(),
|
|
29
|
+
): boolean {
|
|
30
|
+
if (typeof value === "function") {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!value || typeof value !== "object") {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (visited.has(value)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
visited.add(value);
|
|
42
|
+
|
|
43
|
+
if (Array.isArray(value)) {
|
|
44
|
+
return value.some(function (item: unknown): boolean {
|
|
45
|
+
return containsFunction(item, visited);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const objectValues = Object.values(value as Record<string, unknown>);
|
|
50
|
+
|
|
51
|
+
return objectValues.some(function (item: unknown): boolean {
|
|
52
|
+
return containsFunction(item, visited);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default defineNuxtModule<NuxtToastflowOptions>({
|
|
57
|
+
meta: {
|
|
58
|
+
name: "nuxt-toastflow",
|
|
59
|
+
configKey: "toastflow",
|
|
60
|
+
compatibility: {
|
|
61
|
+
nuxt: "^3.0.0 || ^4.0.0",
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
defaults: {
|
|
65
|
+
config: {},
|
|
66
|
+
css: true,
|
|
67
|
+
componentName: "ToastContainer",
|
|
68
|
+
},
|
|
69
|
+
setup(options, nuxt) {
|
|
70
|
+
if (containsFunction(options.config)) {
|
|
71
|
+
console.warn(
|
|
72
|
+
"[nuxt-toastflow] Functions in `toastflow.config` are not serializable in nuxt.config and will be omitted.",
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let serializedConfig = "{}";
|
|
77
|
+
try {
|
|
78
|
+
serializedConfig = JSON.stringify(options.config ?? {}, null, 2) ?? "{}";
|
|
79
|
+
} catch {
|
|
80
|
+
throw new Error(
|
|
81
|
+
"[nuxt-toastflow] `toastflow.config` must be JSON-serializable.",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
addPluginTemplate({
|
|
86
|
+
filename: "toastflow.mjs",
|
|
87
|
+
mode: "client",
|
|
88
|
+
getContents: function () {
|
|
89
|
+
return [
|
|
90
|
+
'import { createToastflow, toast } from "vue-toastflow";',
|
|
91
|
+
"",
|
|
92
|
+
"export default defineNuxtPlugin((nuxtApp) => {",
|
|
93
|
+
` nuxtApp.vueApp.use(createToastflow(${serializedConfig}));`,
|
|
94
|
+
"",
|
|
95
|
+
" return {",
|
|
96
|
+
" provide: {",
|
|
97
|
+
" toast,",
|
|
98
|
+
" },",
|
|
99
|
+
" };",
|
|
100
|
+
"});",
|
|
101
|
+
"",
|
|
102
|
+
].join("\n");
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const toastTemplate = addTemplate({
|
|
107
|
+
filename: "toastflow/toast.ts",
|
|
108
|
+
getContents: function () {
|
|
109
|
+
return [
|
|
110
|
+
'import { useNuxtApp } from "#app";',
|
|
111
|
+
"",
|
|
112
|
+
'type ToastApi = typeof import("vue-toastflow")["toast"];',
|
|
113
|
+
"",
|
|
114
|
+
"export function useToast(): ToastApi {",
|
|
115
|
+
" return useNuxtApp().$toast;",
|
|
116
|
+
"}",
|
|
117
|
+
"",
|
|
118
|
+
"export const toast: ToastApi = new Proxy({} as ToastApi, {",
|
|
119
|
+
" get(_target, key) {",
|
|
120
|
+
" const currentToast = useNuxtApp().$toast as Record<PropertyKey, unknown>;",
|
|
121
|
+
" const value = currentToast[key];",
|
|
122
|
+
" if (typeof value === \"function\") {",
|
|
123
|
+
" return (value as (...args: unknown[]) => unknown).bind(currentToast);",
|
|
124
|
+
" }",
|
|
125
|
+
" return value;",
|
|
126
|
+
" },",
|
|
127
|
+
"});",
|
|
128
|
+
"",
|
|
129
|
+
].join("\n");
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
addImports({
|
|
134
|
+
name: "useToast",
|
|
135
|
+
from: toastTemplate.dst,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
addImports({
|
|
139
|
+
name: "toast",
|
|
140
|
+
from: toastTemplate.dst,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
addTypeTemplate({
|
|
144
|
+
filename: "types/nuxt-toastflow.d.ts",
|
|
145
|
+
getContents: function () {
|
|
146
|
+
return [
|
|
147
|
+
'type ToastApi = typeof import("vue-toastflow")["toast"];',
|
|
148
|
+
"",
|
|
149
|
+
'declare module "#app" {',
|
|
150
|
+
" interface NuxtApp {",
|
|
151
|
+
" $toast: ToastApi;",
|
|
152
|
+
" }",
|
|
153
|
+
"}",
|
|
154
|
+
"",
|
|
155
|
+
'declare module "vue" {',
|
|
156
|
+
" interface ComponentCustomProperties {",
|
|
157
|
+
" $toast: ToastApi;",
|
|
158
|
+
" }",
|
|
159
|
+
"}",
|
|
160
|
+
"",
|
|
161
|
+
"export {};",
|
|
162
|
+
"",
|
|
163
|
+
].join("\n");
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
if (options.componentName) {
|
|
168
|
+
addComponent({
|
|
169
|
+
name: options.componentName,
|
|
170
|
+
export: "ToastContainer",
|
|
171
|
+
filePath: "vue-toastflow",
|
|
172
|
+
mode: "client",
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (options.css) {
|
|
177
|
+
const cssEntry = "vue-toastflow/src/styles.css";
|
|
178
|
+
const hasCssEntry = nuxt.options.css.some(function (entry) {
|
|
179
|
+
if (typeof entry === "string") {
|
|
180
|
+
return entry === cssEntry;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (Array.isArray(entry) && typeof entry[0] === "string") {
|
|
184
|
+
return entry[0] === cssEntry;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return false;
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
if (!hasCssEntry) {
|
|
191
|
+
nuxt.options.css.push(cssEntry);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
});
|
package/tsconfig.json
ADDED