Skip to content
16 changes: 16 additions & 0 deletions Sources/MeiliSearch/Model/KeyParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ public struct KeyParams: Codable, Equatable {
/// Date and time when the key will expire, represented in RFC 3339 format.
public let expiresAt: String?

public init(
description: String? = nil,
name: String? = nil,
uid: String? = nil,
actions: [String],
indexes: [String],
expiresAt: String?
) {
self.description = description
self.name = name
self.uid = uid
self.actions = actions
self.indexes = indexes
self.expiresAt = expiresAt
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

Expand Down
2 changes: 1 addition & 1 deletion Sources/MeiliSearch/QueryParameters/KeysQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class KeysQuery: Queryable {
/// Offset the number of indexes that were skipped in the current response.
private var offset: Int?

init(limit: Int? = nil, offset: Int? = nil) {
public init(limit: Int? = nil, offset: Int? = nil) {
self.offset = offset
self.limit = limit
}
Expand Down
34 changes: 34 additions & 0 deletions Tests/MeiliSearchUnitTests/KeyParamsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@testable import MeiliSearch

import XCTest

class KeyParamsTests: XCTestCase {
func testCompleteInitializer() {
let keyParams = KeyParams(
description: "The default search key",
name: "Default Search Key",
uid: "74c9c733-3368-4738-bbe5-1d18a5fecb37",
actions: ["action1"],
indexes: ["index1"],
expiresAt: "2021-08-11T10:00:00Z"
)

XCTAssertEqual(keyParams.actions, ["action1"])
XCTAssertEqual(keyParams.indexes, ["index1"])
XCTAssertEqual(keyParams.expiresAt, "2021-08-11T10:00:00Z")
XCTAssertEqual(keyParams.description, "The default search key")
XCTAssertEqual(keyParams.name, "Default Search Key")
XCTAssertEqual(keyParams.uid, "74c9c733-3368-4738-bbe5-1d18a5fecb37")
}

func testCompactInitializer() {
let keyParams = KeyParams(actions: ["action1"], indexes: ["index1"], expiresAt: nil)

XCTAssertEqual(keyParams.actions, ["action1"])
XCTAssertEqual(keyParams.indexes, ["index1"])
XCTAssertNil(keyParams.expiresAt)
XCTAssertNil(keyParams.description)
XCTAssertNil(keyParams.name)
XCTAssertNil(keyParams.uid)
}
}