@fedify/vocab-tools 2.2.14-dev.2082 → 2.2.14

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.
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-tools",
3
- "version": "2.2.14-dev.2082+79cd52e9",
3
+ "version": "2.2.14",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./src/mod.ts"
package/dist/mod.cjs CHANGED
@@ -8,7 +8,7 @@ let yaml = require("yaml");
8
8
  let es_toolkit = require("es-toolkit");
9
9
  //#region deno.json
10
10
  var name = "@fedify/vocab-tools";
11
- var version = "2.2.14-dev.2082+79cd52e9";
11
+ var version = "2.2.14";
12
12
  //#endregion
13
13
  //#region src/type.ts
14
14
  const HEURISTICS_CONTEXTS = [
@@ -331,13 +331,16 @@ const scalarTypes = {
331
331
  return `${v}.href`;
332
332
  },
333
333
  dataCheck(v) {
334
- return `typeof ${v} === "object" && "@value" in ${v}
335
- && typeof ${v}["@value"] === "string"
336
- && ${v}["@value"] !== "" && ${v}["@value"] !== "/"`;
334
+ return `typeof ${v} === "object" &&
335
+ ((typeof ${v}["@id"] === "string" && canDecodeIri(${v}["@id"])) ||
336
+ (typeof ${v}["@value"] === "string" && canDecodeIri(${v}["@value"])))`;
337
337
  },
338
338
  decoder(v) {
339
- return `new URL(${v}["@value"])`;
340
- }
339
+ return `decodeIri(
340
+ typeof ${v}["@id"] === "string" ? ${v}["@id"] : ${v}["@value"]
341
+ )`;
342
+ },
343
+ skipUnparsable: true
341
344
  },
342
345
  "fedify:publicKey": {
343
346
  name: "CryptoKey",
@@ -549,6 +552,9 @@ function getDecoder(typeUri, types, variable, optionsVariable, baseUrlVariable)
549
552
  )`;
550
553
  throw new Error(`Unknown type: ${typeUri}`);
551
554
  }
555
+ function skipsUnparsable(typeUri) {
556
+ return scalarTypes[typeUri]?.skipUnparsable ?? false;
557
+ }
552
558
  function getDataCheck(typeUri, types, variable) {
553
559
  if (typeUri in scalarTypes) return scalarTypes[typeUri].dataCheck(variable);
554
560
  if (typeUri in types) {
@@ -1076,8 +1082,15 @@ async function* generateDecoder(typeUri, types) {
1076
1082
  yield `
1077
1083
  const decoded =
1078
1084
  `;
1079
- if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1080
- else {
1085
+ const lenient = property.range.length == 1 && skipsUnparsable(property.range[0]);
1086
+ if (property.range.length == 1) {
1087
+ if (lenient) {
1088
+ yield getDataCheck(property.range[0], types, "v");
1089
+ yield " ? ";
1090
+ }
1091
+ yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1092
+ if (lenient) yield " : undefined";
1093
+ } else {
1081
1094
  const decoders = getDecoders(property.range, types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1082
1095
  for (const code of decoders) yield code;
1083
1096
  }
@@ -1697,7 +1710,16 @@ async function* generateProperty(type, property, types) {
1697
1710
  ${JSON.stringify(property.compactName)}];
1698
1711
  const doc = Array.isArray(prop) ? prop[0] : prop;
1699
1712
  if (doc != null && typeof doc === "object" && "@context" in doc) {
1700
- v = await this.#${property.singularName}_fromJsonLd(doc, options);
1713
+ try {
1714
+ v = await this.#${property.singularName}_fromJsonLd(doc, options);
1715
+ } catch (error) {
1716
+ if (!options.suppressError) throw error;
1717
+ getLogger(["fedify", "vocab"]).debug(
1718
+ "Failed to parse embedded value of {property}: {error}",
1719
+ { property: ${JSON.stringify(property.uri)}, error },
1720
+ );
1721
+ return null;
1722
+ }
1701
1723
  }
1702
1724
  }
1703
1725
  `;
@@ -1794,7 +1816,16 @@ async function* generateProperty(type, property, types) {
1794
1816
  ${JSON.stringify(property.compactName)}];
1795
1817
  const obj = Array.isArray(prop) ? prop[i] : prop;
1796
1818
  if (obj != null && typeof obj === "object" && "@context" in obj) {
1797
- v = await this.#${property.singularName}_fromJsonLd(obj, options);
1819
+ try {
1820
+ v = await this.#${property.singularName}_fromJsonLd(obj, options);
1821
+ } catch (error) {
1822
+ if (!options.suppressError) throw error;
1823
+ getLogger(["fedify", "vocab"]).debug(
1824
+ "Failed to parse embedded value of {property} at index {index}: {error}",
1825
+ { property: ${JSON.stringify(property.uri)}, index: i, error },
1826
+ );
1827
+ continue;
1828
+ }
1798
1829
  }
1799
1830
  }
1800
1831
  `;
@@ -2011,6 +2042,17 @@ async function* generateClasses(types) {
2011
2042
  isTemporalInstant,
2012
2043
  } from "@fedify/vocab-runtime/temporal";\n`;
2013
2044
  yield `
2045
+ function canDecodeIri(iri: string): boolean {
2046
+ return URL.canParse(iri) || iri.startsWith("at://");
2047
+ }
2048
+
2049
+ function decodeIri(iri: string): URL {
2050
+ return !URL.canParse(iri) && iri.startsWith("at://")
2051
+ ? new URL("at://" + encodeURIComponent(iri.substring(5)))
2052
+ : new URL(iri);
2053
+ }
2054
+ `;
2055
+ yield `
2014
2056
  function isValidLanguageTag(language: string): boolean {
2015
2057
  try {
2016
2058
  new Intl.Locale(language);
package/dist/mod.js CHANGED
@@ -7,7 +7,7 @@ import { parse } from "yaml";
7
7
  import { pascalCase } from "es-toolkit";
8
8
  //#region deno.json
9
9
  var name = "@fedify/vocab-tools";
10
- var version = "2.2.14-dev.2082+79cd52e9";
10
+ var version = "2.2.14";
11
11
  //#endregion
12
12
  //#region src/type.ts
13
13
  const HEURISTICS_CONTEXTS = [
@@ -330,13 +330,16 @@ const scalarTypes = {
330
330
  return `${v}.href`;
331
331
  },
332
332
  dataCheck(v) {
333
- return `typeof ${v} === "object" && "@value" in ${v}
334
- && typeof ${v}["@value"] === "string"
335
- && ${v}["@value"] !== "" && ${v}["@value"] !== "/"`;
333
+ return `typeof ${v} === "object" &&
334
+ ((typeof ${v}["@id"] === "string" && canDecodeIri(${v}["@id"])) ||
335
+ (typeof ${v}["@value"] === "string" && canDecodeIri(${v}["@value"])))`;
336
336
  },
337
337
  decoder(v) {
338
- return `new URL(${v}["@value"])`;
339
- }
338
+ return `decodeIri(
339
+ typeof ${v}["@id"] === "string" ? ${v}["@id"] : ${v}["@value"]
340
+ )`;
341
+ },
342
+ skipUnparsable: true
340
343
  },
341
344
  "fedify:publicKey": {
342
345
  name: "CryptoKey",
@@ -548,6 +551,9 @@ function getDecoder(typeUri, types, variable, optionsVariable, baseUrlVariable)
548
551
  )`;
549
552
  throw new Error(`Unknown type: ${typeUri}`);
550
553
  }
554
+ function skipsUnparsable(typeUri) {
555
+ return scalarTypes[typeUri]?.skipUnparsable ?? false;
556
+ }
551
557
  function getDataCheck(typeUri, types, variable) {
552
558
  if (typeUri in scalarTypes) return scalarTypes[typeUri].dataCheck(variable);
553
559
  if (typeUri in types) {
@@ -1075,8 +1081,15 @@ async function* generateDecoder(typeUri, types) {
1075
1081
  yield `
1076
1082
  const decoded =
1077
1083
  `;
1078
- if (property.range.length == 1) yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1079
- else {
1084
+ const lenient = property.range.length == 1 && skipsUnparsable(property.range[0]);
1085
+ if (property.range.length == 1) {
1086
+ if (lenient) {
1087
+ yield getDataCheck(property.range[0], types, "v");
1088
+ yield " ? ";
1089
+ }
1090
+ yield getDecoder(property.range[0], types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1091
+ if (lenient) yield " : undefined";
1092
+ } else {
1080
1093
  const decoders = getDecoders(property.range, types, "v", "options", `(values["@id"] == null ? options.baseUrl : new URL(values["@id"]))`);
1081
1094
  for (const code of decoders) yield code;
1082
1095
  }
@@ -1696,7 +1709,16 @@ async function* generateProperty(type, property, types) {
1696
1709
  ${JSON.stringify(property.compactName)}];
1697
1710
  const doc = Array.isArray(prop) ? prop[0] : prop;
1698
1711
  if (doc != null && typeof doc === "object" && "@context" in doc) {
1699
- v = await this.#${property.singularName}_fromJsonLd(doc, options);
1712
+ try {
1713
+ v = await this.#${property.singularName}_fromJsonLd(doc, options);
1714
+ } catch (error) {
1715
+ if (!options.suppressError) throw error;
1716
+ getLogger(["fedify", "vocab"]).debug(
1717
+ "Failed to parse embedded value of {property}: {error}",
1718
+ { property: ${JSON.stringify(property.uri)}, error },
1719
+ );
1720
+ return null;
1721
+ }
1700
1722
  }
1701
1723
  }
1702
1724
  `;
@@ -1793,7 +1815,16 @@ async function* generateProperty(type, property, types) {
1793
1815
  ${JSON.stringify(property.compactName)}];
1794
1816
  const obj = Array.isArray(prop) ? prop[i] : prop;
1795
1817
  if (obj != null && typeof obj === "object" && "@context" in obj) {
1796
- v = await this.#${property.singularName}_fromJsonLd(obj, options);
1818
+ try {
1819
+ v = await this.#${property.singularName}_fromJsonLd(obj, options);
1820
+ } catch (error) {
1821
+ if (!options.suppressError) throw error;
1822
+ getLogger(["fedify", "vocab"]).debug(
1823
+ "Failed to parse embedded value of {property} at index {index}: {error}",
1824
+ { property: ${JSON.stringify(property.uri)}, index: i, error },
1825
+ );
1826
+ continue;
1827
+ }
1797
1828
  }
1798
1829
  }
1799
1830
  `;
@@ -2010,6 +2041,17 @@ async function* generateClasses(types) {
2010
2041
  isTemporalInstant,
2011
2042
  } from "@fedify/vocab-runtime/temporal";\n`;
2012
2043
  yield `
2044
+ function canDecodeIri(iri: string): boolean {
2045
+ return URL.canParse(iri) || iri.startsWith("at://");
2046
+ }
2047
+
2048
+ function decodeIri(iri: string): URL {
2049
+ return !URL.canParse(iri) && iri.startsWith("at://")
2050
+ ? new URL("at://" + encodeURIComponent(iri.substring(5)))
2051
+ : new URL(iri);
2052
+ }
2053
+ `;
2054
+ yield `
2013
2055
  function isValidLanguageTag(language: string): boolean {
2014
2056
  try {
2015
2057
  new Intl.Locale(language);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-tools",
3
- "version": "2.2.14-dev.2082+79cd52e9",
3
+ "version": "2.2.14",
4
4
  "description": "Code generator for Activity Vocabulary APIs",
5
5
  "homepage": "https://fedify.dev/",
6
6
  "repository": {