-
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
588927a
commit 0509358
Showing
5 changed files
with
88 additions
and
0 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,9 +1,31 @@ | ||
import { Schema } from '@effect/schema' | ||
import * as Doi from './Doi.js' | ||
import * as Url from './Url.js' | ||
|
||
export const ReviewActionSchema = Schema.struct({ | ||
'@context': Schema.tuple( | ||
Schema.literal('https://www.w3.org/ns/activitystreams'), | ||
Schema.literal('https://purl.org/coar/notify'), | ||
), | ||
id: Schema.string, | ||
type: Schema.tuple(Schema.literal('Offer'), Schema.literal('coar-notify:ReviewAction')), | ||
origin: Schema.struct({ | ||
id: Url.UrlFromStringSchema(Schema.string), | ||
inbox: Url.UrlFromStringSchema(Schema.string), | ||
type: Schema.literal('Organization', 'Service'), | ||
}), | ||
target: Schema.struct({ | ||
id: Url.UrlFromStringSchema(Schema.string), | ||
inbox: Url.UrlFromStringSchema(Schema.string), | ||
type: Schema.literal('Organization', 'Service'), | ||
}), | ||
object: Schema.struct({ | ||
id: Schema.string, | ||
'ietf:cite-as': Doi.DoiUrlSchema, | ||
}), | ||
actor: Schema.struct({ | ||
id: Url.UrlFromStringSchema(Schema.string), | ||
type: Schema.literal('Application', 'Group', 'Organization', 'Person', 'Service'), | ||
name: 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ParseResult, Schema } from '@effect/schema' | ||
import doiRegex from 'doi-regex' | ||
import { Brand, Either, type Option, type Predicate, String, flow } from 'effect' | ||
|
||
export type Doi = Brand.Branded<string, 'Doi'> | ||
|
||
const isDoi: Predicate.Refinement<unknown, Doi> = (u): u is Doi => | ||
typeof u === 'string' && doiRegex({ exact: true }).test(u) && !u.endsWith('/.') && !u.endsWith('/..') | ||
|
||
export const Doi = Brand.refined<Doi>(isDoi, s => Brand.error(`Expected ${s} to be a DOI`)) | ||
|
||
const toUrl: (doi: Doi) => URL = doi => { | ||
const url = new URL('https://doi.org') | ||
url.pathname = doi.replace(/\/(\.{1,2})\//g, '/$1%2F').replace(/\\/g, '%5C') | ||
|
||
return url | ||
} | ||
|
||
const parse: (s: string) => Option.Option<Doi> = flow( | ||
String.trim, | ||
String.replace(/^(?:https?:\/\/(?:dx\.)?doi\.org\/|doi:)?/i, ''), | ||
s => Doi.option(s), | ||
) | ||
|
||
export const DoiSchema = Schema.string.pipe(Schema.fromBrand(Doi)) | ||
|
||
export const DoiUrlSchema: Schema.Schema<string, Doi> = Schema.transformOrFail( | ||
Schema.string, | ||
Schema.to(DoiSchema), | ||
s => Either.fromOption(parse(s), () => ParseResult.parseError([ParseResult.type(DoiSchema.ast, s)])), | ||
doi => ParseResult.succeed(toUrl(doi).href), | ||
) |
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,16 @@ | ||
import { ParseResult, Schema } from '@effect/schema' | ||
|
||
export const UrlSchema: Schema.Schema<URL> = Schema.instanceOf(URL) | ||
|
||
export const UrlFromStringSchema = <I, A extends string>(self: Schema.Schema<I, A>): Schema.Schema<I, URL> => | ||
Schema.transformOrFail( | ||
self, | ||
UrlSchema, | ||
(s, _, ast) => | ||
ParseResult.try({ | ||
try: () => new URL(s), | ||
catch: () => ParseResult.parseError([ParseResult.type(ast, s)]), | ||
}), | ||
url => ParseResult.succeed(url.href), | ||
{ strict: false }, | ||
) |