Skip to content

Commit

Permalink
chore: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Nov 26, 2023
1 parent 0562a67 commit ced8c3e
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 168 deletions.
71 changes: 27 additions & 44 deletions e2e/node/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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, {
Expand Down Expand Up @@ -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,
Expand Down
176 changes: 87 additions & 89 deletions src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T & DatasetWithId>;
export async function verifiableCredentialToDataset<
T extends Object & { id?: string },
>(
// eslint-disable-next-line camelcase
export function internal_applyDataset<T extends { id: string }>(
vc: T,
store: DatasetCore,
options?: ParseOptions & {
includeVcProperties?: boolean;
additionalProperties?: Record<string, unknown>;
},
): Promise<DatasetWithId>;
export async function verifiableCredentialToDataset<
T extends Object & { id: string },
>(
vc: T,
options?: ParseOptions & {
includeVcProperties?: boolean;
},
): Promise<DatasetWithId> {
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<T extends Object & { id: string }>(
vc: T,
store: DatasetCore,
options?: ParseOptions & {
includeVcProperties?: boolean;
additionalProperties?: Record<string, unknown>;
}): DatasetWithId {
): DatasetWithId {
return Object.freeze({
id: vc.id,
...(options?.includeVcProperties && vc),
Expand Down Expand Up @@ -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<T extends { id?: string }>(
vc: T,
options?: ParseOptions & {
fetch?: typeof fetch;
returnLegacyJsonld?: true;
includeVcProperties: true;
},
): Promise<VerifiableCredential>;
/**
* 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<T & DatasetWithId>;
export async function verifiableCredentialToDataset<T extends { id?: string }>(
vc: T,
options?: ParseOptions & {
fetch?: typeof fetch;
returnLegacyJsonld?: boolean;
includeVcProperties?: boolean;
},
): Promise<DatasetWithId>;
export async function getVerifiableCredential(
vcUrl: UrlString,
export async function verifiableCredentialToDataset<T extends { id?: string }>(
vc: T,
options?: ParseOptions & {
fetch?: typeof fetch;
returnLegacyJsonld?: boolean;
includeVcProperties?: boolean;
},
): Promise<DatasetWithId> {
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,
Expand All @@ -601,14 +543,15 @@ export async function internal_getVerifiableCredentialFromResponse(
},
): Promise<DatasetWithId>;
export async function internal_getVerifiableCredentialFromResponse(
vcUrl: UrlString | undefined,
vcUrlInput: UrlString | undefined,
response: Response,
options?: ParseOptions & {
returnLegacyJsonld?: boolean;
},
): Promise<DatasetWithId> {
const returnLegacy = options?.returnLegacyJsonld !== false;
let vc: unknown | VerifiableCredentialBase;
let vcUrl = vcUrlInput;
try {
vc = await response.json();

Expand Down Expand Up @@ -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<VerifiableCredential>;
/**
* 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<DatasetWithId>;
export async function getVerifiableCredential(
vcUrl: UrlString,
options?: ParseOptions & {
fetch?: typeof fetch;
returnLegacyJsonld?: boolean;
},
): Promise<DatasetWithId> {
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);
}
7 changes: 1 addition & 6 deletions src/common/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/issue/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
concatenateContexts,
defaultContext,
defaultCredentialTypes,
// eslint-disable-next-line camelcase
internal_getVerifiableCredentialFromResponse,
} from "../common/common";
import type { ParseOptions } from "../parser/jsonld";
Expand Down
3 changes: 1 addition & 2 deletions src/lookup/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
//

import { fetch as fallbackFetch } from "@inrupt/universal-fetch";
import { DataFactory } from "n3";
import type {
Iri,
VerifiableCredential,
Expand Down Expand Up @@ -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 ?? [];
}
Expand Down
Loading

0 comments on commit ced8c3e

Please sign in to comment.