|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Parameters that can be passed to the get function. |
| 4 | +public enum MicrocmsParameter { |
| 5 | + // MARK: - Common Parameters |
| 6 | + /// |
| 7 | + /// Elements to be retrieved in the content. |
| 8 | + /// ex) .fields(["title", "main_image", "updatedAt", "author.name"] |
| 9 | + case fields([String]) |
| 10 | + |
| 11 | + /// Depth of the hierarchy from which to retrieve the reference content. |
| 12 | + /// Default value is 1, and maximum is 3. |
| 13 | + case depth(Int) |
| 14 | + |
| 15 | + // MARK: - List Parameters |
| 16 | + |
| 17 | + /// Number of retrievals, default value is 10. |
| 18 | + case limit(Int) |
| 19 | + |
| 20 | + /// Offset, default value is 0. |
| 21 | + case offset(Int) |
| 22 | + |
| 23 | + /// Sort the contents to be retrieved. |
| 24 | + /// ex) .orders(["publishedAt"]) |
| 25 | + /// |
| 26 | + /// If you want to specify descending order, prefix the field name with - (minus). |
| 27 | + /// .orders(["publishedAt", "-updatedAt"]) |
| 28 | + /// |
| 29 | + /// Only fields with "Date", "True/False", and "Numeric" can be sorted, |
| 30 | + /// and any other field specification will be ignored. |
| 31 | + case orders([String]) |
| 32 | + |
| 33 | + /// Performs a full-text search of the content. |
| 34 | + /// The fields to be searched are "text field", "text area", and "rich editor". |
| 35 | + case q(String) |
| 36 | + |
| 37 | + /// Get only the target content by specifying the content id. |
| 38 | + /// ex) .ids(["first_id", "second_id", "third_id"]) |
| 39 | + case ids([String]) |
| 40 | + |
| 41 | + /// Filters the content to be retrieved by specifying conditions. |
| 42 | + /// There are various options, so please refer to the API reference for available conditional expressions. |
| 43 | + /// https://document.microcms.io/content-api/get-list-contents#hdebbdc8e86 |
| 44 | + case filters(String) |
| 45 | + |
| 46 | + var queryItem: URLQueryItem { |
| 47 | + switch self { |
| 48 | + |
| 49 | + case .fields(let value): |
| 50 | + return .init(name: "fields", value: value.joined(separator: ",")) |
| 51 | + case .depth(let value): |
| 52 | + return .init(name: "depth", value: String(value)) |
| 53 | + case .limit(let value): |
| 54 | + return .init(name: "limit", value: String(value)) |
| 55 | + case .offset(let value): |
| 56 | + return .init(name: "offset", value: String(value)) |
| 57 | + case .orders(let value): |
| 58 | + return .init(name: "orders", value: value.joined(separator: ",")) |
| 59 | + case .q(let value): |
| 60 | + return .init(name: "q", value: value) |
| 61 | + case .ids(let value): |
| 62 | + return .init(name: "ids", value: value.joined(separator: ",")) |
| 63 | + case .filters(let value): |
| 64 | + return .init(name: "filters", value: value) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments