@fusion.io/utils
Advanced tools
Comparing version 3.0.1-alpha.35 to 3.0.1-alpha.36
export * from "./Monad"; | ||
export * from "./Manager"; | ||
export * from "./Serializer"; |
@@ -8,2 +8,3 @@ "use strict"; | ||
__export(require("./Manager")); | ||
__export(require("./Serializer")); | ||
//# sourceMappingURL=index.js.map |
@@ -12,7 +12,39 @@ declare type C = { | ||
} | ||
/** | ||
* Utility for serialize/deserialize objects with a given type. | ||
* This utility will be useful when dealing with data storage and message services. | ||
* | ||
*/ | ||
export declare class Serializer { | ||
/** | ||
* List of strategies | ||
*/ | ||
private strategies; | ||
/** | ||
* Register a new strategy | ||
* | ||
* @param constructor | ||
* @param strategy | ||
*/ | ||
register(constructor: C, strategy: Strategy): this; | ||
/** | ||
* Get a strategy | ||
* | ||
* @param constructor | ||
*/ | ||
getOrFail(constructor: C): Strategy; | ||
/** | ||
* Serialize an object with the given strategy | ||
* | ||
* @param constructor | ||
* @param object | ||
*/ | ||
serialize(constructor: C, object: any): Promise<string>; | ||
/** | ||
* Deserialize the raw and get back | ||
* the object | ||
* | ||
* @param constructor | ||
* @param raw | ||
*/ | ||
deserialize<T>(constructor: { | ||
@@ -22,4 +54,13 @@ new (...args: any[]): T; | ||
} | ||
/** | ||
* The global serializer | ||
* | ||
*/ | ||
export declare const serializer: Serializer; | ||
/** | ||
* Decorator for adding a strategy | ||
* | ||
* @param strategy | ||
*/ | ||
export declare const serializable: (strategy: Strategy) => (Target: C) => void; | ||
export {}; |
"use strict"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var X_1; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
"use strict"; | ||
class SerializerError extends Error { | ||
} | ||
exports.SerializerError = SerializerError; | ||
/** | ||
* Utility for serialize/deserialize objects with a given type. | ||
* This utility will be useful when dealing with data storage and message services. | ||
* | ||
*/ | ||
class Serializer { | ||
constructor() { | ||
/** | ||
* List of strategies | ||
*/ | ||
this.strategies = new Map(); | ||
} | ||
/** | ||
* Register a new strategy | ||
* | ||
* @param constructor | ||
* @param strategy | ||
*/ | ||
register(constructor, strategy) { | ||
@@ -36,2 +31,7 @@ if (this.strategies.has(constructor)) { | ||
} | ||
/** | ||
* Get a strategy | ||
* | ||
* @param constructor | ||
*/ | ||
getOrFail(constructor) { | ||
@@ -44,5 +44,18 @@ const strategy = this.strategies.get(constructor); | ||
} | ||
/** | ||
* Serialize an object with the given strategy | ||
* | ||
* @param constructor | ||
* @param object | ||
*/ | ||
serialize(constructor, object) { | ||
return this.getOrFail(constructor).serialize(object); | ||
} | ||
/** | ||
* Deserialize the raw and get back | ||
* the object | ||
* | ||
* @param constructor | ||
* @param raw | ||
*/ | ||
deserialize(constructor, raw) { | ||
@@ -53,23 +66,15 @@ return this.getOrFail(constructor).deserialize(raw); | ||
exports.Serializer = Serializer; | ||
/** | ||
* The global serializer | ||
* | ||
*/ | ||
exports.serializer = new Serializer(); | ||
/** | ||
* Decorator for adding a strategy | ||
* | ||
* @param strategy | ||
*/ | ||
exports.serializable = (strategy) => (Target) => { | ||
exports.serializer.register(Target, strategy); | ||
}; | ||
let X = X_1 = class X { | ||
constructor(message) { | ||
this.message = message; | ||
} | ||
getMessage() { | ||
return this.message; | ||
} | ||
}; | ||
X = X_1 = __decorate([ | ||
exports.serializable({ | ||
serialize: (x) => __awaiter(this, void 0, void 0, function* () { return JSON.stringify(x.getMessage()); }), | ||
deserialize: (data) => __awaiter(this, void 0, void 0, function* () { return new X_1(JSON.parse(data)); }) | ||
}), | ||
__metadata("design:paramtypes", [String]) | ||
], X); | ||
exports.serializer.serialize(X, new X('hello world')).then(payload => console.log(payload)); | ||
exports.serializer.deserialize(X, "\"hello world\"").then(result => console.log(result)); | ||
//# sourceMappingURL=Serializer.js.map |
{ | ||
"name": "@fusion.io/utils", | ||
"version": "3.0.1-alpha.35", | ||
"version": "3.0.1-alpha.36", | ||
"description": "Utilities for the Geeks, by the Geeks.", | ||
@@ -32,3 +32,3 @@ "author": "rikky <son.levuthai@gmail.com>", | ||
}, | ||
"gitHead": "f0c2503bd22319b409ba93a82da78fec1e63fa3c" | ||
"gitHead": "d15b5ab529d35a3eba0919c11d6994a270b0e544" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
417
15317