feat: add decorator/HOF API for server-side payment configuration#4
Merged
feat: add decorator/HOF API for server-side payment configuration#4
Conversation
Add simplified decorator and higher-order function APIs for configuring x402 payment requirements on server routes, reducing boilerplate while preserving full PaymentOption flexibility. TypeScript: withPayment() HOF for Express, Hono, and Next.js that wraps a PaymentOption into a wildcard RouteConfig and returns framework middleware. Python: @x402_app.pay() decorator for Flask and FastAPI that attaches payment config to route handlers, with init_app() scanning decorated routes to build the middleware automatically. Also fixes a bug where the assets field in PaymentOption was not passed through to ResourceConfig in buildPaymentRequirementsFromOptions.
49 177 2420773 |
| using Python decorators, as an alternative to the routes dict approach. | ||
| """ | ||
|
|
||
| from ..types import PaymentOption, RouteConfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
withPayment()HOF for Express, Hono, and Next.js — wrapsPaymentOption | PaymentOption[]into framework middleware with a single function call@x402_app.pay()decorator for Flask and FastAPI — attaches payment config to route handlers,init_app()scans decorated routes and builds middleware automaticallyassetsfield fromPaymentOptionthrough toResourceConfiginbuildPaymentRequirementsFromOptions(both TS and Python)Changes
x402ResourceServer.ts,x402_http_server.pyassetstoResourceConfigexpress/withPayment.ts,hono/withPayment.ts,next/withPayment.tswithPayment()for each frameworkdecorators/flask.py,decorators/fastapi.pyx402_appclass with@pay()decoratorTest plan
pnpm lint/pnpm build/pnpm test— all green (40/40 tasks)pytest— 730 passed,ruff check/ruff format --checkclean