Skip to content

Commit 3426039

Browse files
committed
Validated whether connector information is passed in correctly
1 parent 4254b10 commit 3426039

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

packages/data-connect/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"test:all": "npm run test:node",
3939
"test:browser": "karma start --single-run",
4040
"test:node": "TS_NODE_FILES=true TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/{,!(browser)/**/}*.test.ts' --file src/index.node.ts --config ../../config/mocharc.node.js",
41+
"test:unit": "TS_NODE_FILES=true TS_NODE_CACHE=NO TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'test/unit/**/*.test.ts' --file src/index.node.ts --config ../../config/mocharc.node.js",
4142
"test:emulator": "ts-node --compiler-options='{\"module\":\"commonjs\"}' ../../scripts/emulator-testing/dataconnect-test-runner.ts",
4243
"api-report": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' ts-node ../../repo-scripts/prune-dts/extract-public-api.ts --package data-connect --packageRoot . --typescriptDts ./dist/src/index.d.ts --rollupDts ./dist/private.d.ts --untrimmedRollupDts ./dist/internal.d.ts --publicDts ./dist/public.d.ts && yarn api-report:api-json",
4344
"api-report:api-json": "rm -rf temp && api-extractor run --local --verbose",

packages/data-connect/src/api/DataConnect.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export function getDataConnect(
210210
app = appOrOptions;
211211
}
212212

213-
if (!app) {
213+
if (!app || Object.keys(app).length === 0) {
214214
app = getApp();
215215
}
216216
const provider = _getProvider(app, 'data-connect');
@@ -224,9 +224,8 @@ export function getDataConnect(
224224
return dcInstance;
225225
}
226226
}
227-
if (!dcOptions) {
228-
throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
229-
}
227+
validateDCOptions(dcOptions);
228+
230229
logDebug('Creating new DataConnect instance');
231230
// Initialize with options.
232231
return provider.initialize({
@@ -235,6 +234,19 @@ export function getDataConnect(
235234
});
236235
}
237236

237+
export function validateDCOptions(dcOptions: ConnectorConfig) {
238+
const fields = ['connector', 'location', 'service'];
239+
if (!dcOptions) {
240+
throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
241+
}
242+
fields.forEach(field => {
243+
if (dcOptions[field] === null || dcOptions[field] === undefined) {
244+
throw new DataConnectError(Code.INVALID_ARGUMENT, `${field} Required`);
245+
}
246+
});
247+
return true;
248+
}
249+
238250
/**
239251
* Delete DataConnect instance
240252
* @param dataConnect DataConnect instance

packages/data-connect/src/register.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { name, version } from '../package.json';
2626
import { setSDKVersion } from '../src/core/version';
2727

2828
import { DataConnect, ConnectorConfig } from './api/DataConnect';
29+
import { Code, DataConnectError } from './core/error';
2930

3031
export function registerDataConnect(variant?: string): void {
3132
setSDKVersion(SDK_VERSION);
@@ -39,6 +40,9 @@ export function registerDataConnect(variant?: string): void {
3940
if (settings) {
4041
newOpts = JSON.parse(settings);
4142
}
43+
if(!app.options.projectId) {
44+
throw new DataConnectError(Code.INVALID_ARGUMENT, "Project ID must be provided. Did you pass in a proper projectId to initializeApp?");
45+
}
4246
return new DataConnect(
4347
app,
4448
{ ...newOpts, projectId: app.options.projectId! },
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { deleteApp, initializeApp } from "@firebase/app";
2+
import { ConnectorConfig, getDataConnect } from "../../src";
3+
import { expect } from "chai";
4+
5+
describe('Data Connect Test', () => {
6+
it('should throw an error if `projectId` is not provided', () => {
7+
const app = initializeApp({});
8+
expect(() => getDataConnect({ connector: 'c', location: 'l', service: 's'})).to.throw('Project ID must be provided. Did you pass in a proper projectId to initializeApp?');
9+
deleteApp(app);
10+
});
11+
it('should not throw an error if `projectId` is provided', () => {
12+
const projectId = 'p';
13+
initializeApp({ projectId});
14+
expect(() => getDataConnect({ connector: 'c', location: 'l', service: 's'})).to.not.throw('Project ID must be provided. Did you pass in a proper projectId to initializeApp?');
15+
const dc = getDataConnect({ connector: 'c', location: 'l', service: 's'});
16+
expect(dc.app.options.projectId).to.eq(projectId);
17+
});
18+
it('should throw an error if `connectorConfig` is not provided', () => {
19+
const projectId = 'p';
20+
initializeApp({ projectId});
21+
expect(() => getDataConnect({ } as ConnectorConfig)).to.throw('DC Option Required');
22+
const dc = getDataConnect({ connector: 'c', location: 'l', service: 's'});
23+
expect(dc.app.options.projectId).to.eq(projectId);
24+
});
25+
});

0 commit comments

Comments
 (0)