Skip to content

Commit

Permalink
refactor!: update IpAdressesTypes to IpAddressTypes (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon committed Apr 18, 2023
1 parent 3fd7ebb commit e38d392
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/cloud-sql-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {IpAdressesTypes, selectIpAddress} from './ip-addresses';
import {IpAddressTypes, selectIpAddress} from './ip-addresses';
import {InstanceConnectionInfo} from './instance-connection-info';
import {parseInstanceConnectionName} from './parse-instance-connection-name';
import {InstanceMetadata} from './sqladmin-fetcher';
Expand All @@ -34,7 +34,7 @@ interface Fetcher {
}

interface CloudSQLInstanceOptions {
ipType: IpAdressesTypes;
ipType: IpAddressTypes;
instanceConnectionName: string;
sqlAdminFetcher: Fetcher;
}
Expand All @@ -48,7 +48,7 @@ export class CloudSQLInstance {
return instance;
}

private readonly ipType: IpAdressesTypes;
private readonly ipType: IpAddressTypes;
private readonly sqlAdminFetcher: Fetcher;
private refreshTimeoutID?: ReturnType<typeof setTimeout>;
private closed = false;
Expand Down
12 changes: 6 additions & 6 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import tls from 'node:tls';
import {CloudSQLInstance} from './cloud-sql-instance';
import {getSocket} from './socket';
import {IpAdressesTypes} from './ip-addresses';
import {IpAddressTypes} from './ip-addresses';
import {SQLAdminFetcher} from './sqladmin-fetcher';
import {CloudSQLConnectorError} from './errors';

Expand All @@ -28,7 +28,7 @@ import {CloudSQLConnectorError} from './errors';
// };
// await connector.getOptions(connectionOptions);
export declare interface ConnectionOptions {
ipType: IpAdressesTypes;
ipType: IpAddressTypes;
instanceConnectionName: string;
}

Expand Down Expand Up @@ -56,7 +56,7 @@ class CloudSQLInstanceMap extends Map {
instanceConnectionName,
sqlAdminFetcher,
}: {
ipType: IpAdressesTypes;
ipType: IpAddressTypes;
instanceConnectionName: string;
sqlAdminFetcher: SQLAdminFetcher;
}): Promise<void> {
Expand Down Expand Up @@ -88,7 +88,7 @@ class CloudSQLInstanceMap extends Map {
ipType,
}: {
instanceConnectionName: string;
ipType: IpAdressesTypes;
ipType: IpAddressTypes;
}): CloudSQLInstance {
const connectionInstance = this.get(instanceConnectionName);
if (!connectionInstance) {
Expand All @@ -109,8 +109,8 @@ class CloudSQLInstanceMap extends Map {
}
}

const getIpAddressType = (type: IpAdressesTypes): IpAdressesTypes | undefined =>
Object.values(IpAdressesTypes).find(x => x === type);
const getIpAddressType = (type: IpAddressTypes): IpAddressTypes | undefined =>
Object.values(IpAddressTypes).find(x => x === type);

// The Connector class is the main public API to interact
// with the Cloud SQL Node.js Connector.
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import {Connector, ConnectionOptions, DriverOptions} from './connector';
import {IpAdressesTypes} from './ip-addresses';
import {IpAddressTypes} from './ip-addresses';

export {Connector, type ConnectionOptions, type DriverOptions};
export {IpAdressesTypes};
export {IpAddressTypes};
20 changes: 10 additions & 10 deletions src/ip-addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
import {sqladmin_v1beta4} from '@googleapis/sqladmin';
import {CloudSQLConnectorError} from './errors';

export enum IpAdressesTypes {
export enum IpAddressTypes {
PUBLIC = 'PUBLIC',
PRIVATE = 'PRIVATE',
}

export declare interface IpAdresses {
export declare interface IpAddresses {
public?: string;
private?: string;
}

const getPublicIpAddress = (ipAddresses: IpAdresses) => {
const getPublicIpAddress = (ipAddresses: IpAddresses) => {
if (!ipAddresses.public) {
throw new CloudSQLConnectorError({
message: 'Cannot connect to instance, public Ip address not found',
Expand All @@ -35,7 +35,7 @@ const getPublicIpAddress = (ipAddresses: IpAdresses) => {
return ipAddresses.public;
};

const getPrivateIpAddress = (ipAddresses: IpAdresses) => {
const getPrivateIpAddress = (ipAddresses: IpAddresses) => {
if (!ipAddresses.private) {
throw new CloudSQLConnectorError({
message: 'Cannot connect to instance, private Ip address not found',
Expand All @@ -47,15 +47,15 @@ const getPrivateIpAddress = (ipAddresses: IpAdresses) => {

export function parseIpAddresses(
ipResponse: sqladmin_v1beta4.Schema$IpMapping[] | undefined
): IpAdresses {
): IpAddresses {
if (!ipResponse) {
throw new CloudSQLConnectorError({
message: 'Cannot connect to instance, it has no supported IP addresses',
code: 'ENOSQLADMINIPADDRESS',
});
}

const ipAddresses: IpAdresses = {};
const ipAddresses: IpAddresses = {};
for (const ip of ipResponse) {
if (ip.type === 'PRIMARY' && ip.ipAddress) {
ipAddresses.public = ip.ipAddress;
Expand All @@ -76,13 +76,13 @@ export function parseIpAddresses(
}

export function selectIpAddress(
ipAddresses: IpAdresses,
type: IpAdressesTypes | unknown
ipAddresses: IpAddresses,
type: IpAddressTypes | unknown
): string {
switch (type) {
case IpAdressesTypes.PUBLIC:
case IpAddressTypes.PUBLIC:
return getPublicIpAddress(ipAddresses);
case IpAdressesTypes.PRIVATE:
case IpAddressTypes.PRIVATE:
return getPrivateIpAddress(ipAddresses);
default:
throw new CloudSQLConnectorError({
Expand Down
4 changes: 2 additions & 2 deletions src/sqladmin-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const {Sqladmin} = sqladmin_v1beta4;
import {InstanceConnectionInfo} from './instance-connection-info';
import {SslCert} from './ssl-cert';
import {parseCert} from './crypto';
import {IpAdresses, parseIpAddresses} from './ip-addresses';
import {IpAddresses, parseIpAddresses} from './ip-addresses';
import {CloudSQLConnectorError} from './errors';

export interface InstanceMetadata {
ipAddresses: IpAdresses;
ipAddresses: IpAddresses;
serverCaCert: SslCert;
}

Expand Down
6 changes: 3 additions & 3 deletions test/cloud-sql-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import t from 'tap';
import {IpAdressesTypes} from '../src/ip-addresses';
import {IpAddressTypes} from '../src/ip-addresses';
import {CA_CERT, CLIENT_CERT, CLIENT_KEY} from './fixtures/certs';
import {setupCredentials} from './fixtures/setup-credentials';

Expand Down Expand Up @@ -56,7 +56,7 @@ t.test('CloudSQLInstance', async t => {
});

const instance = await CloudSQLInstance.getCloudSQLInstance({
ipType: IpAdressesTypes.PUBLIC,
ipType: IpAddressTypes.PUBLIC,
instanceConnectionName: 'my-project:us-east1:my-instance',
sqlAdminFetcher: fetcher,
});
Expand Down Expand Up @@ -93,7 +93,7 @@ t.test('CloudSQLInstance', async t => {
const start = Date.now();
let refreshCount = 0;
const refreshInstance = new CloudSQLInstance({
ipType: IpAdressesTypes.PUBLIC,
ipType: IpAddressTypes.PUBLIC,
instanceConnectionName: 'my-project:us-east1:my-instance',
sqlAdminFetcher: fetcher,
});
Expand Down
4 changes: 2 additions & 2 deletions test/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import t from 'tap';
import {Connector} from '../src/connector';
import {setupCredentials} from './fixtures/setup-credentials';
import {IpAdressesTypes} from '../src/ip-addresses';
import {IpAddressTypes} from '../src/ip-addresses';
import {CA_CERT, CLIENT_CERT, CLIENT_KEY} from './fixtures/certs';

t.test('Connector', async t => {
Expand Down Expand Up @@ -70,7 +70,7 @@ t.test('Connector invalid type error', async t => {
const connector = new Connector();
t.rejects(
connector.getOptions({
ipType: 'foo' as IpAdressesTypes,
ipType: 'foo' as IpAddressTypes,
instanceConnectionName: 'my-project:us-east1:my-instance',
}),
{
Expand Down
4 changes: 2 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import t from 'tap';
import {Connector, IpAdressesTypes} from '../src/index';
import {Connector, IpAddressTypes} from '../src/index';

t.ok(Connector, 'should export Connector constructor');
t.ok(IpAdressesTypes, 'should export IpAdressesTypes enum');
t.ok(IpAddressTypes, 'should export IpAddressTypes enum');
10 changes: 5 additions & 5 deletions test/ip-addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import t from 'tap';
import {
IpAdressesTypes,
IpAddressTypes,
parseIpAddresses,
selectIpAddress,
} from '../src/ip-addresses';
Expand Down Expand Up @@ -101,13 +101,13 @@ t.same(
);

t.throws(
() => selectIpAddress({}, IpAdressesTypes.PUBLIC),
() => selectIpAddress({}, IpAddressTypes.PUBLIC),
{code: 'ENOPUBLICSQLADMINIPADDRESS'},
'should throw if no public ip defined'
);

t.throws(
() => selectIpAddress({}, IpAdressesTypes.PRIVATE),
() => selectIpAddress({}, IpAddressTypes.PRIVATE),
{code: 'ENOPRIVATESQLADMINIPADDRESS'},
'should throw if no private ip defined'
);
Expand All @@ -124,7 +124,7 @@ t.same(
public: '0.0.0.0',
private: '0.0.0.2',
},
IpAdressesTypes.PUBLIC
IpAddressTypes.PUBLIC
),
'0.0.0.0',
'should select public ip'
Expand All @@ -135,7 +135,7 @@ t.same(
{
private: '0.0.0.2',
},
IpAdressesTypes.PRIVATE
IpAddressTypes.PRIVATE
),
'0.0.0.2',
'should select private ip'
Expand Down

0 comments on commit e38d392

Please sign in to comment.