From 251b6b5c47f577f0f2fd842f7b51f86db254977a Mon Sep 17 00:00:00 2001 From: Nicolas Ayral Seydoux Date: Tue, 17 Dec 2024 11:52:04 +0100 Subject: [PATCH] Name intermediary types --- src/gConsent/index.ts | 8 ++++++ src/gConsent/query/query.ts | 53 ++++++++++++++++++++----------------- src/index.ts | 11 ++++---- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/gConsent/index.ts b/src/gConsent/index.ts index fb76e1508..13f92662b 100644 --- a/src/gConsent/index.ts +++ b/src/gConsent/index.ts @@ -45,6 +45,14 @@ export { getAccessGrantFromRedirectUrl, } from "./request"; +export { + CredentialFilter, + CredentialResult, + CredentialStatus, + CredentialType, + query, +} from "./query/query"; + export { approveAccessRequest, denyAccessRequest, diff --git a/src/gConsent/query/query.ts b/src/gConsent/query/query.ts index c92414097..b3d53f09e 100644 --- a/src/gConsent/query/query.ts +++ b/src/gConsent/query/query.ts @@ -24,28 +24,36 @@ import LinkHeader from "http-link-header"; import { handleErrorResponse } from "@inrupt/solid-client-errors"; import type { DatasetWithId } from "@inrupt/solid-client-vc"; import { verifiableCredentialToDataset } from "@inrupt/solid-client-vc"; +import type { UrlString } from "@inrupt/solid-client"; + +export type CredentialStatus = + | "Pending" + | "Denied" + | "Granted" + | "Canceled" + | "Expired" + | "Active" + | "Revoked"; + +export type CredentialType = + | "SolidAccessRequest" + | "SolidAccessGrant" + | "SolidAccessDenial"; export type CredentialFilter = { - type?: "SolidAccessRequest" | "SolidAccessGrant" | "SolidAccessDenial"; - status?: - | "Pending" - | "Denied" - | "Granted" - | "Canceled" - | "Expired" - | "Active" - | "Revoked"; - fromAgent?: string; - toAgent?: string; - resource?: string; - purpose?: string; + type?: CredentialType; + status?: CredentialStatus; + fromAgent?: UrlString; + toAgent?: UrlString; + resource?: UrlString; + purpose?: UrlString; issuedWithin?: "P1D" | "P7D" | "P1M" | "P3M"; revokedWithin?: "P1D" | "P7D" | "P1M" | "P3M"; pageSize?: number; page?: string; }; -const FILTER_ELEMENTS: (keyof CredentialFilter)[] = [ +const FILTER_ELEMENTS: Array = [ "fromAgent", "issuedWithin", "page", @@ -69,13 +77,6 @@ const isSupportedFilterElement = (x: unknown): x is SupportedFilterElements => const PAGING_RELS = ["first", "last", "prev", "next"] as const; -type SupportedPagingRels = - typeof PAGING_RELS extends Array ? E : never; - -// The type assertion is okay in the context of the type guard. -const isSupportedPagingRel = (x: unknown): x is SupportedPagingRels => - PAGING_RELS.includes(x as SupportedPagingRels); - export type CredentialResult = { items: DatasetWithId[]; first?: CredentialFilter; @@ -126,7 +127,9 @@ async function toCredentialResult( } const body = await response.json(); if (!hasItems(body) || !body.items.every(isObjectWithId)) { - return result; + throw Error( + `Unexpected response, no items found in ${JSON.stringify(body)}`, + ); } result.items = await Promise.all( body.items.map((vc) => { @@ -136,8 +139,8 @@ async function toCredentialResult( return result; } -function addQueryParams(filter: CredentialFilter, url: URL): URL { - const result = new URL(url); +function toQueryUrl(endpoint: URL, filter: CredentialFilter): URL { + const result = new URL(endpoint); Object.entries(filter).forEach(([key, value]) => { result.searchParams.append(key, value.toString()); }); @@ -151,7 +154,7 @@ export async function query( fetch?: typeof fetch; }, ): Promise { - const queryUrl = addQueryParams(filter, queryEndpoint); + const queryUrl = toQueryUrl(queryEndpoint, filter); const response = await (options.fetch ?? fetch)(queryUrl); if (!response.ok) { throw handleErrorResponse( diff --git a/src/index.ts b/src/index.ts index bda9da182..b3d25304e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,6 +46,10 @@ export { CredentialIsAccessGrantAny } from "./type/AccessGrant"; export { approveAccessRequest, cancelAccessRequest, + CredentialFilter, + CredentialResult, + CredentialStatus, + CredentialType, denyAccessRequest, getAccessApiEndpoint, getAccessGrant, @@ -55,6 +59,7 @@ export { getAccessManagementUi, getAccessRequestFromRedirectUrl, issueAccessRequest, + query, redirectToAccessManagementUi, redirectToRequestor, revokeAccessGrant, @@ -63,12 +68,6 @@ export { REQUEST_VC_URL_PARAM_NAME, } from "./gConsent"; -export { - query, - CredentialFilter, - CredentialResult, -} from "./gConsent/query/query"; - // Add an API object to the exports to allow explicitly relying on the gConsent-based // functions even when not relying on named exports. export * as gConsent from "./gConsent";