Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ts): enable noImplicitAny on src/iam.ts #348

Merged
merged 2 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 122 additions & 56 deletions src/iam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,92 @@

import * as arrify from 'arrify';
import {promisifyAll} from '@google-cloud/promisify';
import * as r from 'request';
import * as is from 'is';
import { PubSub } from '.';
import { CallOptions } from 'google-gax';

/**
* @callback GetPolicyCallback
* @param {?Error} err Request error, if any.
* @param {object} acl The policy.
* @param {object} apiResponse The full API response.
*/
export interface GetPolicyCallback {
(err?: Error|null, acl?: Policy, apiResponse?: r.Response): void;
}

/**
* @callback SetPolicyCallback
* @param {?Error} err Request error, if any.
* @param {object} acl The policy.
* @param {object} apiResponse The full API response.
*/
export interface SetPolicyCallback {
(err?: Error|null, acl?: Policy, apiResponse?: r.Response): void;
}

/**
* @typedef {array} SetPolicyResponse
* @property {object} 0 The policy.
* @property {object} 1 The full API response.
*/
export type SetPolicyResponse = [Policy, r.Response];

/**
* @typedef {array} GetPolicyResponse
* @property {object} 0 The policy.
* @property {object} 1 The full API response.
*/
export type GetPolicyResponse = [Policy, r.Response];

/**
* @typedef {array} TestIamPermissionsResponse
* @property {object[]} 0 A subset of permissions that the caller is allowed.
* @property {object} 1 The full API response.
*/
export type TestIamPermissionsResponse = [object[], r.Response];

/**
* @callback TestIamPermissionsCallback
* @param {?Error} err Request error, if any.
* @param {object[]} permissions A subset of permissions that the caller is allowed.
* @param {object} apiResponse The full API response.
*/
export interface TestIamPermissionsCallback {
(err?: Error|null, permissions?: object|null, apiResponse?: r.Response): void;
}

/**
* @see https://cloud.google.com/pubsub/docs/reference/rest/v1/Policy#Expr
*/
export interface Expr {
expression: string;
title: string;
description: string;
location: string;
}

/**
* @see https://cloud.google.com/pubsub/docs/reference/rest/v1/Policy#Binding
*/
export interface Binding {
role: string;
members: string[];
condition?: Expr;
}

/**
* @see https://cloud.google.com/pubsub/docs/reference/rest/v1/Policy
*/
export interface Policy {
/**
* @deprecated
*/
version?: number;
etag?: string;
bindings: Binding[];
}

/**
* [IAM (Identity and Access Management)](https://cloud.google.com/pubsub/access_control)
Expand Down Expand Up @@ -66,6 +150,7 @@ export class IAM {
pubsub: PubSub;
request: typeof PubSub.prototype.request;
id: string;

constructor(pubsub: PubSub, id: string) {
if (pubsub.Promise) {
this.Promise = pubsub.Promise;
Expand All @@ -74,17 +159,7 @@ export class IAM {
this.request = pubsub.request.bind(pubsub);
this.id = id;
}
/**
* @typedef {array} GetPolicyResponse
* @property {object} 0 The policy.
* @property {object} 1 The full API response.
*/
/**
* @callback GetPolicyCallback
* @param {?Error} err Request error, if any.
* @param {object} acl The policy.
* @param {object} apiResponse The full API response.
*/

/**
* Get the IAM policy
*
Expand Down Expand Up @@ -115,35 +190,28 @@ export class IAM {
* const apiResponse = data[1];
* });
*/
getPolicy(gaxOpts, callback?) {
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}
getPolicy(gaxOpts?: CallOptions): Promise<GetPolicyCallback>;
getPolicy(callback: GetPolicyCallback): void;
getPolicy(gaxOpts: CallOptions, callback: GetPolicyCallback): void;
getPolicy(gaxOptsOrCallback?: CallOptions|GetPolicyCallback, callback?: GetPolicyCallback): Promise<GetPolicyCallback>|void {
let gaxOpts = typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {};
callback = typeof gaxOptsOrCallback === 'function' ? gaxOptsOrCallback : callback;

const reqOpts = {
resource: this.id,
};

this.request(
{
client: 'SubscriberClient',
method: 'getIamPolicy',
reqOpts: reqOpts,
gaxOpts: gaxOpts,
},
callback
callback!
);
}
/**
* @typedef {array} SetPolicyResponse
* @property {object} 0 The policy.
* @property {object} 1 The full API response.
*/
/**
* @callback SetPolicyCallback
* @param {?Error} err Request error, if any.
* @param {object} acl The policy.
* @param {object} apiResponse The full API response.
*/

/**
* Set the IAM policy
*
Expand All @@ -160,7 +228,7 @@ export class IAM {
*
* @see [Topics: setIamPolicy API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/setIamPolicy}
* @see [Subscriptions: setIamPolicy API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/setIamPolicy}
* @see [Policy]{@link https://cloud.google.com/pubsub/docs/reference/rest/Shared.Types/Policy}
* @see [Policy]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/Policy}
*
* @example
* const {PubSub} = require('@google-cloud/pubsub');
Expand Down Expand Up @@ -190,39 +258,33 @@ export class IAM {
* const apiResponse = data[1];
* });
*/
setPolicy(policy, gaxOpts, callback?) {
if (!is.object(policy)) {
setPolicy(policy: Policy, gaxOpts?: CallOptions): Promise<SetPolicyResponse>;
setPolicy(policy: Policy, gaxOpts: CallOptions, callback: SetPolicyCallback): void;
setPolicy(policy: Policy, callback: SetPolicyCallback): void;
setPolicy(policy: Policy, gaxOptsOrCallback?: CallOptions|SetPolicyCallback, callback?: SetPolicyCallback): Promise<SetPolicyResponse>|void {
if (!(typeof policy === 'object')) {
throw new Error('A policy object is required.');
}
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}

let gaxOpts = typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {};
callback = typeof gaxOptsOrCallback === 'function' ? gaxOptsOrCallback : callback;

const reqOpts = {
resource: this.id,
policy,
};

this.request(
{
client: 'SubscriberClient',
method: 'setIamPolicy',
reqOpts: reqOpts,
gaxOpts: gaxOpts,
},
callback
callback!
);
}
/**
* @typedef {array} TestIamPermissionsResponse
* @property {object[]} 0 A subset of permissions that the caller is allowed.
* @property {object} 1 The full API response.
*/
/**
* @callback TestIamPermissionsCallback
* @param {?Error} err Request error, if any.
* @param {object[]} permissions A subset of permissions that the caller is allowed.
* @param {object} apiResponse The full API response.
*/

/**
* Test a set of permissions for a resource.
*
Expand Down Expand Up @@ -283,18 +345,22 @@ export class IAM {
* const apiResponse = data[1];
* });
*/
testPermissions(permissions: string|string[], gaxOpts, callback?) {
if (!is.array(permissions) && !is.string(permissions)) {
testPermissions(permissions: string|string[], gaxOpts?: CallOptions): Promise<TestIamPermissionsResponse>;
testPermissions(permissions: string|string[], gaxOpts: CallOptions, callback: TestIamPermissionsCallback): void;
testPermissions(permissions: string|string[], callback: TestIamPermissionsCallback): void;
testPermissions(permissions: string|string[], gaxOptsOrCallback?: CallOptions|TestIamPermissionsCallback, callback?: TestIamPermissionsCallback): Promise<TestIamPermissionsResponse>|void {
if (!Array.isArray(permissions) && !(typeof permissions === 'string')) {
throw new Error('Permissions are required.');
}
if (is.fn(gaxOpts)) {
callback = gaxOpts;
gaxOpts = null;
}

let gaxOpts = typeof gaxOptsOrCallback === 'object' ? gaxOptsOrCallback : {};
callback = typeof gaxOptsOrCallback === 'function' ? gaxOptsOrCallback : callback;

const reqOpts = {
resource: this.id,
permissions: arrify(permissions),
};

this.request(
{
client: 'SubscriberClient',
Expand All @@ -304,15 +370,15 @@ export class IAM {
},
function(err, resp) {
if (err) {
callback(err, null, resp);
callback!(err, null, resp);
return;
}
const availablePermissions = arrify(resp.permissions);
const permissionHash = (permissions as string[]).reduce(function(acc, permission) {
acc[permission] = availablePermissions.indexOf(permission) > -1;
return acc;
}, {});
callback(null, permissionHash, resp);
}, {} as {[key: string]: boolean});
callback!(null, permissionHash, resp);
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions system-test/pubsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ describe('pubsub', function() {
topic.iam.getPolicy(function(err, policy) {
assert.ifError(err);

assert.deepStrictEqual(policy.bindings, []);
assert.strictEqual(policy.version, 0);
assert.deepStrictEqual(policy!.bindings, []);
assert.strictEqual(policy!.version, 0);

done();
});
Expand Down