vendus-sdk
Version:
Unofficial Vendus API SDK for Node.js
237 lines (231 loc) • 7.31 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
__accessCheck(obj, member, "write to private field");
setter ? setter.call(obj, value) : member.set(obj, value);
return value;
};
// src/index.ts
var src_exports = {};
__export(src_exports, {
date: () => date,
default: () => VendusClient,
number: () => number
});
module.exports = __toCommonJS(src_exports);
// src/client.ts
var _client;
var ClientApi = class {
constructor({ client }) {
__privateAdd(this, _client, void 0);
__privateSet(this, _client, client);
}
async getClients(params) {
const response = await __privateGet(this, _client).request({
endpoint: "clients",
parameters: __spreadValues({}, params),
headers: __privateGet(this, _client).authenticationHeader(),
method: "GET"
});
return response;
}
async getClient(id) {
const response = await __privateGet(this, _client).request({
endpoint: `clients/${id}`,
headers: __privateGet(this, _client).authenticationHeader(),
method: "GET"
});
return response;
}
async updateClient(params) {
const _a = params, { id } = _a, client = __objRest(_a, ["id"]);
const response = await __privateGet(this, _client).request({
endpoint: `clients/${id}`,
headers: __privateGet(this, _client).authenticationHeader(),
method: "PATCH",
body: client
});
return response;
}
async createClient(params) {
const response = await __privateGet(this, _client).request({
endpoint: `clients`,
headers: __privateGet(this, _client).authenticationHeader(),
method: "POST",
body: params
});
return response;
}
};
_client = new WeakMap();
// src/document.ts
var _client2;
var DocumentApi = class {
constructor({ client }) {
__privateAdd(this, _client2, void 0);
__privateSet(this, _client2, client);
}
async createDocument(document) {
const response = await __privateGet(this, _client2).request({
endpoint: "documents",
body: __spreadValues({}, document),
headers: __privateGet(this, _client2).authenticationHeader(),
method: "POST"
});
return response;
}
};
_client2 = new WeakMap();
// src/utils.ts
function date(date2) {
const year = date2.getFullYear();
const month = (date2.getMonth() + 1).toString().padStart(2, "0");
const day = date2.getDate().toString().padStart(2, "0");
return `${year}-${month}-${day}`;
}
function number(number2) {
return number2.toString();
}
// src/index.ts
var VendusClient = class {
constructor({
apiKey,
baseUrl = "https://www.vendus.pt/ws/v1.1",
debug = false
}) {
this.apiKey = apiKey;
this.baseUrl = baseUrl;
this.debug = debug;
this.headers = {
accept: "application/json",
"User-Agent": "Vendus-Node-JS"
};
this.client = new ClientApi({ client: this });
this.document = new DocumentApi({ client: this });
}
async request({
endpoint,
headers,
parameters,
body,
method
}) {
const queryParamsString = this.queryParameters(parameters || {});
const fetchOptions = {
method
};
if (body) {
fetchOptions.body = JSON.stringify(body);
fetchOptions.headers = __spreadValues(__spreadValues(__spreadValues({}, this.headers), { "Content-Type": "application/json" }), headers);
} else {
fetchOptions.headers = __spreadValues(__spreadValues({}, this.headers), headers);
}
fetchOptions.cache = "no-store";
const url = `${this.baseUrl}/${endpoint}`;
if (this.debug)
console.log(
`Sending request to ${url}`,
JSON.stringify(
{
method,
headers: fetchOptions.headers,
parameters,
body
},
null,
4
)
);
const response = await fetch(
queryParamsString ? `${url}?${queryParamsString}` : url,
fetchOptions
);
if (response.status >= 300) {
throw {
name: "Request failed",
message: `Request failed with status ${response.status}: ${response.statusText}`,
status: response.status,
statusText: response.statusText,
body: await response.json(),
request: {
url: `${url}?${queryParamsString}`,
options: JSON.stringify(fetchOptions, null, 4)
},
response
};
}
const data = await response.json();
return data;
}
queryParameters(parameters) {
return Object.entries(parameters).map(
([key, value]) => Array.isArray(value) ? value.map((v) => `${encodeURIComponent(key)}=${encodeURIComponent(v)}`).join("&") : `${encodeURIComponent(key)}=${encodeURIComponent(value.toString())}`
).join("&");
}
authenticationHeader() {
const token = Buffer.from(this.apiKey + ":").toString("base64");
return {
Authorization: `Basic ${token}`
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
date,
number
});