Skip to content

Commit 5458c3f

Browse files
committed
Improve documentation of the fileFromPath and fileFromPathSync functions.
1 parent 1b676c9 commit 5458c3f

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

lib/fileFromPath.ts

+39-7
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,24 @@ function createFileFromPath(
107107
/**
108108
* Creates a `File` referencing the one on a disk by given path. Synchronous version of the `fileFromPath`
109109
*
110-
* @param path Path to read a file from
111-
* @param filename Optional file name. If not presented, the path will be used to get it
112-
* @param options File options
110+
* @param path Path to a file
111+
* @param filename Optional name of the file. Will be passed as the second argument in `File` constructor. If not presented, the name will be taken from the file's path.
112+
* @param options Additional `File` options, except for `lastModified`.
113+
*
114+
* @example
115+
*
116+
* ```js
117+
* import {FormData, File} from "formdata-node"
118+
* import {fileFromPathSync} from "formdata-node/file-from-path"
119+
*
120+
* const form = new FormData()
121+
*
122+
* const file = fileFromPathSync("/path/to/some/file.txt")
123+
*
124+
* form.set("file", file)
125+
*
126+
* form.get("file") // -> Your `File` object
127+
* ```
113128
*/
114129
export function fileFromPathSync(path: string): File
115130
export function fileFromPathSync(path: string, filename?: string): File
@@ -127,15 +142,32 @@ export function fileFromPathSync(
127142
filenameOrOptions?: string | FileFromPathOptions,
128143
options: FileFromPathOptions = {}
129144
): File {
130-
return createFileFromPath(path, statSync(path), filenameOrOptions, options)
145+
const stats = statSync(path)
146+
147+
return createFileFromPath(path, stats, filenameOrOptions, options)
131148
}
132149

133150
/**
134151
* Creates a `File` referencing the one on a disk by given path.
135152
*
136-
* @param path Path to read a file from
137-
* @param filename Optional file name. If not presented, the path will be used to get it
138-
* @param options File options
153+
* @param path Path to a file
154+
* @param filename Optional name of the file. Will be passed as the second argument in `File` constructor. If not presented, the name will be taken from the file's path.
155+
* @param options Additional `File` options, except for `lastModified`.
156+
*
157+
* @example
158+
*
159+
* ```js
160+
* import {FormData, File} from "formdata-node"
161+
* import {fileFromPath} from "formdata-node/file-from-path"
162+
*
163+
* const form = new FormData()
164+
*
165+
* const file = await fileFromPath("/path/to/some/file.txt")
166+
*
167+
* form.set("file", file)
168+
*
169+
* form.get("file") // -> Your `File` object
170+
* ```
139171
*/
140172
export async function fileFromPath(path: string): Promise<File>
141173
export async function fileFromPath(

readme.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,18 @@ Available from `formdata-node/file-from-path` subpath.
405405
Creates a `File` referencing the one on a disk by given path.
406406

407407
- **{string}** path - Path to a file
408-
- **{string}** [filename] - Name of the file. Will be passed as second argument in `File` constructor. If not presented, the file path will be used to get it.
409-
- **{object}** [options = {}] - File options.
408+
- **{string}** [filename] - Optional name of the file. Will be passed as the second argument in `File` constructor. If not presented, the name will be taken from the file's path.
409+
- **{object}** [options = {}] - Additional `File` options, except for `lastModified`.
410410
- **{string}** [options.type = ""] - Returns the media type ([`MIME`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)) of the file represented by a `File` object.
411411

412412
### `fileFromPathSync(path[, filename, options]) -> {File}`
413413

414414
Available from `formdata-node/file-from-path` subpath.
415415

416416
Creates a `File` referencing the one on a disk by given path. Synchronous version of the `fileFromPath`.
417-
418417
- **{string}** path - Path to a file
419-
- **{string}** [filename] - Name of the file. Will be passed as second argument in `File` constructor. If not presented, the file path will be used to get it.
420-
- **{object}** [options = {}] - File options.
418+
- **{string}** [filename] - Optional name of the file. Will be passed as the second argument in `File` constructor. If not presented, the name will be taken from the file's path.
419+
- **{object}** [options = {}] - Additional `File` options, except for `lastModified`.
421420
- **{string}** [options.type = ""] - Returns the media type ([`MIME`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)) of the file represented by a `File` object.
422421

423422
### `isFile(value) -> {boolean}`

0 commit comments

Comments
 (0)