Skip to content

SwiftMesh

Swift砖块系列:基于Alamofire5的Combine封装,更加快捷的发起网络请求,支持SwiftUI

Install

$git clone https://github.com/zjinhu/SwiftMesh

Install command taken from the project's README or registry entry. Always review code before running it.

SwiftMesh is a secondary encapsulation based on Alamofire and Codable, uses Combine and Swift Concurrency, supports SwiftUI and UIKit, removes closure callbacks, is more concise, faster, and more convenient to use.

The design patterns involved are: adapter, singleton, builder, and property wrapper.

Features

  • Async/Await API — All request methods use async throws, eliminating callback hell
  • Fluent/Chainable Configuration — Builder pattern with methods like .setRequestMethod(.get).setUrlHost("...")
  • JSON Key Path Parsing — Extract nested JSON values at dot-separated paths (e.g., "data.yesterday")
  • File Upload — Supports file URL, Data, InputStream, and multipart form uploads
  • File Download — Supports standard and resumable downloads
  • Global Defaults — Set default headers, parameters, and URL host once; they merge with per-request overrides
  • Resilient Codable — Property wrappers (@Default, @IgnoreError, @ConvertToString, etc.) for handling inconsistent API responses
  • Built-in Retry Policy — Configurable retry with linear backoff
  • Network Logging — Built-in logger that outputs cURL commands, status codes, timing, and pretty-printed JSON
  • Combine Integration — Works naturally with ObservableObject and @Published for reactive UI updates
  • SwiftUI + UIKit Support — Designed for mixed development environments

Quick Start

1. Basic GET Request

let result = try await Mesh()
    .setRequestMethod(.get)
    .setUrlHost("https://api.example.com")
    .setUrlPath("/weather/city/101030100")
    .request(of: Weather.self)

2. GET with JSON Key Path

Extract only a nested portion of the JSON:

// JSON: { "code": 200, "data": { "yesterday": { "temp": 25 } } }
let yesterday = try await Mesh()
    .setRequestMethod(.get)
    .setUrlHost("https://api.example.com")
    .setUrlPath("/weather")
    .request(of: Forecast.self, modelKeyPath: "data.yesterday")

3. POST with Parameters

let result = try await Mesh()
    .setRequestMethod(.post)
    .setUrlHost("https://api.example.com")
    .setUrlPath("/login")
    .setParameters(["username": "admin", "password": "123456"])
    .request(of: LoginResult.self)

4. File Download

let fileURL = try await Mesh()
    .setRequestMethod(.get)
    .setUrlHost("https://example.com")
    .setUrlPath("/files/document.pdf")
    .setDestination { _, _ in
        let dest = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            .appendingPathComponent("document.pdf")
        return (dest, [.removePreviousFile, .createIntermediateDirectories])
    }
    .download()

5. File Upload (Multipart)

let result = try await Mesh()
    .setRequestMethod(.post)
    .setUrlHost("https://api.example.com")
    .setUrlPath("/upload/image")
    .setUploadType(.multipart)
    .setAddformData(name: "file",
                    fileName: "photo.jpg",
                    fileData: imageData,
                    mimeType: "image/jpeg")
    .upload(of: UploadResult.self)

Usage

Swift+UIKit:

import UIKit
import Combine
import SwiftMesh

Similar plugs

Turn any codebase, with its docs, SQL schemas, configs, and PDFs, into a queryable knowledge graph. A /graphify skill for Claude Code, Cursor, Codex, and Gemini CLI: local…

Why use many token when few token do trick. Viral skill + proxy for coding agents that cuts 65% of tokens by talking like a caveman.