9
9
import Foundation
10
10
11
11
extension VoluntaryApplicationServerIdentification {
12
- public struct Configuration : Hashable , Codable , Sendable {
12
+ public struct Configuration : Hashable , Sendable {
13
13
/// The VAPID key that identifies the push service to subscribers.
14
14
///
15
15
/// This key should be shared by all instances of your push service, and should be kept secure. Rotating this key is not recommended as you'll lose access to subscribers that registered against it.
@@ -64,26 +64,6 @@ extension VoluntaryApplicationServerIdentification {
64
64
self . validityDuration = validityDuration
65
65
}
66
66
67
- public init ( from decoder: any Decoder ) throws {
68
- let container = try decoder. container ( keyedBy: CodingKeys . self)
69
-
70
- let primaryKey = try container. decodeIfPresent ( Key . self, forKey: CodingKeys . primaryKey)
71
- let keys = try container. decode ( Set< Key> . self , forKey: CodingKeys . keys)
72
- let deprecatedKeys = try container. decodeIfPresent ( Set< Key> . self , forKey: CodingKeys . deprecatedKeys)
73
- let contactInformation = try container. decode ( ContactInformation . self, forKey: CodingKeys . contactInformation)
74
- let expirationDuration = try container. decode ( Duration . self, forKey: CodingKeys . expirationDuration)
75
- let validityDuration = try container. decode ( Duration . self, forKey: CodingKeys . validityDuration)
76
-
77
- try self . init (
78
- primaryKey: primaryKey,
79
- keys: keys,
80
- deprecatedKeys: deprecatedKeys,
81
- contactInformation: contactInformation,
82
- expirationDuration: expirationDuration,
83
- validityDuration: validityDuration
84
- )
85
- }
86
-
87
67
mutating func updateKeys(
88
68
primaryKey: Key ? ,
89
69
keys: Set < Key > ,
@@ -105,6 +85,57 @@ extension VoluntaryApplicationServerIdentification {
105
85
}
106
86
}
107
87
88
+ extension VAPID . Configuration : Codable {
89
+ public enum CodingKeys : CodingKey {
90
+ case primaryKey
91
+ case keys
92
+ case deprecatedKeys
93
+ case contactInformation
94
+ case expirationDuration
95
+ case validityDuration
96
+ }
97
+
98
+ public init ( from decoder: any Decoder ) throws {
99
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
100
+
101
+ let primaryKey = try container. decodeIfPresent ( VAPID . Key. self, forKey: CodingKeys . primaryKey)
102
+ let keys = try container. decodeIfPresent ( Set< VAPID . Key> . self , forKey: CodingKeys . keys) ?? [ ]
103
+ let deprecatedKeys = try container. decodeIfPresent ( Set< VAPID . Key> . self , forKey: CodingKeys . deprecatedKeys)
104
+ let contactInformation = try container. decode ( ContactInformation . self, forKey: CodingKeys . contactInformation)
105
+ let expirationDuration = try container. decode ( Duration . self, forKey: CodingKeys . expirationDuration)
106
+ let validityDuration = try container. decode ( Duration . self, forKey: CodingKeys . validityDuration)
107
+
108
+ try self . init (
109
+ primaryKey: primaryKey,
110
+ keys: keys,
111
+ deprecatedKeys: deprecatedKeys,
112
+ contactInformation: contactInformation,
113
+ expirationDuration: expirationDuration,
114
+ validityDuration: validityDuration
115
+ )
116
+ }
117
+
118
+ public func encode( to encoder: any Encoder ) throws {
119
+ var container = encoder. container ( keyedBy: CodingKeys . self)
120
+
121
+ /// Remove the primary key from the list so it's not listed twice
122
+ var keys : Set < VAPID . Key > ? = self . keys
123
+ if let primaryKey {
124
+ keys? . remove ( primaryKey)
125
+ }
126
+ if keys? . isEmpty == true {
127
+ keys = nil
128
+ }
129
+
130
+ try container. encodeIfPresent ( primaryKey, forKey: . primaryKey)
131
+ try container. encodeIfPresent ( keys, forKey: . keys)
132
+ try container. encodeIfPresent ( deprecatedKeys, forKey: . deprecatedKeys)
133
+ try container. encode ( contactInformation, forKey: . contactInformation)
134
+ try container. encode ( expirationDuration, forKey: . expirationDuration)
135
+ try container. encode ( validityDuration, forKey: . validityDuration)
136
+ }
137
+ }
138
+
108
139
extension VAPID . Configuration {
109
140
/// The contact information for the push service.
110
141
///
0 commit comments