Swiftpack.co -  Package - swifweb/web
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
swifweb/web
🧱 Write your website in pure Swift with power of webassembly. DOM, CSS and all the WebAPIs are available out of the box.
.package(url: "https://github.com/swifweb/web.git", from: "1.0.0-beta.1.4.0")

SwifWeb

MIT License Swift 5.3 Swift.Stream

This library provides DOM, CSS and all possible web APIs beautifully wrapped into Swift ❤️

With it you will easily build your awesome reactive web app in beloved Swift ❤️

Installation

Go to webber repository and install it.

Usage

Clone spa or pwa template

Go to the project folder and open Package.swift to explore the code.

Then execute webber serve or webber serve -t pwa -s Service for the pwa app to debug your project in the browser.

Then take a look at the hidden .webber folder, you will find the entrypoint/dev folder where webber just generated entry files, you could edit these files if swift is not enough, e.g. to make you custom loading bar.

To release the project just call webber release or webber release -t pwa -s Service for pwa, and then grab production files from .webber/release folder.

Optimization

Lighthouse gives it 100 points and search engine crawlers works well with it too!

Lighthouse

// TODO: to be continued soon

GitHub

link
Stars: 11
Last commit: 3 weeks ago

Release Notes

🚀 Implement StreamsAPI, FetchAPI, XMLHttpRequest, WebSocketAPI
3 weeks ago

Fetch

import FetchAPI

Fetch("https://jsonplaceholder.typicode.com/todos/1") {
    switch $0 {
    case .failure:
        break
    case .success(let response):
        print("response.code: \(response.status)")
        print("response.statusText: \(response.statusText)")
        print("response.ok: \(response.ok)")
        print("response.redirected: \(response.redirected)")
        print("response.headers: \(response.headers.dictionary)")
        struct Todo: Decodable {
            let id, userId: Int
            let title: String
            let completed: Bool
        }
        response.json(as: Todo.self) {
            switch $0 {
            case .failure(let error):
                break
            case .success(let todo):
                print("decoded todo: \(todo)")
            }
        }
    }
}

XMLHttpRequest

import XMLHttpRequest

XMLHttpRequest()
    .open(method: "GET", url: "https://jsonplaceholder.typicode.com/todos/1")
    .onAbort {
        print("XHR onAbort")
    }.onLoad {
        print("XHR onLoad")
    }.onError {
        print("XHR onError")
    }.onTimeout {
        print("XHR onTimeout")
    }.onProgress{ progress in
        print("XHR onProgress")
    }.onLoadEnd {
        print("XHR onLoadEnd")
    }.onLoadStart {
        print("XHR onLoadStart")
    }.onReadyStateChange { readyState in
        print("XHR onReadyStateChange")
    }
    .send()

WebSocket

import WebSocket

let webSocket = WebSocket("wss://echo.websocket.org").onOpen {
    print("ws connected")
}.onClose { (closeEvent: CloseEvent) in
    print("ws disconnected code: \(closeEvent.code) reason: \(closeEvent.reason)")
}.onError {
    print("ws error")
}.onMessage { message in
    print("ws message: \(message)")
    switch message.data {
    case .arrayBuffer(let arrayBuffer): break
    case .blob(let blob): break
    case .text(let text): break
    case .unknown(let jsValue): break
    }
}
Dispatch.asyncAfter(2) {
    // send as simple string
    webSocket.send("Hello from SwifWeb")
    // send as Blob
    webSocket.send(Blob("Hello from SwifWeb"))
}

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco