Unverified Commit 85cf50d5 authored by Hong Minhee's avatar Hong Minhee
Browse files

Improve `baseUrl` handling for relative URLs

Remove unnecessary baseUrl storage from generated classes and streamline
relative URL resolution to only occur during JSON-LD decoding.

Changes:

- Remove baseUrl property and getter from generated vocabulary classes
- Remove baseUrl option from class constructors
- Add explicit baseUrlVar parameter to scalar type decoder methods
- Use optional chaining (options?.baseUrl) for safer URL resolution
- Maintain relative URL support without storing baseUrl in instances

This improves upon PR #443's approach by avoiding unnecessary property
storage while still resolving relative URLs when baseUrl is provided
during fromJsonLd() calls.
parent 0f07cf27
Loading
Loading
Loading
Loading
+20 −566

File changed.

Preview size limit exceeded, changes collapsed.

+0 −8
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ async function* generateClass(
    readonly #documentLoader?: DocumentLoader;
    readonly #contextLoader?: DocumentLoader;
    readonly #tracerProvider?: TracerProvider;
    protected readonly _baseUrl?: URL;
    readonly #warning?: {
      category: string[];
      message: string;
@@ -96,13 +95,6 @@ async function* generateClass(
    static ${emitOverride(typeUri, types)} get typeId(): URL {
      return new URL(${JSON.stringify(typeUri)});
    }

    /**
     * The base URL used for resolving relative URLs in this object.
     */
    ${emitOverride(typeUri, types)} get baseUrl(): URL | undefined {
      return this._baseUrl;
    }
  `;
  for await (const code of generateFields(typeUri, types)) yield code;
  for await (const code of generateConstructor(typeUri, types)) yield code;
+0 −2
Original line number Diff line number Diff line
@@ -89,7 +89,6 @@ export async function* generateConstructor(
      documentLoader?: DocumentLoader,
      contextLoader?: DocumentLoader,
      tracerProvider?: TracerProvider,
      baseUrl?: URL,
    } = {},
  ) {
  `;
@@ -98,7 +97,6 @@ export async function* generateConstructor(
    this.#documentLoader = options.documentLoader;
    this.#contextLoader = options.contextLoader;
    this.#tracerProvider = options.tracerProvider;
    this._baseUrl = options.baseUrl;
    if ("$warning" in options) {
      this.#warning = options.$warning as unknown as {
        category: string[];
+10 −5
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ interface ScalarType {
  encoder(variable: string): string;
  compactEncoder?: (variable: string) => string;
  dataCheck(variable: string): string;
  decoder(variable: string): string;
  decoder(variable: string, baseUrlVar: string): string;
}

const scalarTypes: Record<string, ScalarType> = {
@@ -143,7 +143,7 @@ const scalarTypes: Record<string, ScalarType> = {
        && typeof ${v}["@id"] === "string"
        && ${v}["@id"] !== ""`;
    },
    decoder(v) {
    decoder(v, baseUrlVar) {
      return `${v}["@id"].startsWith("at://")
        ? new URL("at://" +
          encodeURIComponent(
@@ -157,9 +157,9 @@ const scalarTypes: Record<string, ScalarType> = {
              : ""
          )
        )
        : URL.canParse(${v}["@id"]) && options.baseUrl
        : URL.canParse(${v}["@id"]) && ${baseUrlVar}
          ? new URL(${v}["@id"])
          : new URL(${v}["@id"], options.baseUrl)`;
          : new URL(${v}["@id"], ${baseUrlVar})`;
    },
  },
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString": {
@@ -578,7 +578,12 @@ export function getDecoder(
  variable: string,
  optionsVariable: string,
): string {
  if (typeUri in scalarTypes) return scalarTypes[typeUri].decoder(variable);
  if (typeUri in scalarTypes) {
    return scalarTypes[typeUri].decoder(
      variable,
      `${optionsVariable}?.baseUrl`,
    );
  }
  if (typeUri in types) {
    return `await ${types[typeUri].name}.fromJsonLd(
      ${variable}, ${optionsVariable})`;
+0 −1
Original line number Diff line number Diff line
@@ -1358,7 +1358,6 @@ for (const typeUri in types) {
      icon?.url,
      new URL("https://example.com/avatars/test-avatar.jpg"),
    );
    assertEquals(personWithBase.baseUrl, new URL("https://example.com"));
  });

  if ("Deno" in globalThis) {