An entrypoint is an endpoint an user can request
entrypoints/
directory.
For instance, to create a new route at POST /test
, just create a file called test
inside of the entrypoints
directory and export a function called POST()
:
Response from `POST /test`
GET()
, PUT()
, DELETE()
and PATCH()
:
Request
and Response
request
and response
objects. If you not familiar with them, you quickly check their MDN documentation.
request
request
object is always passed as the first paremeter to your entrypoint function — export function POST(req: Request) {}
.
With it, you can get information about the client’s request:
req
stricly follows the Web Standards — which means if you ever have any questions about it (on how to get the query params, for example) you can directly search on MDN.response
response
object, you can just return a new Response()
from your entrypoint, like:
POST /test
this is now the response:
Response from `POST /test` with a custom response
Response
object, you can return almost anything — more on this below.
response
stricly follows the Web Standards — which means if you ever have any questions about it (on how to return a custom header) you can directly search on MDN.export function* POST() {}
or export async function* POST() {}
.What is a `*` after `function`?
function*
makes it a generator. generators
are a native JS feature that allow for a function to return multiple times rather than just once. Though the syntax is a bit different than what you might expect.Since this feature is not widely used, we do explain it in the following doc page about the reason()
function. For now, don’t worry about too much.export function POST() {}
or export async function POST() {}
.return new Response()
.
So, what can you actually return in an entrypoint?
JSON.parse()
in it would work.Response
with return new Response()
.yield
keyword.
The response
yield
keyword and that’s ok! It is part of the generator
feature set of JavaScript and we’ll go in-depth about them in the next page. Don’t worry for now.
You can, of course, also use the return
keyword:
The response with `return`
yield
instead of return
? Because when you use return
in a function, that function immediatly stops executing — meaning you will not be able to stream more values after you call return
.
Cool! Now we can go over the types of values you can yield
/return
in a streaming entrypoint.
yield
:
yield
a certain type you cannot yield the other type in that entrypoint. For instance, the following code will error:return
:
return
the type you previously yielded. You cannot yield
an object and then return
a string. If you haven’t yielded anything, then you are free to return
whatever you want.objects
object
. It is extremely important that you understand how it works.
By default, HTTP streams only supports text. So how does RΞASON allows you to stream back objects? That’s because we apply a special encoding to your data before sending it. This is key to understand.
So, what encoding does RΞASON apply? A custom one.
Using an encoding means that your client needs to decode your data before being able to read it. We know this is a cost — both in terms of developer ergonomic & maintainability.
Why encode at all then?
{ foo: 'bar', baaz: true }
) and in your frontend (client) you will receive the JSON object the backend streamed back:
The streaming response
obj
— that is not needed though. Here’s an equivalent code that’ll produce the same output:
The streaming response
The streaming response
string
return
/yield
string
).
return
/yield
either strings or objects. Never both in the same entrypoint.generator
reasonStream
is an async generator, and we want to be able to just return it for ease-of-use:
The streaming response
reason()
function!