[perf] Return Promise types

This commit is contained in:
Elliot Hesp
2018-07-12 13:25:11 +01:00
parent 417b6ba542
commit 2c776a60e8
5 changed files with 121 additions and 5 deletions

View File

@@ -16,7 +16,7 @@ export default class HttpMetric {
this.httpMethod = httpMethod;
}
getAttribute(attribute: string): Promise<string> {
getAttribute(attribute: string): Promise<string | null> {
return getNativeModule(this._perf).getHttpMetricAttribute(
this.url,
this.httpMethod,
@@ -31,7 +31,8 @@ export default class HttpMetric {
);
}
putAttribute(attribute: string, value: string): Promise<null> {
// TODO return true or false
putAttribute(attribute: string, value: string): Promise<true | false> {
return getNativeModule(this._perf).putHttpMetricAttribute(
this.url,
this.httpMethod,

View File

@@ -14,7 +14,7 @@ export default class Trace {
this.identifier = identifier;
}
getAttribute(attribute: string): Promise<string> {
getAttribute(attribute: string): Promise<string | null> {
return getNativeModule(this._perf).getTraceAttribute(
this.identifier,
attribute
@@ -40,7 +40,8 @@ export default class Trace {
);
}
putAttribute(attribute: string, value: string): Promise<null> {
// TODO return true or false
putAttribute(attribute: string, value: string): Promise<true | false> {
return getNativeModule(this._perf).putTraceAttribute(
this.identifier,
attribute,

View File

@@ -12,6 +12,29 @@ import type App from '../core/app';
export const MODULE_NAME = 'RNFirebasePerformance';
export const NAMESPACE = 'perf';
const HTTP_METHODS = {
CONNECT: true,
DELETE: true,
GET: true,
HEAD: true,
OPTIONS: true,
PATCH: true,
POST: true,
PUT: true,
TRACE: true,
};
type HttpMethod =
| 'CONNECT'
| 'DELETE'
| 'GET'
| 'HEAD'
| 'OPTIONS'
| 'PATCH'
| 'POST'
| 'PUT'
| 'TRACE';
export default class PerformanceMonitoring extends ModuleBase {
constructor(app: App) {
super(app, {
@@ -49,13 +72,27 @@ export default class PerformanceMonitoring extends ModuleBase {
return new Trace(this, trace);
}
newHttpMetric(url: string, httpMethod: string) {
/**
* Return a new HttpMetric instance
* @param url
* @param httpMethod
* @returns {HttpMetric}
*/
newHttpMetric(url: string, httpMethod: HttpMethod): HttpMetric {
if (typeof url !== 'string' || typeof httpMethod !== 'string') {
throw new Error(
'firebase.perf().newHttpMetric() requires url and httpMethod string values'
);
}
if (!HTTP_METHODS[httpMethod]) {
throw new Error(
`firebase.perf().newHttpMetric() httpMethod should be one of ${Object.keys(
HTTP_METHODS
).join(', ')}`
);
}
return new HttpMetric(this, url, httpMethod);
}
}