put this in your backend and name is http-function.js
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 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]);
} else if (typeof obj[prop] === 'object') {
convertWixImageUrlsInObject(obj[prop]);
}
}
}
// Making a common open API for your data collections in Wix Code
// so other systems can consume your data
// Type in the Data Collection names you do allow to be a part
// of the common API functions (change each ‘database’ for your name of your database on query side ie: ’items’
let whiteList = ['ReplacewithDatabseName', 'ReplacewithDatabseName1', 'ReplacewithDatabseNameect'];
export function get_api(request) {
const response = {
headers: {
'Content-Type': 'application/json'
}
};
// Get name of Data Collection from path[0]
let datacollection = request.path[0];
// DummyData
if (whiteList.includes(datacollection)) {
return wixData.query(datacollection)
.limit(1000) // You can set a reasonable limit here for performance reasons
.ascending('title') // Sort alphabetically by title in ascending order
.find()
.then((apiResults) => {
if (apiResults.totalCount > 0) {
// Convert Wix image URLs in the response before sending to the client
apiResults.items.forEach((item) => {
convertWixImageUrlsInObject(item);
});
response.body = {
items: apiResults.items
};
return ok(response);
}
response.body = {
items: "No items found in the collection."
};
return ok(response);
});
}
response.body = {
error: "Data Collection is not allowed to be used through this API."
};
return badRequest(response);
}
My next problem to add is that i want it to allow access to Posts, messages, groups, forums, and other things if the request has the sites Oath Key in the header. but this works perfect for now. and it even allows you to get the images url instead of the wix.image link that dont work in flutter yet. Hope this helps anyone as it was hard to get done. lots of help from wix and GPT chat. but its clean and works awesome! enjoy