Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite rtdb in typescript, using apiv2 (#4568)
* rewrite rtdb in typescript, using apiv2 * use queryparams with apiv2 instead of putting it into the path
- Loading branch information
Showing
2 changed files
with
47 additions
and
39 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Client } from "./apiv2"; | ||
import { DatabaseInstance, populateInstanceDetails } from "./management/database"; | ||
import { FirebaseError } from "./error"; | ||
import { realtimeOriginOrCustomUrl } from "./database/api"; | ||
import * as utils from "./utils"; | ||
|
||
/** | ||
* Updates rules, optionally specifying a dry run flag for validation purposes. | ||
*/ | ||
export async function updateRules( | ||
projectId: string, | ||
instance: string, | ||
src: any, | ||
options: { dryRun?: boolean } = {} | ||
): Promise<void> { | ||
const queryParams: { dryRun?: string } = {}; | ||
if (options.dryRun) { | ||
queryParams.dryRun = "true"; | ||
} | ||
const downstreamOptions: { | ||
instance: string; | ||
project: string; | ||
instanceDetails?: DatabaseInstance; | ||
} = { instance: instance, project: projectId }; | ||
await populateInstanceDetails(downstreamOptions); | ||
if (!downstreamOptions.instanceDetails) { | ||
throw new FirebaseError(`Could not get instance details`, { exit: 2 }); | ||
} | ||
const origin = utils.getDatabaseUrl( | ||
realtimeOriginOrCustomUrl(downstreamOptions.instanceDetails.databaseUrl), | ||
instance, | ||
"" | ||
); | ||
const client = new Client({ urlPrefix: origin }); | ||
const response = await client.request<any, any>({ | ||
method: "PUT", | ||
path: ".settings/rules.json", | ||
queryParams, | ||
body: src, | ||
resolveOnHTTPError: true, | ||
}); | ||
if (response.status === 400) { | ||
throw new FirebaseError(`Syntax error in database rules:\n\n${response.body.error}`); | ||
} else if (response.status > 400) { | ||
throw new FirebaseError("Unexpected error while deploying database rules.", { exit: 2 }); | ||
} | ||
} |