nuxt-file-storage 0.3.2 → 0.3.3-beta.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
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
<!-- [](https://badges.pufler.dev) -->
|
|
6
6
|
[![npm version][npm-version-src]][npm-version-href]
|
|
7
7
|
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
8
|
-
[](https://nuxt.care/?search=nuxt-file-storage)
|
|
9
8
|
[![License][license-src]][license-href]
|
|
9
|
+
[](https://nuxt.care/?search=nuxt-file-storage)
|
|
10
10
|
[![Nuxt][nuxt-src]][nuxt-href]
|
|
11
11
|
|
|
12
12
|
Easy solution to store files in your nuxt apps. Be able to upload files from the frontend and receive them from the backend to then save the files in your project.
|
|
@@ -84,6 +84,20 @@ You can use Nuxt Storage to get the files from the `<input>` tag:
|
|
|
84
84
|
```
|
|
85
85
|
The `files` return a ref object that contains the files
|
|
86
86
|
|
|
87
|
+
The `clearFiles` function empties the files list without needing to reassign `files.value = []`. It also accepts an optional `Ref<HTMLInputElement | null>` to clear the file input element in the DOM:
|
|
88
|
+
|
|
89
|
+
```html
|
|
90
|
+
<template>
|
|
91
|
+
<input type="file" ref="fileInputRef" @input="handleFileInput" multiple />
|
|
92
|
+
<button @click="clearFiles(fileInputRef)">Clear</button>
|
|
93
|
+
</template>
|
|
94
|
+
|
|
95
|
+
<script setup>
|
|
96
|
+
const fileInputRef = ref<HTMLInputElement | null>(null)
|
|
97
|
+
const { handleFileInput, clearFiles } = useFileStorage()
|
|
98
|
+
</script>
|
|
99
|
+
```
|
|
100
|
+
|
|
87
101
|
> `handleFileInput` returns a promise in case you need to check if the file input has concluded
|
|
88
102
|
|
|
89
103
|
<br>
|
|
@@ -131,10 +145,57 @@ You have to create a new instance of `useFileStorage` for each input field
|
|
|
131
145
|
```
|
|
132
146
|
by calling a new `useFileStorage` instance you separate the internal logic between the inputs
|
|
133
147
|
|
|
148
|
+
<br>
|
|
149
|
+
|
|
150
|
+
#### Using with `defineExpose`
|
|
151
|
+
|
|
152
|
+
The `files` ref is iterable, so it works naturally with `defineExpose` in child components:
|
|
153
|
+
|
|
154
|
+
```html
|
|
155
|
+
<!-- Child Component -->
|
|
156
|
+
<template>
|
|
157
|
+
<input type="file" @input="handleFileInput" multiple />
|
|
158
|
+
</template>
|
|
159
|
+
|
|
160
|
+
<script setup>
|
|
161
|
+
const { handleFileInput, files, clearFiles } = useFileStorage()
|
|
162
|
+
|
|
163
|
+
defineExpose({
|
|
164
|
+
files,
|
|
165
|
+
handleFileInput,
|
|
166
|
+
clearFiles,
|
|
167
|
+
})
|
|
168
|
+
</script>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
```html
|
|
172
|
+
<!-- Parent Component -->
|
|
173
|
+
<template>
|
|
174
|
+
<ChildComponent ref="childRef" />
|
|
175
|
+
<button @click="iterateFiles">Show Files</button>
|
|
176
|
+
</template>
|
|
177
|
+
|
|
178
|
+
<script setup>
|
|
179
|
+
const childRef = ref()
|
|
180
|
+
|
|
181
|
+
const iterateFiles = () => {
|
|
182
|
+
// Direct iteration — no double .value needed
|
|
183
|
+
for (const file of childRef.value.files) {
|
|
184
|
+
console.log(file.name)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Traditional .value access still works
|
|
188
|
+
console.log(childRef.value.files.value)
|
|
189
|
+
}
|
|
190
|
+
</script>
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
<br>
|
|
194
|
+
|
|
134
195
|
### Handling files in the backend
|
|
135
196
|
using Nitro Server Engine, we will make an api route that receives the files and stores them in the folder `userFiles`
|
|
136
197
|
```ts
|
|
137
|
-
import { ServerFile } from "
|
|
198
|
+
import { ServerFile } from "#file-storage/types";
|
|
138
199
|
|
|
139
200
|
export default defineEventHandler(async (event) => {
|
|
140
201
|
const { files } = await readBody<{ files: ServerFile[] }>(event)
|
|
@@ -188,11 +249,11 @@ npm run dev
|
|
|
188
249
|
|
|
189
250
|
<!-- Badges -->
|
|
190
251
|
|
|
191
|
-
[npm-version-src]: https://
|
|
192
|
-
[npm-version-href]: https://
|
|
193
|
-
[npm-downloads-src]: https://
|
|
194
|
-
[npm-downloads-href]: https://
|
|
195
|
-
[license-src]: https://
|
|
196
|
-
[license-href]: https://
|
|
252
|
+
[npm-version-src]: https://npmx.dev/api/registry/badge/version/nuxt-file-storage
|
|
253
|
+
[npm-version-href]: https://npmx.dev/package/nuxt-file-storage
|
|
254
|
+
[npm-downloads-src]: https://npmx.dev/api/registry/badge/downloads-year/nuxt-file-storage
|
|
255
|
+
[npm-downloads-href]: https://npmx.dev/package/nuxt-file-storage
|
|
256
|
+
[license-src]: https://npmx.dev/api/registry/badge/license/nuxt-file-storage
|
|
257
|
+
[license-href]: https://npmx.dev/package/nuxt-file-storage
|
|
197
258
|
[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
|
|
198
259
|
[nuxt-href]: https://nuxt.com/modules/nuxt-file-storage
|
package/dist/module.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { type Ref } from 'vue';
|
|
1
2
|
type Options = {
|
|
2
3
|
clearOldFiles: boolean;
|
|
3
4
|
};
|
|
4
5
|
export default function (options?: Options): {
|
|
5
|
-
files:
|
|
6
|
+
files: Ref<ClientFile[], ClientFile[]> & Iterable<ClientFile>;
|
|
6
7
|
handleFileInput: (event: any) => Promise<void>;
|
|
7
|
-
clearFiles: () => void;
|
|
8
|
+
clearFiles: (fileInputRef?: Ref<HTMLInputElement | null>) => void;
|
|
8
9
|
};
|
|
9
10
|
export {};
|
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import { ref } from "vue";
|
|
1
|
+
import { ref, unref } from "vue";
|
|
2
|
+
function createIterableRef(initialValue) {
|
|
3
|
+
const refObj = ref(initialValue);
|
|
4
|
+
Object.defineProperty(refObj, Symbol.iterator, {
|
|
5
|
+
value: function* () {
|
|
6
|
+
yield* refObj.value;
|
|
7
|
+
},
|
|
8
|
+
enumerable: false,
|
|
9
|
+
configurable: true
|
|
10
|
+
});
|
|
11
|
+
return refObj;
|
|
12
|
+
}
|
|
2
13
|
export default function(options = { clearOldFiles: true }) {
|
|
3
|
-
const files =
|
|
14
|
+
const files = createIterableRef([]);
|
|
4
15
|
const serializeFile = (file) => {
|
|
5
16
|
return new Promise((resolve, reject) => {
|
|
6
17
|
const reader = new FileReader();
|
|
@@ -21,8 +32,12 @@ export default function(options = { clearOldFiles: true }) {
|
|
|
21
32
|
reader.readAsDataURL(file);
|
|
22
33
|
});
|
|
23
34
|
};
|
|
24
|
-
const clearFiles = () => {
|
|
35
|
+
const clearFiles = (fileInputRef) => {
|
|
25
36
|
files.value.splice(0, files.value.length);
|
|
37
|
+
const el = fileInputRef ? unref(fileInputRef) : null;
|
|
38
|
+
if (el) {
|
|
39
|
+
el.value = "";
|
|
40
|
+
}
|
|
26
41
|
};
|
|
27
42
|
const handleFileInput = async (event) => {
|
|
28
43
|
if (options.clearOldFiles) {
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-file-storage",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3-beta.1",
|
|
4
4
|
"description": "Easy solution to store files in your nuxt apps. Be able to upload files from the frontend and recieve them from the backend to then save the files in your project.",
|
|
5
|
-
"repository": "NyllRE/nuxt-file-storage",
|
|
6
5
|
"license": "MIT",
|
|
6
|
+
"repository": "NyllRE/nuxt-file-storage",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
7
10
|
"type": "module",
|
|
11
|
+
"main": "./dist/module.mjs",
|
|
8
12
|
"exports": {
|
|
9
13
|
".": {
|
|
10
14
|
"types": "./dist/types.d.mts",
|
|
11
15
|
"import": "./dist/module.mjs"
|
|
12
16
|
}
|
|
13
17
|
},
|
|
14
|
-
"main": "./dist/module.mjs",
|
|
15
|
-
"files": [
|
|
16
|
-
"dist"
|
|
17
|
-
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"prepack": "nuxt-module-build build",
|
|
20
20
|
"dev": "nuxi dev playground",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"lint": "oxlint .",
|
|
26
26
|
"test": "vitest run",
|
|
27
27
|
"test:watch": "vitest watch",
|
|
28
|
+
"test:ui": "vitest --ui",
|
|
28
29
|
"changelogen": "changelogen"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
@@ -36,7 +37,7 @@
|
|
|
36
37
|
"@nuxt/eslint-config": "^1.13.0",
|
|
37
38
|
"@nuxt/module-builder": "^1.0.2",
|
|
38
39
|
"@nuxt/schema": "^4.3.0",
|
|
39
|
-
"@nuxt/test-utils": "^
|
|
40
|
+
"@nuxt/test-utils": "^4.0.0",
|
|
40
41
|
"@types/node": "^20.17.19",
|
|
41
42
|
"@vitest/ui": "4.0.16",
|
|
42
43
|
"@vue/test-utils": "^2.4.6",
|