Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions Sources/SimpleHTTP/ContentData/ContentDataCodersConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@ public struct ContentDataCodersConfiguration {
public var decoders: ContentDataDecodersConfiguration
public let defaultType: HTTPContentType

/// - Parameter default: the default type. This will be used when:
/// - No encoding type is provided inside the request
/// - Response does not set **an explicit type**
/// - Parameter encoders: Encoders to use. They will be merged with `defaultEncoders`
/// - Parameter decoders: Decoders to use. They will be merged with `defaultDecoders`
public init(
default: HTTPContentType,
encoders: ContentDataEncodersConfiguration,
decoders: ContentDataDecodersConfiguration,
) {
self.encoders = encoders
self.decoders = decoders
self.encoders = .defaultEncoders.merging(encoders) { $1 }
self.decoders = .defaultDecoders.merging(decoders) { $1 }
self.defaultType = `default`
}

/// Creates a configuration with default coders and decoders.
/// Creates a configuration with default coders and decoders
///
/// - Note: default encoder/decoder is set to JSON
public init() {
self.init(
default: .json,
encoders: [
.json: JSONEncoder(),
.formURLEncoded: FormURLEncoder()
],
decoders: [
.json: JSONDecoder()
]
)
self.init(default: .json, encoders: [:], decoders: [:])
}
}

Expand All @@ -51,3 +47,20 @@ extension ContentDataCodersConfiguration {
return copy
}
}

public extension ContentDataEncodersConfiguration {
static var defaultEncoders: Self {
[
.json: JSONEncoder(),
.formURLEncoded: FormURLEncoder()
]
}
}

public extension ContentDataDecodersConfiguration {
static var defaultDecoders: Self {
[
.json: JSONDecoder()
]
}
}
Loading