Commit 629a9a1f authored by Diegodlh's avatar Diegodlh
Browse files

Required "title" field may have non-base alternatives

parent 651fcc56
import {
RequiredFields,
BaseTitleFields,
NonBaseTitleFields,
SpecialFields,
BaseFields,
NonBaseFields,
......@@ -9,11 +11,12 @@ import {
ZoteroFields,
SimpleCitoidFields,
} from "./fieldTypes";
import { REQUIRED_FIELDS } from "./keyTypes";
import { REQUIRED_FIELDS, TITLE_FIELDS } from "./keyTypes";
// Citoid citation types
export type MediaWikiCitation = RequiredFields &
(BaseTitleFields | NonBaseTitleFields) &
Partial<
SpecialFields &
BaseFields &
......@@ -24,9 +27,11 @@ export type MediaWikiCitation = RequiredFields &
>;
export type MediaWikiBaseFieldCitation = RequiredFields &
BaseTitleFields &
Partial<SpecialFields & BaseFields & BaseCreatorFields & MediaWikiFields>;
export type ZoteroCitation = RequiredFields &
(BaseTitleFields | NonBaseTitleFields) &
Partial<SpecialFields & BaseFields & NonBaseFields & ZoteroFields>;
export type CitoidCitation =
......@@ -37,8 +42,10 @@ export type CitoidCitation =
export function isCitoidCitation(
citation: unknown
): citation is CitoidCitation {
return REQUIRED_FIELDS.every(
(field) => (citation as CitoidCitation)[field] !== undefined
return (
REQUIRED_FIELDS.every(
(field) => (citation as CitoidCitation)[field] !== undefined
) && TITLE_FIELDS.some((field) => field in (citation as CitoidCitation))
);
}
......
import { assert, Equals, Implements } from "./utils";
import { assert, Equals, Implements, OneKey } from "./utils";
import {
ItemType,
Tag,
......@@ -8,6 +8,8 @@ import {
} from "./valueTypes";
import {
RequiredField,
BaseTitleField,
NonBaseTitleField,
SpecialField,
BaseField,
NonBaseField,
......@@ -25,12 +27,17 @@ import {
export interface RequiredFields {
// required - https://www.mediawiki.org/wiki/Citoid/API#Field_names
itemType: ItemType;
title: string;
url: string;
}
// confirm that RequiredFields has all and only the keys in REQUIRED_FIELDS
assert<Equals<RequiredField, keyof RequiredFields>>(true);
// Title fields
export type BaseTitleFields = {
[key in BaseTitleField]: string;
};
export type NonBaseTitleFields = OneKey<NonBaseTitleField, string>;
// Special fields
export interface SpecialFields {
tags: Array<Tag>;
......@@ -78,7 +85,10 @@ export interface ZoteroFields {
}
// Simple-citoid fields
export type SimpleCitoidFields = Record<RequiredField, string | string[]> & {
export type SimpleCitoidFields = Record<
RequiredField | BaseTitleField,
string | string[]
> & {
itemType: ItemType;
} & Partial<
Record<
......
......@@ -5,9 +5,19 @@ import {
} from "./creatorTypes";
// Required field key type
export const REQUIRED_FIELDS = ["itemType", "title", "url"] as const;
export const REQUIRED_FIELDS = ["itemType", "url"] as const;
export type RequiredField = typeof REQUIRED_FIELDS[number];
// Title field key types
const BASE_TITLE_FIELDS = ["title"] as const;
export type BaseTitleField = typeof BASE_TITLE_FIELDS[number];
const NON_BASE_TITLE_FIELDS = ["caseName", "subject", "nameOfAct"] as const;
export type NonBaseTitleField = typeof NON_BASE_TITLE_FIELDS[number];
export const TITLE_FIELDS = [
...BASE_TITLE_FIELDS,
...NON_BASE_TITLE_FIELDS,
] as const;
// Special field key type
const SPECIAL_FIELDS = ["tags", "key", "version"] as const;
export type SpecialField = typeof SPECIAL_FIELDS[number];
......@@ -141,6 +151,7 @@ export type MediaWikiField = typeof MEDIA_WIKI_FIELDS[number];
// Simple-citoid field key type
const SIMPLE_CITOID_NON_CREATOR_FIELDS = [
...REQUIRED_FIELDS,
...BASE_TITLE_FIELDS,
"tags",
...BASE_FIELDS,
...MEDIA_WIKI_ID_FIELDS,
......
......@@ -12,3 +12,10 @@ export function assert<T extends boolean>(expect: T) {
// see https://github.com/Microsoft/TypeScript/issues/24274
export type Implements<T, R extends T> = R;
// https://stackoverflow.com/questions/57571664/typescript-type-for-an-object-with-only-one-key-no-union-type-allowed-as-a-key
export type OneKey<K extends string, V = any> = {
[P in K]: Record<P, V> & Partial<Record<Exclude<K, P>, never>> extends infer O
? { [Q in keyof O]: O[Q] }
: never;
}[K];
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment