the following Velo code goes pasted into you backend code and named http-functions.js
it will open your api for each database you list (make sure you change them.they say Database1name, ect...). and add your site name where it askes for your site adress.
what else will it do... you can remove what functions you dont need as they are all seperate.
1 it opens the api and alphabetizes it.
2 it converts all image links to urls - as we cant import wix npm into flutterflow yet
3 it makes sure that nothing hiden shows up.
4 it makes sure database called scores is sorted my numeral, and decending. meaning you high scores first.
5 it prepends your sites URL to any response starting with "/". that way the links work too.
6 it limits the feed to 1000 so you get no errors as wix limit is 1000 at a time
7 it give and error message if database if returned null
now if you need it private just add your wix headless Oath and key as berrer in your header, and add an oath request to this code..
Hope it helps you all. also if anyone figures out how to get login, groups, blog, ect... and notifications from wix Id love you!
so here is the code:
import { ok, badRequest } from 'wix-http-functions';
import wixData from 'wix-data';
// Function to convert the Wix image URL to an external accessible URL
function convertWixImageUrl(wixImageUrl) {
if (wixImageUrl.startsWith('wix:image://v1/')) {
let convertedUrl = wixImageUrl.replace('wix:image://v1/', 'https://static.wixstatic.com/media/');
const match = convertedUrl.match(/\/([^/]+)$/);
if (match && match[1]) {
convertedUrl = convertedUrl.replace(match[0], '');
}
return convertedUrl;
} else {
return wixImageUrl;
}
}
// Function to prepend base URL to links that start with "/"
function prependBaseUrlIfNeeded(value) {
if (typeof value === 'string' && value.startsWith('/')) {
return "https://(yourwixwebsite).com" + value;
}
return value;
}
// Function to recursively convert Wix image URLs in an object
function convertWixImageUrlsInObject(obj) {
for (let prop in obj) {
if (typeof obj[prop] === 'string') {
obj[prop] = convertWixImageUrl(obj[prop]);
obj[prop] = prependBaseUrlIfNeeded(obj[prop]);
// Check if the property starts with "link-"
if (prop.startsWith('link-')) {
// Split the property by hyphen and remove hyphens from the second part
const parts = prop.split('-');
parts.splice(1, 1); // Remove the hyphenated part
obj[parts.join('')] = obj[prop]; // Create the new property without hyphens
delete obj[prop]; // Delete the old property
}
} else if (typeof obj[prop] === 'object') {
convertWixImageUrlsInObject(obj[prop]);
}
}
}
// Making a common open API for your data collections in Wix Code
let whiteList = [‘Database1name, ‘database2name’, ‘andsoon’];
export function get_api(request) {
const response = {
headers: {
'Content-Type': 'application/json'
}
};
let datacollection = request.path[0];
if (whiteList.includes(datacollection)) {
return wixData.query(datacollection)
.limit(1000)
.ascending('title')
.find()
.then((apiResults) => {
if (apiResults.totalCount > 0) {
// Sort data by "title1" field's number value for "VPinOwners" collection
if (datacollection === 'VPinOwners') {
apiResults.items.sort((a, b) => {
const title1A = parseFloat(a.title1);
const title1B = parseFloat(b.title1);
return title1B - title1A; // Sort in descending order
});
}
// Convert Wix image URLs and prepend base URL to links starting with "/"
apiResults.items.forEach((item) => {
convertWixImageUrlsInObject(item);
});
response.body = {
items: apiResults.items
};