import * as Schema from '@effect/schema/Schema';
import * as Effect from 'effect/Effect';
import {v4 as uuidv4} from 'uuid';
import {UUIDGenerationError} from '../errors.server';
const UuidBrand = Symbol.for('UuidBrand');
export const Uuid = Schema.string.pipe(Schema.brand(UuidBrand)); // todo: more validations here
export type Uuid = Schema.Schema.To<typeof Uuid>;
export function parse(value: unknown) {
return Schema.parseEither(Uuid)(value);
}
export function generate() {
return Effect.gen(function* (_) {
const id = yield* _(Effect.sync(() => uuidv4()));
return yield* _(parse(id));
// no reason for this to fail the parsing
}).pipe(Effect.catchAll(() => Effect.fail(new UUIDGenerationError())));
}
import * as Schema from '@effect/schema/Schema';
import * as Effect from 'effect/Effect';
import {v4 as uuidv4} from 'uuid';
import {UUIDGenerationError} from '../errors.server';
const UuidBrand = Symbol.for('UuidBrand');
export const Uuid = Schema.string.pipe(Schema.brand(UuidBrand)); // todo: more validations here
export type Uuid = Schema.Schema.To<typeof Uuid>;
export function parse(value: unknown) {
return Schema.parseEither(Uuid)(value);
}
export function generate() {
return Effect.gen(function* (_) {
const id = yield* _(Effect.sync(() => uuidv4()));
return yield* _(parse(id));
// no reason for this to fail the parsing
}).pipe(Effect.catchAll(() => Effect.fail(new UUIDGenerationError())));
}