diff --git a/bun.lockb b/bun.lockb index dc82173..8243ed5 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 777cd5e..3609a69 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thilawyn/thilalib", - "version": "0.1.8", + "version": "0.1.9", "type": "module", "files": [ "./dist" @@ -17,6 +17,16 @@ "default": "./dist/index.cjs" } }, + "./Layers": { + "import": { + "types": "./dist/Layers/index.d.ts", + "default": "./dist/Layers/index.js" + }, + "require": { + "types": "./dist/Layers/index.d.cts", + "default": "./dist/Layers/index.cjs" + } + }, "./Types": { "import": { "types": "./dist/Types/index.d.ts", @@ -56,20 +66,20 @@ "clean:node": "rm -rf node_modules" }, "dependencies": { - "remeda": "^2.6.0", - "type-fest": "^4.23.0" + "remeda": "^2.11.0", + "type-fest": "^4.25.0" }, "devDependencies": { - "bun-types": "^1.1.21", - "npm-check-updates": "^16.14.20", + "@effect/schema": "^0.71.0", + "@types/jsonwebtoken": "^9.0.6", + "bun-types": "^1.1.24", + "effect": "^3.6.4", + "jsonwebtoken": "^9.0.2", + "mobx": "^6.13.1", + "npm-check-updates": "^17.0.6", "npm-sort": "^0.0.4", - "tsup": "^8.2.3", - "tsx": "^4.16.2", + "tsup": "^8.2.4", + "tsx": "^4.17.0", "typescript": "^5.5.4" - }, - "optionalDependencies": { - "@effect/schema": "*", - "effect": "*", - "mobx": "*" } } diff --git a/src/Layers/JSONWebToken.ts b/src/Layers/JSONWebToken.ts new file mode 100644 index 0000000..0ee5d14 --- /dev/null +++ b/src/Layers/JSONWebToken.ts @@ -0,0 +1,46 @@ +import { Context, Effect, Layer } from "effect" +import jwt from "jsonwebtoken" + + +export class JSONWebToken extends Context.Tag("JSONWebToken") Effect.Effect< + string, + Error, + never + >, + + verify: ( + token: string, + secretOrPublicKey: jwt.Secret, + options: jwt.VerifyOptions, + ) => Effect.Effect< + string | jwt.Jwt | jwt.JwtPayload, + jwt.VerifyErrors | Error, + never + >, +}>() {} + + +export const JSONWebTokenLive = Layer.succeed(JSONWebToken, { + sign: (payload, secretOrPrivateKey, options) => Effect.async(resume => + jwt.sign(payload, secretOrPrivateKey, options, (err, token) => { + resume(token + ? Effect.succeed(token) + : Effect.fail(err || new Error("Unknown error")) + ) + }) + ), + + verify: (token, secretOrPublicKey, options) => Effect.async(resume => + jwt.verify(token, secretOrPublicKey, options, (err, decoded) => { + resume(decoded + ? Effect.succeed(decoded) + : Effect.fail(err || new Error("Unknown error")) + ) + }) + ), +}) diff --git a/src/Layers/index.ts b/src/Layers/index.ts new file mode 100644 index 0000000..77d7071 --- /dev/null +++ b/src/Layers/index.ts @@ -0,0 +1,5 @@ +/** + * A wrapper around the jsonwebtoken library for Effect + * Requires `effect`, `jsonwebtoken` and `@types/jsonwebtoken` to be installed + */ +export * as JSONWebToken from "./JSONWebToken" diff --git a/src/Schema/DateTime.ts b/src/Schema/DateTime.ts new file mode 100644 index 0000000..8964f2c --- /dev/null +++ b/src/Schema/DateTime.ts @@ -0,0 +1,52 @@ +import { ParseResult, Schema } from "@effect/schema" +import { DateTime } from "effect" + + +export const DateTimeUtcFromDate = Schema.transformOrFail( + Schema.DateFromSelf, + Schema.DateTimeUtcFromSelf, + + { + strict: true, + + decode: (date, _, ast) => ParseResult.try({ + try: () => DateTime.unsafeMake(date), + + catch: e => new ParseResult.Type( + ast, + date, + e instanceof Error ? e.message : undefined, + ), + }), + + encode: dateTimeUtc => ParseResult.succeed( + DateTime.toDateUtc(dateTimeUtc) + ), + }, +) + +export const DateTimeZonedFromDate = (options: { + readonly timeZone: number | string | DateTime.TimeZone + readonly adjustForTimeZone?: boolean | undefined +}) => Schema.transformOrFail( + Schema.DateFromSelf, + Schema.DateTimeZonedFromSelf, + + { + strict: true, + + decode: (date, _, ast) => ParseResult.try({ + try: () => DateTime.unsafeMakeZoned(date, options), + + catch: e => new ParseResult.Type( + ast, + date, + e instanceof Error ? e.message : undefined, + ), + }), + + encode: dateTime => ParseResult.succeed( + DateTime.toDate(dateTime) + ), + }, +) diff --git a/src/Schema/index.ts b/src/Schema/index.ts index e3fa60a..8e5942f 100644 --- a/src/Schema/index.ts +++ b/src/Schema/index.ts @@ -1,4 +1,5 @@ export * from "./Class" +export * from "./DateTime" export * from "./Jsonifiable" export * from "./Kind" export * as MobX from "./MobX" diff --git a/src/index.ts b/src/index.ts index 05f2af0..1129b8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ +export * as Layers from "./Layers" export * as Schema from "./Schema" export * as Types from "./Types"