Skip to content

Commit

Permalink
rewrite rtdb in typescript, using apiv2 (#4568)
Browse files Browse the repository at this point in the history
* rewrite rtdb in typescript, using apiv2

* use queryparams with apiv2 instead of putting it into the path
  • Loading branch information
bkendall committed May 19, 2022
1 parent 4bba5e9 commit cb8afa8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
39 changes: 0 additions & 39 deletions src/rtdb.js

This file was deleted.

47 changes: 47 additions & 0 deletions src/rtdb.ts
@@ -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 });
}
}

0 comments on commit cb8afa8

Please sign in to comment.