-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CustomFields type in its own module
- Loading branch information
Showing
7 changed files
with
41 additions
and
41 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export type CustomFields = { | ||
/* The custom field name (this must be a URL). */ | ||
key: URL; | ||
/* The custom field value (this must be a litteral). */ | ||
value: string | number | boolean; | ||
}; | ||
|
||
/** | ||
* Internal function to collapse the user-provided custom fields into | ||
* a simple JSON object. | ||
* | ||
* @hidden | ||
*/ | ||
export const toJson = ( | ||
c: Set<CustomFields> = new Set(), | ||
): Record<string, CustomFields["value"]> => { | ||
return ( | ||
Array.from(c) | ||
// Check that all the provided custom fields match the expected type, | ||
// and change the validated CustomField into a plain JSON object entry. | ||
.map((field) => { | ||
if (typeof field.key !== "object") { | ||
throw new Error( | ||
`All custom fields keys must be URL objects, found ${field.key}`, | ||
); | ||
} | ||
return { [`${field.key.toString()}`]: field.value }; | ||
}) | ||
// Collapse all the JSON object entries into a single object. | ||
.reduce( | ||
(acc, cur) => Object.assign(acc, cur), | ||
{} as Record<string, CustomFields["value"]>, | ||
) | ||
); | ||
}; |
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
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