swift-http-types-htmx

0.3.0

swift-http-types extensions for htmx
alephao/swift-http-types-htmx

What's New

0.3.0

2024-09-16T22:58:01Z
  • Using structs instead of enums for value types HxPushUrl, HxReplaceUrl and HxSwap
  • Change license to MIT (previous license, BSD Zero is not recognized by GH and SPI)

Full Changelog: 0.2.0...0.3.0

swift-http-types-htmx

CI codecov

HTMX extensions for apple/swift-http-types.

The package provides extensions for:

  • HTTPField.Name initializers, e.g.: HTTPField.Name.hxLocation = 'HX-Location'
  • HTTPField initializers e.g.: HTTPField.hxLocation('/path') = HX-Location: /path
  • HTTPFields getters e.g.: HTTPFields.hxRequest: Bool, HTTPFields.hxLocation: String?

It also provides HTMX documentation in-code:

Usage

import HTTPTypes 
import HTTPTypesHtmx

// Creating HTTPFields

let headersViaArray = HTTPFields([
  .hxRetarget("#myDiv"),
  .hxReswap(.outerHTML),
])

let headersViaDictionary: HTTPFields = [
  .hxRetarget: "#myDiv",
  .hxReswap: HxSwap.outerHTML,
] 

// Getting values from HTTPFields
let exampleRequestHeaders = HTTPFields([
  .hxBoosted(),
  .hxRequest(),
  .hxTarget(id: "myDiv")
])

requestHeaders.hxBoosted // true
requestHeaders.hxRequest // true
requestHeaders.hxTarget  // "myDiv"

Examples

There is an example project of a server using the Hummingbird Framework with a few exampels on how to use HTMX response headers to drive the application.

There are also tests for all handlers, demonstrating how this library can be used to mock HTMX requests for testing purposes.

Installation

Add the dependency to Package.swift

dependencies: [
  .package(url: "https://github.com/alephao/swift-http-types-htmx.git", from: "0.2.0")
],

Add the products you want to use to the target dependencies

.target(
  name: "MyTarget",
  dependencies: [
    .product(name: "HTTPTypesHtmx", package: "swift-htmx"),
  ]
)

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Sun May 11 2025 08:14:02 GMT-0900 (Hawaii-Aleutian Daylight Time)