From de5f9050137acc9ed1490082e5aa429b5de3cb2a Mon Sep 17 00:00:00 2001 From: Feiyang Date: Wed, 24 Mar 2021 11:48:40 -0700 Subject: [PATCH] Utility function and type for compat interop API (#4673) * compat interop util * Create cyan-wasps-worry.md * Update cyan-wasps-worry.md --- .changeset/cyan-wasps-worry.md | 5 +++++ packages/util/index.node.ts | 1 + packages/util/index.ts | 1 + packages/util/src/compat.ts | 30 ++++++++++++++++++++++++++ packages/util/test/compat.test.ts | 35 +++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 .changeset/cyan-wasps-worry.md create mode 100644 packages/util/src/compat.ts create mode 100644 packages/util/test/compat.test.ts 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); + }); +});