Network service Combine REST API CRUD

Easy and lightweight network service. Reactive wrapper for URLSession.shared.dataTaskPublisher to create different set of network requests like GET, POST, PUT, DELETE


  • Stand alone package without any dependencies using just Apple’s Combine facilities
  • Error handling from forming URLRequest object to gettting data on a subscription
  • Customizable for different environments development, production…
  • Customizable for different requests schemes from classic CRUD Rest to what suits to your own custom routes
  • Based on interfaces not implementations
  • Ability to log and customize logers for different environment

1. Environment

Define enum with interface IEnvironment

enum Environment: IEnvironment {

    case development

    case production

    var baseURL: String {
        switch self {
            case .development: return "http://localhost:3000"
            case .production: return ""
    var headers: [IRequestHeader]? {
        switch self {
            case .development: return [ContentType.applicationJSON]
            case .production: return [ContentType.textJSON]
    var logger : ILogger? {
        switch self {
            case .development: return ServiceLogger()
            case .production: return nil

Request headers

All headers for a request have to conform to the interface IRequestHeader

The example implemetation for content type headers is here ContentType.swift


You can use out of the box standard logger ServiceLogger if you don’t need some specific data from URLRequest and URLResponse or define your own with interface ILogger

2. API for endpoint

Define endpoint API enum

enum UserRestAPI {

    case index
    case read(id: Int)
    case create
    case update
    case delete(id: Int)

Extend the enum with interface IRequest

field type
route String
method RequestMethod

extension UserRestAPI: IRequest {
    var route: String {
        switch self {
            case .index: return "/user"
            case .read(let id): return "/user/\(id)"
            case .create: return "/user"
            case .update: return "/user"
            case .delete(let id): return "/user/\(id)"
    var method: RequestMethod {
        switch self {
            case .index: return .get
            case .read(_): return .get
            case .create: return .post
            case .update: return .put
            case .delete(_): return .delete

The example implemetation is here UserRestAPI.swift UserRestAPI.swift

3. Create network sevice

    let network = NetworkService(environment: Environment.development)

execute – There’s only one API method to do requests that gets what type of request you whant to do from the endpont configuration GET, POST, PUT, DELETE

There are four methods are available currently GET, POST, PUT, DELETE


Pass a [String: CustomStringConvertible] dictionary to the parameter that avalable for GET, POST, PUT requests. It’s an optinal parameter.


   let cfg = 1)
   let publisher: Output = network.execute(with: cfg, ["token" : 65678])


    let cfg = UserRestAPI.create
    let data = Model(id: 11, name: "Igor")

    let publisher: Output = network.execute(body: user, with: cfg)


    let cfg = UserRestAPI.update
    let data = Model(id: 11, name: "Igor")    

    let publisher: Output = network.execute(body: user, with: cfg)


    let cfg = UserRestAPI.delete(id: 11)
    let publisher: Output = network.execute(with: cfg)

Package installation

In Xcode – Select Xcode>File> Swift Packages>Add Package Dependency... and add

Try it in the real environment

Simple server instalation (NodeJS Express)

To try it in the real environment. I suggest installing the basic NodeJS Express boilerplate. Take a look on the video snippet how easy it is to get it through Webstorm that is accessible for free for a trial period.

Server instalation (NodeJS Express)

Real SwiftUI example


  • You need to have Xcode 13 installed in order to have access to Documentation Compiler (DocC)
  • Go to Product > Build Documentation or โŒƒโ‡งโŒ˜ D


View Github