-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ServiceAccount interface definition errors #522
Comments
I found a few problems with this issue:
|
One of the following should work for pretty much any use case:
|
@hiranya911 - I am facing this issue when trying something similar to the last code snipped you posted. import serviceAccount from "./serviceAccountKey.json";
admin.credential.cert(serviceAccount) Here is the linter message:
Basically it expects camel case keys, while the json file come in snake case, as described by @dgobaud . |
Just
If you insist on importing, then you should do something like the following:
|
But this must be a bug right? We want the type checking shouldn't the type match the data? |
It seems a conscious effort has been made to keep snake-cased attributes out of the typings. I'm not sure why, but I guess it's a style-related choice made by the original developers of this library. This interface is quite old (last modified over 2 years ago), and it hasn't changed since I joined the project. We can add the snake-cased properties to the interface as a solution. But given that there are simple workarounds, and this is the first time this issue has come up in over 2 years, I'm not sure if it's even necessary. Does the problem occur only when the service account json is imported as a module? Any other ways to reproduce the issue? |
I think think this is it and thinking about it more I think you have a good enough point it isn't necessary the JSON format match the interface so probably can leave it. Below I think makes sense as the way to do it. import serviceAccount from "./serviceAccountKey.json";
admin.credential.cert({
privateKey: serviceAccount.private_key,
clientEmail: serviceAccount.client_email,
projectId: serviceAccount.project_id,
}); |
This seems to work: import admin from 'firebase-admin';
import serviceAccount from './serviceAccountKey.json';
admin.initializeApp({
credential: admin.credential.cert(serviceAccount as admin.ServiceAccount),
databaseURL: 'https://twisteringo-310c3.firebaseio.com',
}); |
TL;DR: The types are wrong in the SDK, but instead of just fixing them, you're advising every single (TypeScript) client of your SDK to hack their code. Given you say that the function actually does support snake_case, why don't you just fix the type? May I PR this?
|
This is the cleanest hack today, but don't forget that it is a straight-up lie! You're asserting to TypeScript that your snake_case config is camelCase. You get away with it because the real JS implementation doesn't care what TS thinks & happily accepts snake_case. If the SDK were to drop snake_case support in the future, your code would runtime error (TypeScript would continue to trust your assertion). |
the ServiceAccount interface specifies the member variables using camel case but they come in from Google Cloud JSON download as snake case and if changed in the JSON file to match the interface, when exporting the JSON file path in GOOGLE_APPLICATION_CREDENTIALS to work with other Google Cloud services locally in the emulator https://firebase.google.com/docs/functions/local-emulator#set_up_admin_credentials_optional it gives error
{"result":"The incoming JSON object does not contain a client_email field"}
firebase-admin-node/src/index.d.ts
Line 35 in 2e48327
The text was updated successfully, but these errors were encountered: