faker-api
Version:
A fully customizible rest api faking package that allows you to mock , clone and fake Rest API with fake yet realistic data
113 lines • 4.1 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatefulViewSet = void 0;
var viewset_1 = require("./viewset");
/**
* This viewset is used to create an in-memory stateful data of a specific method, this is useful when you need random data but can be access from different part of your application an expect it to provide thesame data everytime
*/
var StatefulViewSet = /** @class */ (function (_super) {
__extends(StatefulViewSet, _super);
function StatefulViewSet(model) {
var _this = _super.call(this) || this;
_this.models = new Map();
_this.lastId = 0;
_this.model = model;
return _this;
}
StatefulViewSet.prototype.get = function (request, response) {
response.send(Array.from(this.models.values()));
};
StatefulViewSet.prototype.create = function (request, response) {
var length = this.lastId;
var _model = this.model.generate(request, {});
var model = __assign(__assign({}, _model), { id: this.lastId });
this.models.set(String(this.lastId), model);
this.lastId++;
response.status(201);
response.send(model);
};
StatefulViewSet.prototype.retreive = function (request, response, id) {
var _model = this.models.get(id);
if (_model) {
response.send(_model);
}
else {
response.status(404);
response.send({
"message": "Item not found"
});
}
};
StatefulViewSet.prototype.update = function (request, response, id) {
var _model = this.models.get(id);
if (_model) {
var model = __assign({ id: id }, this.model.generate(request, { id: id }));
this.models.set(id, model);
response.send(model);
}
else {
response.status(404);
response.send({
"message": "Item not found"
});
}
};
StatefulViewSet.prototype.delete = function (request, response, id) {
var _model = this.models.get(id);
if (_model) {
this.models.delete(id);
response.send({
message: "Item removed/deleted"
});
}
else {
response.status(404);
response.send({
"message": "Item not found"
});
}
};
StatefulViewSet.prototype.getItem = function (id) {
return this.models.get(id);
};
StatefulViewSet.prototype.addItem = function (item, id) {
if (id) {
this.models.set(id, item);
return id;
}
else {
var _id = String(this.lastId);
this.models.set(_id, item);
return _id;
}
};
return StatefulViewSet;
}(viewset_1.ViewSet));
exports.StatefulViewSet = StatefulViewSet;
//# sourceMappingURL=stateful-viewset.js.map
;