diff --git a/.changeset/cyan-wasps-worry.md b/.changeset/cyan-wasps-worry.md new file mode 100644 index 00000000000..a21ef699685 --- /dev/null +++ b/.changeset/cyan-wasps-worry.md @@ -0,0 +1,5 @@ +--- +"@firebase/util": patch +--- + +Added a utility function and type for compat interop API diff --git a/packages/util/index.node.ts b/packages/util/index.node.ts index 2c4916b9ed8..a603393d987 100644 --- a/packages/util/index.node.ts +++ b/packages/util/index.node.ts @@ -37,3 +37,4 @@ export * from './src/validation'; export * from './src/utf8'; export * from './src/exponential_backoff'; export * from './src/formatters'; +export * from './src/compat'; diff --git a/packages/util/index.ts b/packages/util/index.ts index e0001274f51..d2da4426c4a 100644 --- a/packages/util/index.ts +++ b/packages/util/index.ts @@ -32,3 +32,4 @@ export * from './src/validation'; export * from './src/utf8'; export * from './src/exponential_backoff'; export * from './src/formatters'; +export * from './src/compat'; diff --git a/packages/util/src/compat.ts b/packages/util/src/compat.ts new file mode 100644 index 00000000000..c7e6de48f54 --- /dev/null +++ b/packages/util/src/compat.ts @@ -0,0 +1,30 @@ +/** + * @license + * Copyright 2021 Google LLC + * + * 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. + */ + +export interface Compat { + _delegate: T; +} + +export function getModularInstance( + service: Compat | ExpService +): ExpService { + if (service && (service as Compat)._delegate) { + return (service as Compat)._delegate; + } else { + return service as ExpService; + } +} diff --git a/packages/util/test/compat.test.ts b/packages/util/test/compat.test.ts new file mode 100644 index 00000000000..ab488a72ebe --- /dev/null +++ b/packages/util/test/compat.test.ts @@ -0,0 +1,35 @@ +/** + * @license + * Copyright 2021 Google LLC + * + * 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 { expect } from 'chai'; +import { getModularInstance } from '../src/compat'; + +describe('getModularInstance()', () => { + it('returns _delegate from a compat object', () => { + const compat = { + _delegate: {} + }; + const modularThing = getModularInstance(compat); + expect(modularThing).to.eq(compat._delegate); + }); + + it('returns the object itself if it is not a compat object', () => { + const thing = {}; + const modularThing = getModularInstance(thing); + expect(modularThing).to.eq(thing); + }); +});