@google-cloud/storage definitions (#15155)

* @google-cloud/storage definitions

* Renamed [at]google-cloud to google-cloud and exposed API classes and interfaces at the top level

* Renamed to google-cloud__storage as per request.

* Renamed and moved into types directory.
* Removed individual files and consolidated all definitions into
index.d.ts file.
* Using <code>declare module</code> to create a single
<code>@google-cloud/storage</code> module that imports each individual
module:
** <code>@google-cloud/storage/bucket</code>
** <code>@google-cloud/storage/channel</code>
** <code>@google-cloud/storage/file</code>
** <code>@google-cloud/storage/storage</code>
* Updated README.

* Remove custom README.

* Removed redundant JSDoc comments.

* Fixed errors for passing nom test.
This commit is contained in:
Brian Love
2017-05-04 18:32:51 -06:00
committed by Mohamed Hegazy
parent 922f5aabf0
commit d41436966c
4 changed files with 774 additions and 0 deletions

View File

@@ -0,0 +1,398 @@
// import file system i/o api
import * as fs from "fs";
// import Google Cloud Storage
import {
Acl,
Bucket,
Channel,
File,
ApiResponse,
BucketConfig,
BucketFileOptions,
BucketGetOptions,
BucketMetadata,
BucketPrivacyOptions,
BucketQuery,
ChannelConfig,
DownloadOptions,
FileMetadata,
FilePrivateOptions,
ReadStreamOptions,
SignedPolicy,
SignedPolicyOptions,
SignedUrlConfig,
WriteStreamOptions,
UploadOptions,
Storage
} from "@google-cloud/storage";
/**
* Test the storage service.
* @class TestStorage
*/
export class TestStorage {
// constants
static BUCKET_CONFIG: BucketConfig = {
multiRegional: true
};
// import Storage class
static gcs: Storage;
// the bucket
private buckets: Bucket[] = [];
/**
* @constructor
*/
constructor() {
// nothing to do
}
/**
* Returns the bucket.
* @method bucket
* @param {string} name
* @return {Bucket}
*/
bucket(name: string): Bucket {
return TestStorage.gcs.bucket(name);
}
/**
* Create a new bucket.
* @param {string} name
* @param {BucketConfig} metadata
* @return {Promise<[Bucket, ApiResponse]>}
*/
createBucket(name: string, config?: BucketConfig): Promise<[Bucket, ApiResponse]> {
// overwrite default values with custom config
config = Object.assign(TestStorage.BUCKET_CONFIG, config);
return TestStorage.gcs.createBucket(name, config);
}
/**
* Query for buckets.
* @param {BucketQuery} query
*/
getBuckets(query?: BucketQuery): Promise<[Bucket[]]> {
return TestStorage.gcs.getBuckets(query);
}
}
/**
* The bucket API wrapper.
* @class TestBucket
*/
export class TestBucket {
// the bucket in the cloud
bucket: Bucket;
/**
* Create a bucket.
* @param {BucketConfig} config
* @return {Promise<[Bucket, ApiResponse]>}
*/
create(config?: BucketConfig): Promise<[Bucket, ApiResponse]> {
return this.bucket.create(config);
}
/**
* Create a channel that will be notified when objects in this bucket changes.
* @method createChannel
* @param {string} id
* @param {ChannelConfig} config
* @return {Promise<[Channel, ApiResponse]>}
*/
createChannel(id: string, config: ChannelConfig): Promise<[Channel, ApiResponse]> {
return this.bucket.createChannel(id, config);
}
/**
* Delete the bucket.
* @method delete
* @return {Promise<[ApiResponse]>}
*/
delete(): Promise<[ApiResponse]> {
return this.bucket.delete();
}
/**
* Iterate over the bucket's files, calling file.delete() on each.
* @method deleteFiles
* @return {Promise<void>}
*/
deleteFiles(query?: BucketQuery): Promise<void> {
return this.bucket.deleteFiles(query);
}
/**
* Check if the bucket exists.
* @method exists
* @return {Promise<[boolean]>}
*/
exists(): Promise<[boolean]> {
return this.bucket.exists();
}
/**
* Create a File object.
* @method file
* @param {string} name
* @param {BucketFileOptions} options
*/
file(name: string, options?: BucketFileOptions): File {
return this.bucket.file(name);
}
/**
* Get a bucket if it exists.
* @method get
* @param {BucketGetOptions} options
*/
get(options?: BucketGetOptions): Promise<[Bucket, ApiResponse]> {
return this.bucket.get(options);
}
/**
* Get File objects for the files currently in the bucket
* @method getFiles
* @param {BucketQuery} query
* @return {Promise<[File[]]>}
*/
getFiles(query?: BucketQuery): Promise<[File[]]> {
return this.bucket.getFiles(query);
}
/**
* Get File objects for the files currently in the bucket as a readable object stream.
* @method getFilesStream
* @param {BucketQuery} query
* @return {ReadStream}
*/
getFilesStream(query?: BucketQuery): fs.ReadStream {
return this.bucket.getFilesStream(query);
}
/**
* Get the bucket's metadata.
* @method getMetadata
* @return {Promise<[BucketMetadata, ApiResponse]>}
*/
getMetadata(): Promise<[BucketMetadata, ApiResponse]> {
return this.bucket.getMetadata();
}
/**
* Make the bucket listing private.
* @method makePrivate
* @param {} options
* @return Promise<[File[]]>
*/
makePrivate(options?: BucketPrivacyOptions): Promise<[File[]]> {
return this.bucket.makePrivate(options);
}
/**
* Make the bucket publicly readable.
* @method makePublic
* @param {} options
* @return Promise<[File[]]>
*/
makePublic(options?: BucketPrivacyOptions): Promise<[File[]]> {
return this.bucket.makePublic(options);
}
/**
* Set the bucket's metadata.
* @method setMetadata
* @param {BucketMetadata} metadata
* @return {Promise<[ApiResponse]>}
*/
setMetadata(metadata?: BucketMetadata): Promise<[ApiResponse]> {
return this.bucket.setMetadata(metadata);
}
/**
* Upload a file.
* @method upload
* @param {localPath} string
* @param {UploadOptions} options
*/
upload(localPath: string, options?: UploadOptions): Promise<[File]> {
return this.bucket.upload(localPath, options);
}
}
/**
* The file API wrapper.
* @class TestFile
*/
export class TestFile {
// the file in the cloud
file: File;
/**
* Copy this file to another file.
* By default, this will copy the file to the same bucket, but you can choose to copy it to another
* Bucket by providing a Bucket or File object or a URL starting with "gs:// ".
* @method copy
* @param {string} destination
* @return Promise<[File, ApiResponse]>
*/
copy(destination: string | Bucket | File): Promise<[File, ApiResponse]> {
return this.file.copy(destination);
}
/**
* Create a readable stream to read the contents of the remote file.
* It can be piped to a writable stream or listened to for 'data' events to read a file's contents.
* @method createReadStream
* @param {ReadStreamOptions} options
* @return {ReadStream}
*/
createReadStream(options?: ReadStreamOptions): fs.ReadStream {
return this.file.createReadStream(options);
}
/**
* Create a writable stream to overwrite the contents of the file in your bucket.
* @method createWriteStream
* @param {WriteStreamOptions} options
* @return {WriteStream}
*/
createWriteStream(options?: WriteStreamOptions): fs.WriteStream {
return this.file.createWriteStream(options);
}
/**
* Delete the file.
* @method delete
* @return {Promise<[ApiResponse]>}
*/
delete(): Promise<[ApiResponse]> {
return this.file.delete();
}
/**
* Convenience method to download a file into memory or to a local destination.
* @method download
* @param {DownloadOptions} options
* @return {Promise<[string]>}
*/
download(options?: DownloadOptions): Promise<[string]> {
return this.file.download(options);
}
/**
* Check if the file exists.
* @method exists
* @return {Promise<[boolean]>}
*/
exists(): Promise<[boolean]> {
return this.file.exists();
}
/**
* Get a file object and its metadata if it exists.
* @method get
* @return {Promise<[File, ApiResponse]>}
*/
get(): Promise<[File, ApiResponse]> {
return this.file.get();
}
/**
* Get the file's metadata.
* @method getMetadata
* @return {Promise<[FileMetadata, ApiResponse]>}
*/
getMetadata(): Promise<[FileMetadata, ApiResponse]> {
return this.file.getMetadata();
}
/**
* Get a signed policy document to allow a user to upload data with a POST request
* @method getSignedPolicy
* @param {SignedPolicyOptions} options
* @return {Promise<[SignedPolicy]>}
*/
getSignedPolicy(options?: SignedPolicyOptions): Promise<[SignedPolicy]> {
return this.file.getSignedPolicy(options);
}
/**
* Get a signed URL to allow limited time access to the file
* @method getSignedUrl
* @param {SignedUrlConfig} config
* @return {Promise<[string]>}
*/
getSignedUrl(config?: SignedUrlConfig): Promise<[string]> {
return this.file.getSignedUrl(config);
}
/**
* Make a file private to the project and remove all other permissions.
* @method makePrivate
* @param {FilePrivateOptions} options
* @return {Promise<[ApiResponse]>}
*/
makePrivate(options?: FilePrivateOptions): Promise<[ApiResponse]> {
return this.file.makePrivate(options);
}
/**
* Set a file to be publicly readable and maintain all previous permissions.
* @method makePublic
* @return {Promise<[ApiResponse]>}
*/
makePublic(): Promise<[ApiResponse]> {
return this.file.makePublic();
}
/**
* Move this file to another location.
* By default, this will move the file to the same bucket, but you can choose to move it to
* another Bucket by providing a Bucket or File object or a URL beginning with "gs:// ".
* @method move
* @param {string|Bucket|File} destination
* @return {Promise<[File, ApiResponse]>}
*/
move(destination: string | Bucket | File): Promise<[File, ApiResponse]> {
return this.file.move(destination);
}
/**
* Write arbitrary data to a file.
* @param {string} data
* @param {WriteStreamOptions} options
* @return {Promise<void>}
*/
save(data: string, options?: WriteStreamOptions): Promise<void> {
return this.file.save(data);
}
/**
* The Storage API allows you to use a custom key for server-side encryption.
* @param {string|Buffer} encryptionKey
* @return {File}
*/
setEncryptionKey(encryptionKey: string | Buffer): File {
return this.file.setEncryptionKey(encryptionKey);
}
/**
* Merge the given metadata with the current remote file's metadata.
* This will set metadata if it was previously unset or update previously set metadata.
* To unset previously set metadata, set its value to null.
* @method setMetadata
* @param {FileMetadata} metadata
* @return {Promise<[ApiResponse]>}
*/
setMetadata(metadata: FileMetadata): Promise<[ApiResponse]> {
return this.file.setMetadata(metadata);
}
}

353
types/google-cloud__storage/index.d.ts vendored Normal file
View File

@@ -0,0 +1,353 @@
// Type definitions for @google-cloud/storage 0.6
// Project: https://github.com/GoogleCloudPlatform/google-cloud-node/tree/master/packages/storage
// Definitions by: Brian Love <http://brianflove.com>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
declare module "@google-cloud/storage" {
export * from "@google-cloud/storage/bucket";
export * from "@google-cloud/storage/channel";
export * from "@google-cloud/storage/file";
export * from "@google-cloud/storage/storage";
}
declare module "@google-cloud/storage/bucket" {
import { ReadStream } from "fs";
import { Channel, ChannelConfig } from "@google-cloud/storage/channel";
import { File } from "@google-cloud/storage/file";
import {
Acl,
ApiResponse,
UploadOptions
} from "@google-cloud/storage/storage";
/**
* A bucket in the cloud.
*/
class Bucket {
acl: Acl;
combine(sources: string[] | File[], destination: string [] | File[]): Promise<[File, ApiResponse]>;
create(config?: BucketConfig): Promise<[Bucket, ApiResponse]>;
createChannel(id: string, config: ChannelConfig): Promise<[Channel, ApiResponse]>;
delete(): Promise<[ApiResponse]>;
deleteFiles(query?: BucketQuery): Promise<void>;
exists(): Promise<[boolean]>;
file(name: string, options?: BucketFileOptions): File;
get(options?: BucketGetOptions): Promise<[Bucket, ApiResponse]>;
getFiles(query?: BucketQuery): Promise<[File[]]>;
getFilesStream(query?: BucketQuery): ReadStream;
getMetadata(): Promise<[BucketMetadata, ApiResponse]>;
id: string;
makePrivate(options?: BucketPrivacyOptions): Promise<[File[]]>;
makePublic(options?: BucketPrivacyOptions): Promise<[File[]]>;
metadata: BucketMetadata;
name: string;
setMetadata(metadata?: BucketMetadata): Promise<[ApiResponse]>;
upload(localPath: string, options?: UploadOptions): Promise<[File]>;
}
/**
* Bucket configuration.
*/
interface BucketConfig {
coldline?: boolean;
dra?: boolean;
location?: string;
multiRegional?: boolean;
nearline?: boolean;
regional?: boolean;
versioning?: {
enabled?: boolean
};
}
/**
* Options for getting a file.
*/
interface BucketFileOptions {
generation?: string | number;
}
/**
* Options for getting a bucket.
*/
interface BucketGetOptions {
autoCreate?: boolean;
}
/**
* Bucket metadata.
*/
interface BucketMetadata {
etag?: string;
id?: string;
kind?: string;
location?: string;
metageneration?: string;
name?: string;
projectNumber?: string;
selfLink?: string;
storageClass?: string;
timeCreated?: string;
updated?: string;
}
/**
* The options for making the bucket private.
*/
interface BucketPrivacyOptions {
includeFiles?: boolean;
force?: boolean;
}
/**
* Query a bucket.
*/
interface BucketQuery {
autoPaginate?: boolean;
delimiter?: string;
prefix?: string;
maxApiCalls?: number;
maxResults?: number;
pageToken?: string;
versions?: boolean;
}
}
declare module "@google-cloud/storage/channel" {
import { ApiResponse } from "@google-cloud/storage/storage";
/**
* This class allows you interact with Google Cloud Storage.
*/
class Channel {
stop(): Promise<[ApiResponse]>;
}
/**
* Channel configuration.
*/
interface ChannelConfig {
address: string;
}
}
declare module "@google-cloud/storage/file" {
import { ReadStream, WriteStream } from "fs";
import { Bucket } from "@google-cloud/storage/bucket";
import {
Acl,
ApiResponse,
DownloadOptions,
ReadStreamOptions,
UploadOptions,
WriteStreamOptions
} from "@google-cloud/storage/storage";
/**
* Options for specifying the content length range.
*/
interface ContentLengthRange {
max?: number;
min?: number;
}
/**
* A file in the cloud.
*/
class File {
acl: Acl;
copy(destination: string | Bucket | File): Promise<[File, ApiResponse]>;
createReadStream(options?: ReadStreamOptions): ReadStream;
createWriteStream(options?: WriteStreamOptions): WriteStream;
delete(): Promise<[ApiResponse]>;
download(options?: DownloadOptions): Promise<[string]>;
exists(): Promise<[boolean]>;
get(): Promise<[File, ApiResponse]>;
getMetadata(): Promise<[FileMetadata, ApiResponse]>;
getSignedPolicy(options?: SignedPolicyOptions): Promise<[SignedPolicy]>;
getSignedUrl(config?: SignedUrlConfig): Promise<[string]>;
makePrivate(options?: FilePrivateOptions): Promise<[ApiResponse]>;
makePublic(): Promise<[ApiResponse]>;
move(destination: string | Bucket | File): Promise<[File, ApiResponse]>;
name: string;
save(data: string, options?: WriteStreamOptions): Promise<void>;
setEncryptionKey(encryptionKey: string | Buffer): File;
setMetadata(metadata: FileMetadata): Promise<[ApiResponse]>;
}
/**
* File metadata.
*/
interface FileMetadata {
contentType?: string;
}
/**
* Options when setting the file as private.
*/
interface FilePrivateOptions {
strict?: boolean;
}
/**
* A signed policy allowing a user to upload a file with a POST.
*/
interface SignedPolicy {
base64?: string;
signature?: string;
string?: string;
}
/**
* Options when obtaining the signed policy.
*/
interface SignedPolicyOptions {
acl?: string;
contentLengthRange?: ContentLengthRange;
equals?: string[] | [string[]];
expires?: number | string;
max?: number;
min?: number;
startsWith?: string[] | [string[]];
successRedirect?: string;
successStatus?: string;
}
/**
* Options when obtaining a temporary signed URL for a file.
*/
interface SignedUrlConfig {
action: string;
cname?: string;
contentMd5?: string;
contentType?: string;
expires?: number | string;
extensionHeaders?: {[key: string]: string};
promptSaveAs?: string;
responseDisposition?: string;
responseType?: string;
}
}
declare module "@google-cloud/storage/storage" {
import { ReadStream } from "fs";
import { Bucket, BucketConfig, BucketQuery } from "@google-cloud/storage/bucket";
import { Channel } from "@google-cloud/storage/channel";
import { FileMetadata } from "@google-cloud/storage/file";
/**
* Access control list for storage buckets and files.
*/
class Acl extends AclActions {
default: AclEntity;
owners: AclEntity;
readers: AclEntity;
writers: AclEntity;
}
/**
* Actions that can be performed on all ACL entities, including the root Acl.
*/
class AclActions {
add(options: AclOptions): Promise<[Acl, ApiResponse]>;
delete(options: AclOptions): Promise<[Acl, ApiResponse]>;
get(options: AclOptions): Promise<[Acl, ApiResponse]>;
update(options: AclOptions): Promise<[Acl, ApiResponse]>;
}
/**
* An object of convenience methods to add or delete reader ACL permissions for a given entity.
*/
class AclEntity extends AclActions {
addAllAuthenticatedUsers(): Promise<[Acl, ApiResponse]>;
addAllUsers(): Promise<[Acl, ApiResponse]>;
addDomain(entity: string): Promise<[Acl, ApiResponse]>;
addGroup(entity: string): Promise<[Acl, ApiResponse]>;
addProject(entity: string): Promise<[Acl, ApiResponse]>;
addUser(entity: string): Promise<[Acl, ApiResponse]>;
deleteAllAuthenticatedUsers(): Promise<[Acl, ApiResponse]>;
deleteAllUsers(): Promise<[Acl, ApiResponse]>;
deleteDomain(entity: string): Promise<[Acl, ApiResponse]>;
deleteGroup(entity: string): Promise<[Acl, ApiResponse]>;
deleteProject(entity: string): Promise<[Acl, ApiResponse]>;
deleteUser(entity: string): Promise<[Acl, ApiResponse]>;
}
/**
* ACL options.
*/
interface AclOptions {
entity?: string;
role?: string;
generation?: number;
}
/**
* The response object.
*/
interface ApiResponse {
etag?: string;
id?: string;
kind?: string;
location?: string;
metageneration?: string;
name?: string;
projectNumber?: string;
selfLink?: string;
storageClass?: string;
timeCreated?: string;
updated?: string;
}
/**
* The options when downloading a file.
*/
interface DownloadOptions extends ReadStreamOptions {
destination?: string;
}
/**
* Options when reading a file stream.
*/
interface ReadStreamOptions {
end?: number;
start?: number;
validation?: string | boolean;
}
/**
* Options when uploading file to bucket.
*/
interface UploadOptions extends WriteStreamOptions {
destination?: string;
}
/**
* Options when writing to a file stream.
*/
interface WriteStreamOptions {
gzip?: boolean;
metadata?: FileMetadata;
offset?: number;
predefinedAcl?: string;
private?: boolean;
public?: boolean;
resumable?: boolean;
uri?: string;
validation?: string | boolean;
}
/**
* The Storage class allows you interact with Google Cloud Storage.
*/
class Storage {
acl: Acl;
bucket(name: string|Bucket): Bucket;
channel(id: string, resourceId: string): Channel;
createBucket(name: string, metadata?: BucketConfig): Promise<[Bucket, ApiResponse]>;
getBuckets(query?: BucketQuery): Promise<[Bucket[]]>;
getBucketsStream(query?: BucketQuery): Promise<[ReadStream]>;
}
}

View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"google-cloud__storage-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }