OfflineHQ / marketplace

https://offline.live/
GNU General Public License v3.0
0 stars 0 forks source link

pages/api: change claim order route to use nextApi type instead #257

Closed AlexandreG-tech closed 10 months ago

AlexandreG-tech commented 10 months ago

Type

Enhancement


Description

This PR contains enhancements to the API routes and the payment webhooks library. The main changes are:


Changes walkthrough

Relevant files                                                                                                                                 
Enhancement
index.ts                                                                                                       
    apps/web/pages/api/order/claim/[id]/index.ts

    **The handler function in this file has been refactored to use
    NextApiRequest and NextApiResponse types from Next.js for
    the request and response objects. This change improves type
    safety and error handling. The error responses now return a
    JSON object with an error message instead of just an error
    status. The id is now extracted from the request query
    instead of the params.**
+11/-6
index.ts                                                                                                       
    apps/web/pages/api/webhooks/payment/stripe/checkout/index.ts

    **The handler function in this file has been refactored to use
    NextApiRequest and NextApiResponse types from Next.js for
    the request and response objects. The function now reads the
    request data into a buffer, converts it to a string, and
    passes it along with the stripe-signature header to the
    stripeCheckoutStatus function. The config object has been
    added to disable the default body parser.**
+29/-4
payment-webhooks.ts                                                                                 
    libs/payment/webhooks/src/lib/payment-webhooks.ts

    **The stripeCheckoutStatus function has been refactored to
    take a signature and a payload as arguments instead of a
    request object. The function now constructs the Stripe event
    using these arguments. This change makes the function more
    flexible and easier to test as it no longer depends on the
    request object.**
+6/-5

โœจ Usage guide:
**Overview:** The `describe` tool scans the PR code changes, and generates a description for the PR - title, type, summary, walkthrough and labels. The tool can be triggered [automatically](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) every time a new PR is opened, or can be invoked manually by commenting on a PR. When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L46) related to the describe tool (`pr_description` section), use the following template: ``` /describe --pr_description.some_config1=... --pr_description.some_config2=... ``` With a [configuration file](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#working-with-github-app), use the following template: ``` [pr_description] some_config1=... some_config2=... ```
Enabling\disabling automation
- When you first install the app, the [default mode](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) for the describe tool is: ``` pr_commands = ["/describe --pr_description.add_original_user_description=true" "--pr_description.keep_original_user_title=true", ...] ``` meaning the `describe` tool will run automatically on every PR, will keep the original title, and will add the original user description above the generated description. - Markers are an alternative way to control the generated description, to give maximal control to the user. If you set: ``` pr_commands = ["/describe --pr_description.use_description_markers=true", ...] ``` the tool will replace every marker of the form `pr_agent:marker_name` in the PR description with the relevant content, where `marker_name` is one of the following: - `type`: the PR type. - `summary`: the PR summary. - `walkthrough`: the PR walkthrough. Note that when markers are enabled, if the original PR description does not contain any markers, the tool will not alter the description at all.
Custom labels
The default labels of the `describe` tool are quite generic: [`Bug fix`, `Tests`, `Enhancement`, `Documentation`, `Other`]. If you specify [custom labels](https://github.com/Codium-ai/pr-agent/blob/main/docs/DESCRIBE.md#handle-custom-labels-from-the-repos-labels-page-gem) in the repo's labels page or via configuration file, you can get tailored labels for your use cases. Examples for custom labels: - `Main topic:performance` - pr_agent:The main topic of this PR is performance - `New endpoint` - pr_agent:A new endpoint was added in this PR - `SQL query` - pr_agent:A new SQL query was added in this PR - `Dockerfile changes` - pr_agent:The PR contains changes in the Dockerfile - ... The list above is eclectic, and aims to give an idea of different possibilities. Define custom labels that are relevant for your repo and use cases. Note that Labels are not mutually exclusive, so you can add multiple label categories. Make sure to provide proper title, and a detailed and well-phrased description for each label, so the tool will know when to suggest it.
Utilizing extra instructions
The `describe` tool can be configured with extra instructions, to guide the model to a feedback tailored to the needs of your project. Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Notice that the general structure of the description is fixed, and cannot be changed. Extra instructions can change the content or style of each sub-section of the PR description. Examples for extra instructions: ``` [pr_description] extra_instructions=""" - The PR title should be in the format: ': ' - The title should be short and concise (up to 10 words) - ... """ ``` Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable. </details></td></tr> <tr><td><details> <summary><strong> More PR-Agent commands</strong></summary><hr> > To invoke the PR-Agent, add a comment using one of the following commands: > - **/review**: Request a review of your Pull Request. > - **/describe**: Update the PR title and description based on the contents of the PR. > - **/improve [--extended]**: Suggest code improvements. Extended mode provides a higher quality feedback. > - **/ask \<QUESTION\>**: Ask a question about the PR. > - **/update_changelog**: Update the changelog based on the PR's contents. > - **/add_docs** ๐Ÿ’Ž: Generate docstring for new components introduced in the PR. > - **/generate_labels** ๐Ÿ’Ž: Generate labels for the PR based on the PR's contents. > - **/analyze** ๐Ÿ’Ž: Automatically analyzes the PR, and presents changes walkthrough for each component. >See the [tools guide](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md) for more details. >To list the possible configuration parameters, add a **/config** comment. </details></td></tr> </table> See the [describe usage](https://github.com/Codium-ai/pr-agent/blob/main/docs/DESCRIBE.md) page for a comprehensive guide on using this tool. </details> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/vercel[bot]"><img src="https://avatars.githubusercontent.com/in/8329?v=4" />vercel[bot]</a> commented <strong> 10 months ago</strong> </div> <div class="markdown-body"> <p><strong>The latest updates on your projects</strong>. Learn more about <a href="https://vercel.link/github-learn-more">Vercel for Git โ†—๏ธŽ</a></p> <table> <thead> <tr> <th style="text-align: left;">Name</th> <th style="text-align: left;">Status</th> <th style="text-align: left;">Preview</th> <th style="text-align: left;">Comments</th> <th style="text-align: left;">Updated (UTC)</th> </tr> </thead> <tbody> <tr> <td style="text-align: left;"><strong>back-office</strong></td> <td style="text-align: left;">โœ… Ready (<a href="https://vercel.com/offline/back-office/CB4baCAptN5JsbhNaGDikr286H3t">Inspect</a>)</td> <td style="text-align: left;"><a href="https://vercel.live/open-feedback/back-office-git-234-17012024-end-to-end-testing-26b942-offline.vercel.app?via=pr-comment-visit-preview-link&passThrough=1">Visit Preview</a></td> <td style="text-align: left;">๐Ÿ’ฌ <a href="https://vercel.live/open-feedback/back-office-git-234-17012024-end-to-end-testing-26b942-offline.vercel.app?via=pr-comment-feedback-link"><strong>Add feedback</strong></a></td> <td style="text-align: left;">Jan 19, 2024 3:31pm</td> </tr> <tr> <td style="text-align: left;"><strong>marketplace</strong></td> <td style="text-align: left;">โœ… Ready (<a href="https://vercel.com/offline/marketplace/DpBGAbM5cX4tYLH9HjBZXgt9zmog">Inspect</a>)</td> <td style="text-align: left;"><a href="https://vercel.live/open-feedback/marketplace-git-234-17012024-end-to-end-testing-84e928-offline.vercel.app?via=pr-comment-visit-preview-link&passThrough=1">Visit Preview</a></td> <td style="text-align: left;">๐Ÿ’ฌ <a href="https://vercel.live/open-feedback/marketplace-git-234-17012024-end-to-end-testing-84e928-offline.vercel.app?via=pr-comment-feedback-link"><strong>Add feedback</strong></a></td> <td style="text-align: left;">Jan 19, 2024 3:31pm</td> </tr> </tbody> </table> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/codiumai-pr-agent-free[bot]"><img src="https://avatars.githubusercontent.com/in/354216?v=4" />codiumai-pr-agent-free[bot]</a> commented <strong> 10 months ago</strong> </div> <div class="markdown-body"> <p><strong><a href="https://github.com/Offline-Project/marketplace/pull/257">PR Description</a></strong> updated to latest commit (<a href="https://github.com/Offline-Project/marketplace/commit/9966ffa62372b60c6fbc287a1fd161b5f5e52084">https://github.com/Offline-Project/marketplace/commit/9966ffa62372b60c6fbc287a1fd161b5f5e52084</a>)</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/codiumai-pr-agent-free[bot]"><img src="https://avatars.githubusercontent.com/in/354216?v=4" />codiumai-pr-agent-free[bot]</a> commented <strong> 10 months ago</strong> </div> <div class="markdown-body"> <h2>PR Analysis</h2> <ul> <li>๐ŸŽฏ <strong>Main theme:</strong> Enhancing API routes and payment webhooks library</li> <li>๐Ÿ“ <strong>PR summary:</strong> This PR refactors the API route handlers to use the NextApiRequest and NextApiResponse types from Next.js for the request and response objects, improving type safety and error handling. It also refactors the stripeCheckoutStatus function in the payment webhooks library to take a signature and a payload as arguments instead of a request object, making the function more flexible and easier to test.</li> <li>๐Ÿ“Œ <strong>Type of PR:</strong> Enhancement</li> <li>๐Ÿงช <strong>Relevant tests added:</strong> No</li> <li>โฑ๏ธ <strong>Estimated effort to review [1-5]:</strong> 3, because the changes are not too complex, but they touch critical parts of the system such as payment handling and API routes, which requires careful review.</li> <li>๐Ÿ”’ <strong>Security concerns:</strong> No <h2>PR Feedback</h2></li> </ul> <p><strong>๐Ÿ’ก General suggestions:</strong> The changes made in this PR are generally good, improving the flexibility and testability of the code. However, it would be beneficial to add some tests to ensure that the new functionality works as expected. Additionally, it would be helpful to add more detailed error messages to aid in debugging in case of failures.</p> <hr> <details> <summary><strong>โœจ Usage guide:</strong></summary><hr> **Overview:** The `review` tool scans the PR code changes, and generates a PR review. The tool can be triggered [automatically](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) every time a new PR is opened, or can be invoked manually by commenting on any PR. When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L19) related to the review tool (`pr_reviewer` section), use the following template: ``` /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=... ``` With a [configuration file](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#working-with-github-app), use the following template: ``` [pr_reviewer] some_config1=... some_config2=... ``` <table><tr><td><details> <summary><strong> Utilizing extra instructions</strong></summary><hr> The `review` tool can be configured with extra instructions, which can be used to guide the model to a feedback tailored to the needs of your project. Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Specify the relevant sub-tool, and the relevant aspects of the PR that you want to emphasize. Examples for extra instructions: ``` [pr_reviewer] # /review # extra_instructions=""" In the code feedback section, emphasize the following: - Does the code logic cover relevant edge cases? - Is the code logic clear and easy to understand? - Is the code logic efficient? ... """ ``` Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable. </details></td></tr> <tr><td><details> <summary><strong> How to enable\disable automation</strong></summary><hr> - When you first install PR-Agent app, the [default mode](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) for the `review` tool is: ``` pr_commands = ["/review", ...] ``` meaning the `review` tool will run automatically on every PR, with the default configuration. Edit this field to enable/disable the tool, or to change the used configurations </details></td></tr> <tr><td><details> <summary><strong> About the 'Code feedback' section</strong></summary><hr> The `review` tool provides several type of feedbacks, one of them is code suggestions. If you are interested **only** in the code suggestions, it is recommended to use the [`improve`](https://github.com/Codium-ai/pr-agent/blob/main/docs/IMPROVE.md) feature instead, since it dedicated only to code suggestions, and usually gives better results. Use the `review` tool if you want to get a more comprehensive feedback, which includes code suggestions as well. </details></td></tr> <tr><td><details> <summary><strong> Auto-labels</strong></summary><hr> The `review` tool can auto-generate two specific types of labels for a PR: - a `possible security issue` label, that detects possible [security issues](https://github.com/Codium-ai/pr-agent/blob/tr/user_description/pr_agent/settings/pr_reviewer_prompts.toml#L136) (`enable_review_labels_security` flag) - a `Review effort [1-5]: x` label, where x is the estimated effort to review the PR (`enable_review_labels_effort` flag) </details></td></tr> <tr><td><details> <summary><strong> Extra sub-tools</strong></summary><hr> The `review` tool provides a collection of possible feedbacks about a PR. It is recommended to review the [possible options](https://github.com/Codium-ai/pr-agent/blob/main/docs/REVIEW.md#enabledisable-features), and choose the ones relevant for your use case. Some of the feature that are disabled by default are quite useful, and should be considered for enabling. For example: `require_score_review`, `require_soc2_review`, `enable_review_labels_effort`, and more. </details></td></tr> <tr><td><details> <summary><strong> More PR-Agent commands</strong></summary><hr> > To invoke the PR-Agent, add a comment using one of the following commands: > - **/review**: Request a review of your Pull Request. > - **/describe**: Update the PR title and description based on the contents of the PR. > - **/improve [--extended]**: Suggest code improvements. Extended mode provides a higher quality feedback. > - **/ask \<QUESTION\>**: Ask a question about the PR. > - **/update_changelog**: Update the changelog based on the PR's contents. > - **/add_docs** ๐Ÿ’Ž: Generate docstring for new components introduced in the PR. > - **/generate_labels** ๐Ÿ’Ž: Generate labels for the PR based on the PR's contents. > - **/analyze** ๐Ÿ’Ž: Automatically analyzes the PR, and presents changes walkthrough for each component. >See the [tools guide](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md) for more details. >To list the possible configuration parameters, add a **/config** comment. </details></td></tr> </table> See the [review usage](https://github.com/Codium-ai/pr-agent/blob/main/docs/REVIEW.md) page for a comprehensive guide on using this tool. </details> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/codiumai-pr-agent-free[bot]"><img src="https://avatars.githubusercontent.com/in/354216?v=4" />codiumai-pr-agent-free[bot]</a> commented <strong> 10 months ago</strong> </div> <div class="markdown-body"> <h2>PR Code Suggestions</h2> <table><thead><tr><th></th><th>Suggestions                                                                                                                                                          </th></tr></thead><tbody><tr><td><strong>error handling</strong></td><td><table><tr><td> <details><summary>Handle potential errors when retrieving the order from the database.         </summary> ___ **Consider using a try-catch block to handle potential errors when retrieving the order from <br>the database. This will allow you to provide more specific error messages and handle <br>exceptions more gracefully.** [apps/web/pages/api/order/claim/[id]/index.ts [12-13]](https://github.com/Offline-Project/marketplace/pull/257/files#diff-01f449dc3dbb24ab53b88505730ca0968f081056aad5605762317844b052ac0cR12-R13) ```diff -const order = (await adminSdk.GetOrderFromId({ id: id as string })) - .order_by_pk; +let order; +try { + order = (await adminSdk.GetOrderFromId({ id: id as string })).order_by_pk; +} catch (e) { + console.error(e); + return res.status(500).json({ error: 'Failed to retrieve order' }); +} ``` </details></td></tr><tr><td> <details><summary>Handle potential errors when processing the request data.                    </summary> ___ **Consider using a try-catch block to handle potential errors when processing the request <br>data. This will allow you to provide more specific error messages and handle exceptions <br>more gracefully.** [apps/web/pages/api/webhooks/payment/stripe/checkout/index.ts [15-22]](https://github.com/Offline-Project/marketplace/pull/257/files#diff-3329d8360a1496db97e13a84df7ff799a8c469408d0056c3dacc375d12b756a0R15-R22) ```diff -const buffer = await new Promise<Buffer>((resolve, reject) => { - let data = Buffer.alloc(0); - req.on('data', (chunk) => { - data = Buffer.concat([data, chunk]); +let buffer; +try { + buffer = await new Promise<Buffer>((resolve, reject) => { + let data = Buffer.alloc(0); + req.on('data', (chunk) => { + data = Buffer.concat([data, chunk]); + }); + req.on('end', () => resolve(data)); + req.on('error', reject); }); - req.on('end', () => resolve(data)); - req.on('error', reject); -}); +} catch (e) { + console.error(e); + return res.status(500).json({ error: 'Failed to process request data' }); +} ``` </details></td></tr><tr><td> <details><summary>Check if the 'stripe-signature' header is present before using it.           </summary> ___ **Consider checking if the 'stripe-signature' header is present before using it. This will <br>prevent potential errors when the header is missing.** [apps/web/pages/api/webhooks/payment/stripe/checkout/index.ts [25]](https://github.com/Offline-Project/marketplace/pull/257/files#diff-3329d8360a1496db97e13a84df7ff799a8c469408d0056c3dacc375d12b756a0R25-R25) ```diff -const signature = req.headers['stripe-signature'] as string; +const signature = req.headers['stripe-signature']; +if (!signature) { + return res.status(400).json({ error: 'Missing stripe-signature header' }); +} ``` </details></td></tr></table></td></tr><tr><td><strong>best practice</strong></td><td><table><tr><td> <details><summary>Use more specific HTTP status codes.                                         </summary> ___ **Consider using HTTP status codes that are more specific to the error. For example, use 404 <br>when the order is not found, and 500 when an error occurs while claiming the order.** [apps/web/pages/api/order/claim/[id]/index.ts [16-25]](https://github.com/Offline-Project/marketplace/pull/257/files#diff-01f449dc3dbb24ab53b88505730ca0968f081056aad5605762317844b052ac0cR16-R25) ```diff if (!order) { - return res.status(400).json({ error: 'Order not found' }); + return res.status(404).json({ error: 'Order not found' }); } ... try { await nft.claimOrder(order); return res.status(200).json({ status: 'success', id }); } catch (e) { console.error(e); - return res.status(500).json({ error: 'An error occurred' }); + return res.status(500).json({ error: 'An error occurred while claiming the order' }); } ``` </details></td></tr></table></td></tr></tr></tbody></table><hr> <details> <summary><strong>โœจ Usage guide:</strong></summary><hr> **Overview:** The `improve` tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered [automatically](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) every time a new PR is opened, or can be invoked manually by commenting on a PR. When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L69) related to the improve tool (`pr_code_suggestions` section), use the following template: ``` /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=... ``` With a [configuration file](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#working-with-github-app), use the following template: ``` [pr_code_suggestions] some_config1=... some_config2=... ``` <table><tr><td><details> <summary><strong> Enabling\disabling automation </strong></summary><hr> When you first install the app, the [default mode](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools) for the improve tool is: ``` pr_commands = ["/improve --pr_code_suggestions.summarize=true", ...] ``` meaning the `improve` tool will run automatically on every PR, with summarization enabled. Delete this line to disable the tool from running automatically. </details></td></tr> <tr><td><details> <summary><strong> Utilizing extra instructions</strong></summary><hr> Extra instructions are very important for the `improve` tool, since they enable to guide the model to suggestions that are more relevant to the specific needs of the project. Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Specify relevant aspects that you want the model to focus on. Examples for extra instructions: ``` [pr_code_suggestions] # /improve # extra_instructions=""" Emphasize the following aspects: - Does the code logic cover relevant edge cases? - Is the code logic clear and easy to understand? - Is the code logic efficient? ... """ ``` Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable. </details></td></tr> <tr><td><details> <summary><strong> A note on code suggestions quality</strong></summary><hr> - While the current AI for code is getting better and better (GPT-4), it's not flawless. Not all the suggestions will be perfect, and a user should not accept all of them automatically. - Suggestions are not meant to be simplistic. Instead, they aim to give deep feedback and raise questions, ideas and thoughts to the user, who can then use his judgment, experience, and understanding of the code base. - Recommended to use the 'extra_instructions' field to guide the model to suggestions that are more relevant to the specific needs of the project. - Best quality will be obtained by using 'improve --extended' mode. </details></td></tr> <tr><td><details> <summary><strong> More PR-Agent commands</strong></summary><hr> > To invoke the PR-Agent, add a comment using one of the following commands: > - **/review**: Request a review of your Pull Request. > - **/describe**: Update the PR title and description based on the contents of the PR. > - **/improve [--extended]**: Suggest code improvements. Extended mode provides a higher quality feedback. > - **/ask \<QUESTION\>**: Ask a question about the PR. > - **/update_changelog**: Update the changelog based on the PR's contents. > - **/add_docs** ๐Ÿ’Ž: Generate docstring for new components introduced in the PR. > - **/generate_labels** ๐Ÿ’Ž: Generate labels for the PR based on the PR's contents. > - **/analyze** ๐Ÿ’Ž: Automatically analyzes the PR, and presents changes walkthrough for each component. >See the [tools guide](https://github.com/Codium-ai/pr-agent/blob/main/docs/TOOLS_GUIDE.md) for more details. >To list the possible configuration parameters, add a **/config** comment. </details></td></tr> </table> See the [improve usage](https://github.com/Codium-ai/pr-agent/blob/main/docs/IMPROVE.md) page for a more comprehensive guide on using this tool. </details> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>ยฉ <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>