diff --git a/CHANGELOG.md b/CHANGELOG.md index 790d6f547c..c1350dfd42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The following changes are pending, and will be applied on the next major release ## Unreleased changes +### New features + +- Expose `ParseOptions` on `getSolidDatasetWithAcl`, `getSolidDatasetWithAcr`, `getSolidDatasetWithAccessDatasets`, `getProfileAll`, `getPodUrlAll`, and `getWebIdDataset`. + ### Patch changes - NodeJS 26 is now officially supported. diff --git a/src/acl/acl.internal.ts b/src/acl/acl.internal.ts index c5a799ed36..c621ea2672 100644 --- a/src/acl/acl.internal.ts +++ b/src/acl/acl.internal.ts @@ -19,6 +19,7 @@ // import type { Quad } from "@rdfjs/types"; +import type { ParseOptions } from "../resource/solidDataset"; import { getSolidDataset } from "../resource/solidDataset"; import type { IriString, @@ -67,7 +68,7 @@ import { isAcr } from "../acp/acp.internal"; */ export async function internal_fetchAcl( resourceInfo: WithServerResourceInfo, - options?: { fetch?: typeof fetch }, + options?: Partial<{ fetch: typeof fetch } & ParseOptions>, ): Promise { if (!hasAccessibleAcl(resourceInfo)) { return { @@ -103,7 +104,7 @@ export async function internal_fetchAcl( /** @internal */ export async function internal_fetchResourceAcl( dataset: WithServerResourceInfo, - options?: { fetch?: typeof fetch }, + options?: Partial<{ fetch: typeof fetch } & ParseOptions>, ): Promise { if (!hasAccessibleAcl(dataset)) { return null; @@ -135,7 +136,7 @@ export async function internal_fetchResourceAcl( /** @internal */ export async function internal_fetchFallbackAcl( resource: WithAccessibleAcl, - options?: { fetch?: typeof fetch }, + options?: Partial<{ fetch: typeof fetch } & ParseOptions>, ): Promise { const resourceUrl = new URL(getSourceUrl(resource)); const resourcePath = resourceUrl.pathname; diff --git a/src/acl/acl.test.ts b/src/acl/acl.test.ts index 973aee90a1..3ab89eab85 100644 --- a/src/acl/acl.test.ts +++ b/src/acl/acl.test.ts @@ -50,6 +50,7 @@ import { import type { WithServerResourceInfo, WithChangeLog } from "../interfaces"; import { getFile } from "../resource/file"; import { mockSolidDatasetFrom } from "../resource/mock"; +import type { Parser } from "../resource/solidDataset"; import { createSolidDataset } from "../resource/solidDataset"; import { createThing, getThingAll, setThing } from "../thing/thing"; import { addIri, addStringNoLocale } from "../thing/add"; @@ -850,6 +851,49 @@ describe("getSolidDatasetWithAcl", () => { statusText: "I'm a teapot!", }); }); + + it("forwards custom parsers to both resource and ACL dataset fetches", async () => { + const mockFetch = jest.fn((url) => { + const headers: HeadersInit = + url === "https://some.pod/resource" + ? { + Link: '; rel="acl"', + "Content-Type": "application/trig", + } + : { "Content-Type": "text/turtle" }; + return Promise.resolve( + mockResponse(undefined, { headers }, url as string), + ); + }); + + const mockTrigParser: Parser = { + onComplete: jest + .fn() + .mockImplementation((completionCallback: any) => completionCallback()), + onQuad: jest.fn(), + parse: jest.fn(), + onError: jest.fn(), + }; + + const fetchedSolidDataset = await getSolidDatasetWithAcl( + "https://some.pod/resource", + { + fetch: mockFetch, + parsers: { "application/trig": mockTrigParser }, + }, + ); + + expect(fetchedSolidDataset.internal_resourceInfo.sourceIri).toBe( + "https://some.pod/resource", + ); + expect(mockTrigParser.parse).toHaveBeenCalledTimes(1); + expect(mockFetch.mock.calls[0][1]).toEqual({ + headers: { Accept: "application/trig" }, + }); + expect(mockFetch.mock.calls[1][1]).toEqual({ + headers: { Accept: "application/trig" }, + }); + }); }); describe("getFileWithAcl", () => { diff --git a/src/acl/acl.ts b/src/acl/acl.ts index 6c9c7f5bee..4a76663b4a 100644 --- a/src/acl/acl.ts +++ b/src/acl/acl.ts @@ -19,6 +19,7 @@ // import { acl } from "../constants"; +import type { ParseOptions } from "../resource/solidDataset"; import { createSolidDataset, getSolidDataset, @@ -113,7 +114,7 @@ export function hasResourceAcl< */ export async function getSolidDatasetWithAcl( url: UrlString | Url, - options?: { fetch?: typeof fetch }, + options?: Partial<{ fetch: typeof fetch } & ParseOptions>, ): Promise { const solidDataset = await getSolidDataset(url, options); const acl = await internal_fetchAcl(solidDataset, options); diff --git a/src/acp/acp.ts b/src/acp/acp.ts index af11fe4b56..aad42414f8 100644 --- a/src/acp/acp.ts +++ b/src/acp/acp.ts @@ -34,6 +34,7 @@ import { getResourceInfo, getSourceUrl } from "../resource/resource"; import type { WithAcl } from "../acl/acl"; import { hasAccessibleAcl } from "../acl/acl"; import { internal_fetchAcl, internal_setAcl } from "../acl/acl.internal"; +import type { ParseOptions } from "../resource/solidDataset"; import { getSolidDataset, saveSolidDatasetAt } from "../resource/solidDataset"; import type { AccessControlResource } from "./control"; import { @@ -61,7 +62,7 @@ import { isAcr } from "./acp.internal"; */ export async function getSolidDatasetWithAcr( url: Url | UrlString, - options?: { fetch?: typeof fetch }, + options?: Partial<{ fetch?: typeof fetch } & ParseOptions>, ): Promise { const urlString = internal_toIriString(url); const solidDataset = await getSolidDataset(urlString, options); @@ -134,7 +135,7 @@ export async function getResourceInfoWithAcr( */ export async function getSolidDatasetWithAccessDatasets( url: Url | UrlString, - options?: { fetch?: typeof fetch }, + options?: Partial<{ fetch?: typeof fetch } & ParseOptions>, ): Promise { const urlString = internal_toIriString(url); const solidDataset = await getSolidDataset(urlString, options); @@ -265,7 +266,7 @@ export function hasAccessibleAcr( async function fetchAcr( resource: WithServerResourceInfo, - options?: { fetch?: typeof fetch }, + options?: Partial<{ fetch?: typeof fetch } & ParseOptions>, ): Promise { let acrUrl: UrlString | undefined; if (hasLinkedAcr(resource)) { diff --git a/src/profile/webid.ts b/src/profile/webid.ts index 1c3875606d..b7efee83ac 100644 --- a/src/profile/webid.ts +++ b/src/profile/webid.ts @@ -19,6 +19,7 @@ // import type { + ParseOptions, SolidDataset, UrlString, WebId, @@ -89,11 +90,14 @@ export async function getProfileAll< options?: { fetch?: typeof fetch; webIdProfile?: T; - }, + } & Partial, ): Promise>; export async function getProfileAll( webId: WebId, - options?: { fetch?: typeof fetch; webIdProfile: undefined }, + options?: { + fetch?: typeof fetch; + webIdProfile: undefined; + } & Partial, ): Promise>; export async function getProfileAll< T extends SolidDataset & WithServerResourceInfo, @@ -102,17 +106,19 @@ export async function getProfileAll< options?: { fetch?: typeof fetch; webIdProfile?: T; - }, + } & Partial, ): Promise> { const authFetch = options?.fetch ?? fetch; + const unauthenticatedOptions = { ...options }; + delete unauthenticatedOptions.fetch; const webIdProfile = options?.webIdProfile ?? // This should always use an unauthenticated fetch. - (await getSolidDataset(webId)); + (await getSolidDataset(webId, unauthenticatedOptions)); const altProfileAll = ( await Promise.allSettled( getAltProfileUrlAllFrom(webId, webIdProfile).map((uniqueProfileIri) => - getSolidDataset(uniqueProfileIri, { fetch: authFetch }), + getSolidDataset(uniqueProfileIri, { ...options, fetch: authFetch }), ), ) ) @@ -146,7 +152,7 @@ export async function getProfileAll< */ export async function getPodUrlAll( webId: WebId, - options?: { fetch?: typeof fetch }, + options?: Partial<{ fetch?: typeof fetch } & ParseOptions>, ): Promise { const profiles = await getProfileAll(webId, options); return getPodUrlAllFrom(profiles, webId); @@ -200,6 +206,7 @@ export function getPodUrlAllFrom( */ export async function getWebIdDataset( webId: WebId, + options?: Partial, ): Promise> { - return getSolidDataset(webId); + return getSolidDataset(webId, options); }