diff --git a/Demo.playground/Pages/Advanced.xcplaygroundpage/Contents.swift b/Demo.playground/Pages/Advanced.xcplaygroundpage/Contents.swift index 5e6ae6a..6de61c7 100644 --- a/Demo.playground/Pages/Advanced.xcplaygroundpage/Contents.swift +++ b/Demo.playground/Pages/Advanced.xcplaygroundpage/Contents.swift @@ -1,5 +1,7 @@ //: [Previous](@previous) +import PlaygroundSupport + import Foundation import APIWrapper @@ -36,7 +38,6 @@ extension API { enum AdvancedAPI { /// Finally, the new initialization method declared above is called on /// the property wrapper to complete the interface definition. - @GET("/api", verification: .normal) static var testAPI: APIParameterBuilder<()>? = nil } diff --git a/Demo.playground/Pages/Basic.xcplaygroundpage/Contents.swift b/Demo.playground/Pages/Basic.xcplaygroundpage/Contents.swift index 0a03fa5..22a7926 100644 --- a/Demo.playground/Pages/Basic.xcplaygroundpage/Contents.swift +++ b/Demo.playground/Pages/Basic.xcplaygroundpage/Contents.swift @@ -1,3 +1,5 @@ +import PlaygroundSupport + import Foundation import APIWrapper diff --git a/Demo.playground/Pages/Combine.xcplaygroundpage/Contents.swift b/Demo.playground/Pages/Combine.xcplaygroundpage/Contents.swift index be75468..e493760 100644 --- a/Demo.playground/Pages/Combine.xcplaygroundpage/Contents.swift +++ b/Demo.playground/Pages/Combine.xcplaygroundpage/Contents.swift @@ -1,8 +1,9 @@ //: [Previous](@previous) -import Foundation +import PlaygroundSupport import Combine +import Foundation import ObjectiveC import APIWrapper diff --git a/Demo.playground/Sources/API+Request.swift b/Demo.playground/Sources/API+Request.swift index aa66f6b..a139f47 100644 --- a/Demo.playground/Sources/API+Request.swift +++ b/Demo.playground/Sources/API+Request.swift @@ -75,7 +75,7 @@ private extension API { } let (data, response) = try await URLSession.shared.data(for: request) - print("✅ \(String(describing: response.url?.absoluteString)) End of request") + print("✅ \(response.url?.absoluteString ?? "nil") End of request") return data } diff --git a/README.md b/README.md index fead957..fb4ecde 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ static var tupleParamAPI: APIParameterBuilder<(id: Int, name: String?)>? = { @POST("/post") static var postWithModel: APIParameterBuilder? = { // You can have your model follow the `APIParameterConvertible` protocol, - // or use `AnyAPIHashableParameter` to wrap your model in an outer layer. - AnyAPIHashableParameter($0) + // or use `AnyAPIParameter` to wrap your model in an outer layer. + AnyAPIParameter($0) } ``` diff --git a/README_CN.md b/README_CN.md index fbc9b0a..191bfd6 100644 --- a/README_CN.md +++ b/README_CN.md @@ -36,8 +36,8 @@ static var tupleParamAPI: APIParameterBuilder<(id: Int, name: String?)>? = { @POST("/post") static var postWithModel: APIParameterBuilder? = { - // 您可以让您的模型遵循 `APIParameterConvertible` 协议,或者使用 `AnyAPIHashableParameter` 在外面包裹一层。 - AnyAPIHashableParameter($0) + // 您可以让您的模型遵循 `APIParameterConvertible` 协议,或者使用 `AnyAPIParameter` 在外面包裹一层。 + AnyAPIParameter($0) } ``` diff --git a/RaAPIWrapper.podspec b/RaAPIWrapper.podspec index 98b8e0b..a48856d 100755 --- a/RaAPIWrapper.podspec +++ b/RaAPIWrapper.podspec @@ -5,7 +5,7 @@ Pod::Spec.new do |s| s.name = 'RaAPIWrapper' - s.version = '1.0.1' + s.version = '1.0.2' s.summary = 'Makes it easier to define a network request.' diff --git a/Sources/Core/RequestInfo/APIParameterConvertible.swift b/Sources/Core/RequestInfo/APIParameterConvertible.swift index 8f7102b..9c2922b 100644 --- a/Sources/Core/RequestInfo/APIParameterConvertible.swift +++ b/Sources/Core/RequestInfo/APIParameterConvertible.swift @@ -1,5 +1,5 @@ // -// APIParameters.swift +// APIParameterConvertible.swift // RaAPIWrapper // // Created by Rakuyo on 2022/8/25. @@ -8,9 +8,6 @@ import Foundation -/// Used to constrain what types can be used as api parameters. -public typealias APIParametrizable = AnyAPIParameter.Input - /// Means that the type can be converted to an interface parameter for requesting an api. public protocol APIParameterConvertible { /// Converts the target to an Encodable-compliant type. @@ -41,6 +38,10 @@ extension Double: APIParameterConvertible { } extension Bool: APIParameterConvertible { } +// MARK: - Data + +extension Data: APIParameterConvertible { } + // MARK: - Array extension Array: APIParameterConvertible { diff --git a/Sources/Core/RequestInfo/APIParametrizable.swift b/Sources/Core/RequestInfo/APIParametrizable.swift new file mode 100644 index 0000000..45015bb --- /dev/null +++ b/Sources/Core/RequestInfo/APIParametrizable.swift @@ -0,0 +1,12 @@ +// +// APIParametrizable.swift +// RaAPIWrapper +// +// Created by Rakuyo on 2023/01/13. +// Copyright © 2022 Rakuyo. All rights reserved. +// + +import Foundation + +/// Used to constrain what types can be used as api parameters. +public typealias APIParametrizable = AnyAPIHashableParameter.Input diff --git a/Sources/Core/RequestInfo/AnyAPIHashable/AnyAPIHashableParameter.swift b/Sources/Core/RequestInfo/AnyAPIHashable/AnyAPIHashableParameter.swift index 3aed0f0..2926d68 100644 --- a/Sources/Core/RequestInfo/AnyAPIHashable/AnyAPIHashableParameter.swift +++ b/Sources/Core/RequestInfo/AnyAPIHashable/AnyAPIHashableParameter.swift @@ -8,9 +8,6 @@ import Foundation -/// Represents an arbitrary api parameter. -public typealias AnyAPIParameter = AnyAPIHashableParameter - /// Make `Encodable` follow `Hashable` protocol. public struct AnyAPIHashableParameter: AnyAPIHashable { public typealias Value = Encodable diff --git a/Sources/Core/RequestInfo/AnyAPIParameter.swift b/Sources/Core/RequestInfo/AnyAPIParameter.swift new file mode 100644 index 0000000..8da712f --- /dev/null +++ b/Sources/Core/RequestInfo/AnyAPIParameter.swift @@ -0,0 +1,12 @@ +// +// AnyAPIParameter.swift +// RaAPIWrapper +// +// Created by Rakuyo on 2023/01/13. +// Copyright © 2022 Rakuyo. All rights reserved. +// + +import Foundation + +/// Represents an arbitrary api parameter. +public typealias AnyAPIParameter = AnyAPIHashableParameter diff --git a/Tests/AvailabilityTests.swift b/Tests/AvailabilityTest.swift similarity index 68% rename from Tests/AvailabilityTests.swift rename to Tests/AvailabilityTest.swift index 1459b01..33615b0 100644 --- a/Tests/AvailabilityTests.swift +++ b/Tests/AvailabilityTest.swift @@ -1,5 +1,5 @@ // -// AvailabilityTests.swift +// AvailabilityTest.swift // RaAPIWrapper // // Created by Rakuyo on 2022/8/26. @@ -14,6 +14,7 @@ final class AvailabilityTests: XCTestCase { let param: (id: Int, name: String?) = (1, nil) let info = TestAPI.$tupleParamAPI.createRequestInfo(param) + // Verify that `nil` is filtered out in `info.parameters` XCTAssertEqual(info.parameters, packToParameters(["id": 1])) XCTAssertNotEqual(info.parameters, packToParameters(["id": 1, "name": Optional.none])) @@ -22,7 +23,7 @@ final class AvailabilityTests: XCTestCase { XCTAssertNil(info.specialBaseURL) } - private func packToParameters(_ value: [String: Optional]) -> AnyAPIParameter { + private func packToParameters(_ value: [String: Int?]) -> AnyAPIParameter { return .init(value.mapValues { AnyAPIParameter($0) }) } } @@ -34,4 +35,14 @@ fileprivate struct TestAPI { static var tupleParamAPI: APIParameterBuilder<(id: Int, name: String?)>? = { ["id": $0.id, "name": $0.name] as [String: Any?] } + + @POST(Self.path) + static var test1API: APIParameterBuilder<(id: Int, name: String)>? = { + ["id": $0.id, "name": $0.name] + } + + @POST(Self.path) + static var test2API: APIParameterBuilder<(id: String, name: String)>? = { + ["id": $0.id, "name": $0.name] + } } diff --git a/Tests/ExtensibilityTests.swift b/Tests/ExtensibilityTest.swift similarity index 96% rename from Tests/ExtensibilityTests.swift rename to Tests/ExtensibilityTest.swift index 92b05c7..39209d2 100644 --- a/Tests/ExtensibilityTests.swift +++ b/Tests/ExtensibilityTest.swift @@ -1,5 +1,5 @@ // -// ExtensibilityTests.swift +// ExtensibilityTest.swift // RaAPIWrapper // // Created by Rakuyo on 2022/12/15.