Skip to content
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

Add custom routes to pages router #7231

Merged

Conversation

mtrezza
Copy link
Member

@mtrezza mtrezza commented Feb 27, 2021

New Pull Request Checklist

Issue Description

Currently, the experimental PagesRouter does not allow to add custom routes.

Related issue: closes #7230

Approach

Add Parse Server option pages.customRoutes to define custom routes that are then auto-mounted by the PagesRouter.

Example:

pages.customRoutes: [
    {
      method: 'GET',
      path: 'custom_route',
      handler: async req => {
        // some custom logic
        // ...
        // then, depending on the outcome, return a HTML file as response
        return { file: 'custom_page.html' };
      },
    },
]

The above route can be invoked by requesting https://example.com/[mount]/apps/[appId]/custom_page.

The returned file enjoys all the internal logic that is also used by the feature pages (e.g password reset) such as localization, and custom placeholders.

Note: This feature is only implemented in the still experimental PagesRouter which is expected to deprecate the PublicAPIRouter in the future.

Side effects

  • Fixed bug in Parse Server definitions builder that did not support the type "array of custom type"

TODOs before merging

  • Add test cases
  • Add config param validation + tests
  • Add entry to changelog
  • Add changes to documentation (guides, repository pages, in-code descriptions)

mtrezza and others added 22 commits November 19, 2020 01:05
* commit 'ccb045b68c5b4d983a90fa125513fc476e4e2387':
  fix: upgrade @graphql-tools/links from 6.2.4 to 6.2.5 (parse-community#7007)
  fix: upgrade pg-promise from 10.7.0 to 10.7.1 (parse-community#7009)
  fix: upgrade jwks-rsa from 1.10.1 to 1.11.0 (parse-community#7008)
  fix: upgrade graphql from 15.3.0 to 15.4.0 (parse-community#7011)
  update stale bot (parse-community#6998)
  fix(beforeSave/afterSave): Return value instead of Parse.Op for nested fields (parse-community#7005)
  fix(beforeSave): Skip Sanitizing Database results (parse-community#7003)
  Fix includeAll for querying a Pointer and Pointer array (parse-community#7002)
  Init (parse-community#6999)
* commit '7f47b0427ea56214d9b0199f0fcfa4af38794e02':
  Add page localization (parse-community#7128)
  Improve contribution guide (parse-community#7075)
  fix: upgrade pg-promise from 10.9.0 to 10.9.1 (parse-community#7170)
  Add tests against multiple MongoDB versions (parse-community#7161)
  fix: upgrade mime from 2.4.7 to 2.5.0 (parse-community#7166)
  fix: upgrade pg-promise from 10.8.7 to 10.9.0 (parse-community#7168)
  fix: upgrade apollo-server-express from 2.19.1 to 2.19.2 (parse-community#7165)
  Upgrade @node-rs/bcrypt to latest version (parse-community#7159)
  Run Prettier after Definitions (parse-community#7164)
@codecov
Copy link

codecov bot commented Feb 27, 2021

Codecov Report

Merging #7231 (ee2a11a) into master (cac6951) will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #7231      +/-   ##
==========================================
+ Coverage   94.00%   94.03%   +0.03%     
==========================================
  Files         172      172              
  Lines       12955    12970      +15     
==========================================
+ Hits        12178    12196      +18     
+ Misses        777      774       -3     
Impacted Files Coverage Δ
src/Options/index.js 100.00% <ø> (ø)
src/Config.js 93.02% <100.00%> (+0.13%) ⬆️
src/Options/Definitions.js 100.00% <100.00%> (ø)
src/Routers/PagesRouter.js 97.70% <100.00%> (+0.11%) ⬆️
src/Adapters/Storage/Mongo/MongoStorageAdapter.js 92.92% <0.00%> (+0.66%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cac6951...54ce26d. Read the comment docs.

@mtrezza mtrezza marked this pull request as draft February 27, 2021 04:51
@mtrezza mtrezza marked this pull request as ready for review February 27, 2021 23:56
@mtrezza
Copy link
Member Author

mtrezza commented Feb 27, 2021

This is ready for review; a good place to get familiar with this feature is to read the added docs in the README.

@davimacedo
Copy link
Member

@mtrezza thanks for the PR! I'd like to better understand the problem that this feature solves. What would be the benefit of using the custom route instead of just mounting a new endpoint in the Express.js app?

@mtrezza
Copy link
Member Author

mtrezza commented Mar 1, 2021

@davimacedo Sure, this is mostly a forward-looking PR, because it provides the basis to make future features available to all custom routes by just adding a feature to the PagesRouter.

But there are already some benefits in this first version:

  • These custom routes are invoked with the same familiar URL patterns as the password reset, email verification built-in routes, so the URL includes the app ID. That means all default placeholders like appName, appId, parseServerPublicUrl are automatically available inside the HTML page. While this can already be achieved with the PagesRouter's custom placeholders feature, the custom route of this PR doesn't require the developer to write additional code to make these placeholders available. That means custom routes make it easy to create HTML forms that post to another custom route, like chaining.
  • Custom routes can get more abstracted in the future to become like a toolbox to create the above mentioned chains even easier and with more convenience features, which means less code for developers.
  • The custom routes are mounted after the built-in routes like password reset. That means a custom route can not accidentally "steal" a request from a built-in route, because requests always are first passed to the built-in routes for a possible match.
  • As the PagesRouter gets new features in the future, they can also be made be available to custom routes, because they are part of the router. For example:
    • Add a feature that logs a warning if a custom route overlaps with a built-in route that uses the same endpoint.
    • Add convenience return values for custom route, currently it is only a webpage, but this can be abstracted and extended further.
    • Add interface to directly override built-in routes with custom routes.
  • I could see the PagesRouter in the future extending its role in Parse Server, for centralized 404, 500 response handling. The custom routes interface may also be used internally by Parse Server for that reason, but that's still a vague idea.

Copy link
Member

@davimacedo davimacedo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am in for this new feature in Parse Server. @dplewis thoughts?

src/Options/docs.js Outdated Show resolved Hide resolved
Copy link
Member

@dplewis dplewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just a quick comment.

@mtrezza
Copy link
Member Author

mtrezza commented Mar 2, 2021

@dplewis Thanks, this is ready for review

@mtrezza
Copy link
Member Author

mtrezza commented Mar 5, 2021

If there is no objection, I'll merge this later today; this also fixes an issue with the config definitions builder (described above) which I again stumble upon in another PR.

@mtrezza mtrezza merged commit de50b7b into parse-community:master Mar 7, 2021
@parseplatformorg
Copy link
Contributor

🎉 This change has been released in version 5.0.0-beta.1

@parseplatformorg parseplatformorg added the state:released-beta Released as beta version label Nov 1, 2021
@mtrezza mtrezza mentioned this pull request Mar 12, 2022
@parseplatformorg
Copy link
Contributor

🎉 This change has been released in version 5.0.0

@parseplatformorg parseplatformorg added the state:released Released as stable version label Mar 14, 2022
@mtrezza mtrezza deleted the add-custom-routes-to-pages-router branch March 24, 2022 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state:released Released as stable version state:released-beta Released as beta version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add custom route building
4 participants