Skip to content

Commit

Permalink
fix(storage): Support typing generation for the storage API (#1019)
Browse files Browse the repository at this point in the history
* fix(storage): Support typing generation for the storage API

* fix(storage): Added newline at eof

* fix(storage): Fixing an accidentally inserted import
  • Loading branch information
hiranya911 committed Sep 3, 2020
1 parent ddce5b6 commit f11aed7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 36 deletions.
12 changes: 6 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ var paths = {
'!src/credential.d.ts',
'!src/database.d.ts',
'!src/instance-id.d.ts',
'!src/security-rules.d.ts',
'!src/messaging.d.ts',
'!src/project-management.d.ts',
'!src/remote-config.d.ts',
'!src/messaging.d.ts',
'!src/security-rules.d.ts',
'!src/storage.d.ts',
],
};

Expand All @@ -71,7 +72,6 @@ const TEMPORARY_TYPING_EXCLUDES = [
'!lib/firebase-service.d.ts',
'!lib/auth/*.d.ts',
'!lib/machine-learning/*.d.ts',
'!lib/storage/*.d.ts',
'!lib/utils/*.d.ts',
];

Expand Down Expand Up @@ -110,10 +110,10 @@ gulp.task('compile', function() {

// Add header
.pipe(header(banner));

// Exclude typings that are unintended (currently excludes all auto-generated
// typings, but as services are refactored to auto-generate typings this will
// change). Moreover, all *-internal.d.ts typings should not be exposed to
// change). Moreover, all *-internal.d.ts typings should not be exposed to
// developers as it denotes internally used types.
if (declaration) {
const configuration = [
Expand Down Expand Up @@ -153,7 +153,7 @@ gulp.task('copyTypings', function() {
let workflow = gulp.src('src/*.d.ts')
// Add header
.pipe(header(banner));

if (declaration) {
workflow = workflow.pipe(filter(paths.curatedTypings));
}
Expand Down
21 changes: 2 additions & 19 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { Bucket } from '@google-cloud/storage';
import * as _firestore from '@google-cloud/firestore';
import { Agent } from 'http';

Expand All @@ -26,6 +25,7 @@ import * as _instanceId from './instance-id';
import * as _projectManagement from './project-management';
import * as _remoteConfig from './remote-config';
import * as _securityRules from './security-rules';
import * as _storage from './storage';

/* eslint-disable @typescript-eslint/ban-types */

Expand Down Expand Up @@ -651,24 +651,7 @@ declare namespace admin.messaging {
}

declare namespace admin.storage {

/**
* The default `Storage` service if no
* app is provided or the `Storage` service associated with the provided
* app.
*/
interface Storage {
/**
* Optional app whose `Storage` service to
* return. If not provided, the default `Storage` service will be returned.
*/
app: admin.app.App;
/**
* @returns A [Bucket](https://cloud.google.com/nodejs/docs/reference/storage/latest/Bucket)
* instance as defined in the `@google-cloud/storage` package.
*/
bucket(name?: string): Bucket;
}
export import Storage = _storage.admin.storage.Storage;
}

declare namespace admin.firestore {
Expand Down
39 changes: 39 additions & 0 deletions src/storage.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Bucket } from '@google-cloud/storage';
import * as _admin from './index.d';

export namespace admin.storage {

/**
* The default `Storage` service if no
* app is provided or the `Storage` service associated with the provided
* app.
*/
export interface Storage {
/**
* Optional app whose `Storage` service to
* return. If not provided, the default `Storage` service will be returned.
*/
app: _admin.app.App;
/**
* @returns A [Bucket](https://cloud.google.com/nodejs/docs/reference/storage/latest/Bucket)
* instance as defined in the `@google-cloud/storage` package.
*/
bucket(name?: string): Bucket;
}
}
32 changes: 32 additions & 0 deletions src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*!
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as _storage from './storage';
import { FirebaseApp } from '../firebase-app';
import * as firebaseAdmin from '../index';

export function storage(app?: FirebaseApp): _storage.Storage {
if (typeof(app) === 'undefined') {
app = firebaseAdmin.app();
}
return app.storage();
}

/* eslint-disable @typescript-eslint/no-namespace */
export namespace admin.storage {
/* eslint-disable @typescript-eslint/no-empty-interface */
export interface Storage extends _storage.Storage {}
}
19 changes: 9 additions & 10 deletions src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class StorageInternals implements FirebaseServiceInternalsInterface {
}

/**
* Storage service bound to the provided app.
* The default `Storage` service if no
* app is provided or the `Storage` service associated with the provided
* app.
*/
export class Storage implements FirebaseServiceInterface {
public readonly INTERNAL: StorageInternals = new StorageInternals();
Expand All @@ -50,6 +52,7 @@ export class Storage implements FirebaseServiceInterface {
/**
* @param {FirebaseApp} app The app for this Storage service.
* @constructor
* @internal
*/
constructor(app: FirebaseApp) {
if (!validator.isNonNullObject(app) || !('options' in app)) {
Expand Down Expand Up @@ -98,12 +101,10 @@ export class Storage implements FirebaseServiceInterface {
}

/**
* Returns a reference to a Google Cloud Storage bucket. Returned reference can be used to upload
* and download content from Google Cloud Storage.
*
* @param {string=} name Optional name of the bucket to be retrieved. If name is not specified,
* retrieves a reference to the default bucket.
* @return {Bucket} A Bucket object from the @google-cloud/storage library.
* @param name Optional name of the bucket to be retrieved. If name is not specified,
* retrieves a reference to the default bucket.
* @returns A [Bucket](https://cloud.google.com/nodejs/docs/reference/storage/latest/Bucket)
* instance as defined in the `@google-cloud/storage` package.
*/
public bucket(name?: string): Bucket {
const bucketName = (typeof name !== 'undefined')
Expand All @@ -120,9 +121,7 @@ export class Storage implements FirebaseServiceInterface {
}

/**
* Returns the app associated with this Storage instance.
*
* @return {FirebaseApp} The app associated with this Storage instance.
* @return The app associated with this Storage instance.
*/
get app(): FirebaseApp {
return this.appInternal;
Expand Down
3 changes: 2 additions & 1 deletion test/integration/typescript/src/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ describe('Init App', () => {
});

it('Should return a Cloud Storage client', () => {
const bucket: Bucket = app.storage().bucket('TestBucket');
const storage: admin.storage.Storage = app.storage();
const bucket: Bucket = storage.bucket('TestBucket');
expect(bucket.name).to.equal('TestBucket');
});

Expand Down

0 comments on commit f11aed7

Please sign in to comment.