Skip to content

Commit

Permalink
Name intermediary types
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Dec 17, 2024
1 parent 0f4546d commit 251b6b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
8 changes: 8 additions & 0 deletions src/gConsent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export {
getAccessGrantFromRedirectUrl,
} from "./request";

export {
CredentialFilter,
CredentialResult,
CredentialStatus,
CredentialType,
query,
} from "./query/query";

export {
approveAccessRequest,
denyAccessRequest,
Expand Down
53 changes: 28 additions & 25 deletions src/gConsent/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof CredentialFilter> = [
"fromAgent",
"issuedWithin",
"page",
Expand All @@ -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<infer E> ? 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;
Expand Down Expand Up @@ -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) => {
Expand All @@ -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());
});
Expand All @@ -151,7 +154,7 @@ export async function query(
fetch?: typeof fetch;
},
): Promise<CredentialResult> {
const queryUrl = addQueryParams(filter, queryEndpoint);
const queryUrl = toQueryUrl(queryEndpoint, filter);
const response = await (options.fetch ?? fetch)(queryUrl);
if (!response.ok) {
throw handleErrorResponse(
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export { CredentialIsAccessGrantAny } from "./type/AccessGrant";
export {
approveAccessRequest,
cancelAccessRequest,
CredentialFilter,
CredentialResult,
CredentialStatus,
CredentialType,
denyAccessRequest,
getAccessApiEndpoint,
getAccessGrant,
Expand All @@ -55,6 +59,7 @@ export {
getAccessManagementUi,
getAccessRequestFromRedirectUrl,
issueAccessRequest,
query,
redirectToAccessManagementUi,
redirectToRequestor,
revokeAccessGrant,
Expand All @@ -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";
Expand Down

0 comments on commit 251b6b5

Please sign in to comment.