This repository was archived by the owner on May 20, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
feat: add dart middleware to api docs #599
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
13b8fa6
add dart middleware to api docs
HomelessDinosaur 3946eaa
move route middleware to route definition
HomelessDinosaur 1d29f28
add middlewares to reference docs
HomelessDinosaur e32037b
reword around spellcheck
HomelessDinosaur 3fc1c7f
add frontmatter to api all
HomelessDinosaur 95a9d06
fix missing dart in code switcher
HomelessDinosaur 2d61cef
remove change to go code
HomelessDinosaur 99ec8fd
fix incorrect tab indentation
HomelessDinosaur 049dad0
Apply suggestions from code review
davemooreuws 132a26c
add api.all for dart to nav
davemooreuws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| --- | ||
| description: "Reference for Nitric's Dart library - Register a single handler for all HTTP Methods (GET, POST, PUT, DELETE, PATCH) on the route." | ||
| --- | ||
|
|
||
| # Dart - api.all() | ||
|
|
||
| Register a single handler for all HTTP Methods (GET, POST, PUT, DELETE, PATCH) on a route. | ||
|
|
||
| <Note> | ||
| This method is a convenient short version of | ||
| [api().route().all()](./api-route-all) | ||
| </Note> | ||
|
|
||
| ```dart | ||
| import 'package:nitric_sdk/nitric.dart'; | ||
|
|
||
| // Create an API named 'public' | ||
| final api = Nitric.api("public"); | ||
|
|
||
| api.all("/customers/:customersId", (ctx) async { | ||
| // construct a response for all incoming HTTP requests | ||
| final responseBody = {}; | ||
| ctx.res.json(responseBody); | ||
|
|
||
| return ctx; | ||
| }); | ||
| ``` | ||
|
|
||
| ## Parameters | ||
|
|
||
| <Properties> | ||
| <Property name="match" required type="String"> | ||
| The path matcher to use for the route. Matchers accept path parameters in | ||
| the form of a colon prefixed string. The string provided will be used as | ||
| that path parameter's name when calling handlers. | ||
| </Property> | ||
| <Property name="handler" required type="HttpHandler"> | ||
| The middleware service to use as the handler for HTTP requests. | ||
| </Property> | ||
| <Property name="security" type="List<OidcOptions>"> | ||
| Security rules to apply with scopes to the entire API. | ||
| </Property> | ||
| <Property name="middlewares" type="List<HttpHandler>"> | ||
| The list of middleware that will run before the handler is called. To call | ||
| the next middleware in the chain use `ctx.next()`, to finish early return | ||
| `ctx` by itself. The ordering of middleware goes: API -> Route -> Method. | ||
| </Property> | ||
| </Properties> | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Register a handler for all requests | ||
|
|
||
| ```dart | ||
| import 'package:nitric_sdk/nitric.dart'; | ||
|
|
||
| // Create an API named 'public' | ||
| final api = Nitric.api("public"); | ||
|
|
||
| api.all("/customers/:customersId", (ctx) async { | ||
| // construct a response for all incoming HTTP requests | ||
| final responseBody = {}; | ||
| ctx.res.json(responseBody); | ||
|
|
||
| return ctx; | ||
| }); | ||
| ``` | ||
|
|
||
| ### Chain services as a single method handler | ||
|
|
||
| When multiple services are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers. You can call the next middleware in the chain using `ctx.next()`. If a middleware in the chain does not call `.next` and instead returns the base context, the call chain will end. | ||
|
|
||
| ```dart | ||
| import 'package:nitric_sdk/nitric.dart'; | ||
| import '../middlewares'; | ||
|
|
||
| Future<HttpContext> validate(HttpContext ctx) async { | ||
| if (!ctx.req.headers.containsKey("Content-Type")) { | ||
| ctx.res.status = 400; | ||
| ctx.res.body = "header Content-Type is required"; | ||
|
|
||
| // End the middleware chain by not calling `ctx.next()`. | ||
| return ctx; | ||
| } | ||
|
|
||
| return ctx.next(); | ||
| } | ||
|
|
||
| Future<HttpContext> handleCustomer(HttpContext ctx) async { | ||
| // handle the request... | ||
| return ctx.next(); | ||
| } | ||
|
|
||
| // The validate middleware will run before the handleCustomer handler | ||
| Nitric.api("public").all("/customers", handleCustomer, middlewares: [validate]); | ||
| ``` | ||
|
|
||
| ### Access the request body | ||
|
|
||
| The request body is accessible from the `ctx.req` object. | ||
|
|
||
| ```dart | ||
| import 'package:nitric_sdk/nitric.dart'; | ||
|
|
||
| // Create an API named 'public' | ||
| final api = Nitric.api("public"); | ||
|
|
||
| api.all("/customers/:customerId", (ctx) async { | ||
| // Extract the path parameter | ||
| final id = ctx.req.pathParams["customerId"]!; | ||
|
|
||
| // Extract the request body | ||
| final body = ctx.req.json(); | ||
|
|
||
| // Construct response for the /customers request... | ||
| final responseBody = {}; | ||
| ctx.res.json(responseBody); | ||
|
|
||
| return ctx; | ||
| }); | ||
| ``` | ||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.