UNPKG

5.49 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2017 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18import { FirebaseApp } from '@firebase/app-types';
19import {
20 CompleteFn,
21 EmulatorMockTokenOptions,
22 FirebaseError,
23 NextFn,
24 Unsubscribe
25} from '@firebase/util';
26
27export interface FullMetadata extends UploadMetadata {
28 bucket: string;
29 fullPath: string;
30 generation: string;
31 metageneration: string;
32 name: string;
33 size: number;
34 timeCreated: string;
35 updated: string;
36}
37
38export interface Reference {
39 bucket: string;
40 child(path: string): Reference;
41 delete(): Promise<void>;
42 fullPath: string;
43 getDownloadURL(): Promise<string>;
44 getMetadata(): Promise<FullMetadata>;
45 name: string;
46 parent: Reference | null;
47 put(
48 data: Blob | Uint8Array | ArrayBuffer,
49 metadata?: UploadMetadata
50 ): UploadTask;
51 putString(
52 data: string,
53 format?: StringFormat,
54 metadata?: UploadMetadata
55 ): UploadTask;
56 root: Reference;
57 storage: FirebaseStorage;
58 toString(): string;
59 updateMetadata(metadata: SettableMetadata): Promise<FullMetadata>;
60 listAll(): Promise<ListResult>;
61 list(options?: ListOptions): Promise<ListResult>;
62}
63
64export interface ListResult {
65 prefixes: Reference[];
66 items: Reference[];
67 nextPageToken: string | null;
68}
69
70export interface ListOptions {
71 maxResults?: number | null;
72 pageToken?: string | null;
73}
74
75export interface SettableMetadata {
76 cacheControl?: string | null;
77 contentDisposition?: string | null;
78 contentEncoding?: string | null;
79 contentLanguage?: string | null;
80 contentType?: string | null;
81 customMetadata?: {
82 [/* warning: coerced from ? */ key: string]: string;
83 } | null;
84}
85
86export type StringFormat = string;
87export type TaskEvent = string;
88export type TaskState = string;
89
90export interface UploadMetadata extends SettableMetadata {
91 md5Hash?: string | null;
92}
93
94export interface StorageObserver<T> {
95 next?: NextFn<T> | null;
96 error?: (error: FirebaseStorageError) => void | null;
97 complete?: CompleteFn | null;
98}
99
100export enum StorageErrorCode {
101 UNKNOWN = 'unknown',
102 OBJECT_NOT_FOUND = 'object-not-found',
103 BUCKET_NOT_FOUND = 'bucket-not-found',
104 PROJECT_NOT_FOUND = 'project-not-found',
105 QUOTA_EXCEEDED = 'quota-exceeded',
106 UNAUTHENTICATED = 'unauthenticated',
107 UNAUTHORIZED = 'unauthorized',
108 UNAUTHORIZED_APP = 'unauthorized-app',
109 RETRY_LIMIT_EXCEEDED = 'retry-limit-exceeded',
110 INVALID_CHECKSUM = 'invalid-checksum',
111 CANCELED = 'canceled',
112 INVALID_EVENT_NAME = 'invalid-event-name',
113 INVALID_URL = 'invalid-url',
114 INVALID_DEFAULT_BUCKET = 'invalid-default-bucket',
115 NO_DEFAULT_BUCKET = 'no-default-bucket',
116 CANNOT_SLICE_BLOB = 'cannot-slice-blob',
117 SERVER_FILE_WRONG_SIZE = 'server-file-wrong-size',
118 NO_DOWNLOAD_URL = 'no-download-url',
119 INVALID_ARGUMENT = 'invalid-argument',
120 INVALID_ARGUMENT_COUNT = 'invalid-argument-count',
121 APP_DELETED = 'app-deleted',
122 INVALID_ROOT_OPERATION = 'invalid-root-operation',
123 INVALID_FORMAT = 'invalid-format',
124 INTERNAL_ERROR = 'internal-error',
125 UNSUPPORTED_ENVIRONMENT = 'unsupported-environment'
126}
127
128export interface FirebaseStorageError extends FirebaseError {
129 /**
130 * Stores custom error data unque to StorageError.
131 */
132 customData: {
133 serverResponse: string | null;
134 };
135
136 get status(): number;
137 set status(status: number);
138 /**
139 * Compares a StorageErrorCode against this error's code, filtering out the prefix.
140 */
141 _codeEquals(code: StorageErrorCode): boolean;
142 /**
143 * Optional response message that was added by the server.
144 */
145 get serverResponse(): null | string;
146 set serverResponse(serverResponse: string | null);
147}
148export interface UploadTask {
149 cancel(): boolean;
150 catch(onRejected: (error: FirebaseStorageError) => any): Promise<any>;
151 on(
152 event: TaskEvent,
153 nextOrObserver?:
154 | StorageObserver<UploadTaskSnapshot>
155 | null
156 | ((snapshot: UploadTaskSnapshot) => any),
157 error?: ((a: FirebaseStorageError) => any) | null,
158 complete?: Unsubscribe | null
159 ): Function;
160 pause(): boolean;
161 resume(): boolean;
162 snapshot: UploadTaskSnapshot;
163 then(
164 onFulfilled?: ((snapshot: UploadTaskSnapshot) => any) | null,
165 onRejected?: ((error: FirebaseStorageError) => any) | null
166 ): Promise<any>;
167}
168
169export interface UploadTaskSnapshot {
170 bytesTransferred: number;
171 metadata: FullMetadata;
172 ref: Reference;
173 state: TaskState;
174 task: UploadTask;
175 totalBytes: number;
176}
177
178export class FirebaseStorage {
179 private constructor();
180
181 app: FirebaseApp;
182 maxOperationRetryTime: number;
183 maxUploadRetryTime: number;
184 ref(path?: string): Reference;
185 refFromURL(url: string): Reference;
186 setMaxOperationRetryTime(time: number): void;
187 setMaxUploadRetryTime(time: number): void;
188
189 useEmulator(
190 host: string,
191 port: number,
192 options?: {
193 mockUserToken?: EmulatorMockTokenOptions | string;
194 }
195 ): void;
196}
197
198declare module '@firebase/component' {
199 interface NameServiceMapping {
200 'storage-compat': FirebaseStorage;
201 }
202}
203
\No newline at end of file