channelape-sdk
Version:
A client for interacting with ChannelApe's API
56 lines • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Q = require("q");
const ResponseStatus_1 = require("../../model/ResponseStatus");
const Resource_1 = require("../../model/Resource");
const Version_1 = require("../../model/Version");
const GenerateApiError_1 = require("../../utils/GenerateApiError");
class StepsService {
constructor(client) {
this.client = client;
}
get(stepId) {
const deferred = Q.defer();
const requestUrl = `/${Version_1.default.V1}${Resource_1.default.STEPS}/${stepId}`;
this.client.get(requestUrl, {}, (error, response, body) => {
this.mapStepPromise(requestUrl, deferred, error, response, body, ResponseStatus_1.default.OK);
});
return deferred.promise;
}
create(step) {
const deferred = Q.defer();
const requestUrl = `/${Version_1.default.V1}${Resource_1.default.STEPS}`;
this.client.post(requestUrl, { data: step }, (error, response, body) => {
this.mapStepPromise(requestUrl, deferred, error, response, body, ResponseStatus_1.default.CREATED);
});
return deferred.promise;
}
update(step) {
const deferred = Q.defer();
const requestUrl = `/${Version_1.default.V1}${Resource_1.default.STEPS}/${step.id}`;
this.client.put(requestUrl, { data: step }, (error, response, body) => {
this.mapStepPromise(requestUrl, deferred, error, response, body, ResponseStatus_1.default.OK);
});
return deferred.promise;
}
formatStep(step) {
step.createdAt = new Date(step.createdAt);
step.updatedAt = new Date(step.updatedAt);
return step;
}
mapStepPromise(requestUrl, deferred, error, response, body, expectedStatusCode) {
if (error) {
deferred.reject(error);
}
else if (response.status === expectedStatusCode) {
const step = this.formatStep(body);
deferred.resolve(step);
}
else {
const stepApeErrorResponse = (0, GenerateApiError_1.default)(requestUrl, response, body, ResponseStatus_1.default.OK);
deferred.reject(stepApeErrorResponse);
}
}
}
exports.default = StepsService;
//# sourceMappingURL=StepsService.js.map