Skip to content

Commit

Permalink
Refactor CustomFields type in its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
NSeydoux committed Dec 6, 2024
1 parent 163ea6c commit 7ea69be
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/gConsent/manage/approveAccessRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { getBaseAccess } from "../util/getBaseAccessVerifiableCredential";
import { initializeGrantParameters } from "../util/initializeGrantParameters";
import { getGrantBody, issueAccessVc } from "../util/issueAccessVc";
import { toVcDataset } from "../../common/util/toVcDataset";
import type { CustomFields } from "../request/issueAccessRequest";
import type { CustomFields } from "../type/CustomFields";

export type ApproveAccessRequestOverrides = Omit<
Omit<AccessGrantParameters, "status">,
Expand Down
2 changes: 1 addition & 1 deletion src/gConsent/request/issueAccessRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { jest, describe, it, expect, beforeAll } from "@jest/globals";
import type * as VcLibrary from "@inrupt/solid-client-vc";

import type { CustomFields } from "./issueAccessRequest";
import type { CustomFields } from "../type/CustomFields";
import {
issueAccessRequest,
normalizeAccessRequest,
Expand Down
37 changes: 1 addition & 36 deletions src/gConsent/request/issueAccessRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
IssueAccessRequestParameters,
} from "../type/IssueAccessRequestParameters";
import type { AccessRequest } from "../type/AccessRequest";
import { type CustomFields, toJson } from "../type/CustomFields";
import {
isAccessRequest,
isRdfjsAccessRequest,
Expand Down Expand Up @@ -73,42 +74,6 @@ export function normalizeAccessRequest<T extends VerifiableCredentialBase>(
return normalized as unknown as T;
}

export type CustomFields = {
/* The custom field name (this must be a URL). */
key: URL;
/* The custom field value (this must be a litteral). */
value: string | number | boolean;
};

/**
* Internal function to collapse the user-provided custom fields into
* a simple JSON object.
*
* @hidden
*/
export const toJson = (
c: Set<CustomFields> = new Set(),
): Record<string, CustomFields["value"]> => {
return (
Array.from(c)
// Check that all the provided custom fields match the expected type,
// and change the validated CustomField into a plain JSON object entry.
.map((field) => {
if (typeof field.key !== "object") {
throw new Error(
`All custom fields keys must be URL objects, found ${field.key}`,
);
}
return { [`${field.key.toString()}`]: field.value };
})
// Collapse all the JSON object entries into a single object.
.reduce(
(acc, cur) => Object.assign(acc, cur),
{} as Record<string, CustomFields["value"]>,
)
);
};

/**
* Request access to a given Resource.
*
Expand Down
35 changes: 35 additions & 0 deletions src/gConsent/type/CustomFields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export type CustomFields = {
/* The custom field name (this must be a URL). */
key: URL;
/* The custom field value (this must be a litteral). */
value: string | number | boolean;
};

/**
* Internal function to collapse the user-provided custom fields into
* a simple JSON object.
*
* @hidden
*/
export const toJson = (
c: Set<CustomFields> = new Set(),
): Record<string, CustomFields["value"]> => {
return (
Array.from(c)
// Check that all the provided custom fields match the expected type,
// and change the validated CustomField into a plain JSON object entry.
.map((field) => {
if (typeof field.key !== "object") {
throw new Error(
`All custom fields keys must be URL objects, found ${field.key}`,
);
}
return { [`${field.key.toString()}`]: field.value };
})
// Collapse all the JSON object entries into a single object.
.reduce(
(acc, cur) => Object.assign(acc, cur),
{} as Record<string, CustomFields["value"]>,
)
);
};
2 changes: 1 addition & 1 deletion src/gConsent/util/access.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
MOCK_CONTEXT,
} from "../constants";
import { normalizeAccessGrant } from "../manage/approveAccessRequest";
import type { CustomFields } from "../request/issueAccessRequest";
import type { CustomFields } from "../type/CustomFields";
import { normalizeAccessRequest } from "../request/issueAccessRequest";
import type { AccessGrant } from "../type/AccessGrant";
import type { AccessRequest } from "../type/AccessRequest";
Expand Down
2 changes: 1 addition & 1 deletion src/gConsent/util/initializeGrantParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
} from "../../common/getters";
import type { ApproveAccessRequestOverrides } from "../manage/approveAccessRequest";
import { INHERIT, XSD_BOOLEAN } from "../../common/constants";
import { toJson } from "../request/issueAccessRequest";
import { toJson } from "../type/CustomFields";

const { quad, literal, defaultGraph } = DataFactory;

Expand Down
2 changes: 1 addition & 1 deletion src/gConsent/util/issueAccessVc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { getAccessApiEndpoint } from "../discover/getAccessApiEndpoint";
import { accessToResourceAccessModeArray } from "./accessToResourceAccessModeArray";
import { isBaseRequest } from "../guard/isBaseRequest";
import type { AccessCredentialType } from "../type/AccessCredentialType";
import type { CustomFields } from "../request/issueAccessRequest";
import type { CustomFields } from "../type/CustomFields";

function getGConsentAttributes(
params: AccessRequestParameters,
Expand Down

0 comments on commit 7ea69be

Please sign in to comment.