Platform Request Middleware
When building a multi-project platform with Makers, you only need to connect one wildcard domain to assign subdomains to tenant projects. Makers automatically routes requests to the corresponding projects, eliminating the need to maintain this distribution logic yourself. The platform request middleware is bound to the wildcard domain and runs before requests enter a project. A single script can centrally handle authentication, traffic control, and other common logic for all projects under the domain, eliminating the need to repeat development in each project.

How to Configure
The middleware takes effect based on the wildcard domain and is uploaded as a single
.js file. Configuration steps:1. Contact us to connect a wildcard domain (for example,
*.example.com) to the platform, and confirm that the wildcard domain has taken effect.2. In the console, go to Settings > Platform and Multi-Tenancy, and upload the script in the Request Middleware column of the target wildcard domain.

It takes effect approximately 15 minutes after upload. Once it takes effect, all requests from projects under the domain pass through it first. Only one middleware is retained for the same wildcard domain, and uploading again replaces the current version. You can also update, download, or delete the existing script at the same entry.
Script Structure
The file can contain only one top-level declaration, which must be the following function. The parameter name must be
context.async function onRequest(context) {}
In addition:
You cannot write
import or export, nor can you use dynamic import().You cannot declare variables, functions, or other statements at the top level. Functions that need to be reused should be written inside
onRequest.You cannot introduce external dependencies or use Node.js APIs such as
require, fs, and process.The script is compiled according to ES2023 before execution. You can use the following Web APIs:
Request, Response, URL, and Headers.Configuration Constraints
It can only be configured for a wildcard domain that has already taken effect, and it takes effect approximately 15 minutes after configuration.
Each wildcard domain has only one request middleware, and uploading again replaces the current version.
Uploads or updates for the same wildcard domain must be at least 30 seconds apart.
Only
.js files are accepted. The encoding must be UTF-8, and the file size must not exceed 1 MB.context Parameter
The
context received by onRequest has the following members:Member | Description |
request | The request that is currently entering the middleware can read the URL, method, request headers, and request body. |
fetch(input, init?) | Obtain the response of the current project. For input, pass a Request object or a URL string.init is an optional parameter and has the same meaning as RequestInit. |
waitUntil(promise) | Register a Promise. After the response is returned, the runtime still waits for it to complete, and it does not change the response that has already been returned. |
async function onRequest(context) {const request = context.request;// Obtain the complete request objectconst url = new URL(request.url);// Construct a URL objectconst response = await context.fetch(request);// Fetch the request without returning it for nowconst copy = response.clone();// Clone the request to test asynchronous waitingcontext.waitUntil(copy.arrayBuffer());// Wait for asynchronous completionreturn response;}
In addition to the APIs mounted on the context, other JS runtime examples can also be used. For details, see Edge Functions.
Examples
API Access Control
Requests other than
/api/ are forwarded to the project as-is. Requests to /api/ must include the agreed Authorization header. Otherwise, a 401 response is returned and the request does not go to the project.async function onRequest(context) {const request = context.request;const url = new URL(request.url);const platformToken = 'xxx';//Platform Tokenif (!url.pathname.startsWith("/api/")) {return context.fetch(request);}const token = request.headers.get("authorization");if(token != platformToken){return new Response(JSON.stringify({ code: 401, message: "unauthorized" }), {status: 401,headers: { "content-type": "application/json; charset=utf-8" },});}return context.fetch(request);}
Adding Response Headers Uniformly
After retrieving the project response, add response headers uniformly and return it.
async function onRequest(context) {const response = await context.fetch(context.request);const headers = new Headers(response.headers);headers.set("x-middleware", "1");return new Response(response.body, {status: response.status,statusText: response.statusText,headers,});}
