Skip to content

Instantly share code, notes, and snippets.

@andreaskviby
Created November 6, 2018 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreaskviby/2964cffde00bab5949c1d132631c3630 to your computer and use it in GitHub Desktop.
Save andreaskviby/2964cffde00bab5949c1d132631c3630 to your computer and use it in GitHub Desktop.
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);
}
@jstanbri
Copy link

jstanbri commented Jun 2, 2021

Hi Andreas - very helpful, 2 questions if I may?

  1. what is the correct formation of the HTTP: Get request (the video is blurred, so I only understand /_functions/api
  2. do you have examples of adding a PUT/POST request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment