vero-ts
Version:
Typescript Wrapper for getvero.com API
79 lines (78 loc) • 2.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Tried using index.ts in types here and it bitched about it relentlessly, so here we are.
const node_fetch_1 = require("node-fetch");
const http_1 = require("./types/http");
class Vero {
constructor(authToken) {
this.apiUrl = "Https://api.getvero.com/api/v2";
this.sendToVero = async (HttpMethod, endpoint, data) => {
const response = await (0, node_fetch_1.default)(`${this.apiUrl}${endpoint}`, {
method: HttpMethod,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
auth_token: this.authToken,
...data,
}),
});
if (response.ok) {
return (await response.json());
}
return {
status: response.status,
message: await response.text(),
};
};
this.track = async (userId, email, data) => {
return this.sendToVero(http_1.default.POST, "/users/track", {
id: userId,
email,
data,
});
};
this.reidentify = async (userId, newId) => {
return this.sendToVero(http_1.default.PUT, "/users/reidentify", {
id: userId,
new_id: newId,
});
};
this.editTags = async (userId, add, remove) => {
return this.sendToVero(http_1.default.PUT, "/users/tags/edit", {
id: userId,
add,
remove,
});
};
this.unsubscribe = async (userId) => {
return this.sendToVero(http_1.default.POST, "/users/unsubscribe", {
id: userId,
});
};
this.resubscribe = async (userId) => {
return this.sendToVero(http_1.default.POST, "/users/resubscribe", {
id: userId,
});
};
this.trackEvent = async (userId, email, eventName, data) => {
return this.sendToVero(http_1.default.POST, "/events/track", {
identity: { id: userId, email },
event_name: eventName,
data,
});
};
this.authToken = authToken;
this.Users = {
track: this.track,
reidentify: this.reidentify,
unsubscribe: this.unsubscribe,
resubscribe: this.resubscribe,
editTags: this.editTags,
};
this.Events = {
track: this.trackEvent,
};
}
}
exports.default = Vero;