Created
November 6, 2018 16:15
-
-
Save andreaskviby/2964cffde00bab5949c1d132631c3630 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Hi Andreas - very helpful, 2 questions if I may?