0.1.9 (#10)
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: #10
This commit was merged in pull request #10.
This commit is contained in:
46
src/Layers/JSONWebToken.ts
Normal file
46
src/Layers/JSONWebToken.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Context, Effect, Layer } from "effect"
|
||||
import jwt from "jsonwebtoken"
|
||||
|
||||
|
||||
export class JSONWebToken extends Context.Tag("JSONWebToken")<JSONWebToken, {
|
||||
sign: (
|
||||
payload: string | object | Buffer,
|
||||
secretOrPrivateKey: jwt.Secret,
|
||||
options: jwt.SignOptions,
|
||||
) => 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"))
|
||||
)
|
||||
})
|
||||
),
|
||||
})
|
||||
5
src/Layers/index.ts
Normal file
5
src/Layers/index.ts
Normal file
@@ -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"
|
||||
52
src/Schema/DateTime.ts
Normal file
52
src/Schema/DateTime.ts
Normal file
@@ -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)
|
||||
),
|
||||
},
|
||||
)
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from "./Class"
|
||||
export * from "./DateTime"
|
||||
export * from "./Jsonifiable"
|
||||
export * from "./Kind"
|
||||
export * as MobX from "./MobX"
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
export * as Layers from "./Layers"
|
||||
export * as Schema from "./Schema"
|
||||
export * as Types from "./Types"
|
||||
|
||||
Reference in New Issue
Block a user