Skip to content

Commit dece9a9

Browse files
authored
BREAKING: remove functions.config() implementation (#1748)
Replace functions.config() implementation with an error that provides clear migration guidance to the params module. This removes the deprecated API as planned for the next major release while helping developers migrate. The error message directs users to: https://firebase.google.com/docs/functions/config-env#migrate-config
1 parent feeee1b commit dece9a9

File tree

2 files changed

+16
-99
lines changed

2 files changed

+16
-99
lines changed

spec/v1/config.spec.ts

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,16 @@
2121
// SOFTWARE.
2222

2323
import { expect } from "chai";
24-
import * as fs from "fs";
25-
import * as process from "process";
26-
import * as sinon from "sinon";
2724

28-
import { config, resetCache } from "../../src/v1/config";
25+
import { config } from "../../src/v1/config";
2926

3027
describe("config()", () => {
31-
let readFileSync: sinon.SinonStub;
32-
let cwdStub: sinon.SinonStub;
33-
34-
before(() => {
35-
readFileSync = sinon.stub(fs, "readFileSync");
36-
readFileSync.throws("Unexpected call");
37-
cwdStub = sinon.stub(process, "cwd");
38-
cwdStub.returns("/srv");
39-
});
40-
41-
after(() => {
42-
sinon.verifyAndRestore();
43-
});
44-
45-
afterEach(() => {
46-
resetCache();
47-
delete process.env.FIREBASE_CONFIG;
48-
delete process.env.CLOUD_RUNTIME_CONFIG;
49-
delete process.env.K_CONFIGURATION;
50-
});
51-
52-
it("will never load in GCFv2", () => {
53-
const json = JSON.stringify({
54-
foo: "bar",
55-
firebase: {},
56-
});
57-
readFileSync.withArgs("/srv/.runtimeconfig.json").returns(Buffer.from(json));
58-
59-
process.env.K_CONFIGURATION = "my-service";
60-
expect(config).to.throw(Error, /transition to using environment variables/);
61-
});
62-
63-
it("loads config values from .runtimeconfig.json", () => {
64-
const json = JSON.stringify({
65-
foo: "bar",
66-
firebase: {},
67-
});
68-
readFileSync.withArgs("/srv/.runtimeconfig.json").returns(Buffer.from(json));
69-
const loaded = config();
70-
expect(loaded).to.not.have.property("firebase");
71-
expect(loaded).to.have.property("foo", "bar");
28+
it("throws an error with migration guidance", () => {
29+
expect(config).to.throw(
30+
Error,
31+
"functions.config() has been removed in firebase-functions v7. " +
32+
"Migrate to environment parameters using the params module. " +
33+
"Migration guide: https://firebase.google.com/docs/functions/config-env#migrate-config"
34+
);
7235
});
7336
});

src/v1/config.ts

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,17 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import * as fs from "fs";
24-
import * as path from "path";
25-
2623
export { firebaseConfig } from "../common/config";
2724

28-
/** @internal */
29-
export let singleton: Record<string, any>;
30-
31-
/** @internal */
32-
export function resetCache(): void {
33-
singleton = undefined;
34-
}
35-
3625
/**
37-
* Store and retrieve project configuration data such as third-party API
38-
* keys or other settings. You can set configuration values using the
39-
* Firebase CLI as described in
40-
* https://firebase.google.com/docs/functions/config-env.
41-
*
42-
* @deprecated Using functions.config() is discouraged. See https://firebase.google.com/docs/functions/config-env.
26+
* @deprecated `functions.config()` has been removed in firebase-functions v7.
27+
* Migrate to environment parameters using the `params` module immediately.
28+
* Migration guide: https://firebase.google.com/docs/functions/config-env#migrate-config
4329
*/
4430
export function config(): Record<string, any> {
45-
// K_CONFIGURATION is only set in GCFv2
46-
if (process.env.K_CONFIGURATION) {
47-
throw new Error(
48-
"functions.config() is no longer available in Cloud Functions for " +
49-
"Firebase v2. Please see the latest documentation for information " +
50-
"on how to transition to using environment variables"
51-
);
52-
}
53-
if (typeof singleton === "undefined") {
54-
init();
55-
}
56-
return singleton;
57-
}
58-
59-
function init() {
60-
try {
61-
const parsed = JSON.parse(process.env.CLOUD_RUNTIME_CONFIG);
62-
delete parsed.firebase;
63-
singleton = parsed;
64-
return;
65-
} catch (e) {
66-
// Do nothing
67-
}
68-
69-
try {
70-
const configPath =
71-
process.env.CLOUD_RUNTIME_CONFIG || path.join(process.cwd(), ".runtimeconfig.json");
72-
const contents = fs.readFileSync(configPath);
73-
const parsed = JSON.parse(contents.toString("utf8"));
74-
delete parsed.firebase;
75-
singleton = parsed;
76-
return;
77-
} catch (e) {
78-
// Do nothing
79-
}
80-
81-
singleton = {};
31+
throw new Error(
32+
"functions.config() has been removed in firebase-functions v7. " +
33+
"Migrate to environment parameters using the params module. " +
34+
"Migration guide: https://firebase.google.com/docs/functions/config-env#migrate-config"
35+
);
8236
}

0 commit comments

Comments
 (0)