RestfulPropertyKit Documentation Beta

Structure Rest

@propertyWrapper public struct Rest<Parent, Value> where Parent: Codable, Value: Codable 

The REST property wrapper.

Allows to send post and get requests with bearer authentication and values that are automatically type marshalled.

Property access:

struct Fruit: Codable {
    let name: String
    let size: Int
    let color: String
}

...

@Rest(path: "fruitoftheday") var fruit: Fruit = Fruit(name: "strawberry", size: 3, color: "red")

...

print(fruit) // wrappedValue access
print($fruit.wrappedValue) // wrappedValue access

print($fruit) // projectedValue (aka query) access

print($fruit.projectedValue) // nested projectedValue (aka binding) access
print($fruit.name) // nested projectedValue (aka binding) property (aka property binding) access

Usage for authentication:

struct Account: Codable {
    var email: String
    var password: String
}

struct SomeView: View {
    @Rest(path: "signin", bearer: false) var account: Account = Account(email: "", password: "")

    var body: some View {
        TextField("Email placeholder", text: self.$account.email)
        TextField("Password placeholder", text: self.$account.password)
        Button("Sign In") {
            self.$account>?.success { ... }
        }
    }
}

Usage for get request:

struct Fruit: Codable {
    let name: String
    let size: Int
    let color: String
}

struct SomeView: View {
    @Rest(path: "fruitoftheday") var fruit: Fruit = Fruit(name: "strawberry", size: 3, color: "red")

    var body: some View {
        Text(fruit.name)
            .onAppear { self.$fruit>?.sink { _ in } }
    }
}

Usage for get request with query parameters:

struct Fruit: Codable {
    let name: String
    let size: Int
    let color: String
}
struct Fruits: Codable {
    var values: [Fruit] = []
}

struct SomeView: View {
    @Rest(path: "fruits", params: ["limit": 10]) var fruits: Fruits = Fruits()

    var body: some View {
        Text(fruits.values[0].name)
            .onAppear { self.$fruits>?.sink { _ in } }
   }
}

Usage for get request with property key path:

struct Fruit: Codable {
    let name: String
    let size: Int
    let color: String
}
struct Fruits: ParentCodable {
    var values: [Fruit] = []

    static func wrap(child: [Fruit]) -> Self {
        return Fruits(values: child)
    }
}

struct SomeView: View {
    @Rest(path: "fruits", parent: Fruits.self, prop: \.values) var fruits: [Fruit] = []

    var body: some View {
        Text(fruits[0].name)
            .onAppear { self.$fruits>?.sink { _ in } }
    }
}

Usage for get request with query parameters and property key path:

struct Fruit: Codable {
    let name: String
    let size: Int
    let color: String
}
struct Fruits: ParentCodable {
    var values: [Fruit] = []

    static func wrap(child: [Fruit]) -> Self {
        return Fruits(values: child)
    }
}

struct SomeView: View {
    @Rest(path: "fruits", params: ["limit": 10], parent: Fruits.self, prop: \.values) var fruits: [Fruit] = []

    var body: some View {
        Text(fruits[0].name)
            .onAppear { self.$fruits>?.sink { _ in } }
    }
}

Usage for post request with existing value:

struct Account: Codable {
    var email: String
    var password: String
}

struct SomeView: View {
    @Rest(path: "signup") var account: Account = Account(email: "", password: "")

    var body: some View {
        TextField("Email placeholder", text: self.$account.email)
        TextField("Password placeholder", text: self.$account.password)
        Button("Sign Up") {
            self.$account<!.success { ... }
        }
    }
}

Usage for post request with new value:

struct Account: Codable {
    var email: String
    var password: String
}

struct SomeView: View {
    @Rest(path: "signup") var account: Account = Account(email: "", password: "")

    var body: some View {
        TextField("Email placeholder", text: self.$account.email)
        TextField("Password placeholder", text: self.$account.password)
        Button("Sign Up") {
            (self.$account <- someNewAccount).success { ... }
        }
    }
}

References

Property Wrapper

Key Paths

Initializers

init(wrapped​Value:​path:​bearer:​)

public init(wrappedValue: Value, path: String, bearer: Bool = true) where Parent == Value 

Creates a Rest property wrapper instance.

Usage:

@Rest(path: "somePath") var someProp: SomeType = SomeType(...)

Parameters

wrapped​Value Value

The value wrapped by this property wrapper.

path String

The url request path component.

bearer Bool

True if the bearer token should be send as part of the request.

Returns

A new Rest property wrapper instance.

init(wrapped​Value:​path:​params:​bearer:​)

public init<ParamKey, ParamValue>(wrappedValue: Value, path: String, params: [ParamKey: ParamValue], bearer: Bool = true) where Parent == Value, ParamKey: CustomStringConvertible, ParamValue: CustomStringConvertible 

Creates a Rest property wrapper instance.

Usage:

@Rest(path: "somePath", params: ["someKey": someValue]) var someProp: SomeType = SomeType(...)

Parameters

wrapped​Value Value

The value wrapped by this property wrapper.

path String

The url request path component.

params [Param​Key:​ Param​Value]

The url request query parameter component.

bearer Bool

True if the bearer token should be send as part of the request.

Returns

A new Rest property wrapper instance.

init(wrapped​Value:​path:​bearer:​parent:​prop:​)

public init(wrappedValue: Value, path: String, bearer: Bool = true, parent: Parent.Type, prop: KeyPath<Parent, Value>) where Parent: ParentCodable, Parent.ChildCodable == Value 

Creates a Rest property wrapper instance.

Usage:

@Rest(path: "somePath", parent: SomeParentType.self, prop: \.somePropKey) var someProp: SomeType = SomeType(...)

Parameters

wrapped​Value Value

The value wrapped by this property wrapper.

path String

The url request path component.

bearer Bool

True if the bearer token should be send as part of the request.

parent Parent.​Type

The parent type conforming to the ParentCodable protocol.

prop Key​Path<Parent, Value>

The property to be extracted from the parent type.

Returns

A new Rest property wrapper instance.

init(wrapped​Value:​path:​params:​bearer:​parent:​prop:​)

public init<ParamKey, ParamValue>(wrappedValue: Value, path: String, params: [ParamKey: ParamValue], bearer: Bool = true, parent: Parent.Type, prop: KeyPath<Parent, Value>) where ParamKey: CustomStringConvertible, ParamValue: CustomStringConvertible, Parent: ParentCodable, Parent.ChildCodable == Value 

Creates a Rest property wrapper instance.

Usage:

@Rest(path: "somePath", params: ["someKey": someValue], parent: SomeParentType.self, prop: \.somePropKey) var someProp: SomeType = SomeType(...)

Parameters

wrapped​Value Value

The value wrapped by this property wrapper.

path String

The url request path component.

params [Param​Key:​ Param​Value]

The url request query parameter component.

bearer Bool

True if the bearer token should be send as part of the request.

parent Parent.​Type

The parent type conforming to the ParentCodable protocol.

prop Key​Path<Parent, Value>

The property to be extracted from the parent type.

Returns

A new Rest property wrapper instance.

Properties

wrapped​Value

public var wrappedValue: Value 

Implicitly used computed property for @propertyWrapper.

Provides access to the wrapped value.

Usage:

struct Fruit: Codable {
    let name: String
    let size: Int
    let color: String
}

...

@Rest(path: "fruitoftheday") var fruit: Fruit = Fruit(name: "strawberry", size: 3, color: "red")

...

print(fruit) // wrappedValue access
print(fruit.name) // wrappedValue property access

projected​Value

public var projectedValue: RestQueryImpl<Parent, Value> 

Implicitly used computed property for @propertyWrapper.

Provides access to the associated query. Visibility is set to fileprivate to restrict query mutations.

Usage:

struct Fruit: Codable {
    let name: String
    let size: Int
    let color: String
}

...

@Rest(path: "fruitoftheday") var fruit: Fruit = Fruit(name: "strawberry", size: 3, color: "red")

...

print($fruit) // projectedValue (aka query) access

print($fruit.projectedValue) // nested projectedValue (aka binding) access
print($fruit.name) // nested projectedValue (aka binding) property (aka property binding) access