-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Implement SvelteKit integration #183
Conversation
WalkthroughThe changes introduce a new module in Changes
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (2)
src/x/sveltekit.ts (2)
7-7
: Update the SvelteKit documentation linkThe link to SvelteKit in the documentation points to the general Svelte site. Updating it to the specific SvelteKit documentation will provide more direct and relevant information.
Apply this diff to correct the link:
* - * [SvelteKit]: https://svelte.dev/ + * [SvelteKit]: https://kit.svelte.dev/ * * @module */
31-31
: Correct the import path in the documentation exampleIn the example, the import statement uses
"federation.ts"
, which may not reflect the actual module path. Ensure the import path is accurate so developers can follow the example without confusion.Consider updating the import statement:
* ``` typescript -import { federation } from "federation.ts"; // Import the `Federation` object +import { federation } from "../federation/federation.ts"; // Import the `Federation` object * * export const handle = fedifyHook(federation, () => undefined); * ```
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
src/x/sveltekit.ts
(1 hunks)
🔇 Additional comments (1)
src/x/sveltekit.ts (1)
60-88
: Ensure FederationFetchOptions
methods match expected signatures
The methods onNotFound
and onNotAcceptable
inside integrateFetchOptions
should align with the expected signatures defined in FederationFetchOptions
. Verify that these implementations are correctly typed and handle all necessary parameters.
Please confirm that the FederationFetchOptions
interface expects methods with these signatures. If discrepancies exist, adjust the method implementations accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you remove debug logs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (4)
src/x/sveltekit.ts (3)
1-10
: Enhance module documentation with more detailsWhile the documentation provides a basic overview, consider adding:
- Purpose and benefits of the integration
- Content negotiation behavior
- Configuration options and examples
- Error handling scenarios
/** * Fedify with SvelteKit * ================ * * This module provides a [SvelteKit] middleware to integrate with the Fedify. + * + * ## Features + * - Seamless integration between Fedify and SvelteKit + * - Content negotiation based on Accept headers + * - Fallback to SvelteKit's default handling for non-federation requests + * + * ## Configuration + * The integration can be configured through the `fedifyHook` function parameters: + * - `federation`: The Federation instance to handle requests + * - `createContextData`: Custom function to provide context data * * [SvelteKit]: https://svelte.dev/ * * @module */
29-34
: Enhance example with more realistic use caseThe current example is minimal. Consider expanding it to show:
- Context data creation
- Error handling
- Content negotiation scenarios
* @example hooks.server.ts * ``` typescript * import { federation } from "federation.ts"; // Import the `Federation` object + * + * // Example context creation + * const createContext = (event: RequestEvent) => ({ + * user: event.locals.user, + * // Add other context data + * }); * - * export const handle = fedifyHook(federation, () => undefined); + * export const handle = fedifyHook(federation, createContext); + * + * // The hook will: + * // 1. Handle federation requests with JSON-LD responses + * // 2. Fall back to SvelteKit for HTML responses + * // 3. Return 406 Not Acceptable when content negotiation fails * ```
75-85
: Consider enhancing error handling in onNotAcceptableWhile the current implementation handles 404 status, consider:
- Adding error logging for debugging
- Handling other error status codes explicitly
- Including more detailed error messages
async onNotAcceptable(): Promise<Response> { const res = await resolve(event); - if (res.status !== 404) return res; + // Handle specific error cases + switch (res.status) { + case 404: + return new Response("Not acceptable - Resource not found", { + status: 406, + headers: { + "Content-Type": "text/plain", + Vary: "Accept", + "X-Error-Code": "RESOURCE_NOT_FOUND" + }, + }); + case 500: + // Log internal errors but return generic message + console.error("Internal error during content negotiation"); + return new Response("Not acceptable - Internal error", { + status: 406, + headers: { + "Content-Type": "text/plain", + Vary: "Accept", + "X-Error-Code": "INTERNAL_ERROR" + }, + }); + default: + return res; + } - return new Response("Not acceptable", { - status: 406, - headers: { - "Content-Type": "text/plain", - Vary: "Accept", - }, - }); },CHANGES.md (1)
70-70
: Fix the unordered list indentation.The unordered list item is not properly indented according to the style guide.
Apply this diff to fix the indentation:
- - Support SvelteKit integrations. [[#183]] + - Support SvelteKit integrations. [[#183]]🧰 Tools
🪛 Markdownlint (0.35.0)
70-70: Expected: 0; Actual: 1
Unordered list indentation(MD007, ul-indent)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
CHANGES.md
(1 hunks)src/x/sveltekit.ts
(1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.35.0)
CHANGES.md
70-70: Expected: 0; Actual: 1
Unordered list indentation
(MD007, ul-indent)
🔇 Additional comments (1)
src/x/sveltekit.ts (1)
43-54
: Verify error handling with Federation framework
The integration looks solid, but let's verify proper error propagation between SvelteKit and Federation.
✅ Verification successful
Error handling is properly implemented in the Federation framework
The integration between SvelteKit and Federation has robust error handling:
- Federation middleware implements comprehensive error handling with proper logging and error propagation
- Errors are caught at multiple levels (JSON parsing, activity processing, signature verification)
- Custom error handlers (
inboxErrorHandler
,onOutboxError
) allow customization of error handling - HTTP status codes and error responses are properly set for different error scenarios
- Error states are logged with appropriate context for debugging
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check error handling implementation in Federation framework
# Look for error handling patterns in Federation
echo "Checking Federation error handling..."
rg -A 5 "catch|throw|Error|error" ../federation/federation.ts
# Check for similar SvelteKit integrations
echo "Checking for similar patterns in other integrations..."
fd -e ts -e js | xargs rg -l "onNotFound|onNotAcceptable"
Length of output: 665
Script:
#!/bin/bash
# Let's check error handling in the actual federation implementation
echo "Checking Federation implementation..."
ast-grep --pattern 'class Federation {
$$$
fetch($$$) {
$$$
}
$$$
}'
echo "\nChecking error handling in federation handler..."
rg -A 5 "catch|throw|Error|error" src/federation/handler.ts
echo "\nChecking error handling in federation middleware..."
rg -A 5 "catch|throw|Error|error" src/federation/middleware.ts
echo "\nChecking error handling in federation tests..."
rg "catch|throw|Error|error" src/federation/handler.test.ts
Length of output: 22629
CHANGES.md
Outdated
@@ -67,6 +67,7 @@ To be released. | |||
|
|||
[#162]: https://github.com/dahlia/fedify/issues/162 | |||
|
|||
- Support SvelteKit integrations. [[#183]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
The SvelteKit integration needs better documentation coverage
The current documentation for SvelteKit integration is limited to code comments in src/x/sveltekit.ts
. While these comments provide basic usage examples, the integration should be documented in the main integration manual alongside other frameworks like Express, Hono, h3, and Fresh.
- Add SvelteKit section to
docs/manual/integration.md
- Include detailed explanation of
fedifyHook
middleware functionality - Document configuration options and content negotiation behavior
- Add code examples similar to other framework integrations
- Consider adding a SvelteKit-specific tutorial or example
🔗 Analysis chain
Enhance the SvelteKit integration documentation.
The changelog entry for SvelteKit integration is quite brief. Consider adding more details about:
- The functionality provided by the
fedifyHook
middleware - Configuration options and usage examples
- Any limitations or considerations when using SvelteKit integration
Let's check if there's any documentation for the SvelteKit integration:
Let me check the content of the SvelteKit integration file to see if there's sufficient documentation.
Let me check if there's any SvelteKit integration documentation in the manual or tutorial sections.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Look for SvelteKit integration documentation
# Test: Search for SvelteKit-related documentation files and code comments
# Check for documentation files
fd -t f -e md -e mdx sveltekit
# Check for detailed code comments in the SvelteKit module
rg -l "SvelteKit" "src/x"
Length of output: 75
Script:
#!/bin/bash
# Check the content of the SvelteKit integration file
cat src/x/sveltekit.ts
# Also check if there are any examples or documentation in docs directory
fd -t f -e md -e mdx . docs
Length of output: 3363
Script:
#!/bin/bash
# Check manual integration docs for SvelteKit content
cat docs/manual/integration.md
# Search for any SvelteKit mentions in tutorials
rg -i sveltekit docs/tutorial/
Length of output: 9065
🧰 Tools
🪛 Markdownlint (0.35.0)
70-70: Expected: 0; Actual: 1
Unordered list indentation
(MD007, ul-indent)
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #183 +/- ##
=======================================
Coverage 84.46% 84.46%
=======================================
Files 42 42
Lines 11618 11618
Branches 1157 1157
=======================================
Hits 9813 9813
Misses 1784 1784
Partials 21 21 ☔ View full report in Codecov by Sentry. 🚨 Try these New Features:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your first contribution!
a27deac
to
819ece9
Compare
819ece9
to
a6eb859
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caution
Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (3)
src/x/sveltekit.ts (2)
1-11
: Enhance module documentation with more examplesThe module documentation is well-structured, but consider adding:
- Examples of different context data scenarios
- Common use cases for federation integration
- Error handling examples
46-58
: Consider enhancing error handling and type safetyThe implementation could benefit from:
- Adding try-catch block to handle potential errors from
createContextData
- Adding runtime validation for the federation object
Consider applying this improvement:
export function fedifyHook<TContextData>( federation: Federation<TContextData>, createContextData: ( event: RequestEvent, ) => TContextData | Promise<TContextData>, ): (params: HookParams) => Promise<Response> { return async ({ event, resolve }: HookParams) => { + try { + const contextData = await createContextData(event); return await federation.fetch(event.request, { - contextData: await createContextData(event), + contextData, ...integrateFetchOptions({ event, resolve }), }); + } catch (error) { + console.error('Error in fedifyHook:', error); + return new Response('Internal Server Error', { status: 500 }); + } }; }docs/manual/integration.md (1)
199-214
: Consider enhancing the SvelteKit integration documentation.While the basic integration is well documented, consider adding:
- A brief explanation of SvelteKit's server hooks concept
- Any specific considerations or limitations when using Fedify with SvelteKit
- Examples of common use cases or patterns
Would you like me to help draft the additional documentation sections?
🛑 Comments failed to post (1)
docs/manual/integration.md (1)
216-216:
⚠️ Potential issueFix typo in SvelteKit link reference.
There's a typo in the link reference: "SevlteKit" should be "SvelteKit".
Apply this fix:
-[SevlteKit]: https://kit.svelte.dev/ +[SvelteKit]: https://kit.svelte.dev/📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.[SvelteKit]: https://kit.svelte.dev/
🧰 Tools
🪛 Markdownlint (0.35.0)
216-216: Unused link or image reference definition: "sevltekit"
Link and image reference definitions should be needed(MD053, link-image-reference-definitions)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (7)
docs/manual/integration.md (2)
194-214
: Enhance the SvelteKit integration documentation.While the section follows the document's structure well, consider these improvements to make it more comprehensive:
- Add comments explaining the expected shape and usage of the context data parameter in the code example
- Include any specific considerations or limitations when using Fedify with SvelteKit
- Add an example of how to handle errors or customize the hook behavior
Here's a suggested addition to the code example:
// This is the entry point to the Fedify hook from the SvelteKit framework: -export const handle = fedifyHook(federation, (req) => "context data"); +// The context data factory function receives the SvelteKit Request object +// and should return any data needed by your Fedify handlers +export const handle = fedifyHook(federation, (req) => { + // Example: return user session data, configuration, etc. + return "context data"; +});
219-219
: Remove extra blank line for consistent section spacing.To maintain consistent formatting with other sections, remove the extra blank line.
CHANGES.md (1)
64-69
: Fix list indentation for consistency.The unordered lists have inconsistent indentation. According to the static analysis:
- The main bullet points should have 0 spaces indentation
- Sub-bullet points should have 2 spaces indentation
Apply this diff to fix the indentation:
- - Added `fedifyHook()` function. + - Added `fedifyHook()` function.🧰 Tools
🪛 Markdownlint (0.35.0)
64-64: Expected: 0; Actual: 1
Unordered list indentation(MD007, ul-indent)
67-67: Expected: 2; Actual: 5
Unordered list indentation(MD007, ul-indent)
69-69: Expected: 0; Actual: 1
Unordered list indentation(MD007, ul-indent)
src/x/sveltekit.ts (4)
53-53
: Remove redundantawait
inreturn await
Since
federation.fetch
returns a Promise, you can return it directly without usingawait
.Apply this diff:
- return await federation.fetch(event.request, { + return federation.fetch(event.request, {
65-65
: Remove redundantawait
inreturn await
The
resolve(event)
call returns a Promise. You can return it directly withoutawait
.Apply this diff:
- return await resolve(event); + return resolve(event);
68-68
: Minor grammatical correction in commentConsider rephrasing the comment for better readability.
Apply this diff:
- // Similar to `onNotFound`, but slightly more tricky one. + // Similar to `onNotFound`, but slightly trickier.
70-72
: Clarify the comment for better understandingConsider rewording the comment to improve clarity.
Apply this diff:
- // provided by the SvelteKit framework so that it renders HTML if there's some - // page. Otherwise, it will simply return a 406 Not Acceptable response. + // provided by the SvelteKit framework to render HTML if there's a corresponding + // page. Otherwise, it returns a 406 Not Acceptable response.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (3)
CHANGES.md
(2 hunks)docs/manual/integration.md
(1 hunks)src/x/sveltekit.ts
(1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.35.0)
CHANGES.md
64-64: Expected: 0; Actual: 1
Unordered list indentation
(MD007, ul-indent)
67-67: Expected: 2; Actual: 5
Unordered list indentation
(MD007, ul-indent)
69-69: Expected: 0; Actual: 1
Unordered list indentation
(MD007, ul-indent)
72-72: Expected: 0; Actual: 1
Unordered list indentation
(MD007, ul-indent)
🔇 Additional comments (6)
docs/manual/integration.md (2)
199-202
: Documentation aligns well with PR objectives.
The section effectively documents the SvelteKit integration, addressing Issue #171's requirements and clearly explaining the server hook implementation.
205-206
: Verify the SvelteKit module exports.
Let's verify that the documented module and exports exist in the codebase.
✅ Verification successful
Documentation accurately reflects the implementation
The documentation correctly references the fedifyHook
function which exists in the @fedify/fedify/x/sveltekit
module. The implementation in src/x/sveltekit.ts
matches the documented usage, showing both the function export and its practical application in creating a SvelteKit handle.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the existence of the SvelteKit module and its exports
# Check if the module file exists
fd -t f "sveltekit.ts" src/x
# Check for fedifyHook export
rg -l "export.*fedifyHook" src/x/sveltekit.ts
Length of output: 114
Script:
#!/bin/bash
# Let's verify the actual implementation and exports
rg -A 5 "export.*fedifyHook" src/x/sveltekit.ts
# Also check the main package exports to ensure the module is properly exposed
fd -t f "index.ts" src/x
rg "export.*sveltekit" src/x/index.ts
Length of output: 664
CHANGES.md (2)
64-68
: Enhance the SvelteKit integration documentation.
The changelog entry for SvelteKit integration is quite brief. Consider adding more details about:
- The functionality provided by the
fedifyHook
middleware - Configuration options and usage examples
- Any limitations or considerations when using SvelteKit integration
🧰 Tools
🪛 Markdownlint (0.35.0)
64-64: Expected: 0; Actual: 1
Unordered list indentation
(MD007, ul-indent)
67-67: Expected: 2; Actual: 5
Unordered list indentation
(MD007, ul-indent)
76-80
: LGTM!
The link references are properly formatted and follow the standard markdown syntax.
src/x/sveltekit.ts (2)
52-57
: Well-implemented integration with federation.fetch
The asynchronous function correctly integrates with the Federation
object, ensuring proper context data handling and fetch options.
63-86
: Effective handling of content negotiation and route sharing
The integrateFetchOptions
function provides appropriate responses for onNotFound
and onNotAcceptable
scenarios, enabling seamless integration between Fedify and SvelteKit routes with proper content negotiation based on the Accept
header.
This PR implements SvelteKit integration with server hooks
resolve #171
Summary by CodeRabbit
New Features
fedifyHook
module for seamless integration of SvelteKit with the Federation framework.Bug Fixes