|
5 | 5 | // Created by Mathew Polzin on 11/5/18. |
6 | 6 | // |
7 | 7 |
|
| 8 | +public protocol JSONAPIDocument: Codable, Equatable { |
| 9 | + associatedtype ResourceBody: JSONAPI.ResourceBody |
| 10 | + associatedtype MetaType: JSONAPI.Meta |
| 11 | + associatedtype LinksType: JSONAPI.Links |
| 12 | + associatedtype IncludeType: JSONAPI.Include |
| 13 | + associatedtype Error: JSONAPIError |
| 14 | +} |
| 15 | + |
8 | 16 | /// A JSON API Document represents the entire body |
9 | 17 | /// of a JSON API request or the entire body of |
10 | 18 | /// a JSON API response. |
11 | 19 | /// Note that this type uses Camel case. If your |
12 | 20 | /// API uses snake case, you will want to use |
13 | 21 | /// a conversion such as the one offerred by the |
14 | 22 | /// Foundation JSONEncoder/Decoder: `KeyDecodingStrategy` |
15 | | -public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: Equatable { |
| 23 | +public struct Document<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: JSONAPIDocument { |
16 | 24 | public typealias Include = IncludeType |
17 | 25 |
|
18 | 26 | public let body: Body |
@@ -81,49 +89,49 @@ public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSON |
81 | 89 | } |
82 | 90 | } |
83 | 91 |
|
84 | | -extension JSONAPIDocument where IncludeType == NoIncludes { |
| 92 | +extension Document where IncludeType == NoIncludes { |
85 | 93 | public init(body: ResourceBody, meta: MetaType, links: LinksType) { |
86 | 94 | self.body = .data(.init(primary: body, includes: .none, meta: meta, links: links)) |
87 | 95 | } |
88 | 96 | } |
89 | 97 |
|
90 | | -extension JSONAPIDocument where MetaType == NoMetadata { |
| 98 | +extension Document where MetaType == NoMetadata { |
91 | 99 | public init(body: ResourceBody, includes: Includes<Include>, links: LinksType) { |
92 | 100 | self.body = .data(.init(primary: body, includes: includes, meta: .none, links: links)) |
93 | 101 | } |
94 | 102 | } |
95 | 103 |
|
96 | | -extension JSONAPIDocument where LinksType == NoLinks { |
| 104 | +extension Document where LinksType == NoLinks { |
97 | 105 | public init(body: ResourceBody, includes: Includes<Include>, meta: MetaType) { |
98 | 106 | self.body = .data(.init(primary: body, includes: includes, meta: meta, links: .none)) |
99 | 107 | } |
100 | 108 | } |
101 | 109 |
|
102 | | -extension JSONAPIDocument where IncludeType == NoIncludes, LinksType == NoLinks { |
| 110 | +extension Document where IncludeType == NoIncludes, LinksType == NoLinks { |
103 | 111 | public init(body: ResourceBody, meta: MetaType) { |
104 | 112 | self.body = .data(.init(primary: body, includes: .none, meta: meta, links: .none)) |
105 | 113 | } |
106 | 114 | } |
107 | 115 |
|
108 | | -extension JSONAPIDocument where IncludeType == NoIncludes, MetaType == NoMetadata { |
| 116 | +extension Document where IncludeType == NoIncludes, MetaType == NoMetadata { |
109 | 117 | public init(body: ResourceBody, links: LinksType) { |
110 | 118 | self.body = .data(.init(primary: body, includes: .none, meta: .none, links: links)) |
111 | 119 | } |
112 | 120 | } |
113 | 121 |
|
114 | | -extension JSONAPIDocument where MetaType == NoMetadata, LinksType == NoLinks { |
| 122 | +extension Document where MetaType == NoMetadata, LinksType == NoLinks { |
115 | 123 | public init(body: ResourceBody, includes: Includes<Include>) { |
116 | 124 | self.body = .data(.init(primary: body, includes: includes, meta: .none, links: .none)) |
117 | 125 | } |
118 | 126 | } |
119 | 127 |
|
120 | | -extension JSONAPIDocument where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks { |
| 128 | +extension Document where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks { |
121 | 129 | public init(body: ResourceBody) { |
122 | 130 | self.body = .data(.init(primary: body, includes: .none, meta: .none, links: .none)) |
123 | 131 | } |
124 | 132 | } |
125 | 133 |
|
126 | | -extension JSONAPIDocument: Codable { |
| 134 | +extension Document { |
127 | 135 | private enum RootCodingKeys: String, CodingKey { |
128 | 136 | case data |
129 | 137 | case errors |
@@ -228,8 +236,8 @@ extension JSONAPIDocument: Codable { |
228 | 236 |
|
229 | 237 | // MARK: - CustomStringConvertible |
230 | 238 |
|
231 | | -extension JSONAPIDocument: CustomStringConvertible { |
| 239 | +extension Document: CustomStringConvertible { |
232 | 240 | public var description: String { |
233 | | - return "JSONAPIDocument(body: \(String(describing: body))" |
| 241 | + return "Document(body: \(String(describing: body))" |
234 | 242 | } |
235 | 243 | } |
0 commit comments