Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(create-pages): api handling #1108

Merged
merged 14 commits into from
Jan 1, 2025
Merged

Conversation

tylersayshi
Copy link
Contributor

@tylersayshi tylersayshi commented Dec 30, 2024

This adds api route handling in createPages with tests

Copy link

vercel bot commented Dec 30, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
waku ✅ Ready (Inspect) Visit Preview Jan 1, 2025 0:08am

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review on the types.


export type CreateApi = <Path extends string>(params: {
path: Path;
render: 'static' | 'dynamic';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we call render for api.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to mode for now... let me know what you think

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm neutral. @sandren may have an idea.

packages/waku/src/router/create-pages.ts Outdated Show resolved Hide resolved
Copy link

codesandbox-ci bot commented Dec 31, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

path,
typeof window !== 'undefined'
? window.location.href
: 'http://localhost', // FIXME what should this be?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dai-shi do you know how this should be handled? Request needs a valid url, which seems to require a host. When we run the tests localhost is the "host".

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having always localhost is okay. When we learn more use cases, we can reconsider. As a workaround, we could use unstable_getHeaders for "host".

new URL(path, 'http://localhost')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm I get this when calling unstable_getHeaders from createPages.handleApi

image

I'm ok with leaving it as localhost though.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure why it errors. let's look into it separately.

Actually, you can probably read "host" header from params.headers, but if it's not available, we can fallback to localhost.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this?

      const req = new Request(
        new URL(path, options.headers['host'] ?? 'http://localhost'),
        options,
      );

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but should we need the protocol part too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

I think it's needed? This is the output when run through my browser console

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I mean the same and options.headers['host'] doesn't include the protocol part, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about the protocol.

However, handApi will run on our server, right?

So will we always want to request to http://localhost actually? I think it makes sense to remove the use of the host header since we expect this to request from the server this code executes on.

Correct me if I'm off here though.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It runs on our server (dynamic) or the build time (static).

I could imagine some use cases would check "host" and change the behavior based on it. So, at least they should be able to read the "host" header.
At that point, creating URL with the "host" header makes sense, but we can defer the decision until we see clear requirements.

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -147,6 +147,15 @@ export type CreateLayout = <Path extends string>(
},
) => void;

type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind, but any reason for this choice?

Copy link
Contributor Author

@tylersayshi tylersayshi Jan 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping just to make this typesafe, but happy to add more methods or remove it if it gets in the way

packages/waku/src/router/define-router.ts Outdated Show resolved Hide resolved
@dai-shi dai-shi merged commit 058f9a1 into dai-shi:main Jan 1, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants