|
| 1 | +@Tutorial(time: 10) { |
| 2 | + @XcodeRequirement(title: "Swift 5.9", destination: "https://developer.apple.com/download/applications/") |
| 3 | + |
| 4 | + @Intro(title: "Using `Request` in OpenAPI's APIProtocol") { |
| 5 | + This tutorial guides you through passing down the `Request` to the OpenAPI Vapor route handler |
| 6 | + } |
| 7 | + |
| 8 | + @Section(title: "Adding the `swift-dependencies` library as a dependency of your app") { |
| 9 | + This tutorial uses [swift-dependencies](https://github.com/pointfreeco/swift-dependencies) to inject the `Request` to the context of `APIProtocol`, using `TaskLocal` values. |
| 10 | + |
| 11 | + @Steps { |
| 12 | + @Step { |
| 13 | + Make sure you add [swift-dependencies](https://github.com/pointfreeco/swift-dependencies) as a dependency of your app, as well as your target. See [this section](https://github.com/pointfreeco/swift-dependencies?tab=readme-ov-file#installation) for more info. |
| 14 | + } |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + @Section(title: "Adding `request` to `DependencyValues`") { |
| 19 | + You need to add a `request` variable as an extension on top of `DependencyValues` so in the next steps we can store each `Request` there. |
| 20 | + |
| 21 | + @Steps { |
| 22 | + @Step { |
| 23 | + Create a `DependencyKey` for the `Request` value. |
| 24 | + @Code(name: "+DependencyValues", file: request-injection.dependency-values.1.swift, reset: true) |
| 25 | + } |
| 26 | + @Step { |
| 27 | + Add a `request` variable while using the `RequestKey` as the identifier to access `DependencyValues`'s underlying storage. |
| 28 | + @Code(name: "+DependencyValues", file: request-injection.dependency-values.2.swift) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + If you want to know more about how `swift-dependencies` works, refer to their [documentation](https://github.com/pointfreeco/swift-dependencies#documentation). |
| 33 | + } |
| 34 | + |
| 35 | + @Section(title: "Using a middleware to inject `Request` to the context of requests") { |
| 36 | + Now you need to using an `AsyncMiddleware` which captures the request and injects it to the context of your requests using Swift's `TaskLocal`s. |
| 37 | + |
| 38 | + @Steps { |
| 39 | + @Step { |
| 40 | + Create an `AsyncMiddleware`. |
| 41 | + @Code(name: "OpenAPIRequestInjectionMiddleware", file: request-injection.middleware.1.swift) |
| 42 | + } |
| 43 | + @Step { |
| 44 | + Use `swift-dependencies` APIs to inject the `request` to the context of the request. |
| 45 | + @Code(name: "OpenAPIRequestInjectionMiddleware", file: request-injection.middleware.2.swift) |
| 46 | + } |
| 47 | + @Step { |
| 48 | + Go to the file where you set up using the OpenAPI handler. |
| 49 | + It should look like this. |
| 50 | + @Code(name: "register-openapi-handler.swift", file: request-injection.using-middleware.1.swift) |
| 51 | + } |
| 52 | + @Step { |
| 53 | + Change it so you're using the new `OpenAPIRequestInjectionMiddleware`. |
| 54 | + Prefer to use this middleware as the last middleware for your routes, to avoid possible known problems with `TaskLocal` and Vapor's underlying implementation. |
| 55 | + @Code(name: "register-openapi-handler.swift", file: request-injection.using-middleware.2.swift) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Section(title: "Using `request` in your OpenAPI handler") { |
| 61 | + Everything is now ready! You can use the `request` dependency value from your OpenAPI handler. |
| 62 | + |
| 63 | + @Steps { |
| 64 | + @Step { |
| 65 | + Navigate to your APIProtocol implementation file. |
| 66 | + It should look like this. |
| 67 | + @Code(name: "MyAPIProtocolImpl.swift", file: request-injection.api-protocol.1.swift) |
| 68 | + } |
| 69 | + @Step { |
| 70 | + Use `swift-dependencies` APIs to retrieve the `Request`. |
| 71 | + Then you can use it freely like with normal Vapor route handlers. |
| 72 | + @Code(name: "MyAPIProtocolImpl.swift", file: request-injection.api-protocol.2.swift) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments