nuxt-file-storage 0.2.8 → 0.2.9

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
@@ -77,15 +77,17 @@ You can use Nuxt Storage to get the files from the `<input>` tag:
77
77
 
78
78
  <script setup>
79
79
  // handleFileInput can handle multiple files
80
- const { handleFileInput, files } = useFileStorage()
80
+ // clearOldFiles: true by default, each time the user adds files the `files` ref will be cleared
81
+ const { handleFileInput, files } = useFileStorage({ clearOldFiles: false })
81
82
  </script>
82
83
  ```
83
84
  The `files` return a ref object that contains the files
84
85
 
85
86
  > `handleFileInput` returns a promise in case you need to check if the file input has concluded
86
87
 
88
+ <br>
87
89
 
88
- Here's an example of using files to send them to the backend:
90
+ Here's an example of using files to send them to the backend
89
91
  ```html
90
92
  <template>
91
93
  <input type="file" @input="handleFileInput" />
@@ -105,7 +107,28 @@ const submit = async () => {
105
107
  }
106
108
  </script>
107
109
  ```
110
+ <br>
108
111
 
112
+ #### Handling multiple file input fields
113
+ You have to create a new instance of `useFileStorage` for each input field
114
+
115
+
116
+ ```html
117
+ <template>
118
+ <input type="file" @input="handleFileInput" multiple /> ← | 1 |
119
+ <input type="file" @input="profileInputHandler" /> ← | 2 |
120
+ </template>
121
+
122
+ <script setup>
123
+ const { handleFileInput, files } = useFileStorage() ← | 1 |
124
+
125
+ const {
126
+ handleFileInput: profileInputHandler,
127
+ files: profileImage
128
+ } = useFileStorage() ← | 2 |
129
+ </script>
130
+ ```
131
+ by calling a new `useFileStorage` instance you seperate the internal logic between the inputs
109
132
 
110
133
  ### Handling files in the backend
111
134
  using Nitro Server Engine, we will make an api route that recieves the files and stores them in the folder `userFiles`
package/dist/module.d.mts CHANGED
@@ -1,9 +1,24 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
2
 
3
+ interface ServerFile {
4
+ name: string
5
+ content: string
6
+ size: string
7
+ type: string
8
+ lastModified: string
9
+ }
10
+
11
+ interface ClientFile extends Blob {
12
+ content: string | ArrayBuffer | null | undefined
13
+ name: string
14
+ lastModified: number
15
+ }
16
+
3
17
  interface ModuleOptions {
4
- mount: string;
5
- version: string;
18
+ mount: string
19
+ version: string
6
20
  }
21
+
7
22
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
8
23
 
9
- export { type ModuleOptions, _default as default };
24
+ export { type ClientFile, type ModuleOptions, type ServerFile, _default as default };
package/dist/module.d.ts CHANGED
@@ -1,9 +1,24 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
2
 
3
+ interface ServerFile {
4
+ name: string
5
+ content: string
6
+ size: string
7
+ type: string
8
+ lastModified: string
9
+ }
10
+
11
+ interface ClientFile extends Blob {
12
+ content: string | ArrayBuffer | null | undefined
13
+ name: string
14
+ lastModified: number
15
+ }
16
+
3
17
  interface ModuleOptions {
4
- mount: string;
5
- version: string;
18
+ mount: string
19
+ version: string
6
20
  }
21
+
7
22
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
8
23
 
9
- export { type ModuleOptions, _default as default };
24
+ export { type ClientFile, type ModuleOptions, type ServerFile, _default as default };
package/dist/module.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "nuxt-file-storage",
3
3
  "configKey": "fileStorage",
4
- "version": "0.2.8"
4
+ "version": "0.2.9"
5
5
  }
package/dist/module.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { defineNuxtModule, logger, createResolver, addImportsDir, addServerScanDir } from '@nuxt/kit';
2
2
  import defu from 'defu';
3
3
 
4
- const version = "0.2.8";
4
+ const version = "0.2.9";
5
5
 
6
6
  const module = defineNuxtModule({
7
7
  meta: {
@@ -1,4 +1,22 @@
1
- export default function (): {
2
- files: import("vue").Ref<any[]>;
1
+ type Options = {
2
+ clearOldFiles: boolean;
3
+ };
4
+ export default function (options?: Options): {
5
+ files: import("vue").Ref<{
6
+ content: string | {
7
+ readonly byteLength: number;
8
+ slice: (begin: number, end?: number | undefined) => ArrayBuffer;
9
+ readonly [Symbol.toStringTag]: string;
10
+ } | null | undefined;
11
+ name: string;
12
+ lastModified: number;
13
+ readonly size: number;
14
+ readonly type: string;
15
+ arrayBuffer: () => Promise<ArrayBuffer>;
16
+ slice: (start?: number | undefined, end?: number | undefined, contentType?: string | undefined) => Blob;
17
+ stream: () => ReadableStream<Uint8Array>;
18
+ text: () => Promise<string>;
19
+ }[]>;
3
20
  handleFileInput: (event: any) => Promise<void>;
4
21
  };
22
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { ref } from "vue";
2
- export default function() {
2
+ export default function(options = { clearOldFiles: true }) {
3
3
  const files = ref([]);
4
4
  const serializeFile = (file) => {
5
5
  return new Promise((resolve, reject) => {
@@ -22,7 +22,9 @@ export default function() {
22
22
  });
23
23
  };
24
24
  const handleFileInput = async (event) => {
25
- files.value.splice(0);
25
+ if (options.clearOldFiles) {
26
+ files.value.splice(0, files.value.length);
27
+ }
26
28
  const promises = [];
27
29
  for (const file of event.target.files) {
28
30
  promises.push(serializeFile(file));
@@ -1,12 +1,18 @@
1
1
  /// <reference types="node" />
2
+ import type { ServerFile } from '../../../types';
2
3
  /**
3
4
  * #### Will store the file in the specified directory
4
5
  * @returns file name: `${filename}`.`${fileExtension}`
5
- * @prop file: provide the file object
6
- * @prop fileNameOrIdLength: you can pass a string or a number, if you enter a string it will be the file name, if you enter a number it will generate a unique ID
7
- * @prop filelocation: provide the folder you wish to locate the file in
6
+ * @prop `file`: provide the file object
7
+ * @prop `fileNameOrIdLength`: you can pass a string or a number, if you enter a string it will be the file name, if you enter a number it will generate a unique ID
8
+ * @prop `filelocation`: provide the folder you wish to locate the file in
8
9
  */
9
10
  export declare const storeFileLocally: (file: ServerFile, fileNameOrIdLength: string | number, filelocation?: string) => Promise<string>;
11
+ /**
12
+ *
13
+ * @param `filename`: the name of the file you want to delete
14
+ * @param `filelocation`: the folder where the file is located, if it is in the root folder you can leave it empty, if it is in a subfolder you can pass the name of the subfolder with a preceding slash: `/subfolder`
15
+ */
10
16
  export declare const deleteFile: (filename: string, filelocation?: string) => Promise<void>;
11
17
  /**
12
18
  Parses a data URL and returns an object with the binary data and the file extension.
package/dist/types.d.mts CHANGED
@@ -13,4 +13,4 @@ declare module 'nuxt/schema' {
13
13
  }
14
14
 
15
15
 
16
- export type { ModuleOptions, default } from './module.js'
16
+ export type { ClientFile, ModuleOptions, ServerFile, default } from './module.js'
package/dist/types.d.ts CHANGED
@@ -13,4 +13,4 @@ declare module 'nuxt/schema' {
13
13
  }
14
14
 
15
15
 
16
- export type { ModuleOptions, default } from './module'
16
+ export type { ClientFile, ModuleOptions, ServerFile, default } from './module'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-file-storage",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
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
5
  "repository": "NyllRE/nuxt-file-storage",
6
6
  "license": "MIT",