-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84a81a5
commit ea3529f
Showing
7 changed files
with
188 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import { Schema } from '@effect/schema' | ||
import { Brand } from 'effect' | ||
import { isOrcid } from 'orcid-id-ts' | ||
|
||
export type OrcidId = Brand.Branded<string, 'OrcidId'> | ||
|
||
export const OrcidId = Brand.refined<OrcidId>(isOrcid, s => Brand.error(`Expected ${s} to be an ORCID iD`)) | ||
|
||
export const OrcidIdSchema = Schema.string.pipe(Schema.fromBrand(OrcidId)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Schema } from '@effect/schema' | ||
import { Context, Data, Effect, type ReadonlyArray, Stream } from 'effect' | ||
import type IoRedis from 'ioredis' | ||
|
||
export type Redis = IoRedis.Redis | ||
|
||
export const Redis = Context.Tag<Redis>('IoRedis/Redis') | ||
|
||
export class RedisError extends Data.TaggedError('RedisError')<{ | ||
readonly error: unknown | ||
}> {} | ||
|
||
export const scanStream = ( | ||
options: Parameters<Redis['scanStream']>[0], | ||
): Stream.Stream<Redis, RedisError, ReadonlyArray<string>> => | ||
Stream.unwrap( | ||
Effect.gen(function* (_) { | ||
const redis = yield* _(Redis) | ||
|
||
return Stream.fromAsyncIterable(redis.scanStream(options), error => new RedisError({ error })) | ||
}), | ||
).pipe(Stream.map(Schema.decodeSync(Schema.array(Schema.string)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
import { Stream } from 'effect' | ||
import { type ParseResult, Schema } from '@effect/schema' | ||
import { ReadonlyArray, type Scope, Stream, String, identity } from 'effect' | ||
import * as OrcidId from './OrcidId.js' | ||
import * as Redis from './Redis.js' | ||
|
||
export const getUsers = Stream.fromIterable([OrcidId.OrcidId('0000-0001-8778-8651')]) | ||
export const getUsers: Stream.Stream< | ||
Redis.Redis | Scope.Scope, | ||
Redis.RedisError | ParseResult.ParseError, | ||
OrcidId.OrcidId | ||
> = Redis.scanStream({ | ||
match: 'orcid-token:*', | ||
}).pipe( | ||
Stream.mapConcat(identity), | ||
Stream.map(String.split(':')), | ||
Stream.map(ReadonlyArray.lastNonEmpty), | ||
Stream.flatMap(Schema.decodeEither(OrcidId.OrcidIdSchema)), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
import { Effect } from 'effect' | ||
import { Effect, Layer } from 'effect' | ||
import IoRedis from 'ioredis' | ||
import { program } from './Program.js' | ||
import * as Redis from './Redis.js' | ||
|
||
await Effect.runPromise(program) | ||
const RedisLive = Layer.effect( | ||
Redis.Redis, | ||
Effect.gen(function* (_) { | ||
const redis = new IoRedis.Redis() | ||
|
||
yield* _(Effect.addFinalizer(() => Effect.sync(() => redis.disconnect()))) | ||
|
||
return redis | ||
}), | ||
) | ||
|
||
const runnable = Effect.provide(program, RedisLive).pipe(Effect.scoped) | ||
|
||
await Effect.runPromise(runnable) |