Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import {ok, badRequest} from 'wix-http-functions';
import wixData from 'wix-data';
// 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
let whiteList = "DummyData, SampleData";
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(100)
.find()
.then((apiResults) => {
if (apiResults.totalCount > 0) {
response.body = {
"items": JSON.stringify(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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.