Skip to content

Commit de50b7b

Browse files
authored
Add custom routes to pages router (parse-community#7231)
* added custom routes * fixed docs typos * added page.customRoutes config validation * added 404 response if missing custom route response * added docs * minor README formatting * added CHANGELOG entry * fixed bug in definitions builder that did not recognize array of custom type * added missing route handler definition * fixed custom routes definition
1 parent cac6951 commit de50b7b

File tree

10 files changed

+327
-17
lines changed

10 files changed

+327
-17
lines changed

CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
__BREAKING CHANGES:__
1616
- 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).
1717
___
18-
- 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).
19-
- 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)
2018
- 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).
19+
- 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).
2120
- 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)
2221
- 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)
2322
- 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)
@@ -33,9 +32,12 @@ ___
3332
- 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)
3433
- 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)
3534
- 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).
35+
- 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)
36+
- 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)
3637
- 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).
3738
- FIX: request.context for afterFind triggers. [#7078](https://github.com/parse-community/parse-server/pull/7078). Thanks to [dblythy](https://github.com/dblythy)
3839
- FIX: Winston Logger interpolating stdout to console [#7114](https://github.com/parse-community/parse-server/pull/7114). Thanks to [dplewis](https://github.com/dplewis)
40+
- 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).
3941

4042
### 4.5.0
4143
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...4.5.0)

README.md

+71-11
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ The full documentation for Parse Server is available in the [wiki](https://githu
5858
- [Basic Options](#basic-options)
5959
- [Client Key Options](#client-key-options)
6060
- [Email Verification and Password Reset](#email-verification-and-password-reset)
61+
- [Custom Routes](#custom-routes)
62+
- [Example](#example)
63+
- [Reserved Paths](#reserved-paths)
64+
- [Parameters](#parameters)
6165
- [Custom Pages](#custom-pages)
6266
- [Using Environment Variables](#using-environment-variables)
6367
- [Available Adapters](#available-adapters)
@@ -67,7 +71,9 @@ The full documentation for Parse Server is available in the [wiki](https://githu
6771
- [Pages](#pages)
6872
- [Localization with Directory Structure](#localization-with-directory-structure)
6973
- [Localization with JSON Resource](#localization-with-json-resource)
70-
- [Parameters](#parameters)
74+
- [Dynamic placeholders](#dynamic-placeholders)
75+
- [Reserved Keys](#reserved-keys)
76+
- [Parameters](#parameters-1)
7177
- [Logging](#logging)
7278
- [Live Query](#live-query)
7379
- [GraphQL](#graphql)
@@ -124,12 +130,12 @@ Parse Server is continuously tested with the most recent releases of MongoDB to
124130
#### PostgreSQL
125131
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.
126132

127-
| Version | PostGIS Version | End-of-Life Date | Compatibility |
128-
|------------------|-----------------|------------------|--------------------|
129-
| Postgres 10.x | 3.0.x, 3.1.x | November 2022 | ✅ Fully compatible |
130-
| Postgres 11.x | 3.0.x, 3.1.x | November 2023 | ✅ Fully compatible |
131-
| Postgres 12.x | 3.0.x, 3.1.x | November 2024 | ✅ Fully compatible |
132-
| Postgres 13.x | 3.0.x, 3.1.x | November 2025 | ✅ Fully compatible |
133+
| Version | PostGIS Version | End-of-Life Date | Compatibility |
134+
|---------------|-----------------|------------------|--------------------|
135+
| Postgres 10.x | 3.0.x, 3.1.x | November 2022 | ✅ Fully compatible |
136+
| Postgres 11.x | 3.0.x, 3.1.x | November 2023 | ✅ Fully compatible |
137+
| Postgres 12.x | 3.0.x, 3.1.x | November 2024 | ✅ Fully compatible |
138+
| Postgres 13.x | 3.0.x, 3.1.x | November 2025 | ✅ Fully compatible |
133139

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

397+
## Custom Routes
398+
**Caution, this is an experimental feature that may not be appropriate for production.**
399+
400+
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:
401+
402+
### Example
403+
404+
```js
405+
const api = new ParseServer({
406+
...otherOptions,
407+
408+
pages: {
409+
enableRouter: true, // Enables the experimental feature; required for custom routes
410+
customRoutes: [{
411+
method: 'GET',
412+
path: 'custom_route',
413+
handler: async request => {
414+
// custom logic
415+
// ...
416+
// then, depending on the outcome, return a HTML file as response
417+
return { file: 'custom_page.html' };
418+
}
419+
}]
420+
}
421+
}
422+
```
423+
424+
The above route can be invoked by sending a `GET` request to:
425+
`https://[parseServerPublicUrl]/[parseMount]/[pagesEndpoint]/[appId]/[customRoute]`
426+
427+
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).
428+
429+
### Reserved Paths
430+
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.
431+
432+
| Path | HTTP Method | Feature |
433+
|-----------------------------|-------------|--------------------|
434+
| `verify_email` | `GET` | email verification |
435+
| `resend_verification_email` | `POST` | email verification |
436+
| `choose_password` | `GET` | password reset |
437+
| `request_password_reset` | `GET` | password reset |
438+
| `request_password_reset` | `POST` | password reset |
439+
440+
### Parameters
441+
442+
| Parameter | Optional | Type | Default value | Example values | Environment variable | Description |
443+
|------------------------------|----------|-----------------|---------------|-----------------------|------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
444+
| `pages` | yes | `Object` | `undefined` | - | `PARSE_SERVER_PAGES` | The options for pages such as password reset and email verification. |
445+
| `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.** |
446+
| `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. |
447+
| `pages.customRoutes.method` | | `String` | - | `GET`, `POST` | - | The HTTP method of the custom route. |
448+
| `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. |
449+
| `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. |
450+
391451
## Custom Pages
392452

393453
It’s possible to change the default pages of the app and redirect the user to another path or domain.
@@ -471,11 +531,11 @@ let api = new ParseServer({
471531
```
472532
### Parameters <!-- omit in toc -->
473533
474-
| Parameter | Optional | Type | Default value | Example values | Environment variable | Description |
475-
|----------------------------|----------|-----------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
476-
| `idempotencyOptions` | yes | `Object` | `undefined` | | PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_OPTIONS | Setting this enables idempotency enforcement for the specified paths. |
534+
| Parameter | Optional | Type | Default value | Example values | Environment variable | Description |
535+
|----------------------------|----------|-----------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
536+
| `idempotencyOptions` | yes | `Object` | `undefined` | | PARSE_SERVER_EXPERIMENTAL_IDEMPOTENCY_OPTIONS | Setting this enables idempotency enforcement for the specified paths. |
477537
| `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/`. |
478-
| `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`. |
538+
| `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`. |
479539
480540
### Notes <!-- omit in toc -->
481541

public/custom_page.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<!--
3+
This page demonstrates how to use custom pages in custom routes.
4+
-->
5+
<html>
6+
7+
<head>
8+
<title>{{appName}}</title>
9+
</head>
10+
11+
<body>
12+
<h1>{{appName}}</h1>
13+
</body>
14+
15+
</html>

0 commit comments

Comments
 (0)