@ixo/supamoto-bot-sdk
Version:
An SDK to easily interact with Supamoto bot db
134 lines (130 loc) • 3.74 kB
JavaScript
import fetch from 'node-fetch';
import { isValidCollectionId, isValidCustomerId } from '../../utils/validators';
import { cleanUrlString } from '../../utils/url';
import { throwResponseError } from '../../utils/error';
// claim data
// =================================================================================================
export async function getClaim({
claimId,
collectionId
}, botUrl, accessToken) {
if (!isValidCollectionId(collectionId)) {
throw new Error('Invalid collection ID');
}
const url = `${botUrl}/action`;
const payload = {
action: 'get-claim',
flags: {
collection: collectionId,
claimId
}
};
const response = await fetch(cleanUrlString(url), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw throwResponseError(response);
}
const data = await response.json();
return data;
}
export async function saveClaim({
collectionId,
claim
}, botUrl, accessToken) {
if (!isValidCollectionId(collectionId)) {
throw new Error('Invalid collection ID');
}
const url = `${botUrl}/action`;
const payload = {
action: 'save-claim',
flags: {
collection: collectionId,
data: claim
}
};
const response = await fetch(cleanUrlString(url), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw throwResponseError(response);
}
const data = await response.json();
return data;
}
// db claims
// =================================================================================================
export async function getCollectionClaims({
collectionId,
collectionIds
}, botUrl, accessToken) {
if (!collectionId && !(collectionIds !== null && collectionIds !== void 0 && collectionIds.length)) {
throw new Error('Collection ID or collection IDs are required');
}
if (!isValidCollectionId(collectionId) || !(collectionIds !== null && collectionIds !== void 0 && collectionIds.every(isValidCollectionId))) {
throw new Error('Invalid collection ID or collection IDs');
}
const url = `${botUrl}/action`;
const payload = {
action: 'get-collection-claims',
flags: {
collectionId,
collectionIds
}
};
const response = await fetch(cleanUrlString(url), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw throwResponseError(response);
}
const data = await response.json();
return data;
}
export async function getCustomerClaims({
customerId,
customerIds
}, botUrl, accessToken) {
if (!customerId && !(customerIds !== null && customerIds !== void 0 && customerIds.length)) {
throw new Error('Customer ID or customer IDs are required');
}
if (!isValidCustomerId(customerId) || !(customerIds !== null && customerIds !== void 0 && customerIds.every(isValidCustomerId))) {
throw new Error('Invalid customer ID or customer IDs');
}
const url = `${botUrl}/action`;
const payload = {
action: 'get-customer-claims',
flags: {
customerId,
customerIds
}
};
const response = await fetch(cleanUrlString(url), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw throwResponseError(response);
}
const data = await response.json();
return data;
}