qonversion-sdk
Version:
⚡️ An SDK to easily access Qonversions Restful API
276 lines (275 loc) • 9.79 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
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 });
const axios_1 = __importDefault(require("axios"));
class QonversionClient {
constructor(secretKey, projectKey) {
this.baseUrl = "https://api.qonversion.io/v3";
this.secretKey = secretKey;
this.projectKey = projectKey;
}
/**
* GET /v3/users/:id
* Get a users information using theit qonversion user id
*/
getUser(userId) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/users/${userId}`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.projectKey}`,
},
};
try {
const res = yield axios_1.default.get(url, config);
return res.data;
}
catch (error) {
console.log(error);
throw error;
}
});
}
/**
* POST /v3/users/:id
* Create a new user using a custom user id
*/
createUser(userId, environment) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/users/${userId}`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.projectKey}`,
},
};
try {
const res = yield axios_1.default.post(url, { environment }, config);
return res.data;
}
catch (error) {
throw error;
}
});
}
/**
* GET /v3/users/:id/properties
* Retrieve a users properties using their qonversion user id
*/
getUserProperties(userId) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/users/${userId}/properties`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.projectKey}`,
},
};
try {
const res = yield axios_1.default.get(url, config);
return res.data;
}
catch (error) {
throw error;
}
});
}
/**
* POST /v3/users/:id/properties
* Set a users properties using their qonversion user id
*/
setUserProperties(userId, properties) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/users/${userId}/properties`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.projectKey}`,
},
};
try {
const res = yield axios_1.default.post(url, properties, config);
return res.data;
}
catch (error) {
throw error;
}
});
}
/**
* GET /v3/identities/:id
* Get a users information using their qonversion identity id
*/
getIdentity(identityId) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/identities/${identityId}`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.projectKey}`,
},
};
try {
const res = yield axios_1.default.get(url, config);
return res.data;
}
catch (error) {
throw error;
}
});
}
/**
* POST /v3/identities/:id
* Create a new identity using a custom identity id
*/
createIdentity(id, userId) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/identities/${id}`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.projectKey}`,
},
};
try {
const res = yield axios_1.default.post(url, { user_id: userId }, config);
return res.data;
}
catch (error) {
throw error;
}
});
}
/**
* GET /v3/users/:userId/entitlements
* Get a users entitlements using their qonversion user id
*/
getEntitlements(userId) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/users/${userId}/entitlements`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.projectKey}`,
},
};
try {
const res = yield axios_1.default.get(url, config);
return res.data;
}
catch (error) {
throw error;
}
});
}
/**
* POST /v3/users/:userId/entitlements
* Grant entitlements to a user using their qonversion user id
*/
grantEntitlement(userId, entitlementId, expires) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/users/${userId}/entitlements`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.secretKey}`,
},
};
const data = {
id: entitlementId,
expires,
};
try {
yield axios_1.default.post(url, data, config);
}
catch (error) {
throw error;
}
});
}
/**
* DELETE /v3/users/:userId/entitlements/:entitlementId
* Revoke entitlements from a user using their qonversion user id
*/
revokeEntitlement(userId, entitlementId) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/users/${userId}/entitlements/${entitlementId}`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.secretKey}`,
},
};
try {
yield axios_1.default.delete(url, config);
}
catch (error) {
throw error;
}
});
}
/**
* POST /v3/users/:userId/purchases
* Create a new purchase for a user using their qonversion user id
*/
createPurchase(userId, price, currency, stripeStoreData) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${this.baseUrl}/users/${userId}/purchases`;
const config = {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${this.projectKey}`,
},
};
const data = {
price,
currency,
stripe_store_data: stripeStoreData,
};
try {
yield axios_1.default.post(url, data, config);
}
catch (error) {
throw error;
}
});
}
}
exports.default = QonversionClient;
__exportStar(require("./types"), exports);