Skip to content

Commit

Permalink
feat: allow to specify serializer function children type for composeS…
Browse files Browse the repository at this point in the history
…erializer function
  • Loading branch information
lihbr committed Jun 25, 2021
1 parent 98e24d8 commit b8de3a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/composeSerializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,29 @@ export const composeSerializers = <SerializerReturnType>(
...serializers: [
(
| RichTextFunctionSerializer<SerializerReturnType>
| RichTextFunctionSerializer<SerializerReturnType | null>
| RichTextFunctionSerializer<
SerializerReturnType | null,
SerializerReturnType
>
),
...(
| RichTextFunctionSerializer<SerializerReturnType>
| RichTextFunctionSerializer<SerializerReturnType | null>
| RichTextFunctionSerializer<
SerializerReturnType | null,
SerializerReturnType
>
)[]
]
) => {
return (
...args: Parameters<RichTextFunctionSerializer<SerializerReturnType>>
): SerializerReturnType => {
): NonNullable<SerializerReturnType> => {
for (let i = 0; i < serializers.length; i++) {
const res = serializers[i](...args);

if (res !== null) {
return res;
if (res != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return res!;
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ import {
* Serializes a node from a rich text or title field with a function
*
* @typeParam ReturnType - Return type of the function serializer
* @typeParam ChildrenType - Type of children, defaults to provided ReturnType
*
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
*/
export type RichTextFunctionSerializer<ReturnType> = (
export type RichTextFunctionSerializer<
ReturnType,
ChildrenType = ReturnType,
> = (
type: RichTextNodeType,
node: RTAnyNode,
text: string | undefined,
children: ReturnType[],
children: ChildrenType[],
key: string,
) => ReturnType;

Expand Down

0 comments on commit b8de3a2

Please sign in to comment.