Core Concepts

#ZuploRequest

The ZuploRequest object is the main parameter passed to both RequestHandlers and Policies. It represents the incoming request.

ZuploRequest inherits from the web standard Request class used with fetch - you can read more about this on MDN including an explanation of how all of its properties and methods work: https://developer.mozilla.org/en-US/docs/Web/API/Request.

In addition to the standard properties, the following are added for convenience.

#Properties

  • params - if you use tokens in your route’s URL, we automatically parse them into properties on the params property of your request. For example, imagine a route with path /products/:productId/vendors/:vendorId. A match on this would yield values as follows:
const productId = request.params.productId;
const vendorId = request.params.vendorId;
ts
  • user - an optional object identifying a ‘user’. If undefined this typically means the request is anonymous. If present, the user object will have a sub property that is a unique identifier for that user. There is also an optional data property that is of any type that typically contains other information about the user. When using JWT tokens you’ll usually find all the claims here.
  • query - a dictionary of query-string values. For example, a URL with a query string like https://example.com?foo=bar would present as follows:
const foo = request.query.foo;
ts

#Constructor

It can be useful to create a new ZuploRequest inside a policy (see policies) to forward to the next policy or handler in the chain. You can create a completely fresh ZuploRequest as follows:

const newRequest = new ZuploRequest("http://new-host.com/", {
  method: "POST",
  headers: {
    "content-type": "application/json",
  },
  body: "test",
});
ts

#Request Query

The request.query property is a helper that takes your QueryString and converts it into a JavaScript dictionary (e.g. Record<string, string> in TypeScript). This helper property does not support multiple values for the same key on a QueryString, e.g.

?foo=bar&foo=wibble

To access the array of values in this case you can instead use the URL type and searchParams:

const url = new URL(request.url);
const foo = url.searchParams.get("foo");
// foo will be an array here
ts