|
| 1 | +--- |
| 2 | +description: "Reference for Nitric's Dart library - Register a single handler for all HTTP Methods (GET, POST, PUT, DELETE, PATCH) on the route." |
| 3 | +--- |
| 4 | + |
| 5 | +# Dart - api.all() |
| 6 | + |
| 7 | +Register a single handler for all HTTP Methods (GET, POST, PUT, DELETE, PATCH) on a route. |
| 8 | + |
| 9 | +<Note> |
| 10 | + This method is a convenient short version of |
| 11 | + [api().route().all()](./api-route-all) |
| 12 | +</Note> |
| 13 | + |
| 14 | +```dart |
| 15 | +import 'package:nitric_sdk/nitric.dart'; |
| 16 | +
|
| 17 | +// Create an API named 'public' |
| 18 | +final api = Nitric.api("public"); |
| 19 | +
|
| 20 | +api.all("/customers/:customersId", (ctx) async { |
| 21 | + // construct a response for all incoming HTTP requests |
| 22 | + final responseBody = {}; |
| 23 | + ctx.res.json(responseBody); |
| 24 | +
|
| 25 | + return ctx; |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +## Parameters |
| 30 | + |
| 31 | +<Properties> |
| 32 | + <Property name="match" required type="String"> |
| 33 | + The path matcher to use for the route. Matchers accept path parameters in |
| 34 | + the form of a colon prefixed string. The string provided will be used as |
| 35 | + that path parameter's name when calling handlers. |
| 36 | + </Property> |
| 37 | + <Property name="handler" required type="HttpHandler"> |
| 38 | + The middleware service to use as the handler for HTTP requests. |
| 39 | + </Property> |
| 40 | + <Property name="security" type="List<OidcOptions>"> |
| 41 | + Security rules to apply with scopes to the entire API. |
| 42 | + </Property> |
| 43 | + <Property name="middlewares" type="List<HttpHandler>"> |
| 44 | + The list of middleware that will run before the handler is called. To call |
| 45 | + the next middleware in the chain use `ctx.next()`, to finish early return |
| 46 | + `ctx` by itself. The ordering of middleware goes: API -> Route -> Method. |
| 47 | + </Property> |
| 48 | +</Properties> |
| 49 | + |
| 50 | +## Examples |
| 51 | + |
| 52 | +### Register a handler for all requests |
| 53 | + |
| 54 | +```dart |
| 55 | +import 'package:nitric_sdk/nitric.dart'; |
| 56 | +
|
| 57 | +// Create an API named 'public' |
| 58 | +final api = Nitric.api("public"); |
| 59 | +
|
| 60 | +api.all("/customers/:customersId", (ctx) async { |
| 61 | + // construct a response for all incoming HTTP requests |
| 62 | + final responseBody = {}; |
| 63 | + ctx.res.json(responseBody); |
| 64 | +
|
| 65 | + return ctx; |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### Chain services as a single method handler |
| 70 | + |
| 71 | +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. |
| 72 | + |
| 73 | +```dart |
| 74 | +import 'package:nitric_sdk/nitric.dart'; |
| 75 | +import '../middlewares'; |
| 76 | +
|
| 77 | +Future<HttpContext> validate(HttpContext ctx) async { |
| 78 | + if (!ctx.req.headers.containsKey("Content-Type")) { |
| 79 | + ctx.res.status = 400; |
| 80 | + ctx.res.body = "header Content-Type is required"; |
| 81 | +
|
| 82 | + // End the middleware chain by not calling `ctx.next()`. |
| 83 | + return ctx; |
| 84 | + } |
| 85 | +
|
| 86 | + return ctx.next(); |
| 87 | +} |
| 88 | +
|
| 89 | +Future<HttpContext> handleCustomer(HttpContext ctx) async { |
| 90 | + // handle the request... |
| 91 | + return ctx.next(); |
| 92 | +} |
| 93 | +
|
| 94 | +// The validate middleware will run before the handleCustomer handler |
| 95 | +Nitric.api("public").all("/customers", handleCustomer, middlewares: [validate]); |
| 96 | +``` |
| 97 | + |
| 98 | +### Access the request body |
| 99 | + |
| 100 | +The request body is accessible from the `ctx.req` object. |
| 101 | + |
| 102 | +```dart |
| 103 | +import 'package:nitric_sdk/nitric.dart'; |
| 104 | +
|
| 105 | +// Create an API named 'public' |
| 106 | +final api = Nitric.api("public"); |
| 107 | +
|
| 108 | +api.all("/customers/:customerId", (ctx) async { |
| 109 | + // Extract the path parameter |
| 110 | + final id = ctx.req.pathParams["customerId"]!; |
| 111 | +
|
| 112 | + // Extract the request body |
| 113 | + final body = ctx.req.json(); |
| 114 | +
|
| 115 | + // Construct response for the /customers request... |
| 116 | + final responseBody = {}; |
| 117 | + ctx.res.json(responseBody); |
| 118 | +
|
| 119 | + return ctx; |
| 120 | +}); |
| 121 | +``` |
0 commit comments