From ced8c3ec0f925408a266dbdafb2587e956704f8f Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Sun, 26 Nov 2023 00:02:12 +0000 Subject: [PATCH] chore: fix lint errors --- e2e/node/e2e.test.ts | 71 +++++++---------- src/common/common.ts | 176 +++++++++++++++++++++--------------------- src/common/getters.ts | 7 +- src/issue/issue.ts | 1 + src/lookup/derive.ts | 3 +- src/lookup/query.ts | 57 +++++++------- 6 files changed, 147 insertions(+), 168 deletions(-) diff --git a/e2e/node/e2e.test.ts b/e2e/node/e2e.test.ts index 285e2d2c..a2d77820 100644 --- a/e2e/node/e2e.test.ts +++ b/e2e/node/e2e.test.ts @@ -260,13 +260,9 @@ describe("End-to-end verifiable credentials tests for environment", () => { ]); expect(credential1.credentialSubject.id).toBe(vcSubject); - expect(getCredentialSubject(credential1).value).toBe( - vcSubject, - ); + expect(getCredentialSubject(credential1).value).toBe(vcSubject); expect(credential2.credentialSubject.id).toBe(vcSubject); - expect(getCredentialSubject(credential2).value).toBe( - vcSubject, - ); + expect(getCredentialSubject(credential2).value).toBe(vcSubject); const matcher = { "@context": [ @@ -280,50 +276,36 @@ describe("End-to-end verifiable credentials tests for environment", () => { forPurpose: purpose, }, }, - } + }; - const [allDeprecated, allNew] = await Promise.all([getVerifiableCredentialAllFromShape( - derivationService, - matcher, - { + const [allDeprecated, allNew] = await Promise.all([ + getVerifiableCredentialAllFromShape(derivationService, matcher, { fetch: session.fetch, includeExpiredVc: false, - }, - ), getVerifiableCredentialAllFromShape( - derivationService, - matcher, - { + }), + getVerifiableCredentialAllFromShape(derivationService, matcher, { fetch: session.fetch, includeExpiredVc: false, returnLegacyJsonld: false, - }, - )]); + }), + ]); expect(allDeprecated).toHaveLength(2); - expect(allDeprecated[0].credentialSubject.id).toBe(vcSubject); - expect(getCredentialSubject(allDeprecated[0]).value).toBe( - vcSubject, - ); + expect(getCredentialSubject(allDeprecated[0]).value).toBe(vcSubject); expect(allDeprecated[1].credentialSubject.id).toBe(vcSubject); - expect(getCredentialSubject(allDeprecated[1]).value).toBe( - vcSubject, - ); + expect(getCredentialSubject(allDeprecated[1]).value).toBe(vcSubject); expect(allNew).toHaveLength(2); - + // @ts-expect-error the credentialSubject property should not exist if legacy json is disabled - expect(allNew[0].credentialSubject).toBe(undefined); - expect(getCredentialSubject(allNew[0]).value).toBe( - vcSubject, - ); + expect(allNew[0].credentialSubject).toBeUndefined(); + expect(getCredentialSubject(allNew[0]).value).toBe(vcSubject); // @ts-expect-error the credentialSubject property should not exist if legacy json is disabled - expect(allNew[1].credentialSubject).toBe(undefined); - expect(getCredentialSubject(allNew[1]).value).toBe( - vcSubject, - ); + expect(allNew[1].credentialSubject).toBeUndefined(); + expect(getCredentialSubject(allNew[1]).value).toBe(vcSubject); await expect( getVerifiableCredentialAllFromShape(derivationService, credential1, { @@ -351,18 +333,19 @@ describe("End-to-end verifiable credentials tests for environment", () => { }), ).resolves.toHaveLength(1); - const [credential1FetchedLegacy, credential1Fetched] = await Promise.all([getVerifiableCredential(credential1.id, { - fetch: session.fetch, - }), getVerifiableCredential(credential1.id, { - fetch: session.fetch, - returnLegacyJsonld: false - })]); + const [credential1FetchedLegacy, credential1Fetched] = await Promise.all([ + getVerifiableCredential(credential1.id, { + fetch: session.fetch, + }), + getVerifiableCredential(credential1.id, { + fetch: session.fetch, + returnLegacyJsonld: false, + }), + ]); // @ts-expect-error the credentialSubject property should not exist if legacy json is disabled - expect(credential1Fetched.credentialSubject).toBe(undefined); - expect(getCredentialSubject(credential1Fetched).value).toBe( - vcSubject, - ); + expect(credential1Fetched.credentialSubject).toBeUndefined(); + expect(getCredentialSubject(credential1Fetched).value).toBe(vcSubject); expect(credential1FetchedLegacy.credentialSubject.id).toBe(vcSubject); expect(getCredentialSubject(credential1FetchedLegacy).value).toBe( vcSubject, diff --git a/src/common/common.ts b/src/common/common.ts index 94923b87..1950d037 100644 --- a/src/common/common.ts +++ b/src/common/common.ts @@ -445,60 +445,15 @@ export async function getVerifiableCredentialApiConfiguration( }; } -/** - * @hidden - */ -export async function verifiableCredentialToDataset< - T extends Object & { id?: string }, ->( - vc: T, - options?: ParseOptions & { - includeVcProperties: true; - }, -): Promise; -export async function verifiableCredentialToDataset< - T extends Object & { id?: string }, ->( +// eslint-disable-next-line camelcase +export function internal_applyDataset( vc: T, + store: DatasetCore, options?: ParseOptions & { includeVcProperties?: boolean; + additionalProperties?: Record; }, -): Promise; -export async function verifiableCredentialToDataset< - T extends Object & { id: string }, ->( - vc: T, - options?: ParseOptions & { - includeVcProperties?: boolean; - }, -): Promise { - let store: DatasetCore; - try { - store = await jsonLdToStore(vc, options); - } catch (e) { - throw new Error( - `Parsing the Verifiable Credential as JSON-LD failed: ${e}`, - ); - } - - if (typeof vc.id !== "string") { - throw new Error( - `Expected vc.id to be a string, found [${ - vc.id - }] of type [${typeof vc.id}] on ${JSON.stringify(vc, null, 2)}`, - ); - } - - return internal_applyDataset(vc, store, options) -} - -export function internal_applyDataset( -vc: T, -store: DatasetCore, -options?: ParseOptions & { - includeVcProperties?: boolean; - additionalProperties?: Record; -}): DatasetWithId { +): DatasetWithId { return Object.freeze({ id: vc.id, ...(options?.includeVcProperties && vc), @@ -532,60 +487,47 @@ options?: ParseOptions & { } /** - * Dereference a VC URL, and verify that the resulting content is valid. - * - * @param vcUrl The URL of the VC. - * @param options Options to customize the function behavior. - * - options.fetch: Specify a WHATWG-compatible authenticated fetch. - * - options.returnLegacyJsonld: Include the normalized JSON-LD in the response - * @returns The dereferenced VC if valid. Throws otherwise. - * @since 0.4.0 - * @deprecated Deprecated in favour of setting returnLegacyJsonld: false. This will be the default value in future - * versions of this library. + * @hidden */ -export async function getVerifiableCredential( - vcUrl: UrlString, +export async function verifiableCredentialToDataset( + vc: T, options?: ParseOptions & { - fetch?: typeof fetch; - returnLegacyJsonld?: true; + includeVcProperties: true; }, -): Promise; -/** - * Dereference a VC URL, and verify that the resulting content is valid. - * - * @param vcUrl The URL of the VC. - * @param options Options to customize the function behavior. - * - options.fetch: Specify a WHATWG-compatible authenticated fetch. - * - options.returnLegacyJsonld: Include the normalized JSON-LD in the response - * @returns The dereferenced VC if valid. Throws otherwise. - * @since 0.4.0 - */ -export async function getVerifiableCredential( - vcUrl: UrlString, +): Promise; +export async function verifiableCredentialToDataset( + vc: T, options?: ParseOptions & { - fetch?: typeof fetch; - returnLegacyJsonld?: boolean; + includeVcProperties?: boolean; }, ): Promise; -export async function getVerifiableCredential( - vcUrl: UrlString, +export async function verifiableCredentialToDataset( + vc: T, options?: ParseOptions & { - fetch?: typeof fetch; - returnLegacyJsonld?: boolean; + includeVcProperties?: boolean; }, ): Promise { - const authFetch = options?.fetch ?? uniFetch; - const response = await authFetch(vcUrl); + let store: DatasetCore; + try { + store = await jsonLdToStore(vc, options); + } catch (e) { + throw new Error( + `Parsing the Verifiable Credential as JSON-LD failed: ${e}`, + ); + } - if (!response.ok) { + if (typeof vc.id !== "string") { throw new Error( - `Fetching the Verifiable Credential [${vcUrl}] failed: ${response.status} ${response.statusText}`, + `Expected vc.id to be a string, found [${ + vc.id + }] of type [${typeof vc.id}] on ${JSON.stringify(vc, null, 2)}`, ); } - return internal_getVerifiableCredentialFromResponse(vcUrl, response, options); + return internal_applyDataset(vc as { id: string }, store, options); } +// eslint-disable-next-line camelcase export async function internal_getVerifiableCredentialFromResponse( vcUrl: UrlString | undefined, response: Response, @@ -601,7 +543,7 @@ export async function internal_getVerifiableCredentialFromResponse( }, ): Promise; export async function internal_getVerifiableCredentialFromResponse( - vcUrl: UrlString | undefined, + vcUrlInput: UrlString | undefined, response: Response, options?: ParseOptions & { returnLegacyJsonld?: boolean; @@ -609,6 +551,7 @@ export async function internal_getVerifiableCredentialFromResponse( ): Promise { const returnLegacy = options?.returnLegacyJsonld !== false; let vc: unknown | VerifiableCredentialBase; + let vcUrl = vcUrlInput; try { vc = await response.json(); @@ -667,3 +610,58 @@ export async function internal_getVerifiableCredentialFromResponse( } return parsedVc; } + +/** + * Dereference a VC URL, and verify that the resulting content is valid. + * + * @param vcUrl The URL of the VC. + * @param options Options to customize the function behavior. + * - options.fetch: Specify a WHATWG-compatible authenticated fetch. + * - options.returnLegacyJsonld: Include the normalized JSON-LD in the response + * @returns The dereferenced VC if valid. Throws otherwise. + * @since 0.4.0 + * @deprecated Deprecated in favour of setting returnLegacyJsonld: false. This will be the default value in future + * versions of this library. + */ +export async function getVerifiableCredential( + vcUrl: UrlString, + options?: ParseOptions & { + fetch?: typeof fetch; + returnLegacyJsonld?: true; + }, +): Promise; +/** + * Dereference a VC URL, and verify that the resulting content is valid. + * + * @param vcUrl The URL of the VC. + * @param options Options to customize the function behavior. + * - options.fetch: Specify a WHATWG-compatible authenticated fetch. + * - options.returnLegacyJsonld: Include the normalized JSON-LD in the response + * @returns The dereferenced VC if valid. Throws otherwise. + * @since 0.4.0 + */ +export async function getVerifiableCredential( + vcUrl: UrlString, + options?: ParseOptions & { + fetch?: typeof fetch; + returnLegacyJsonld?: boolean; + }, +): Promise; +export async function getVerifiableCredential( + vcUrl: UrlString, + options?: ParseOptions & { + fetch?: typeof fetch; + returnLegacyJsonld?: boolean; + }, +): Promise { + const authFetch = options?.fetch ?? uniFetch; + const response = await authFetch(vcUrl); + + if (!response.ok) { + throw new Error( + `Fetching the Verifiable Credential [${vcUrl}] failed: ${response.status} ${response.statusText}`, + ); + } + + return internal_getVerifiableCredentialFromResponse(vcUrl, response, options); +} diff --git a/src/common/getters.ts b/src/common/getters.ts index 6caa888a..4bba0827 100644 --- a/src/common/getters.ts +++ b/src/common/getters.ts @@ -18,12 +18,7 @@ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -import type { - BlankNode, - DatasetCore, - Literal, - NamedNode -} from "@rdfjs/types"; +import type { BlankNode, DatasetCore, Literal, NamedNode } from "@rdfjs/types"; import { DataFactory } from "n3"; import { isUrl } from "./common"; import { cred, dc, rdf, sec, xsd } from "./constants"; diff --git a/src/issue/issue.ts b/src/issue/issue.ts index 75876f98..93b8a585 100644 --- a/src/issue/issue.ts +++ b/src/issue/issue.ts @@ -30,6 +30,7 @@ import { concatenateContexts, defaultContext, defaultCredentialTypes, + // eslint-disable-next-line camelcase internal_getVerifiableCredentialFromResponse, } from "../common/common"; import type { ParseOptions } from "../parser/jsonld"; diff --git a/src/lookup/derive.ts b/src/lookup/derive.ts index bc902ef4..0939212b 100644 --- a/src/lookup/derive.ts +++ b/src/lookup/derive.ts @@ -20,7 +20,6 @@ // import { fetch as fallbackFetch } from "@inrupt/universal-fetch"; -import { DataFactory } from "n3"; import type { Iri, VerifiableCredential, @@ -167,7 +166,7 @@ export async function getVerifiableCredentialAllFromShape( if (options?.returnLegacyJsonld === false) { const vp = await query(holderEndpoint, vpRequest, { fetch: options?.fetch ?? fallbackFetch, - returnLegacyJsonld: false + returnLegacyJsonld: false, }); return vp.verifiableCredential ?? []; } diff --git a/src/lookup/query.ts b/src/lookup/query.ts index 51a1ab16..74d58abc 100644 --- a/src/lookup/query.ts +++ b/src/lookup/query.ts @@ -21,6 +21,7 @@ import { fetch as fallbackFetch } from "@inrupt/universal-fetch"; import { DataFactory } from "n3"; +import { isRdfjsVerifiableCredential } from ".."; import type { Iri, VerifiableCredential, @@ -28,17 +29,14 @@ import type { VerifiablePresentation, } from "../common/common"; import { - internal_applyDataset, isVerifiablePresentation, normalizeVp, verifiableCredentialToDataset, } from "../common/common"; -import { jsonLdToStore, type ParseOptions } from "../parser/jsonld"; import type { DatasetWithId } from "../common/getters"; -import { isRdfjsVerifiableCredential, isRdfjsVerifiablePresentation } from ".."; -import { cred, rdf } from "../common/constants"; +import { type ParseOptions } from "../parser/jsonld"; -const { namedNode, defaultGraph } = DataFactory; +const { namedNode } = DataFactory; /** * Based on https://w3c-ccg.github.io/vp-request-spec/#query-by-example. @@ -140,8 +138,7 @@ export async function query( returnLegacyJsonld?: boolean; }>, ): Promise< - | ParsedVerifiablePresentation - | { verifiableCredential?: DatasetWithId[] } + ParsedVerifiablePresentation | { verifiableCredential?: DatasetWithId[] } > { const internalOptions = { ...options }; if (internalOptions.fetch === undefined) { @@ -185,7 +182,7 @@ export async function query( // )}`, // ); // } - + // // In the future we want to get rid of this and get the verifiableCredential ids from the store // // the reason we need this for now is because we need the verifiableCredential JSON object for // // the toJSON method. @@ -199,7 +196,7 @@ export async function query( // } // const c = internal_applyDataset(vc as { id: string }, store, options) - + // if (!isRdfjsVerifiableCredential(store, namedNode(c.id))) { // throw new Error(`[${c.id}] is not a valid Verifiable Credential`); // } @@ -220,7 +217,7 @@ export async function query( // All code below here should is deprecated let data; try { - data = await response.json() + data = await response.json(); if (options?.returnLegacyJsonld !== false) { data = normalizeVp(data); @@ -230,7 +227,10 @@ export async function query( `The holder [${queryEndpoint}] did not return a valid JSON response: parsing failed with error ${e}`, ); } - if (options?.returnLegacyJsonld !== false && !isVerifiablePresentation(data)) { + if ( + options?.returnLegacyJsonld !== false && + !isVerifiablePresentation(data) + ) { throw new Error( `The holder [${queryEndpoint}] did not return a Verifiable Presentation: ${JSON.stringify( data, @@ -246,24 +246,27 @@ export async function query( // https://github.com/inrupt/solid-client-vc-js/pull/849#discussion_r1377400688 // eslint-disable-next-line no-await-in-loop ...(await Promise.all( - data.verifiableCredential.slice(i, i + 100).map(async (vc: unknown) => { - if (typeof vc !== 'object' || vc === null) { - throw new Error(`Verifiable Credentail is an invalid object`); - } - - const res = await verifiableCredentialToDataset(vc, { - ...options, - includeVcProperties: options?.returnLegacyJsonld !== false, - }); + data.verifiableCredential + .slice(i, i + 100) + .map(async (vc: unknown) => { + if (typeof vc !== "object" || vc === null) { + throw new Error(`Verifiable Credentail is an invalid object`); + } + + const res = await verifiableCredentialToDataset(vc, { + ...options, + includeVcProperties: options?.returnLegacyJsonld !== false, + }); - // FIXME: Address the type issue here - if (!isRdfjsVerifiableCredential(res, namedNode(res.id))) { - throw new Error(`[${res.id}] is not a Valid Verifiable Credential`); - } + // FIXME: Address the type issue here + if (!isRdfjsVerifiableCredential(res, namedNode(res.id))) { + throw new Error( + `[${res.id}] is not a Valid Verifiable Credential`, + ); + } - return res; - } - ), + return res; + }), )), ); }