-
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.
Allow the page to be filtered by OpenAlex Domain
Refs #13
- Loading branch information
1 parent
91da2b0
commit 77634fb
Showing
2 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
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,35 @@ | ||
import { HttpClient, Terminal } from '@effect/platform' | ||
import { NodeTerminal } from '@effect/platform-node' | ||
import { Schema } from '@effect/schema' | ||
import { Effect, Record } from 'effect' | ||
import { DomainIdFromUrlSchema, DomainIdSchema } from '../lib/OpenAlex.js' | ||
import { UrlFromStringSchema } from '../lib/Url.js' | ||
|
||
const Domains = Schema.Struct({ | ||
results: Schema.Array( | ||
Schema.Struct({ | ||
id: Schema.compose(UrlFromStringSchema, DomainIdFromUrlSchema), | ||
display_name: Schema.String, | ||
}), | ||
), | ||
}) | ||
|
||
const DomainNames = Schema.Record(DomainIdSchema, Schema.String) | ||
|
||
const program = Effect.gen(function* () { | ||
const terminal = yield* Terminal.Terminal | ||
|
||
const request = HttpClient.request.get('https://api.openalex.org/domains?per-page=200') | ||
|
||
const data = yield* HttpClient.client | ||
.fetchOk(request) | ||
.pipe(Effect.andThen(HttpClient.response.schemaBodyJson(Domains)), Effect.scoped) | ||
|
||
const transformedData = Record.fromIterableWith(data.results, domain => [domain.id, domain.display_name]) | ||
|
||
const encoded = yield* Schema.encode(Schema.parseJson(DomainNames))(transformedData) | ||
|
||
yield* terminal.display(encoded) | ||
}) | ||
|
||
await Effect.runPromise(program.pipe(Effect.provide(NodeTerminal.layer))) |
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