Skip to content
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

rewrite rtdb in typescript, using apiv2 #4568

Merged
merged 2 commits into from May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 { Options } from "./options";
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> {
let path = ".settings/rules.json";
if (options.dryRun) {
path += "?dryRun=true";
bkendall marked this conversation as resolved.
Show resolved Hide resolved
}
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,
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 });
}
}