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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
dc9f90d
Merge commit 'ccb045b68c5b4d983a90fa125513fc476e4e2387'
mtrezza Nov 19, 2020
4d72525
Merge remote-tracking branch 'upstream/master'
mtrezza Dec 4, 2020
65a6bdb
Merge remote-tracking branch 'upstream/master'
mtrezza Dec 5, 2020
50274b8
Merge remote-tracking branch 'upstream/master'
mtrezza Dec 8, 2020
3b337cd
Merge remote-tracking branch 'upstream/master'
mtrezza Dec 15, 2020
073f0fc
Merge remote-tracking branch 'upstream/master'
mtrezza Dec 18, 2020
2ee5907
Merge remote-tracking branch 'upstream/master'
mtrezza Dec 19, 2020
241a1d8
Merge remote-tracking branch 'upstream/master'
mtrezza Dec 26, 2020
4f097ce
Merge remote-tracking branch 'upstream/master'
mtrezza Jan 14, 2021
8f3ea1c
Merge remote-tracking branch 'upstream/master'
mtrezza Jan 23, 2021
4743cbc
Merge remote-tracking branch 'upstream/master'
mtrezza Jan 28, 2021
59de429
Merge remote-tracking branch 'upstream/master'
mtrezza Feb 1, 2021
ed2944f
Merge remote-tracking branch 'upstream/master'
mtrezza Feb 2, 2021
3ff82c8
Merge commit '7f47b0427ea56214d9b0199f0fcfa4af38794e02'
mtrezza Feb 9, 2021
19d7556
Merge remote-tracking branch 'upstream/master'
mtrezza Feb 12, 2021
935b913
Merge remote-tracking branch 'upstream/master'
mtrezza Feb 13, 2021
38f3739
Merge remote-tracking branch 'upstream/master'
mtrezza Feb 16, 2021
098a5c3
Merge remote-tracking branch 'upstream/master'
mtrezza Feb 21, 2021
669766a
Merge remote-tracking branch 'upstream/master'
mtrezza Feb 22, 2021
dcb91ee
Merge remote-tracking branch 'upstream/master'
mtrezza Feb 24, 2021
12395b9
added custom routes
mtrezza Feb 27, 2021
fd09d0e
fixed docs typos
mtrezza Feb 27, 2021
b9115cb
added page.customRoutes config validation
mtrezza Feb 27, 2021
8c7e3d4
added 404 response if missing custom route response
mtrezza Feb 27, 2021
164640e
added docs
mtrezza Feb 27, 2021
dc3a0ff
minor README formatting
mtrezza Feb 27, 2021
68dc593
added CHANGELOG entry
mtrezza Feb 27, 2021
d5aa98b
fixed bug in definitions builder that did not recognize array of cust…
mtrezza Mar 2, 2021
90a68e4
added missing route handler definition
mtrezza Mar 2, 2021
73290b6
fixed custom routes definition
mtrezza Mar 2, 2021
093af98
Merge remote-tracking branch 'upstream/master'
mtrezza Mar 2, 2021
8fde141
Merge branch 'master' into add-custom-routes-to-pages-router
mtrezza Mar 2, 2021
a5b27d0
Merge remote-tracking branch 'upstream/master'
mtrezza Mar 3, 2021
7935fd6
Merge branch 'master' into add-custom-routes-to-pages-router
mtrezza Mar 3, 2021
54ce26d
Merge branch 'master' into add-custom-routes-to-pages-router
mtrezza Mar 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
__BREAKING CHANGES:__
- NEW: Added file upload restriction. File upload is now only allowed for authenticated users by default for improved security. To allow file upload also for Anonymous Users or Public, set the `fileUpload` parameter in the [Parse Server Options](https://parseplatform.org/parse-server/api/master/ParseServerOptions.html). [#7071](https://github.com/parse-community/parse-server/pull/7071). Thanks to [dblythy](https://github.com/dblythy), [Manuel Trezza](https://github.com/mtrezza).
___
- FIX: Move graphql-tag from devDependencies to dependencies. [7183](https://github.com/parse-community/parse-server/pull/7183). Thanks to [Antonio Davi Macedo Coelho de Castro](https://github.com/davimacedo).
- IMPROVE: Allow Cloud Validator `options` to be async [#7155](https://github.com/parse-community/parse-server/pull/7155). Thanks to [dblythy](https://github.com/dblythy)
- NEW (EXPERIMENTAL): Added new page router with placeholder rendering and localization of custom and feature pages such as password reset and email verification. **Caution, this is an experimental feature that may not be appropriate for production.** [#6891](https://github.com/parse-community/parse-server/issues/6891). Thanks to [Manuel Trezza](https://github.com/mtrezza).
- NEW (EXPERIMENTAL): Added custom routes to easily customize flows for password reset, email verification or build entirely new flows. **Caution, this is an experimental feature that may not be appropriate for production.** [#7231](https://github.com/parse-community/parse-server/issues/7231). Thanks to [Manuel Trezza](https://github.com/mtrezza).
- NEW: Added convenience method `Parse.Cloud.sendEmail(...)` to send email via email adapter in Cloud Code. [#7089](https://github.com/parse-community/parse-server/pull/7089). Thanks to [dblythy](https://github.com/dblythy)
- NEW: LiveQuery support for $and, $nor, $containedBy, $geoWithin, $geoIntersects queries [#7113](https://github.com/parse-community/parse-server/pull/7113). Thanks to [dplewis](https://github.com/dplewis)
- NEW: Supporting patterns in LiveQuery server's config parameter `classNames` [#7131](https://github.com/parse-community/parse-server/pull/7131). Thanks to [Nes-si](https://github.com/Nes-si)
Expand All @@ -33,9 +32,12 @@ ___
- IMPROVE: Allow Cloud Validator `options` to be async [#7155](https://github.com/parse-community/parse-server/pull/7155). Thanks to [dblythy](https://github.com/dblythy)
- IMPROVE: Optimize queries on classes with pointer permissions. [#7061](https://github.com/parse-community/parse-server/pull/7061). Thanks to [Pedro Diaz](https://github.com/pdiaz)
- IMPROVE: Parse Server will from now on be continuously tested against all relevant Postgres versions (minor versions). Added Postgres compatibility table to Parse Server docs. [#7176](https://github.com/parse-community/parse-server/pull/7176). Thanks to [Corey Baker](https://github.com/cbaker6).
- IMPROVE: Allow Cloud Validator `options` to be async [#7155](https://github.com/parse-community/parse-server/pull/7155). Thanks to [dblythy](https://github.com/dblythy)
- IMPROVE: Allow Cloud Validator `options` to be async [#7155](https://github.com/parse-community/parse-server/pull/7155). Thanks to [dblythy](https://github.com/dblythy)
- FIX: Fix error when a not yet inserted job is updated [#7196](https://github.com/parse-community/parse-server/pull/7196). Thanks to [Antonio Davi Macedo Coelho de Castro](https://github.com/davimacedo).
- FIX: request.context for afterFind triggers. [#7078](https://github.com/parse-community/parse-server/pull/7078). Thanks to [dblythy](https://github.com/dblythy)
- FIX: Winston Logger interpolating stdout to console [#7114](https://github.com/parse-community/parse-server/pull/7114). Thanks to [dplewis](https://github.com/dplewis)
- FIX: Move graphql-tag from devDependencies to dependencies. [7183](https://github.com/parse-community/parse-server/pull/7183). Thanks to [Antonio Davi Macedo Coelho de Castro](https://github.com/davimacedo).

### 4.5.0
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)
Expand Down
82 changes: 71 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ The full documentation for Parse Server is available in the [wiki](https://githu
- [Basic Options](#basic-options)
- [Client Key Options](#client-key-options)
- [Email Verification and Password Reset](#email-verification-and-password-reset)
- [Custom Routes](#custom-routes)
- [Example](#example)
- [Reserved Paths](#reserved-paths)
- [Parameters](#parameters)
- [Custom Pages](#custom-pages)
- [Using Environment Variables](#using-environment-variables)
- [Available Adapters](#available-adapters)
Expand All @@ -67,7 +71,9 @@ The full documentation for Parse Server is available in the [wiki](https://githu
- [Pages](#pages)
- [Localization with Directory Structure](#localization-with-directory-structure)
- [Localization with JSON Resource](#localization-with-json-resource)
- [Parameters](#parameters)
- [Dynamic placeholders](#dynamic-placeholders)
- [Reserved Keys](#reserved-keys)
- [Parameters](#parameters-1)
- [Logging](#logging)
- [Live Query](#live-query)
- [GraphQL](#graphql)
Expand Down Expand Up @@ -124,12 +130,12 @@ Parse Server is continuously tested with the most recent releases of MongoDB to
#### PostgreSQL
Parse Server is continuously tested with the most recent releases of PostgreSQL and PostGIS to ensure compatibility. We follow the [PostGIS docker tags](https://registry.hub.docker.com/r/postgis/postgis/tags?page=1&ordering=last_updated) and only test against versions that are officially supported and have not reached their end-of-life date.

| Version | PostGIS Version | End-of-Life Date | Compatibility |
|------------------|-----------------|------------------|--------------------|
| Postgres 10.x | 3.0.x, 3.1.x | November 2022 | ✅ Fully compatible |
| Postgres 11.x | 3.0.x, 3.1.x | November 2023 | ✅ Fully compatible |
| Postgres 12.x | 3.0.x, 3.1.x | November 2024 | ✅ Fully compatible |
| Postgres 13.x | 3.0.x, 3.1.x | November 2025 | ✅ Fully compatible |
| Version | PostGIS Version | End-of-Life Date | Compatibility |
|---------------|-----------------|------------------|--------------------|
| Postgres 10.x | 3.0.x, 3.1.x | November 2022 | ✅ Fully compatible |
| Postgres 11.x | 3.0.x, 3.1.x | November 2023 | ✅ Fully compatible |
| Postgres 12.x | 3.0.x, 3.1.x | November 2024 | ✅ Fully compatible |
| Postgres 13.x | 3.0.x, 3.1.x | November 2025 | ✅ Fully compatible |

### Locally
```bash
Expand Down Expand Up @@ -388,6 +394,60 @@ You can also use other email adapters contributed by the community such as:
- [parse-server-generic-email-adapter](https://www.npmjs.com/package/parse-server-generic-email-adapter)
- [parse-server-api-mail-adapter](https://www.npmjs.com/package/parse-server-api-mail-adapter)

## Custom Routes
**Caution, this is an experimental feature that may not be appropriate for production.**

Custom routes allow to build user flows with webpages, similar to the existing password reset and email verification features. Custom routes are defined with the `pages` option in the Parse Server configuration:

### Example

```js
const api = new ParseServer({
...otherOptions,

pages: {
enableRouter: true, // Enables the experimental feature; required for custom routes
customRoutes: [{
method: 'GET',
path: 'custom_route',
handler: async request => {
// 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 sending a `GET` request to:
`https://[parseServerPublicUrl]/[parseMount]/[pagesEndpoint]/[appId]/[customRoute]`

The `handler` receives the `request` and returns a `custom_page.html` webpage from the `pages.pagesPath` directory as response. The advantage of building a custom route this way is that it automatically makes use of Parse Server's built-in capabilities, such as [page localization](#pages) and [dynamic placeholders](#dynamic-placeholders).

### Reserved Paths
The following paths are already used by Parse Server's built-in features and are therefore not available for custom routes. Custom routes with an identical combination of `path` and `method` are ignored.

| Path | HTTP Method | Feature |
|-----------------------------|-------------|--------------------|
| `verify_email` | `GET` | email verification |
| `resend_verification_email` | `POST` | email verification |
| `choose_password` | `GET` | password reset |
| `request_password_reset` | `GET` | password reset |
| `request_password_reset` | `POST` | password reset |

### Parameters

| Parameter | Optional | Type | Default value | Example values | Environment variable | Description |
|------------------------------|----------|-----------------|---------------|-----------------------|------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `pages` | yes | `Object` | `undefined` | - | `PARSE_SERVER_PAGES` | The options for pages such as password reset and email verification. |
| `pages.enableRouter` | yes | `Boolean` | `false` | - | `PARSE_SERVER_PAGES_ENABLE_ROUTER` | Is `true` if the pages router should be enabled; this is required for any of the pages options to take effect. **Caution, this is an experimental feature that may not be appropriate for production.** |
| `pages.customRoutes` | yes | `Array` | `[]` | - | `PARSE_SERVER_PAGES_CUSTOM_ROUTES` | The custom routes. The routes are added in the order they are defined here, which has to be considered since requests traverse routes in an ordered manner. Custom routes are traversed after build-in routes such as password reset and email verification. |
| `pages.customRoutes.method` | | `String` | - | `GET`, `POST` | - | The HTTP method of the custom route. |
| `pages.customRoutes.path` | | `String` | - | `custom_page` | - | The path of the custom route. Note that the same path can used if the `method` is different, for example a path `custom_page` can have two routes, a `GET` and `POST` route, which will be invoked depending on the HTTP request method. |
| `pages.customRoutes.handler` | | `AsyncFunction` | - | `async () => { ... }` | - | The route handler that is invoked when the route matches the HTTP request. If the handler does not return a page, the request is answered with a 404 `Not found.` response. |

## Custom Pages

It’s possible to change the default pages of the app and redirect the user to another path or domain.
Expand Down Expand Up @@ -471,11 +531,11 @@ let api = new ParseServer({
```
### Parameters <!-- omit in toc -->

| Parameter | Optional | Type | Default value | Example values | Environment variable | Description |
|----------------------------|----------|-----------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `idempotencyOptions` | yes | `Object` | `undefined` | | PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_OPTIONS | Setting this enables idempotency enforcement for the specified paths. |
| Parameter | Optional | Type | Default value | Example values | Environment variable | Description |
|----------------------------|----------|-----------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `idempotencyOptions` | yes | `Object` | `undefined` | | PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_OPTIONS | Setting this enables idempotency enforcement for the specified paths. |
| `idempotencyOptions.paths` | yes | `Array<String>` | `[]` | `.*` (all paths, includes the examples below), <br>`functions/.*` (all functions), <br>`jobs/.*` (all jobs), <br>`classes/.*` (all classes), <br>`functions/.*` (all functions), <br>`users` (user creation / update), <br>`installations` (installation creation / update) | PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_PATHS | An array of path patterns that have to match the request path for request deduplication to be enabled. The mount path must not be included, for example to match the request path `/parse/functions/myFunction` specify the path pattern `functions/myFunction`. A trailing slash of the request path is ignored, for example the path pattern `functions/myFunction` matches both `/parse/functions/myFunction` and `/parse/functions/myFunction/`. |
| `idempotencyOptions.ttl` | yes | `Integer` | `300` | `60` (60 seconds) | PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_TTL | The duration in seconds after which a request record is discarded from the database. Duplicate requests due to network issues can be expected to arrive within milliseconds up to several seconds. This value must be greater than `0`. |
| `idempotencyOptions.ttl` | yes | `Integer` | `300` | `60` (60 seconds) | PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_TTL | The duration in seconds after which a request record is discarded from the database. Duplicate requests due to network issues can be expected to arrive within milliseconds up to several seconds. This value must be greater than `0`. |

### Notes <!-- omit in toc -->

Expand Down
15 changes: 15 additions & 0 deletions public/custom_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<!--
This page demonstrates how to use custom pages in custom routes.
-->
<html>

<head>
<title>{{appName}}</title>
</head>

<body>
<h1>{{appName}}</h1>
</body>

</html>
Loading