-
Notifications
You must be signed in to change notification settings - Fork 393
Implemented createRulesFileFromSource() and createRuleset() APIs #607
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
Changes from all commits
9b1a25a
c57c382
08cb1a4
0f6fd73
612823a
1886575
83eb796
608ee10
0990f8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ import { FirebaseServiceInterface, FirebaseServiceInternalsInterface } from '../ | |
import { FirebaseApp } from '../firebase-app'; | ||
import * as utils from '../utils/index'; | ||
import * as validator from '../utils/validator'; | ||
import { SecurityRulesApiClient } from './security-rules-api-client'; | ||
import { SecurityRulesApiClient, RulesetResponse, RulesetContent } from './security-rules-api-client'; | ||
import { AuthorizedHttpClient } from '../utils/api-request'; | ||
import { FirebaseSecurityRulesError } from './security-rules-utils'; | ||
|
||
|
@@ -38,21 +38,6 @@ export interface RulesetMetadata { | |
readonly createTime: string; | ||
} | ||
|
||
interface Release { | ||
readonly name: string; | ||
readonly rulesetName: string; | ||
readonly createTime: string; | ||
readonly updateTime: string; | ||
} | ||
|
||
interface RulesetResponse { | ||
readonly name: string; | ||
readonly createTime: string; | ||
readonly source: { | ||
readonly files: RulesFile[]; | ||
}; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you removing these in favor of defining them in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. They are being moved to the api-client module, which in this case is treated as an internal implementation module. |
||
/** | ||
* Represents a set of Firebase security rules. | ||
*/ | ||
|
@@ -114,20 +99,7 @@ export class SecurityRules implements FirebaseServiceInterface { | |
* @returns {Promise<Ruleset>} A promise that fulfills with the specified Ruleset. | ||
*/ | ||
public getRuleset(name: string): Promise<Ruleset> { | ||
if (!validator.isNonEmptyString(name)) { | ||
const err = new FirebaseSecurityRulesError( | ||
'invalid-argument', 'Ruleset name must be a non-empty string.'); | ||
return Promise.reject(err); | ||
} | ||
|
||
if (name.indexOf('/') !== -1) { | ||
const err = new FirebaseSecurityRulesError( | ||
'invalid-argument', 'Ruleset name must not contain any "/" characters.'); | ||
return Promise.reject(err); | ||
} | ||
|
||
const resource = `rulesets/${name}`; | ||
return this.client.getResource<RulesetResponse>(resource) | ||
return this.client.getRuleset(name) | ||
.then((rulesetResponse) => { | ||
return new Ruleset(rulesetResponse); | ||
}); | ||
|
@@ -143,9 +115,57 @@ export class SecurityRules implements FirebaseServiceInterface { | |
return this.getRulesetForRelease(SecurityRules.CLOUD_FIRESTORE); | ||
} | ||
|
||
/** | ||
* Creates a `RulesFile` with the given name and source. Throws if any of the arguments are invalid. This is a | ||
* local operation, and does not involve any network API calls. | ||
* | ||
* @param {string} name Name to assign to the rules file. | ||
* @param {string|Buffer} source Contents of the rules file. | ||
* @returns {RulesFile} A new rules file instance. | ||
*/ | ||
public createRulesFileFromSource(name: string, source: string | Buffer): RulesFile { | ||
if (!validator.isNonEmptyString(name)) { | ||
throw new FirebaseSecurityRulesError( | ||
'invalid-argument', 'Name must be a non-empty string.'); | ||
} | ||
|
||
let content: string; | ||
if (validator.isNonEmptyString(source)) { | ||
content = source; | ||
} else if (validator.isBuffer(source)) { | ||
content = source.toString('utf-8'); | ||
} else { | ||
throw new FirebaseSecurityRulesError( | ||
'invalid-argument', 'Source must be a non-empty string or a Buffer.'); | ||
} | ||
|
||
return { | ||
name, | ||
content, | ||
}; | ||
} | ||
|
||
/** | ||
* Creates a new `Ruleset` from the given `RulesFile`. | ||
* | ||
* @param {RulesFile} file Rules file to include in the new Ruleset. | ||
* @returns {Promise<Ruleset>} A promise that fulfills with the newly created Ruleset. | ||
*/ | ||
public createRuleset(file: RulesFile): Promise<Ruleset> { | ||
const ruleset: RulesetContent = { | ||
source: { | ||
files: [ file ], | ||
}, | ||
}; | ||
|
||
return this.client.createRuleset(ruleset) | ||
.then((rulesetResponse) => { | ||
return new Ruleset(rulesetResponse); | ||
}); | ||
} | ||
|
||
private getRulesetForRelease(releaseName: string): Promise<Ruleset> { | ||
const resource = `releases/${releaseName}`; | ||
return this.client.getResource<Release>(resource) | ||
return this.client.getRelease(releaseName) | ||
.then((release) => { | ||
const rulesetName = release.rulesetName; | ||
if (!validator.isNonEmptyString(rulesetName)) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.