@apihawk/document-sdk
Version:
The ApiHawk Document SDK
126 lines (125 loc) • 4.52 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const billia_sdk_1 = require("@apihawk/billia-sdk");
const to_rest_resource_1 = require("./common/to-rest-resource");
class DocumentSDK extends billia_sdk_1.BilliaSDKServiceBase {
/**
* Document Manager SDK
*
* @param api - Apihawk Connector instance
* @param apiUrl - the URL to the document manager API
*/
constructor(api, apiUrl) {
super(api);
this.apiUrl = apiUrl;
}
/**
* Lists documents with pagination.
*
* @param {number} page
* @param {number} pageSize
* @param {{document_id?: number | number[]; file_name?: string; file?: string; status?: string; columns?: string[]}} query
* @returns {Promise<IRestPaginatedResource<IDocumentArchiveEntity>>}
*/
listDocuments(page = 1, pageSize = 25, query) {
return __awaiter(this, void 0, void 0, function* () {
const filter = {
page,
page_size: pageSize,
where: []
};
if (query && query.columns) {
filter.columns = query.columns;
}
if (query && query.document_id) {
if (Array.isArray(query.document_id)) {
filter.where.push({
field: 'document_id',
where: 'and',
type: 'in',
values: query.document_id
});
}
else {
filter.where.push({
field: 'document_id',
where: 'and',
type: 'equalTo',
value: query.document_id
});
}
}
if (query && query.file_name) {
filter.where.push({
field: 'file_name',
where: 'and',
type: 'equalTo',
value: query.file_name
});
}
if (query && query.file) {
filter.where.push({
field: 'file',
where: 'and',
type: 'equalTo',
value: query.file
});
}
if (query && query.status) {
filter.where.push({
field: 'status',
where: 'and',
type: 'equalTo',
value: query.status
});
}
const result = yield this.api.call({
url: `/document/archive`,
query: filter,
method: 'GET'
});
const documents = to_rest_resource_1.default(result, 'document');
documents.items = documents.items.map((i) => this.documentMapper(i, this.apiUrl));
return documents;
});
}
/**
* Gets a document by ID.
*
* @param {number} id
* @returns {Promise<IDocumentArchiveEntity>}
*/
getDocument(id) {
return __awaiter(this, void 0, void 0, function* () {
const document = yield this.api.call({
url: `/document/archive/${id}`,
method: 'GET'
});
return this.documentMapper(document, this.apiUrl);
});
}
documentMapper(item, url) {
if (item.file) {
// set the file URL to the full address
item.file = `${url}/data/${item.file}`;
}
try {
item.data = JSON.parse(item.data || 'null');
}
catch (error) {
item.data = null;
}
delete item._links;
return item;
}
}
exports.DocumentSDK = DocumentSDK;