Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
docs: TSDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed Feb 5, 2024
1 parent 86cc47e commit ca06e5c
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/AbstractFuseAPIFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ import { Platform } from './Platform';
* An factory class that defines the base signature for creating a FuseAPI bridge object.
*/
export abstract class AbstractFuseAPIFactory {

/**
* Implement a create API that returns a FuseAPI for the given Platform
*
* @param platform
*/
public abstract create(platform: Platform): FuseAPI;
}
6 changes: 6 additions & 0 deletions src/AbstractFuseLoggerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ limitations under the License.

import { IFuseLogger } from "./IFuseLogger";

/**
* An FuseLogger factory for creating logging instances.
*/
export abstract class AbstractFuseLoggerFactory {
public constructor() {}

/**
* Implement to create a FuseLogger
*/
public abstract create(): IFuseLogger;
}
63 changes: 61 additions & 2 deletions src/FuseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { FuseSerializer } from "./FuseSerializer";
import { ISerializable } from "./ISerializable";
import { TFuseSerializable, TSerializable } from "./TSerializable";
import { TFuseSerializable } from "./TSerializable";

/**
* A union of acceptable type for error causes.
*/
export type TFuseErrorCause = string | Error | FuseError | null;

interface _IFuseErrorSerialized {
Expand All @@ -28,14 +30,26 @@ interface _IFuseErrorSerialized {
stack?: string;
}

/**
* A type that represents a fuse error in a serialized state.
*/
export type IFuseErrorSerialized = TFuseSerializable<_IFuseErrorSerialized>;

/**
* A structured error object.
*/
export class FuseError extends Error implements ISerializable {
private $domain: string;
private $message: string;
private $cause: TFuseErrorCause;
private $code: number;

/**
* @param domain The error domain, usually represents a library, class, or plugin.
* @param message The error message
* @param cause The underlying cause of the error. May be null.
* @param code An error code. May be null.
*/
public constructor(domain: string, message: string, cause?: TFuseErrorCause, code?: number) {
super(message);
this.name = this.constructor.name;
Expand All @@ -45,22 +59,37 @@ export class FuseError extends Error implements ISerializable {
this.$cause = cause || null;
}

/**
* @returns The error message
*/
public getMessage(): string {
return this.$message;
}

/**
* @returns The error domain, usually representing a library, class, or plugin.
*/
public getDomain(): string {
return this.$domain;
}

/**
* @returns The error code
*/
public getCode(): number {
return this.$code;
}

/**
* @returns The underlying cause of the error, if known. May be null.
*/
public getCause(): TFuseErrorCause | null {
return this.$cause;
}

/**
* @returns A serialized object representing an error.
*/
public serialize(): IFuseErrorSerialized {
return {
domain: this.getDomain(),
Expand All @@ -70,6 +99,30 @@ export class FuseError extends Error implements ISerializable {
};
}

/**
* Wraps the given object into a FuseError object. Accepts several different
* formats, which influences the behaviour of this method.
*
* If the input is a string, a FuseError object is created with the string as
* the error message of an unknown domain.
*
* If the input is a FuseError, then this method does nothing but passes through
* the FuseError. The returned FuseError is the input FuseError, a copy is not made.
*
* If the input is an Error, then a FuseError is created using the name as the
* domain, and it's message as the error message. The error object is also used
* as the FuseError's cause parameter.
*
* If the input is of the shape of IFuseErrorSerialized, then the object is
* deserialized into a FuseError instance.
*
* If any other type of object is given, an console error message will be
* printed and a "FuseError" domain error will be returned stating the error
* is not wrappable.
*
* @param error A value that can represent an error
* @returns A FuseError instance
*/
public static wrap(error: string | Error | FuseError | IFuseErrorSerialized | unknown): FuseError {
let ferr: FuseError = null;
if (typeof error === 'string') {
Expand All @@ -92,6 +145,12 @@ export class FuseError extends Error implements ISerializable {
return ferr;
}

/**
* Deserializes and creates a new FuseError instance
*
* @param error The serialized error object
* @returns A FuseError instance
*/
public static fromSerialized(error: IFuseErrorSerialized): FuseError {
return new FuseError(error.domain, error.message, null, error.code);
}
Expand Down
12 changes: 12 additions & 0 deletions src/FuseLoggerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ import { Platform } from "./Platform";
import {IOSFuseLogger} from './ios/IOSFuseLogger';
import {AndroidFuseLogger} from './android/AndroidFuseLogger';

/**
* A default logger factory for creating loggers for the given platform.
*/
export class FuseLoggerFactory {
private $platform: Platform;

/**
*
* @param platform The current Platform in this runtime environment
*/
public constructor(platform: Platform) {
this.$platform = platform;
}

/**
* Creates a FuseLogger for the current Platform.
*
* @returns {IFuseLogger}
*/
public create(): IFuseLogger {
switch (this.$platform) {
case Platform.IOS:
Expand Down
3 changes: 3 additions & 0 deletions src/FuseLoggerLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

/**
* A bitmask option of logger levels
*/
export enum FuseLoggerLevel {
SILENT = 0,
DEBUG = 1,
Expand Down
3 changes: 3 additions & 0 deletions src/FusePermissionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

/**
* A set of constants representing permission states.
*/
export enum FusePermissionState {
GRANTED,
REQUIRES_JUSTIFICATION,
Expand Down
32 changes: 30 additions & 2 deletions src/FusePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,31 @@ export abstract class FusePlugin<TAPIOpts = unknown> {

/**
* Creates the API bridge
* @param platform
* @param platform
* @returns
*/
protected _createAPI(platform: Platform): FuseAPI {
return this._getAPIFactory().create(platform);
}

/**
* @virtual
*
* @remarks
*
* Create a concrete {@link FuseAPI} factory capable of creating FuseAPI
* instance for the current runtime.
*
* @returns A concrete {@link FuseAPI} Factory
*/
protected _createAPIFactory(): AbstractFuseAPIFactory {
return null;
}

/**
*
* @returns The concrete API factory
*/
protected _getAPIFactory(): AbstractFuseAPIFactory {
return this.$apiFactory;
}
Expand Down Expand Up @@ -111,13 +125,14 @@ export abstract class FusePlugin<TAPIOpts = unknown> {
/**
* Returns the FuseContext
*
* @returns
* @returns The current context
*/
public getContext(): FuseContext {
return this.$context;
}

/**
* @remarks
*
* Concrete classes should implement and return a string that uniquely represents this plugin.
* The string must conform to URL fragment rules. It shall only contain the following characters:
Expand All @@ -126,6 +141,7 @@ export abstract class FusePlugin<TAPIOpts = unknown> {
* - dots and hyphens
*
* @abstract
* @virtual
*/
protected abstract _getID(): string;

Expand All @@ -150,6 +166,18 @@ export abstract class FusePlugin<TAPIOpts = unknown> {
return await this._getAPI(apiOpts).execute(this.getID(), method, contentType, data);
}

/**
* @remarks
* This is useful when you want to use an API as a callback, without exposing
* the plugin implementation. The returned function is a bounded function.
* When invoked, it will call on the API endpoint and returns a {@link FuseAPIResponse}
* asynchronously.
*
* @sealed
* @param route The API end point
* @param serializer The serializer to use. Defaults to {@link FuseSerializer} which is a sensible serializer.
* @returns A context-binding function that can be given to another object.
*/
protected _createAPIBridge(route: string, serializer?: FuseSerializer): TAPIBridgeFunction {
if (!serializer) {
serializer = new FuseSerializer();
Expand Down
19 changes: 19 additions & 0 deletions src/FuseResponseReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ limitations under the License.
export class FuseResponseReader {
private constructor() {}

/**
* @remarks
* Reads the data buffer as a string
*
* @param data input data
* @returns The buffer contents as a string
*/
public static async readAsText(data: ArrayBuffer): Promise<string> {
return await new Promise<string>((resolve, reject) => {
let reader: FileReader = new FileReader();
Expand All @@ -35,6 +42,18 @@ export class FuseResponseReader {
});
}

/**
* @remarks
* Reads the given data buffer as a JSON object. The JSON object
* can be typed as T generic. No validations occurs on whether the given
* data is actually a type of T.
*
* @throws {SyntaxError}
* If data is not parseable as JSON.
*
* @param data input data
* @returns The buffer contents as a JSON object.
*/
public static async readAsJSON<T>(data: ArrayBuffer): Promise<T> {
let str: string = await this.readAsText(data);
return JSON.parse(str);
Expand Down
11 changes: 11 additions & 0 deletions src/FuseSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ limitations under the License.
import { ISerializable } from "./ISerializable";
import { TSerializable } from "./TSerializable";

/**
* A class to serialize several different types of objects into a data structure
* that can be reconstructed across the Fuse API bridge.
*/
export class FuseSerializer {
public constructor() {}

Expand Down Expand Up @@ -57,6 +61,13 @@ export class FuseSerializer {
return obj.toISOString();
}

/**
* Serializes the given object into a blob.
*
* @param obj A supported serializable object. See {@link TSerializable} for
* a list of currently supported types
* @returns A serialized blob
*/
public serialize(obj: TSerializable): Blob {
if (obj === null || obj === undefined) {
return null;
Expand Down
17 changes: 16 additions & 1 deletion src/TSerializable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,23 @@ import {ISerializable} from './ISerializable';

/**
* Type of supported serializable obejcts that can go over the Fuse API bridge.
*
* Currently the supported types are:
* - Error
* - Blob
* - ArrayBuffer
* - Primitives (string, number, boolean)
* - Date
* - Any object or array consisting exclusively of the above types
*/
export type TSerializable = Error | string | Blob | ArrayBuffer| ISerializable<any> | number | boolean | Date | TSerializable[] | {[key: string]: TSerializable };
export type TSerializable = Error |
string |
Blob |
ArrayBuffer |
ISerializable<any> |
number |
boolean |
Date | TSerializable[] | {[key: string]: TSerializable };

/**
* Utility type wrap, useful if you have a concrete interface of TSerializable properties.
Expand Down
Loading

0 comments on commit ca06e5c

Please sign in to comment.