New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Public API: Create new project (fixes #1095) #1106
Public API: Create new project (fixes #1095) #1106
Conversation
The endpoints don't exist yet, but this is a good way to see how the implementation of the data structures differ.
This transforms the nested tree of file data into a mongoose Project model
server/server.js
Outdated
@@ -95,6 +96,7 @@ app.use(session({ | |||
|
|||
app.use(passport.initialize()); | |||
app.use(passport.session()); | |||
app.use('/api', requestsOfTypeJSON(), api); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these public endpoints should be mounted at /api
and all the rest that only the editor uses should be mounted at /editor
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to me that sounds logical. do you think we should add the API versioning as well? i was trying to figure out what best practices are for this, but it seems like the options all have their own drawbacks (here's a blog post that breaks this down).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added URL-based versioning in 57ced9c. Although it's not ideal, I think it's more obvious for casual users that may write scripts to import data into the Editor.
I'll change the namespaces of the private and public APIs in another PR.
@@ -30,3 +31,27 @@ export default function createProject(req, res) { | |||
.then(populateUserData) | |||
.catch(sendFailure); | |||
} | |||
|
|||
// TODO: What happens if you don't supply any files? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think there are two options here: (1) create default files (the ones that are currently the default when you click sketch-> new) or (2) the project isn't valid. my instinct here is (1)...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done. See ee6891d. The default files are created unless a root-level .html
file is created. Also, if sketch.js
and style.css
exist, then they are not overwritten by defaults.
@@ -0,0 +1,35 @@ | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for writing this class and and moving towards better error handling/capturing ✨
Fixes a bug where the slug was auto-generated using the sketch name, even if a slug property had been provided.
@catarak Just pushed some tests and a check for slug uniqueness for the user. If a slug is specified and it is not unique in the sketches that the user has created, then the ids of the project(s) with identical slugs are returned as an error. Eventually, I think this should live as a pre-save hook on the Project mongoose model. But as we don't have a UI for editing slugs, then we'd need to discuss what that behaviour should be first. |
- implements tests - update apiKey tests to use new User mocks
`message` is used as a high-level description of the errors `detail` is optional and has an plain language explanation of the individual errors `errors` is an array of each individual problem from `detail` in a machine-readable format
ba4ead7
to
3041b35
Compare
Fixes a bug where the slug was auto-generated using the sketch name, even if a slug property had been provided.
- implements tests - update apiKey tests to use new User mocks
`message` is used as a high-level description of the errors `detail` is optional and has an plain language explanation of the individual errors `errors` is an array of each individual problem from `detail` in a machine-readable format
…n/p5.js-web-editor into feature/public-api-create-project
@catarak I still can't reproduce this. I dropped the
I created a new user (andrewn), with a new personal access token and POSTed to create a new sketch:
Perhaps my |
let me try testing this again to sanity check, it's super weird! |
i'm using postman to test this, maybe that has to do with it? |
In testing this again, I found a bug. I made a POST to |
…mespace Previously, the project was always created under the authenticated user's namespace, but this not obvious behaviour.
This is now fixed. An |
sweet, thanks for fixing that! i don't think there's any other issues so i'll merge this in. |
* Converts import script to use public API endpoints The endpoints don't exist yet, but this is a good way to see how the implementation of the data structures differ. * Exposes public API endpoint to fetch user's sketches * Implements public API delete endpoint * Adds helper to create custom ApplicationError classes * Adds create project endpoint that understand API's data structure This transforms the nested tree of file data into a mongoose Project model * Returns '201 Created' to match API spec * Removes 'CustomError' variable assignment as it shows up in test output * transformFiles will return file validation errors * Tests API project controller * Tests toModel() * Creates default files if no root-level .html file is provided * Do not auto-generate a slug if it is provided Fixes a bug where the slug was auto-generated using the sketch name, even if a slug property had been provided. * Validates uniqueness of slugs for projects created by the public API * Adds tests for slug uniqueness * Configures node's Promise implementation for mongoose (fixes warnings) * Moves createProject tests to match controller location * Adds support for code to ApplicationErrors * deleteProject controller tests * getProjectsForUser controller tests - implements tests - update apiKey tests to use new User mocks * Ensure error objects have consistent property names `message` is used as a high-level description of the errors `detail` is optional and has an plain language explanation of the individual errors `errors` is an array of each individual problem from `detail` in a machine-readable format * Assert environment variables are provided at script start * Version public API * Expect "files" property to always be provided * Fixes linting error * Converts import script to use public API endpoints The endpoints don't exist yet, but this is a good way to see how the implementation of the data structures differ. * Exposes public API endpoint to fetch user's sketches * Implements public API delete endpoint * Adds helper to create custom ApplicationError classes * Adds create project endpoint that understand API's data structure This transforms the nested tree of file data into a mongoose Project model * Returns '201 Created' to match API spec * Removes 'CustomError' variable assignment as it shows up in test output * transformFiles will return file validation errors * Tests API project controller * Tests toModel() * Creates default files if no root-level .html file is provided * Do not auto-generate a slug if it is provided Fixes a bug where the slug was auto-generated using the sketch name, even if a slug property had been provided. * Validates uniqueness of slugs for projects created by the public API * Adds tests for slug uniqueness * Configures node's Promise implementation for mongoose (fixes warnings) * Moves createProject tests to match controller location * deleteProject controller tests * Adds support for code to ApplicationErrors * getProjectsForUser controller tests - implements tests - update apiKey tests to use new User mocks * Ensure error objects have consistent property names `message` is used as a high-level description of the errors `detail` is optional and has an plain language explanation of the individual errors `errors` is an array of each individual problem from `detail` in a machine-readable format * Assert environment variables are provided at script start * Version public API * Expect "files" property to always be provided * Fixes linting error * Checks that authenticated user has permission to create under this namespace Previously, the project was always created under the authenticated user's namespace, but this not obvious behaviour.
This implements the following new endpoints that match the Public API spec (#1076). I've modified
npm run fetch-examples-ml5
so that it uses the new endpoints. It expects a user to exists and a Public Access Token to be provided to the script.GET /api/:user/sketches
to list all sketchesDELETE /api/:user/sketches/:id
to delete a sketchPOST /api/:user/sketches
to create a new sketchI need to add more tests around the controllers, but I wanted to share now to get feedback.
I have verified that this pull request:
npm run lint
)Fixes #123