nuxt-file-storage 0.3.2 → 0.3.3-beta.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/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,8 @@ 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 = []`
|
|
88
|
+
|
|
87
89
|
> `handleFileInput` returns a promise in case you need to check if the file input has concluded
|
|
88
90
|
|
|
89
91
|
<br>
|
|
@@ -131,10 +133,57 @@ You have to create a new instance of `useFileStorage` for each input field
|
|
|
131
133
|
```
|
|
132
134
|
by calling a new `useFileStorage` instance you separate the internal logic between the inputs
|
|
133
135
|
|
|
136
|
+
<br>
|
|
137
|
+
|
|
138
|
+
#### Using with `defineExpose`
|
|
139
|
+
|
|
140
|
+
The `files` ref is iterable, so it works naturally with `defineExpose` in child components:
|
|
141
|
+
|
|
142
|
+
```html
|
|
143
|
+
<!-- Child Component -->
|
|
144
|
+
<template>
|
|
145
|
+
<input type="file" @input="handleFileInput" multiple />
|
|
146
|
+
</template>
|
|
147
|
+
|
|
148
|
+
<script setup>
|
|
149
|
+
const { handleFileInput, files, clearFiles } = useFileStorage()
|
|
150
|
+
|
|
151
|
+
defineExpose({
|
|
152
|
+
files,
|
|
153
|
+
handleFileInput,
|
|
154
|
+
clearFiles,
|
|
155
|
+
})
|
|
156
|
+
</script>
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```html
|
|
160
|
+
<!-- Parent Component -->
|
|
161
|
+
<template>
|
|
162
|
+
<ChildComponent ref="childRef" />
|
|
163
|
+
<button @click="iterateFiles">Show Files</button>
|
|
164
|
+
</template>
|
|
165
|
+
|
|
166
|
+
<script setup>
|
|
167
|
+
const childRef = ref()
|
|
168
|
+
|
|
169
|
+
const iterateFiles = () => {
|
|
170
|
+
// Direct iteration — no double .value needed
|
|
171
|
+
for (const file of childRef.value.files) {
|
|
172
|
+
console.log(file.name)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Traditional .value access still works
|
|
176
|
+
console.log(childRef.value.files.value)
|
|
177
|
+
}
|
|
178
|
+
</script>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
<br>
|
|
182
|
+
|
|
134
183
|
### Handling files in the backend
|
|
135
184
|
using Nitro Server Engine, we will make an api route that receives the files and stores them in the folder `userFiles`
|
|
136
185
|
```ts
|
|
137
|
-
import { ServerFile } from "
|
|
186
|
+
import { ServerFile } from "#file-storage/types";
|
|
138
187
|
|
|
139
188
|
export default defineEventHandler(async (event) => {
|
|
140
189
|
const { files } = await readBody<{ files: ServerFile[] }>(event)
|
|
@@ -188,11 +237,11 @@ npm run dev
|
|
|
188
237
|
|
|
189
238
|
<!-- Badges -->
|
|
190
239
|
|
|
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://
|
|
240
|
+
[npm-version-src]: https://npmx.dev/api/registry/badge/version/nuxt-file-storage
|
|
241
|
+
[npm-version-href]: https://npmx.dev/package/nuxt-file-storage
|
|
242
|
+
[npm-downloads-src]: https://npmx.dev/api/registry/badge/downloads-year/nuxt-file-storage
|
|
243
|
+
[npm-downloads-href]: https://npmx.dev/package/nuxt-file-storage
|
|
244
|
+
[license-src]: https://npmx.dev/api/registry/badge/license/nuxt-file-storage
|
|
245
|
+
[license-href]: https://npmx.dev/package/nuxt-file-storage
|
|
197
246
|
[nuxt-src]: https://img.shields.io/badge/Nuxt-18181B?logo=nuxt.js
|
|
198
247
|
[nuxt-href]: https://nuxt.com/modules/nuxt-file-storage
|
package/dist/module.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
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
8
|
clearFiles: () => void;
|
|
8
9
|
};
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { ref } 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();
|
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.0",
|
|
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",
|