bling-erp-api-with-rate-limit
Version:
Pacote de integração com a API do Bling ERP
122 lines • 5.81 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlingRepository = void 0;
const axios_1 = __importDefault(require("axios"));
const bling_api_exception_1 = require("../exceptions/bling-api.exception");
const bling_internal_exception_1 = require("../exceptions/bling-internal.exception");
const axios_rate_limit_1 = __importDefault(require("axios-rate-limit"));
class BlingRepository {
constructor(props) {
this.props = props;
const axiosInstance = axios_1.default.create({ baseURL: this.props.baseUrl });
this.api = this.props.rateLimitOptions ?
(0, axios_rate_limit_1.default)(axiosInstance, this.props.rateLimitOptions) :
axiosInstance;
this.api.interceptors.request.use((config) => {
config.headers.Authorization = `Bearer ${this.props.accessToken}`;
return config;
});
}
get axiosInstance() {
return this.api;
}
index(options) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.api
.get(`${options.endpoint}`, {
params: options.params,
headers: options.headers,
data: options.body
})
.then((response) => options.shouldIncludeHeadersInResponse
? Object.assign({ headers: response.headers }, response.data) : response.data)
.catch((error) => this.defaultCatchBehavior(error, options.endpoint));
});
}
show(options) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${options.endpoint}/${options.id}`;
return yield this.api
.get(endpoint, {
params: options.params,
headers: options.headers
})
.then((response) => options.shouldIncludeHeadersInResponse
? Object.assign({ headers: response.headers }, response.data) : response.data)
.catch((error) => this.defaultCatchBehavior(error, endpoint));
});
}
store(options) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.api
.post(`${options.endpoint}`, options.body, {
params: options.params,
headers: options.headers
})
.then((response) => options.shouldIncludeHeadersInResponse
? Object.assign({ headers: response.headers }, response.data) : response.data)
.catch((error) => this.defaultCatchBehavior(error, options.endpoint));
});
}
update(options) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${options.endpoint}/${options.id}`;
return yield this.api
.patch(endpoint, options.body, {
params: options.params,
headers: options.headers
})
.then((response) => options.shouldIncludeHeadersInResponse
? Object.assign({ headers: response.headers }, response.data) : response.data)
.catch((error) => this.defaultCatchBehavior(error, endpoint));
});
}
replace(options) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${options.endpoint}/${options.id}`;
return yield this.api
.patch(endpoint, options.body, {
params: options.params,
headers: options.headers
})
.then((response) => options.shouldIncludeHeadersInResponse
? Object.assign({ headers: response.headers }, response.data) : response.data)
.catch((error) => this.defaultCatchBehavior(error, endpoint));
});
}
destroy(options) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = `${options.endpoint}/${options.id}`;
return yield this.api
.delete(endpoint, {
params: options.params,
headers: options.headers
})
.then((response) => options.shouldIncludeHeadersInResponse
? Object.assign({ headers: response.headers }, response.data) : response.data)
.catch((error) => this.defaultCatchBehavior(error, endpoint));
});
}
defaultCatchBehavior(rawError, endpoint) {
var _a, _b;
const data = (_a = rawError.response) === null || _a === void 0 ? void 0 : _a.data;
if (!data) {
throw new bling_internal_exception_1.BlingInternalException(`Não foi possível realizar a chamada HTTP: ${(_b = rawError.config) === null || _b === void 0 ? void 0 : _b.method} ${endpoint}`);
}
throw new bling_api_exception_1.BlingApiException(data);
}
}
exports.BlingRepository = BlingRepository;
//# sourceMappingURL=bling.repository.js.map