tsondb 0.20.10 → 0.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,19 @@ export type TypeScriptRendererOptions = {
27
27
  */
28
28
  inferTranslationParameters?: {
29
29
  format: "mf2";
30
+ /**
31
+ * The default type to use for translation parameters when the type cannot be inferred from the message string.
32
+ * @default "string"
33
+ */
34
+ defaultTranslationParameterType?: "string" | "StringableTranslationParameter";
35
+ /**
36
+ * New formatter types can be introduced to MF2 messages. This option allows you to specify a mapping of formatter names to their corresponding TypeScript types. For example, a list formatter could be defined as `{ list: "string[]" }`, which would infer the type of a translation parameter using the `list` formatter as `string[]`.
37
+ */
38
+ otherTypes?: Record<string, string>;
30
39
  };
40
+ /**
41
+ * The generated type to use for Date types. The data is always stored as a string, but Date can be helpful if you need to differenciate between a string and a date in code. Defaults to `string`.
42
+ */
43
+ dateType: "string" | "Date";
31
44
  };
32
45
  export declare const render: (options: Partial<TypeScriptRendererOptions> | undefined, declarations: readonly Decl[]) => string;
@@ -22,7 +22,9 @@ const defaultOptions = {
22
22
  preserveFiles: false,
23
23
  generateHelpers: {},
24
24
  addIdentifierToEntities: false,
25
+ dateType: "string",
25
26
  };
27
+ const defaultTranslationParameterType = "string";
26
28
  const renderDocumentation = (comment, isDeprecated) => syntax `${comment === undefined
27
29
  ? ""
28
30
  : syntax `/**
@@ -55,7 +57,7 @@ const renderObjectType = (options, type) => {
55
57
  .map(([name, config]) => syntax `${renderDocumentation(config.comment, config.isDeprecated)}${name}${config.isRequired ? "" : "?"}: ${renderType(options, config.type)}`), Object.values(type.properties).some(prop => prop.comment !== undefined) ? EOL + EOL : EOL));
56
58
  };
57
59
  const renderBooleanType = (_options, _type) => syntax `boolean`;
58
- const renderDateType = (_options, _type) => syntax `Date`;
60
+ const renderDateType = (options, _type) => options.dateType === "Date" ? syntax `Date` : syntax `string`;
59
61
  const renderFloatType = (_options, _type) => syntax `number`;
60
62
  const renderIntegerType = (_options, _type) => syntax `number`;
61
63
  const renderStringType = (_options, _type) => syntax `string`;
@@ -75,7 +77,7 @@ const renderEnumType = (options, type) => combineSyntaxes(Object.entries(type.va
75
77
  ? ""
76
78
  : syntax `${EOL}${caseName}: ${renderType(options, caseDef.type)}`}`)}${EOL}}`)));
77
79
  const renderChildEntitiesType = () => syntax `never`;
78
- const mapTypeNameToType = (typeName) => {
80
+ const mapTypeNameToType = (options, typeName) => {
79
81
  switch (typeName) {
80
82
  case "string":
81
83
  return "string";
@@ -88,7 +90,14 @@ const mapTypeNameToType = (typeName) => {
88
90
  return "Date";
89
91
  case null:
90
92
  default:
91
- return "StringableTranslationParameter";
93
+ if (typeName !== null &&
94
+ options.inferTranslationParameters?.otherTypes !== undefined &&
95
+ typeName in options.inferTranslationParameters.otherTypes) {
96
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- already checked that the key exists
97
+ return options.inferTranslationParameters.otherTypes[typeName];
98
+ }
99
+ return (options.inferTranslationParameters?.defaultTranslationParameterType ??
100
+ defaultTranslationParameterType);
92
101
  }
93
102
  };
94
103
  const renderTranslationParameterBrand = (options, params) => {
@@ -102,7 +111,7 @@ const renderTranslationParameterBrand = (options, params) => {
102
111
  return syntax ` & { __params: { ${entries.map(([name, type]) => `"${name}": ${type}`).join("; ")} } }`;
103
112
  };
104
113
  const renderTranslationObjectType = (options, type) => {
105
- return wrapAsObject(options, combineSyntaxes(Object.entries(type.properties).map(([name, config]) => syntax `"${name.replace('"', '\\"')}"${type.allKeysAreRequired ? "" : "?"}: ${renderType(options, getTypeOfKey(config, type))}${renderTranslationParameterBrand(options, mapParameterTypeNames(extractParameterTypeNamesFromMessage(name), mapTypeNameToType))}`), EOL));
114
+ return wrapAsObject(options, combineSyntaxes(Object.entries(type.properties).map(([name, config]) => syntax `"${name.replace('"', '\\"')}"${type.allKeysAreRequired ? "" : "?"}: ${renderType(options, getTypeOfKey(config, type))}${renderTranslationParameterBrand(options, mapParameterTypeNames(extractParameterTypeNamesFromMessage(name), typeName => mapTypeNameToType(options, typeName)))}`), EOL));
106
115
  };
107
116
  const renderType = (options, type) => {
108
117
  switch (type.kind) {
@@ -187,7 +196,10 @@ const renderEntityMapType = (options, declarations) => renderMapHelperType(optio
187
196
  const renderChildEntityMapType = (options, declarations) => renderMapHelperType(options, declarations, "childEntityMap", "ChildEntityMap", decl => isEntityDecl(decl) && isEntityDeclWithParentReference(decl), decl => `${decl.name}: [${decl.name}, "${decl.parentReferenceKey}", ${decl.name}["${decl.parentReferenceKey}"]]`);
188
197
  const renderEnumMapType = (options, declarations) => renderMapHelperType(options, declarations, "enumMap", "EnumMap", isEnumDecl);
189
198
  const renderTypeAliasMapType = (options, declarations) => renderMapHelperType(options, declarations, "typeAliasMap", "TypeAliasMap", isTypeAliasDecl);
190
- const renderStringableTranslationParameterType = (options) => options.inferTranslationParameters?.format === "mf2"
199
+ const renderStringableTranslationParameterType = (options) => options.inferTranslationParameters?.format === "mf2" &&
200
+ (options.inferTranslationParameters.defaultTranslationParameterType === undefined ||
201
+ options.inferTranslationParameters.defaultTranslationParameterType ===
202
+ "StringableTranslationParameter")
191
203
  ? "export type StringableTranslationParameter = {" +
192
204
  EOL +
193
205
  prefixLines(getIndentation(options.indentation, 1), "toString(): string") +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsondb",
3
- "version": "0.20.10",
3
+ "version": "0.21.1",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "Lukas Obermann",