Skip to content

Commit

Permalink
Make OpenAlex domain information available
Browse files Browse the repository at this point in the history
Refs #13
  • Loading branch information
thewilkybarkid committed Jun 28, 2024
1 parent 1cf7c94 commit 91da2b0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/data/openalex-fields.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import { HttpClient, Terminal } from '@effect/platform'
import { NodeTerminal } from '@effect/platform-node'
import { Schema } from '@effect/schema'
import { Effect, Record } from 'effect'
import { FieldIdFromUrlSchema, FieldIdSchema } from '../lib/OpenAlex.js'
import { DomainIdFromUrlSchema, DomainIdSchema, FieldIdFromUrlSchema, FieldIdSchema } from '../lib/OpenAlex.js'
import { UrlFromStringSchema } from '../lib/Url.js'

const Fields = Schema.Struct({
results: Schema.Array(
Schema.Struct({
id: Schema.compose(UrlFromStringSchema, FieldIdFromUrlSchema),
display_name: Schema.String,
domain: Schema.Struct({
id: Schema.compose(UrlFromStringSchema, DomainIdFromUrlSchema),
}),
}),
),
})

const FieldNames = Schema.Record(FieldIdSchema, Schema.String)
const FieldNames = Schema.Record(FieldIdSchema, Schema.Struct({ name: Schema.String, domain: DomainIdSchema }))

const program = Effect.gen(function* () {
const terminal = yield* Terminal.Terminal
Expand All @@ -25,7 +28,10 @@ const program = Effect.gen(function* () {
.fetchOk(request)
.pipe(Effect.andThen(HttpClient.response.schemaBodyJson(Fields)), Effect.scoped)

const transformedData = Record.fromIterableWith(data.results, field => [field.id, field.display_name])
const transformedData = Record.fromIterableWith(data.results, field => [
field.id,
{ name: field.display_name, domain: field.domain.id },
])

const encoded = yield* Schema.encode(Schema.parseJson(FieldNames))(transformedData)

Expand Down
3 changes: 2 additions & 1 deletion src/data/requests.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { NodeTerminal } from '@effect/platform-node'
import { Schema } from '@effect/schema'
import { Effect } from 'effect'
import * as LanguageCode from '../lib/LanguageCode.js'
import { FieldIdSchema } from '../lib/OpenAlex.js'
import { DomainIdSchema, FieldIdSchema } from '../lib/OpenAlex.js'
import * as Temporal from '../lib/Temporal.js'

const Requests = Schema.Array(
Schema.Struct({
timestamp: Temporal.InstantFromStringSchema,
language: Schema.optional(LanguageCode.LanguageCodeSchema, { nullable: true }),
fields: Schema.Array(FieldIdSchema),
domains: Schema.Array(DomainIdSchema),
}),
)

Expand Down
14 changes: 14 additions & 0 deletions src/lib/OpenAlex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ export const FieldIdFromUrlSchema = Schema.transformOrFail(UrlFromSelfSchema, Fi
: Either.left(new ParseResult.Type(ast, url)),
encode: topicId => ParseResult.succeed(new URL(`https://openalex.org/fields/${encodeURIComponent(topicId)}`)),
})

type DomainId = string & Brand.Brand<'OpenAlexDomainId'>

const DomainId = Brand.nominal<DomainId>()

export const DomainIdSchema = Schema.String.pipe(Schema.fromBrand(DomainId))

export const DomainIdFromUrlSchema = Schema.transformOrFail(UrlFromSelfSchema, DomainIdSchema, {
decode: (url, _, ast) =>
url.origin === 'https://openalex.org' && url.pathname.startsWith('/domains/')
? Either.right(decodeURIComponent(url.pathname.substring(9)))
: Either.left(new ParseResult.Type(ast, url)),
encode: topicId => ParseResult.succeed(new URL(`https://openalex.org/domains/${encodeURIComponent(topicId)}`)),
})
2 changes: 1 addition & 1 deletion src/requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const languageColor = Plot.scale({
})

const requestsByField = requests.flatMap(({ fields, ...request }) =>
fields.map(field => ({ ...request, field: openAlexFields[field] })),
fields.map(field => ({ ...request, field: openAlexFields[field].name })),
)
```

Expand Down

0 comments on commit 91da2b0

Please sign in to comment.