Skip to content

Commit 3abbdee

Browse files
Nikita Matrosovstevengill
Nikita Matrosov
authored andcommitted
CB-12369: Add plugin typings from DefinitelyTyped
This closes #201
1 parent 8c0de8a commit 3abbdee

File tree

2 files changed

+379
-0
lines changed

2 files changed

+379
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "cordova-plugin-file",
33
"version": "4.3.2-dev",
44
"description": "Cordova File Plugin",
5+
"types": "./types/index.d.ts",
56
"cordova": {
67
"id": "cordova-plugin-file",
78
"platforms": [

types/index.d.ts

+378
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,378 @@
1+
// Type definitions for Apache Cordova File System plugin
2+
// Project: https://github.com/apache/cordova-plugin-file
3+
// Definitions by: Microsoft Open Technologies Inc <http://msopentech.com>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
//
6+
// Copyright (c) Microsoft Open Technologies, Inc.
7+
// Licensed under the MIT license.
8+
9+
interface Window {
10+
/**
11+
* Requests a filesystem in which to store application data.
12+
* @param type Whether the filesystem requested should be persistent, as defined above. Use one of TEMPORARY or PERSISTENT.
13+
* @param size This is an indicator of how much storage space, in bytes, the application expects to need.
14+
* @param successCallback The callback that is called when the user agent provides a filesystem.
15+
* @param errorCallback A callback that is called when errors happen, or when the request to obtain the filesystem is denied.
16+
*/
17+
requestFileSystem(
18+
type: LocalFileSystem,
19+
size: number,
20+
successCallback: (fileSystem: FileSystem) => void,
21+
errorCallback?: (fileError: FileError) => void): void;
22+
/**
23+
* Look up file system Entry referred to by local URL.
24+
* @param string url URL referring to a local file or directory
25+
* @param successCallback invoked with Entry object corresponding to URL
26+
* @param errorCallback invoked if error occurs retrieving file system entry
27+
*/
28+
resolveLocalFileSystemURL(url: string,
29+
successCallback: (entry: Entry) => void,
30+
errorCallback?: (error: FileError) => void): void;
31+
/**
32+
* Look up file system Entry referred to by local URI.
33+
* @param string uri URI referring to a local file or directory
34+
* @param successCallback invoked with Entry object corresponding to URI
35+
* @param errorCallback invoked if error occurs retrieving file system entry
36+
*/
37+
resolveLocalFileSystemURI(uri: string,
38+
successCallback: (entry: Entry) => void,
39+
errorCallback?: (error: FileError) => void): void;
40+
TEMPORARY: number;
41+
PERSISTENT: number;
42+
}
43+
44+
/** This interface represents a file system. */
45+
interface FileSystem {
46+
/* The name of the file system, unique across the list of exposed file systems. */
47+
name: string;
48+
/** The root directory of the file system. */
49+
root: DirectoryEntry;
50+
}
51+
52+
/**
53+
* An abstract interface representing entries in a file system,
54+
* each of which may be a File or DirectoryEntry.
55+
*/
56+
interface Entry {
57+
/** Entry is a file. */
58+
isFile: boolean;
59+
/** Entry is a directory. */
60+
isDirectory: boolean;
61+
/** The name of the entry, excluding the path leading to it. */
62+
name: string;
63+
/** The full absolute path from the root to the entry. */
64+
fullPath: string;
65+
/** The file system on which the entry resides. */
66+
fileSystem: FileSystem;
67+
nativeURL: string;
68+
/**
69+
* Look up metadata about this entry.
70+
* @param successCallback A callback that is called with the time of the last modification.
71+
* @param errorCallback A callback that is called when errors happen.
72+
*/
73+
getMetadata(
74+
successCallback: (metadata: Metadata) => void,
75+
errorCallback?: (error: FileError) => void): void;
76+
/**
77+
* Move an entry to a different location on the file system. It is an error to try to:
78+
* move a directory inside itself or to any child at any depth;move an entry into its parent if a name different from its current one isn't provided;
79+
* move a file to a path occupied by a directory;
80+
* move a directory to a path occupied by a file;
81+
* move any element to a path occupied by a directory which is not empty.
82+
* A move of a file on top of an existing file must attempt to delete and replace that file.
83+
* A move of a directory on top of an existing empty directory must attempt to delete and replace that directory.
84+
* @param parent The directory to which to move the entry.
85+
* @param newName The new name of the entry. Defaults to the Entry's current name if unspecified.
86+
* @param successCallback A callback that is called with the Entry for the new location.
87+
* @param errorCallback A callback that is called when errors happen.
88+
*/
89+
moveTo(parent: DirectoryEntry,
90+
newName?: string,
91+
successCallback?: (entry: Entry) => void,
92+
errorCallback?: (error: FileError) => void): void;
93+
/**
94+
* Copy an entry to a different location on the file system. It is an error to try to:
95+
* copy a directory inside itself or to any child at any depth;
96+
* copy an entry into its parent if a name different from its current one isn't provided;
97+
* copy a file to a path occupied by a directory;
98+
* copy a directory to a path occupied by a file;
99+
* copy any element to a path occupied by a directory which is not empty.
100+
* A copy of a file on top of an existing file must attempt to delete and replace that file.
101+
* A copy of a directory on top of an existing empty directory must attempt to delete and replace that directory.
102+
* Directory copies are always recursive--that is, they copy all contents of the directory.
103+
* @param parent The directory to which to move the entry.
104+
* @param newName The new name of the entry. Defaults to the Entry's current name if unspecified.
105+
* @param successCallback A callback that is called with the Entry for the new object.
106+
* @param errorCallback A callback that is called when errors happen.
107+
*/
108+
copyTo(parent: DirectoryEntry,
109+
newName?: string,
110+
successCallback?: (entry: Entry) => void,
111+
errorCallback?: (error: FileError) => void): void;
112+
/**
113+
* Returns a URL that can be used as the src attribute of a <video> or <audio> tag.
114+
* If that is not possible, construct a cdvfile:// URL.
115+
* @return string URL
116+
*/
117+
toURL(): string;
118+
/**
119+
* Return a URL that can be passed across the bridge to identify this entry.
120+
* @return string URL that can be passed across the bridge to identify this entry
121+
*/
122+
toInternalURL(): string;
123+
/**
124+
* Deletes a file or directory. It is an error to attempt to delete a directory that is not empty. It is an error to attempt to delete the root directory of a filesystem.
125+
* @param successCallback A callback that is called on success.
126+
* @param errorCallback A callback that is called when errors happen.
127+
*/
128+
remove(successCallback: () => void,
129+
errorCallback?: (error: FileError) => void): void;
130+
/**
131+
* Look up the parent DirectoryEntry containing this Entry. If this Entry is the root of its filesystem, its parent is itself.
132+
* @param successCallback A callback that is called with the time of the last modification.
133+
* @param errorCallback A callback that is called when errors happen.
134+
*/
135+
getParent(successCallback: (entry: Entry) => void,
136+
errorCallback?: (error: FileError) => void): void;
137+
}
138+
139+
/** This interface supplies information about the state of a file or directory. */
140+
interface Metadata {
141+
/** This is the time at which the file or directory was last modified. */
142+
modificationTime: Date;
143+
/** The size of the file, in bytes. This must return 0 for directories. */
144+
size: number;
145+
}
146+
147+
/** This interface represents a directory on a file system. */
148+
interface DirectoryEntry extends Entry {
149+
/**
150+
* Creates a new DirectoryReader to read Entries from this Directory.
151+
*/
152+
createReader(): DirectoryReader;
153+
/**
154+
* Creates or looks up a file.
155+
* @param path Either an absolute path or a relative path from this DirectoryEntry
156+
* to the file to be looked up or created.
157+
* It is an error to attempt to create a file whose immediate parent does not yet exist.
158+
* @param options If create and exclusive are both true, and the path already exists, getFile must fail.
159+
* If create is true, the path doesn't exist, and no other error occurs, getFile must create it as a zero-length file and return a corresponding FileEntry.
160+
* If create is not true and the path doesn't exist, getFile must fail.
161+
* If create is not true and the path exists, but is a directory, getFile must fail.
162+
* Otherwise, if no other error occurs, getFile must return a FileEntry corresponding to path.
163+
* @param successCallback A callback that is called to return the File selected or created.
164+
* @param errorCallback A callback that is called when errors happen.
165+
*/
166+
getFile(path: string, options?: Flags,
167+
successCallback?: (entry: FileEntry) => void,
168+
errorCallback?: (error: FileError) => void): void;
169+
/**
170+
* Creates or looks up a directory.
171+
* @param path Either an absolute path or a relative path from this DirectoryEntry
172+
* to the directory to be looked up or created.
173+
* It is an error to attempt to create a directory whose immediate parent does not yet exist.
174+
* @param options If create and exclusive are both true and the path already exists, getDirectory must fail.
175+
* If create is true, the path doesn't exist, and no other error occurs, getDirectory must create and return a corresponding DirectoryEntry.
176+
* If create is not true and the path doesn't exist, getDirectory must fail.
177+
* If create is not true and the path exists, but is a file, getDirectory must fail.
178+
* Otherwise, if no other error occurs, getDirectory must return a DirectoryEntry corresponding to path.
179+
* @param successCallback A callback that is called to return the Directory selected or created.
180+
* @param errorCallback A callback that is called when errors happen.
181+
*/
182+
getDirectory(path: string, options?: Flags,
183+
successCallback?: (entry: DirectoryEntry) => void,
184+
errorCallback?: (error: FileError) => void): void;
185+
/**
186+
* Deletes a directory and all of its contents, if any. In the event of an error (e.g. trying
187+
* to delete a directory that contains a file that cannot be removed), some of the contents
188+
* of the directory may be deleted. It is an error to attempt to delete the root directory of a filesystem.
189+
* @param successCallback A callback that is called on success.
190+
* @param errorCallback A callback that is called when errors happen.
191+
*/
192+
removeRecursively(successCallback: () => void,
193+
errorCallback?: (error: FileError) => void): void;
194+
}
195+
196+
/**
197+
* This dictionary is used to supply arguments to methods
198+
* that look up or create files or directories.
199+
*/
200+
interface Flags {
201+
/** Used to indicate that the user wants to create a file or directory if it was not previously there. */
202+
create?: boolean;
203+
/** By itself, exclusive must have no effect. Used with create, it must cause getFile and getDirectory to fail if the target path already exists. */
204+
exclusive?: boolean;
205+
}
206+
207+
/**
208+
* This interface lets a user list files and directories in a directory. If there are
209+
* no additions to or deletions from a directory between the first and last call to
210+
* readEntries, and no errors occur, then:
211+
* A series of calls to readEntries must return each entry in the directory exactly once.
212+
* Once all entries have been returned, the next call to readEntries must produce an empty array.
213+
* If not all entries have been returned, the array produced by readEntries must not be empty.
214+
* The entries produced by readEntries must not include the directory itself ["."] or its parent [".."].
215+
*/
216+
interface DirectoryReader {
217+
/**
218+
* Read the next block of entries from this directory.
219+
* @param successCallback Called once per successful call to readEntries to deliver the next
220+
* previously-unreported set of Entries in the associated Directory.
221+
* If all Entries have already been returned from previous invocations
222+
* of readEntries, successCallback must be called with a zero-length array as an argument.
223+
* @param errorCallback A callback indicating that there was an error reading from the Directory.
224+
*/
225+
readEntries(
226+
successCallback: (entries: Entry[]) => void,
227+
errorCallback?: (error: FileError) => void): void;
228+
}
229+
230+
/** This interface represents a file on a file system. */
231+
interface FileEntry extends Entry {
232+
/**
233+
* Creates a new FileWriter associated with the file that this FileEntry represents.
234+
* @param successCallback A callback that is called with the new FileWriter.
235+
* @param errorCallback A callback that is called when errors happen.
236+
*/
237+
createWriter(successCallback: (
238+
writer: FileWriter) => void,
239+
errorCallback?: (error: FileError) => void): void;
240+
/**
241+
* Returns a File that represents the current state of the file that this FileEntry represents.
242+
* @param successCallback A callback that is called with the File.
243+
* @param errorCallback A callback that is called when errors happen.
244+
*/
245+
file(successCallback: (file: File) => void,
246+
errorCallback?: (error: FileError) => void): void;
247+
}
248+
249+
/**
250+
* This interface provides methods to monitor the asynchronous writing of blobs
251+
* to disk using progress events and event handler attributes.
252+
*/
253+
interface FileSaver extends EventTarget {
254+
/** Terminate file operation */
255+
abort(): void;
256+
/**
257+
* The FileSaver object can be in one of 3 states. The readyState attribute, on getting,
258+
* must return the current state, which must be one of the following values:
259+
* INIT
260+
* WRITING
261+
* DONE
262+
*/
263+
readyState: number;
264+
/** Handler for writestart events. */
265+
onwritestart: (event: ProgressEvent) => void;
266+
/** Handler for progress events. */
267+
onprogress: (event: ProgressEvent) => void;
268+
/** Handler for write events. */
269+
onwrite: (event: ProgressEvent) => void;
270+
/** Handler for abort events. */
271+
onabort: (event: ProgressEvent) => void;
272+
/** Handler for error events. */
273+
onerror: (event: ProgressEvent) => void;
274+
/** Handler for writeend events. */
275+
onwriteend: (event: ProgressEvent) => void;
276+
/** The last error that occurred on the FileSaver. */
277+
error: Error;
278+
}
279+
280+
/**
281+
* This interface expands on the FileSaver interface to allow for multiple write
282+
* actions, rather than just saving a single Blob.
283+
*/
284+
interface FileWriter extends FileSaver {
285+
/**
286+
* The byte offset at which the next write to the file will occur. This always less or equal than length.
287+
* A newly-created FileWriter will have position set to 0.
288+
*/
289+
position: number;
290+
/**
291+
* The length of the file. If the user does not have read access to the file,
292+
* this will be the highest byte offset at which the user has written.
293+
*/
294+
length: number;
295+
/**
296+
* Write the supplied data to the file at position.
297+
* @param {Blob} data The blob to write.
298+
*/
299+
write(data: Blob): void;
300+
/**
301+
* The file position at which the next write will occur.
302+
* @param offset If nonnegative, an absolute byte offset into the file.
303+
* If negative, an offset back from the end of the file.
304+
*/
305+
seek(offset: number): void;
306+
/**
307+
* Changes the length of the file to that specified. If shortening the file, data beyond the new length
308+
* will be discarded. If extending the file, the existing data will be zero-padded up to the new length.
309+
* @param size The size to which the length of the file is to be adjusted, measured in bytes.
310+
*/
311+
truncate(size: number): void;
312+
}
313+
314+
/* FileWriter states */
315+
declare var FileWriter: {
316+
INIT: number;
317+
WRITING: number;
318+
DONE: number
319+
};
320+
321+
interface FileError {
322+
/** Error code */
323+
code: number;
324+
}
325+
326+
declare var FileError: {
327+
new (code: number): FileError;
328+
NOT_FOUND_ERR: number;
329+
SECURITY_ERR: number;
330+
ABORT_ERR: number;
331+
NOT_READABLE_ERR: number;
332+
ENCODING_ERR: number;
333+
NO_MODIFICATION_ALLOWED_ERR: number;
334+
INVALID_STATE_ERR: number;
335+
SYNTAX_ERR: number;
336+
INVALID_MODIFICATION_ERR: number;
337+
QUOTA_EXCEEDED_ERR: number;
338+
TYPE_MISMATCH_ERR: number;
339+
PATH_EXISTS_ERR: number;
340+
};
341+
342+
/*
343+
* Constants defined in fileSystemPaths
344+
*/
345+
interface Cordova {
346+
file: {
347+
/* Read-only directory where the application is installed. */
348+
applicationDirectory: string;
349+
/* Root of app's private writable storage */
350+
applicationStorageDirectory: string;
351+
/* Where to put app-specific data files. */
352+
dataDirectory: string;
353+
/* Cached files that should survive app restarts. Apps should not rely on the OS to delete files in here. */
354+
cacheDirectory: string;
355+
/* Android: the application space on external storage. */
356+
externalApplicationStorageDirectory: string;
357+
/* Android: Where to put app-specific data files on external storage. */
358+
externalDataDirectory: string;
359+
/* Android: the application cache on external storage. */
360+
externalCacheDirectory: string;
361+
/* Android: the external storage (SD card) root. */
362+
externalRootDirectory: string;
363+
/* iOS: Temp directory that the OS can clear at will. */
364+
tempDirectory: string;
365+
/* iOS: Holds app-specific files that should be synced (e.g. to iCloud). */
366+
syncedDataDirectory: string;
367+
/* iOS: Files private to the app, but that are meaningful to other applciations (e.g. Office files) */
368+
documentsDirectory: string;
369+
/* BlackBerry10: Files globally available to all apps */
370+
sharedDirectory: string
371+
}
372+
}
373+
374+
375+
declare enum LocalFileSystem {
376+
PERSISTENT=0,
377+
TEMPORARY=1
378+
}

0 commit comments

Comments
 (0)