Skip to content

Commit

Permalink
Added docs for beforeEncode and afterEncode to Content.md (#471)
Browse files Browse the repository at this point in the history
* Added docs for beforeEncode and afterEncode

* Update content.md

* Update content.md

Co-authored-by: Tanner <tannernelson@gmail.com>
  • Loading branch information
grosch and tanner0101 committed Apr 15, 2020
1 parent e41d39d commit f10729c
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions 4.0/docs/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The first step to decoding this HTTP message is creating a Codable type that mat

```swift
struct Greeting: Content {
var hello: String
var hello: String
}
```

Expand All @@ -32,9 +32,9 @@ Once you have the content structure, you can decode it from the incoming request

```swift
app.post("greeting") { req in
let greeting = try req.content.decode(Greeting.self)
print(greeting.hello) // "world"
return HTTPStatus.ok
let greeting = try req.content.decode(Greeting.self)
print(greeting.hello) // "world"
return HTTPStatus.ok
}
```

Expand Down Expand Up @@ -81,7 +81,7 @@ Just like the APIs for handling HTTP message body content, the first step for pa

```swift
struct Hello: Content {
var name: String?
var name: String?
}
```

Expand All @@ -91,8 +91,8 @@ Now that you have a `Content` struct for this route's expected query string, you

```swift
app.get("hello") { req -> String in
let hello = try req.query.decode(Hello.self)
return "Hello, \(hello.name ?? "Anonymous")"
let hello = try req.query.decode(Hello.self)
return "Hello, \(hello.name ?? "Anonymous")"
}
```

Expand Down Expand Up @@ -120,6 +120,33 @@ In addition to decoding to a `Content` struct, Vapor also supports fetching sing
let name: String? = req.query["name"]
```

## Hooks

Vapor will automatically call `beforeDecode` and `afterDecode` on a `Content` type. Default implementations are provided which do nothing, but you can use these methods to run custom logic.

```swift
// Runs after this Content is decoded.
func afterDecode() throws {
// Name may not be passed in, but if it is, then it can't be an empty string.
self.name = self.name?.trimmingCharacters(in: .whitespacesAndNewlines)
if let name = self.name, name.isEmpty {
throw Abort(.badRequest, reason: "Name must not be empty.")
}
}

// Runs before this Content is encoded.
func beforeEncode() throws {
// Have to *always* pass a name back, and it can't be an empty string.
guard
let name = self.name?.trimmingCharacters(in: .whitespacesAndNewlines),
!name.isEmpty
else {
throw Abort(.badRequest, reason: "Name must not be empty.")
}
self.name = name
}
```

## Override Defaults

The default encoders and decoders used by Vapor's Content APIs can be configured.
Expand Down Expand Up @@ -191,4 +218,3 @@ public protocol URLQueryEncoder {
```

Conforming to these protocols allows your custom coders to be registered to `ContentConfiguration` for handling URL query strings using the `use(urlEncoder:)` and `use(urlDecoder:)` methods.

0 comments on commit f10729c

Please sign in to comment.