Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lekko/react-sdk",
"version": "0.7.2-beta-1",
"version": "0.7.2-beta-2",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
Expand All @@ -14,6 +14,8 @@
"@lekko/js-sdk": "^0.1.0-beta-5",
"@tanstack/react-query": "^5.8.1",
"dotenv": "^16.3.1",
"fp-ts": "^2.16.2",
"io-ts": "^2.2.21",
"lz-string": "^1.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
25 changes: 25 additions & 0 deletions src/hooks/useLekkoConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { upsertHistoryItem } from "../utils/overrides"
import { LekkoSettingsContext } from "../providers/lekkoSettingsProvider"
import { getCombinedContext } from "../utils/context"
import { type ClientContext } from "@lekko/js-sdk"
import type * as t from "io-ts"

export function useLekkoConfig<E extends EvaluationType>(
config: LekkoConfig<E>,
Expand Down Expand Up @@ -140,3 +141,27 @@ export function useProtoConfig(
options,
)
}

export function useTypedJSONConfig<A>(
config: UntypedLekkoConfig,
type: t.Type<A>,
options?: ConfigOptions,
) {
const result = useLekkoConfig(
{ ...config, evaluationType: EvaluationType.JSON },
options,
)
const evaluation = type.decode(result)

if (evaluation._tag === "Left") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, either monad detected :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can handle right case first as it's shorter

const errorMessage = evaluation.left
.map((validationError) => validationError.message)
.join("\n")

const error = new Error(errorMessage)

throw error
}

return evaluation.right
}
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,8 @@ __metadata:
eslint-plugin-promise: ^6.0.0
eslint-plugin-react: latest
eslint-plugin-react-hooks: ^4.6.0
fp-ts: ^2.16.2
io-ts: ^2.2.21
jest: ^29.7.0
jest-environment-jsdom: ^29.7.0
lz-string: ^1.5.0
Expand Down Expand Up @@ -3228,6 +3230,13 @@ __metadata:
languageName: node
linkType: hard

"fp-ts@npm:^2.16.2":
version: 2.16.2
resolution: "fp-ts@npm:2.16.2"
checksum: 5c2e3f096ba4be5646bda38a43e3e8ca3bd7693fefb26e6c4d4c451efbc9a227f7f7cad562b087cb134d4e41f4ae615602f0b17890e9e40a9e83f2f8822da666
languageName: node
linkType: hard

"fs-minipass@npm:^2.0.0":
version: 2.1.0
resolution: "fs-minipass@npm:2.1.0"
Expand Down Expand Up @@ -3694,6 +3703,15 @@ __metadata:
languageName: node
linkType: hard

"io-ts@npm:^2.2.21":
version: 2.2.21
resolution: "io-ts@npm:2.2.21"
peerDependencies:
fp-ts: ^2.5.0
checksum: c6ae5237e313f7428c874fa5667b3656adaa5ec29f7f34194ad8ea8894b525c89322a5b74ca560e7cd66f8334b0b48cae6c4dc517c662de72da86110140646d4
languageName: node
linkType: hard

"ip-address@npm:^9.0.5":
version: 9.0.5
resolution: "ip-address@npm:9.0.5"
Expand Down