-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2660b9f
commit f1b8e51
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# D1Kit | ||
|
||
A Swift client for Cloudflare's D1 service. | ||
|
||
## Example | ||
|
||
```swift | ||
import D1Kit | ||
import D1KitFoundation | ||
|
||
let client = D1Client( | ||
httpClient: .urlSession(.shared), | ||
accountID: "YOUR_ACCOUNT_ID", | ||
apiToken: "YOUR_API_TOKEN" | ||
) | ||
let db = D1Database(client: client, databaseID: "YOUR_DATABASE_ID") | ||
|
||
struct Row: Decodable { | ||
var id: Int | ||
var name: String | ||
} | ||
|
||
let rows = try await db.query( | ||
"SELECT * FROM users WHERE name = \(bind: "Bob")", | ||
as: Row.self | ||
) | ||
``` | ||
|
||
## Installation | ||
|
||
D1Kit supports the Swift Package Manager. | ||
To integrate it into your project, follow the common Swift Package Manager setup process. | ||
|
||
## Usage with URLSession | ||
|
||
To use `D1Kit` with `URLSession`, add the `D1KitFoundation` dependency and pass a `URLSession` instance as the `httpClient` when initializing `D1Client`. | ||
|
||
## Usage with AsyncHTTPClient | ||
|
||
To use `D1Kit` with `AsyncHTTPClient`, you will need to conform `AsyncHTTPClient.HTTPClient` to `D1Kit.HTTPClientProtocol`. Below is an example code snippet: | ||
|
||
```swift | ||
import AsyncHTTPClient | ||
import D1Kit | ||
import Foundation | ||
import HTTPTypes | ||
|
||
extension HTTPClient: D1Kit.HTTPClientProtocol { | ||
public func execute(_ request: HTTPRequest, body: Data?) async throws -> (Data, HTTPResponse) { | ||
guard let url = request.url else { | ||
throw HTTPClientError.invalidURL | ||
} | ||
var req = HTTPClientRequest(url: url.absoluteString) | ||
req.headers = .init(request.headerFields.map { ($0.name.rawName, $0.value) }) | ||
req.method = .init(rawValue: request.method.rawValue) | ||
req.body = body.map { .bytes($0) } | ||
|
||
let res = try await execute(req, timeout: .seconds(15)) | ||
let resData = Data(buffer: try await res.body.collect(upTo: .max)) | ||
|
||
var response = HTTPResponse(status: .init(code: numericCast(res.status.code))) | ||
response.headerFields = HTTPFields() | ||
|
||
for header in res.headers { | ||
if let name = HTTPField.Name(header.name) { | ||
response.headerFields[name] = header.value | ||
} | ||
} | ||
|
||
return (resData, response) | ||
} | ||
} | ||
``` |