mirror of https://github.com/vapor/docs.git
3.0 updates
This commit is contained in:
parent
c037c83714
commit
fe61dbc545
|
|
@ -99,7 +99,7 @@ pages:
|
|||
- Version (2.0):
|
||||
- '1.5': 'version/1_5.md'
|
||||
- '2.0': 'version/2_0.md'
|
||||
- '3.0-rc': 'version/3_0.md'
|
||||
- '3.0': 'version/3_0.md'
|
||||
- 'Support': 'version/support.md'
|
||||
|
||||
markdown_extensions:
|
||||
|
|
@ -112,8 +112,8 @@ markdown_extensions:
|
|||
theme:
|
||||
name: 'material'
|
||||
palette:
|
||||
primary: 'blue'
|
||||
accent: 'purple'
|
||||
primary: 'grey'
|
||||
accent: 'grey'
|
||||
logo: 'images/droplet-white.svg'
|
||||
|
||||
repo_url: http://github.com/vapor/vapor
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Using Client
|
||||
|
||||
[`Client`](#fixme) is a convenience wrapper around the lower level [HTTP → Client](../http/client.md). It automatically parses things like hostname and port from URIs and helps you encode and decode [Content](content.md).
|
||||
[`Client`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html) is a convenience wrapper around the lower level [HTTP → Client](../http/client.md). It automatically parses things like hostname and port from URIs and helps you encode and decode [Content](content.md).
|
||||
|
||||
```swift
|
||||
let res = try req.client().get("http://vapor.codes")
|
||||
|
|
@ -15,7 +15,7 @@ If you are making this external API request as the result of an incoming request
|
|||
|
||||
If you need a client during boot, use the `Application` or if you are in a `Command` use the command context's container.
|
||||
|
||||
Once you have a `Container`, use the [`client()`](#fixme) method to create a `Client`.
|
||||
Once you have a `Container`, use the [`client()`](https://api.vapor.codes/vapor/latest/Vapor/Extensions/Container.html#/s:5Vapor6clientXeXeF) method to create a `Client`.
|
||||
|
||||
```swift
|
||||
// Creates a generic Client
|
||||
|
|
@ -24,7 +24,7 @@ let client = try container.client()
|
|||
|
||||
## Send
|
||||
|
||||
Once you have a `Client`, you can use the [`send(...)`](#fixme) method to send a `Request`. Note that the request URI must include a scheme and hostname.
|
||||
Once you have a `Client`, you can use the [`send(...)`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html#/s:5Vapor6ClientP4sendXeXeF) method to send a `Request`. Note that the request URI must include a scheme and hostname.
|
||||
|
||||
```swift
|
||||
let req: Request ...
|
||||
|
|
@ -32,7 +32,7 @@ let res = try client.send(req)
|
|||
print(res) // Future<Response>
|
||||
```
|
||||
|
||||
You can also use the convenience methods like [`get(...)`](#fixme), [`post(...)`](#fixme), etc.
|
||||
You can also use the convenience methods like [`get(...)`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html#/s:5Vapor6ClientPAAE3getXeXeF), [`post(...)`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html#/s:5Vapor6ClientPAAE4postXeXeF), etc.
|
||||
|
||||
```swift
|
||||
let user: User ...
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ Notice the key names exactly match the keys in the request data. The expected da
|
|||
|
||||
#### Decode
|
||||
|
||||
Now we are ready to decode that HTTP request. Every [`Request`](#fixme) has a [`ContentContainer`](#fixme) that we can use to decode content from the message's body.
|
||||
Now we are ready to decode that HTTP request. Every [`Request`](https://api.vapor.codes/vapor/latest/Vapor/Classes/Request.html) has a [`ContentContainer`](https://api.vapor.codes/vapor/latest/Vapor/Structs/ContentContainer.html) that we can use to decode content from the message's body.
|
||||
|
||||
```swift
|
||||
router.post("login") { req -> Future<HTTPStatus> in
|
||||
|
|
@ -56,7 +56,7 @@ We use `.map(to:)` here since `decode(...)` returns a [future](../async/getting-
|
|||
|
||||
#### Router
|
||||
|
||||
To help make decoding content from incoming requests easier, Vapor offers a few extensions on [`Router`](#fixme) to do this automatically.
|
||||
To help make decoding content from incoming requests easier, Vapor offers a few extensions on [`Router`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Router.html) to do this automatically.
|
||||
|
||||
```swift
|
||||
router.post(LoginRequest.self, at: "login") { req, loginRequest in
|
||||
|
|
@ -128,7 +128,7 @@ router.get("user") { req -> User in
|
|||
}
|
||||
```
|
||||
|
||||
This will create a default `Response` with `200 OK` status code and minimal headers. You can customize the response using a convenience [`encode(...)`](#fixme) method.
|
||||
This will create a default `Response` with `200 OK` status code and minimal headers. You can customize the response using a convenience `encode(...)` method.
|
||||
|
||||
```swift
|
||||
router.get("user") { req -> Future<Response> in
|
||||
|
|
@ -159,7 +159,7 @@ struct User: Content {
|
|||
|
||||
## Client
|
||||
|
||||
Encoding content to HTTP requests sent by [`Client`](#fixme)s is similar to encoding HTTP responses returned by your server.
|
||||
Encoding content to HTTP requests sent by [`Client`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html)s is similar to encoding HTTP responses returned by your server.
|
||||
|
||||
### Request
|
||||
|
||||
|
|
@ -238,7 +238,7 @@ print(user) // Future<User>
|
|||
|
||||
### Example
|
||||
|
||||
Let's now take a look at our complete [`Client`](#fixme) request that both encodes and decodes content.
|
||||
Let's now take a look at our complete [`Client`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html) request that both encodes and decodes content.
|
||||
|
||||
```swift
|
||||
// Create the LoginRequest data
|
||||
|
|
@ -256,7 +256,7 @@ print(user) // Future<User>
|
|||
|
||||
## Query String
|
||||
|
||||
URL-Encoded Form data can be encoded and decoded from an HTTP request's URI query string just like content. All you need is a class or struct that conforms to [`Content`](#fixme). In these examples, we will be using the following struct.
|
||||
URL-Encoded Form data can be encoded and decoded from an HTTP request's URI query string just like content. All you need is a class or struct that conforms to [`Content`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Content.html). In these examples, we will be using the following struct.
|
||||
|
||||
```swift
|
||||
struct Flags: Content {
|
||||
|
|
@ -267,7 +267,7 @@ struct Flags: Content {
|
|||
|
||||
### Decode
|
||||
|
||||
All [`Request`](#fixme)s have a [`QueryContainer`](#fixme) that you can use to decode the query string.
|
||||
All [`Request`](https://api.vapor.codes/vapor/latest/Vapor/Classes/Request.html)s have a [`QueryContainer`](https://api.vapor.codes/vapor/latest/Vapor/Structs/QueryContainer.html) that you can use to decode the query string.
|
||||
|
||||
```swift
|
||||
let flags = try req.query.decode(Flags.self)
|
||||
|
|
@ -276,7 +276,7 @@ print(flags) // Flags
|
|||
|
||||
### Encode
|
||||
|
||||
You can also encode content. This is useful for encoding query strings when using [`Client`](#fixme).
|
||||
You can also encode content. This is useful for encoding query strings when using [`Client`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html).
|
||||
|
||||
```swift
|
||||
let flags: Flags ...
|
||||
|
|
@ -347,7 +347,7 @@ This method may seem a bit verbose at first when compared to dynamic solutions,
|
|||
|
||||
## JSON
|
||||
|
||||
JSON is a very popular encoding format for APIs and the way in which dates, data, floats, etc are encoded is non-standard. Because of this, Vapor makes it easy to use custom [`JSONDecoder`](#fixme)s when you interact with other APIs.
|
||||
JSON is a very popular encoding format for APIs and the way in which dates, data, floats, etc are encoded is non-standard. Because of this, Vapor makes it easy to use custom [`JSONDecoder`](https://api.vapor.codes/vapor/latest/Vapor/Extensions/JSONDecoder.html#/s:5Vapor6customXeXeFZ)s when you interact with other APIs.
|
||||
|
||||
```swift
|
||||
// Conforms to Encodable
|
||||
|
|
@ -367,7 +367,7 @@ If you would like to set a custom JSON encoder or decoder globally, you can do s
|
|||
|
||||
## Configure
|
||||
|
||||
Use [`ContentConfig`](#fixme) to register custom encoder/decoders for your application. These custom coders will be used anywhere you do `content.encode`/`content.decode`.
|
||||
Use [`ContentConfig`](https://api.vapor.codes/vapor/latest/Vapor/Structs/ContentConfig.html) to register custom encoder/decoders for your application. These custom coders will be used anywhere you do `content.encode`/`content.decode`.
|
||||
|
||||
```swift
|
||||
/// Create default content config
|
||||
|
|
|
|||
|
|
@ -8,11 +8,11 @@ When a new client connects and session data is set, Vapor will return a `Set-Coo
|
|||
|
||||
## Middleware
|
||||
|
||||
The first step to using sessions is enabling [`SessionsMiddleware`](#fixme). This can be done globally for the entire application or on a per-route basis.
|
||||
The first step to using sessions is enabling [`SessionsMiddleware`](https://api.vapor.codes/vapor/latest/Vapor/Classes/SessionsMiddleware.html). This can be done globally for the entire application or on a per-route basis.
|
||||
|
||||
### Globally
|
||||
|
||||
To globally enable sessions, add the middleware to your [`MiddlewareConfig`](#fixme).
|
||||
To globally enable sessions, add the middleware to your [`MiddlewareConfig`](https://api.vapor.codes/vapor/latest/Vapor/Structs/MiddlewareConfig.html).
|
||||
|
||||
|
||||
```swift
|
||||
|
|
@ -25,7 +25,7 @@ This is usually done in [`configure.swift`](../getting-started/structure.md#conf
|
|||
|
||||
### Per Route
|
||||
|
||||
To enable sessions for a group of routes, use the [`grouped(...)`](#fixme) methods on `Router`.
|
||||
To enable sessions for a group of routes, use the [`grouped(...)`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Router.html) methods on `Router`.
|
||||
|
||||
```swift
|
||||
// create a grouped router at /sessions w/ sessions enabled
|
||||
|
|
@ -39,13 +39,13 @@ sessions.get("foo") { req in
|
|||
|
||||
## Sessions
|
||||
|
||||
When `SessionsMiddleware` boots it will attempt to make a [`Sessions`](#fixme) and a [`SessionsConfig`](#fixme). Vapor will use an in-memory session by default. You can override both of these services by registering them in `configure.swift`.
|
||||
When `SessionsMiddleware` boots it will attempt to make a [`Sessions`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Sessions.html) and a [`SessionsConfig`](https://api.vapor.codes/vapor/latest/Vapor/Structs/SessionsConfig.html). Vapor will use an in-memory session by default. You can override both of these services by registering them in `configure.swift`.
|
||||
|
||||
You can use Fluent databases (like MySQL, PostgreSQL, etc) or caches like Redis to store your sessions. See the respective guides for more information.
|
||||
|
||||
## Session
|
||||
|
||||
Once you have `SessionsMiddleware` enabled, you can use [`req.session()`](#fixme) to access the session. Here is a simple example that does simple CRUD operations on a `"name"` value in the session.
|
||||
Once you have `SessionsMiddleware` enabled, you can use [`req.session()`](https://api.vapor.codes/vapor/latest/Vapor/Classes/Request.html#/s:5Vapor7RequestC7sessionAA7SessionCyKF) to access the session. Here is a simple example that does simple CRUD operations on a `"name"` value in the session.
|
||||
|
||||
```swift
|
||||
// create a route at GET /sessions/get
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ Vapor includes convenience methods for working with the lower level WebSocket [c
|
|||
|
||||
Vapor's WebSocket server includes the ability to route incoming requests just like its HTTP server.
|
||||
|
||||
When Vapor's main HTTP [`Server`](#fixme) boots it will attempt to create a [`WebSocketServer`](#fixme). If one is registered, it will be added as an HTTP upgrade handler to the server.
|
||||
When Vapor's main HTTP [`Server`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Server.html) boots it will attempt to create a [`WebSocketServer`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/WebSocketServer.html). If one is registered, it will be added as an HTTP upgrade handler to the server.
|
||||
|
||||
So to create a WebSocket server, all you need to do is register one in [`configure.swift`](../getting-started/structure.md#configureswift).
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ Welcome, Vapor!
|
|||
|
||||
## Client
|
||||
|
||||
Vapor also supports connecting to WebSocket servers as a client. The easiest way to connect to a WebSocket server is through the [`webSocket(...)`](#fixme) method on [`Client`](#fixme).
|
||||
Vapor also supports connecting to WebSocket servers as a client. The easiest way to connect to a WebSocket server is through the [`webSocket(...)`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html#/s:5Vapor6ClientPAAE9webSocketXeXeF) method on [`Client`](https://api.vapor.codes/vapor/latest/Vapor/Protocols/Client.html).
|
||||
|
||||
For this example, we will assume our application connects to a WebSocket server in [`boot.swift`](../getting-started/structure.md#bootswift)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue