@effect/platform 0.55.0 → 0.55.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +118 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -310,6 +310,95 @@ const readFileString = Effect.gen(function* (_) {
|
|
|
310
310
|
NodeRuntime.runMain(readFileString.pipe(Effect.provide(NodeFileSystem.layer)))
|
|
311
311
|
```
|
|
312
312
|
|
|
313
|
+
# KeyValueStore
|
|
314
|
+
|
|
315
|
+
## Overview
|
|
316
|
+
|
|
317
|
+
The `KeyValueStore` module provides a robust and effectful interface for managing key-value pairs. It supports asynchronous operations, ensuring data integrity and consistency, and includes built-in implementations for in-memory, file system-based, and schema-validated stores.
|
|
318
|
+
|
|
319
|
+
## Basic Usage
|
|
320
|
+
|
|
321
|
+
The `KeyValueStore` interface includes the following operations:
|
|
322
|
+
|
|
323
|
+
- **get**: Retrieve a value by key.
|
|
324
|
+
- **set**: Store a key-value pair.
|
|
325
|
+
- **remove**: Delete a key-value pair.
|
|
326
|
+
- **clear**: Remove all key-value pairs.
|
|
327
|
+
- **size**: Get the number of stored pairs.
|
|
328
|
+
- **modify**: Atomically modify a value.
|
|
329
|
+
- **has**: Check if a key exists.
|
|
330
|
+
- **isEmpty**: Check if the store is empty.
|
|
331
|
+
|
|
332
|
+
**Example**
|
|
333
|
+
|
|
334
|
+
```ts
|
|
335
|
+
import { KeyValueStore, layerMemory } from "@effect/platform/KeyValueStore"
|
|
336
|
+
import { Effect } from "effect"
|
|
337
|
+
|
|
338
|
+
const program = Effect.gen(function* () {
|
|
339
|
+
const store = yield* KeyValueStore
|
|
340
|
+
console.log(yield* store.size) // Outputs: 0
|
|
341
|
+
|
|
342
|
+
yield* store.set("key", "value")
|
|
343
|
+
console.log(yield* store.size) // Outputs: 1
|
|
344
|
+
|
|
345
|
+
const value = yield* store.get("key")
|
|
346
|
+
console.log(value) // Outputs: { _id: 'Option', _tag: 'Some', value: 'value' }
|
|
347
|
+
|
|
348
|
+
yield* store.remove("key")
|
|
349
|
+
console.log(yield* store.size) // Outputs: 0
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
Effect.runPromise(program.pipe(Effect.provide(layerMemory)))
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Built-in Implementations
|
|
356
|
+
|
|
357
|
+
The module provides several built-in implementations to suit different needs:
|
|
358
|
+
|
|
359
|
+
- **In-Memory Store**: `layerMemory` provides a simple, in-memory key-value store, ideal for lightweight or testing scenarios.
|
|
360
|
+
- **File System Store**: `layerFileSystem` offers a file-based store for persistent storage needs.
|
|
361
|
+
- **Schema Store**: `layerSchema` enables schema-based validation for stored values, ensuring data integrity and type safety.
|
|
362
|
+
|
|
363
|
+
## Schema Store
|
|
364
|
+
|
|
365
|
+
The `SchemaStore` implementation allows you to validate and parse values according to a defined schema. This ensures that all data stored in the key-value store adheres to the specified structure, enhancing data integrity and type safety.
|
|
366
|
+
|
|
367
|
+
**Example**
|
|
368
|
+
|
|
369
|
+
```ts
|
|
370
|
+
import { KeyValueStore, layerMemory } from "@effect/platform/KeyValueStore"
|
|
371
|
+
import { Schema } from "@effect/schema"
|
|
372
|
+
import { Effect } from "effect"
|
|
373
|
+
|
|
374
|
+
// Define a schema for the values
|
|
375
|
+
const Person = Schema.Struct({
|
|
376
|
+
name: Schema.String,
|
|
377
|
+
age: Schema.Number
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
const program = Effect.gen(function* () {
|
|
381
|
+
const store = (yield* KeyValueStore).forSchema(Person)
|
|
382
|
+
|
|
383
|
+
// Create a value that adheres to the schema
|
|
384
|
+
const value = { name: "Alice", age: 30 }
|
|
385
|
+
yield* store.set("user1", value)
|
|
386
|
+
console.log(yield* store.size) // Outputs: 1
|
|
387
|
+
|
|
388
|
+
// Retrieve and validate the value
|
|
389
|
+
const retrievedValue = yield* store.get("user1")
|
|
390
|
+
console.log(retrievedValue) // Outputs: { _id: 'Option', _tag: 'Some', value: { name: 'Alice', age: 30 } }
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
Effect.runPromise(program.pipe(Effect.provide(layerMemory)))
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
In this example:
|
|
397
|
+
|
|
398
|
+
- **Person**: Defines the structure for the values stored in the key-value store.
|
|
399
|
+
- **store.set**: Stores a value adhering to `Person`.
|
|
400
|
+
- **store.get**: Retrieves and validates the stored value against `Person`.
|
|
401
|
+
|
|
313
402
|
# HTTP Client
|
|
314
403
|
|
|
315
404
|
## Retrieving Data (GET)
|
|
@@ -1682,3 +1771,32 @@ curl -i http://localhost:3000
|
|
|
1682
1771
|
# Request with the valid cookie
|
|
1683
1772
|
curl -i http://localhost:3000 --cookie "test=myvalue"
|
|
1684
1773
|
```
|
|
1774
|
+
|
|
1775
|
+
## ServerRequest
|
|
1776
|
+
|
|
1777
|
+
### How do I get the raw request?
|
|
1778
|
+
|
|
1779
|
+
The native request object depends on the platform you are using, and it is not directly modeled in `@effect/platform`. Instead, you need to refer to the specific platform package you are working with, such as `@effect/platform-node` or `@effect/platform-bun`.
|
|
1780
|
+
|
|
1781
|
+
Here is an example using Node.js:
|
|
1782
|
+
|
|
1783
|
+
```ts
|
|
1784
|
+
import { HttpServer } from "@effect/platform"
|
|
1785
|
+
import { NodeHttpServer } from "@effect/platform-node"
|
|
1786
|
+
import { Effect } from "effect"
|
|
1787
|
+
import { listen } from "./listen.js"
|
|
1788
|
+
|
|
1789
|
+
const router = HttpServer.router.empty.pipe(
|
|
1790
|
+
HttpServer.router.get(
|
|
1791
|
+
"/",
|
|
1792
|
+
Effect.gen(function* () {
|
|
1793
|
+
const req = yield* HttpServer.request.ServerRequest
|
|
1794
|
+
const raw = NodeHttpServer.request.toIncomingMessage(req)
|
|
1795
|
+
console.log(raw)
|
|
1796
|
+
return HttpServer.response.empty()
|
|
1797
|
+
})
|
|
1798
|
+
)
|
|
1799
|
+
)
|
|
1800
|
+
|
|
1801
|
+
listen(HttpServer.server.serve(router), 3000)
|
|
1802
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect/platform",
|
|
3
|
-
"version": "0.55.
|
|
3
|
+
"version": "0.55.1",
|
|
4
4
|
"description": "Unified interfaces for common platform-specific services",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"path-browserify": "^1.0.1"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|
|
18
|
-
"@effect/schema": "^0.67.
|
|
18
|
+
"@effect/schema": "^0.67.14",
|
|
19
19
|
"effect": "^3.2.5"
|
|
20
20
|
},
|
|
21
21
|
"publishConfig": {
|