API Routes Proposal #450
Replies: 6 comments 17 replies
-
I love all of this ❤️ FYI we won't need I'm OK with |
Beta Was this translation helpful? Give feedback.
-
From @vlucas
The reason that a route cannot be both a react server component (RSC) and an API route is that inherently RSC routes are GET requests. The API route handles every request that hits it, including GET. It becomes ambiguous and/or confusing if we allow both in the same file. |
Beta Was this translation helpful? Give feedback.
-
Thanks for championing this @blittle ! Everything looks fine to me except for this part:
In this case, I find weird that we keep these API functions in Should we consider moving them to |
Beta Was this translation helpful? Give feedback.
-
This is great! I have a comment on Considering we're already proposing named exports, what are your thoughts on having different exports for different methods? Examples: export async function Get(request) {
return new Response(await getAllComments());
}
export async function Post(request) {
return new Response(await createComments(await request.json()));
}
export async function Put(request) {
return new Response(await updateComment(await request.json()));
}
export async function Patch(request) {
return new Response(await updateComment(await request.json()));
}
export async function Delete(request) {
return new Response(await deleteComment(await request.json()));
} |
Beta Was this translation helpful? Give feedback.
-
What do you think about providing a set of predefined error constructors that can be thrown from API handlers? export class RestError extends Error {
constructor(message, status, details) {
super(message)
this.name = this.constructor.name
this.status = status
this.details = details
}
}
export class BadRequestError extends RestError {
constructor(message, details) {
super(message, 400, details)
}
}
export class UnauthorizedError extends RestError {
constructor(message, details) {
super(message, 401, details)
}
}
// ... This could be caught by Hydrogen and converted to responses: |
Beta Was this translation helpful? Give feedback.
-
how about mimicing the service worker even more like this: export async function api (evt) {
const request = evt.request
evt.respondWith(new Response(...))
}
// or:
export async function api (request) {
request.respondWith(new Response(...))
} |
Beta Was this translation helpful? Give feedback.
-
API Routes
Draft implementation at: Shopify/hydrogen#391
Right now Hydrogen only serves routes that host React Server Components. This proposal adds the ability to host arbitrary data routes.
A route is added by creating a ".server.js" file in the
pages
directory (just like normal). Instead of exporting a default function that is a component, the file must export anapi
named function. When called, that function must return aResponse
object.Note: a route file can only export a default React component or an
api
named function, not both.Switch on HTTP Method
The request is passed a Request object. This object can be used to process the response:
Streamed request
Streamed response
Accessing Shopify Data
Because these routes exist outside of react, the
useQuery
anduseShopQuery
hooks cannot be directly used. But what if we pass them as params to theapi
function? These could be special implementations that are usable outside react.Note: this aligns with the proposal in the caching section of Shopify/hydrogen#445
Beta Was this translation helpful? Give feedback.
All reactions