Unverified Commit c4529cfa authored by Hong Minhee's avatar Hong Minhee
Browse files

Add context option to Object.toJsonLd() method

parent 8cacf374
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -83,6 +83,9 @@ To be released.
     -  `Object.clone()` method now accepts `proof` option.
     -  `Object.clone()` method now accepts `proofs` option.

 -  Added `context` option to `Object.toJsonLd()` method.  This applies to
    any subclasses of the `Object` class too.

 -  Deprecated `treatHttps` option in `FederationParameters` interface.
    Instead, use the [x-forwarded-fetch] library to recognize the
    `X-Forwarded-Host` and `X-Forwarded-Proto` headers.
+96 −48

File changed.

Preview size limit exceeded, changes collapsed.

+2 −1
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ export async function* generateEncoder(
  async toJsonLd(options: {
    expand?: boolean,
    contextLoader?: DocumentLoader,
    context?: string | Record<string, string> | (string | Record<string, string>)[],
  } = {}): Promise<unknown> {
    options = {
      ...options,
@@ -69,7 +70,7 @@ export async function* generateEncoder(
    }
    return await jsonld.compact(
      values,
      ${JSON.stringify(type.defaultContext)},
      options.context ?? ${JSON.stringify(type.defaultContext)},
      { documentLoader: options.contextLoader },
    );
  }
+17 −5
Original line number Diff line number Diff line
@@ -724,15 +724,27 @@ for (const typeUri in types) {
    });
    assertEquals(restored, instance);

    const jsonLd2 = await instance.toJsonLd({
      contextLoader: mockDocumentLoader,
      context: "https://www.w3.org/ns/activitystreams",
    });
    assertEquals(jsonLd2["@context"], "https://www.w3.org/ns/activitystreams");
    assertEquals(jsonLd2.id, "https://example.com/");
    const restored2 = await cls.fromJsonLd(jsonLd2, {
      documentLoader: mockDocumentLoader,
      contextLoader: mockDocumentLoader,
    });
    assertEquals(restored2, instance);

    const expanded = await instance.toJsonLd({
      contextLoader: mockDocumentLoader,
      expand: true,
    });
    const restored2 = await cls.fromJsonLd(expanded, {
    const restored3 = await cls.fromJsonLd(expanded, {
      documentLoader: mockDocumentLoader,
      contextLoader: mockDocumentLoader,
    });
    assertEquals(restored2, instance);
    assertEquals(restored3, instance);

    const instance2 = new cls({
      id: new URL("https://example.com/"),
@@ -746,14 +758,14 @@ for (const typeUri in types) {
        ),
      ),
    });
    const jsonLd2 = await instance2.toJsonLd({
    const jsonLd3 = await instance2.toJsonLd({
      contextLoader: mockDocumentLoader,
    });
    const restored3 = await cls.fromJsonLd(jsonLd2, {
    const restored4 = await cls.fromJsonLd(jsonLd3, {
      documentLoader: mockDocumentLoader,
      contextLoader: mockDocumentLoader,
    });
    assertEquals(restored3, instance2);
    assertEquals(restored4, instance2);
  });

  if (isDeno) {