Skip to content

alephao/swift-http-types-htmx

Repository files navigation

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.3.0")
],

Add the products you want to use to the target dependencies

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