Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPC-9766: Rewrite legacy RPM endpoints in typescript #475

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions libs/hpc-data/src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as t from 'io-ts';
import { isRight } from 'fp-ts/lib/Either';

export const resultWithPermissions = <D, P extends { [id: string]: boolean }>(
data: t.Type<D>,
Expand Down Expand Up @@ -232,3 +233,44 @@ export const DATE_FROM_STRING = new t.Type(
},
t.identity
);

export const ITEM_ARRAY_FROM_STRING = <C extends t.Mixed>(
codecName: string,
itemCodec: C
) => {
type ItemType = t.TypeOf<typeof itemCodec>;
return new t.Type<ItemType[], ItemType[]>(
codecName,
(input: unknown): input is ItemType[] => {
if (Array.isArray(input)) {
return input.every(itemCodec.is);
}
if (typeof input === 'string') {
return input
.split(',')
.map((s) => s.trim())
.every(itemCodec.is);
}
return false;
},
(input, context) => {
if (Array.isArray(input)) {
const inputs = input.map(itemCodec.decode);
return inputs.every(isRight)
? t.success(inputs.map(({ right }) => right))
: t.failure(input, context);
}
if (typeof input === 'string') {
const items = input
.split(',')
.map((s) => s.trim())
.map(itemCodec.decode);
return items.every(isRight)
? t.success(items.map(({ right }) => right))
: t.failure(input, context);
}
return t.failure(input, context);
},
t.identity
);
};