@vincentt-sdks/campaign-sdk
Version:
Campaign SDK by Vincentt
433 lines (432 loc) • 13.8 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 __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { libConfig } from '../config';
export const createNewUser = (_a) => __awaiter(void 0, void 0, void 0, function* () {
var { apiKey } = _a, params = __rest(_a, ["apiKey"]);
try {
const response = yield fetch(`${libConfig.apiBaseUrl}/users`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey,
},
body: JSON.stringify(params),
});
const newUser = yield response.json();
return newUser;
}
catch (err) {
throw err;
}
});
export const refreshAuthToken = (_b) => __awaiter(void 0, [_b], void 0, function* ({ refreshToken, apiKey, }) {
if (!refreshToken) {
return null;
}
try {
const response = yield fetch(`${libConfig.apiBaseUrl}/auth/refresh`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey,
},
body: JSON.stringify({ refreshToken }),
});
if (!response.ok) {
throw new Error('Failed to refresh token');
}
const data = yield response.json();
localStorage.setItem(`${libConfig.storageKeyPrefix}accessToken`, data.accessToken);
localStorage.setItem(`${libConfig.storageKeyPrefix}refreshToken`, data.refreshToken);
return data;
}
catch (e) {
throw e;
}
});
export const customFetch = (_c) => __awaiter(void 0, [_c], void 0, function* ({ url, options, auth, apiKey, }) {
// if (auth.accessToken) {
// options.headers.Authorization = `Bearer ${auth.accessToken}`;
// }
let response = yield fetch(url, options);
if (response.status === 401) {
const tokens = yield refreshAuthToken({
refreshToken: auth.refreshToken,
apiKey,
});
if (tokens) {
options.headers.Authorization = `Bearer ${tokens.accessToken}`;
response = yield fetch(url, options);
}
}
return response;
});
export const getCurrentUser = (accessToken, refreshToken, apiKey) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/users/me`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const newUser = yield response.json();
return newUser;
}
catch (err) {
throw err;
}
});
export const updateUser = (accessToken, refreshToken, apiKey, params) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(params),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/users/me`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const updatedUser = yield response.json();
return updatedUser;
}
catch (err) {
throw err;
}
});
export const createNewEntry = (accessToken, refreshToken, apiKey, body) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(body),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/entries`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const newEntry = yield response.json();
return newEntry;
}
catch (err) {
throw err;
}
});
export const updateEntry = (accessToken, refreshToken, apiKey, id, body) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(body),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/entries/${id}`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const updatedEntry = yield response.json();
return updatedEntry;
}
catch (err) {
throw err;
}
});
export const getAllEntries = (accessToken, refreshToken, apiKey, query) => __awaiter(void 0, void 0, void 0, function* () {
const { page, pageSize } = query;
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/entries?page=${page}&pageSize=${pageSize}`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const entries = yield response.json();
return entries;
}
catch (err) {
throw err;
}
});
export const createNewFormEntry = (accessToken, refreshToken, apiKey, params) => __awaiter(void 0, void 0, void 0, function* () {
const { formId, entry } = params;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({ entry }),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/forms/${formId}`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const formEntry = yield response.json();
return formEntry;
}
catch (err) {
throw err;
}
});
export const uploadNewImage = (accessToken, refreshToken, apiKey, params) => __awaiter(void 0, void 0, void 0, function* () {
const { file, body } = params;
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(body),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/upload/generate-presigned-url`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
if (!response.ok) {
throw new Error('Failed to generate presigned URL');
}
const imageUrlData = yield response.json();
const { presignedUrl, fileUrl } = imageUrlData;
const filePutOptions = {
method: 'PUT',
headers: {
'Content-Type': file.type,
},
body: file,
};
yield customFetch({
url: `${presignedUrl}`,
options: filePutOptions,
auth: { accessToken, refreshToken },
apiKey,
});
return {
url: fileUrl,
};
}
catch (err) {
throw err;
}
});
export const drawNewCoupon = (accessToken, refreshToken, apiKey, body) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(body),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/coupons/draw`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const coupon = yield response.json();
return coupon;
}
catch (err) {
throw err;
}
});
export const fetchPrizeItemsByCampaign = (accessToken, refreshToken, apiKey, campaignId) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/prize-items/campaign/${campaignId}`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const coupon = yield response.json();
return coupon;
}
catch (err) {
throw err;
}
});
export const createSession = (accessToken, refreshToken, apiKey, params) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: params ? JSON.stringify(params) : undefined,
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/sessions`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
if (!response.ok) {
throw new Error('Failed to create session');
}
const data = yield response.json();
return data;
}
catch (err) {
throw err;
}
});
export const getSession = (accessToken, refreshToken, apiKey, sessionId) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/sessions/${sessionId}`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
if (!response.ok) {
throw new Error('Failed to fetch session');
}
const session = yield response.json();
return session;
}
catch (err) {
throw err;
}
});
export const claimCoupon = (accessToken, refreshToken, apiKey, query) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(query),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/coupons/claim`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const entries = yield response.json();
return entries;
}
catch (err) {
throw err;
}
});
export const claimCouponByPrizeItem = (accessToken, refreshToken, apiKey, body) => __awaiter(void 0, void 0, void 0, function* () {
const { prizeItemId } = body, rest = __rest(body, ["prizeItemId"]);
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(rest),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/coupons/draw/${prizeItemId}`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
const entries = yield response.json();
return entries;
}
catch (err) {
throw err;
}
});
// user journey logs
export const createUserJourneyLogService = (accessToken, refreshToken, apiKey, body) => __awaiter(void 0, void 0, void 0, function* () {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(body),
};
try {
const response = yield customFetch({
url: `${libConfig.apiBaseUrl}/user-journey-logs`,
options,
auth: { accessToken, refreshToken },
apiKey,
});
if (!response.ok) {
throw new Error('Failed to generate presigned URL');
}
const data = yield response.json();
return data;
}
catch (err) {
throw err;
}
});