@credo-ts/core
Version:
<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1
165 lines (164 loc) • 7.58 kB
JavaScript
import { asArray, mapSingleOrArray } from "../../../../utils/array.mjs";
import { JsonTransformer } from "../../../../utils/JsonTransformer.mjs";
import { IsInstanceOrArrayOfInstances, IsNever, IsStringOrInstanceOrArrayOfInstances, IsUri } from "../../../../utils/validators.mjs";
import "../../../../utils/index.mjs";
import __decorateMetadata from "../../../../_virtual/_@oxc-project_runtime@0.151.0/helpers/esm/decorateMetadata.mjs";
import __decorate from "../../../../_virtual/_@oxc-project_runtime@0.151.0/helpers/esm/decorate.mjs";
import { CREDENTIALS_CONTEXT_V2_URL } from "../../constants.mjs";
import { IsCredentialJsonLdContext, IsCredentialType } from "../../validators.mjs";
import { W3cV2CredentialSchema } from "./W3cV2CredentialSchema.mjs";
import { W3cV2CredentialStatus } from "./W3cV2CredentialStatus.mjs";
import { W3cV2CredentialSubject } from "./W3cV2CredentialSubject.mjs";
import { W3cV2Evidence } from "./W3cV2Evidence.mjs";
import { IsW3cV2Issuer, W3cV2Issuer, W3cV2IssuerTransformer } from "./W3cV2Issuer.mjs";
import { W3cV2LocalizedValue, W3cV2LocalizedValueTransformer } from "./W3cV2LocalizedValue.mjs";
import { W3cV2RefreshService } from "./W3cV2RefreshService.mjs";
import { W3cV2TermsOfUse } from "./W3cV2TermsOfUse.mjs";
import { Expose, Type, plainToClassFromExist } from "class-transformer";
import { IsOptional, IsRFC3339, ValidateNested } from "class-validator";
//#region src/modules/vc/models/credential/W3cV2Credential.ts
var W3cV2Credential = class {
constructor(options) {
if (options) {
const { context, id, type, name, description, issuer, credentialSubject, validFrom, validUntil, credentialStatus, credentialSchema, refreshService, termsOfUse, evidence, ...rest } = options;
plainToClassFromExist(this, rest);
this.context = context ?? ["https://www.w3.org/ns/credentials/v2"];
this.id = id;
this.type = type || ["VerifiableCredential"];
if (name) this.name = typeof name === "string" ? name : mapSingleOrArray(name, (n) => n instanceof W3cV2LocalizedValue ? n : new W3cV2LocalizedValue(n));
if (description) this.description = typeof description === "string" ? description : mapSingleOrArray(description, (n) => n instanceof W3cV2LocalizedValue ? n : new W3cV2LocalizedValue(n));
this.issuer = typeof issuer === "string" || issuer instanceof W3cV2Issuer ? issuer : new W3cV2Issuer(issuer);
this.credentialSubject = mapSingleOrArray(credentialSubject, (subject) => subject instanceof W3cV2CredentialSubject ? subject : new W3cV2CredentialSubject(subject));
this.validFrom = validFrom;
this.validUntil = validUntil;
if (credentialStatus) this.credentialStatus = mapSingleOrArray(credentialStatus, (status) => status instanceof W3cV2CredentialStatus ? status : new W3cV2CredentialStatus(status));
if (credentialSchema) this.credentialSchema = mapSingleOrArray(credentialSchema, (schema) => schema instanceof W3cV2CredentialSchema ? schema : new W3cV2CredentialSchema(schema));
if (refreshService) this.refreshService = mapSingleOrArray(refreshService, (service) => service instanceof W3cV2RefreshService ? service : new W3cV2RefreshService(service));
if (termsOfUse) this.termsOfUse = mapSingleOrArray(termsOfUse, (tos) => tos instanceof W3cV2TermsOfUse ? tos : new W3cV2TermsOfUse(tos));
if (evidence) this.evidence = mapSingleOrArray(evidence, (evidence) => evidence instanceof W3cV2Evidence ? evidence : new W3cV2Evidence(evidence));
}
}
get issuerId() {
return this.issuer instanceof W3cV2Issuer ? this.issuer.id : this.issuer;
}
get credentialSchemaIds() {
if (!this.credentialSchema) return [];
if (Array.isArray(this.credentialSchema)) return this.credentialSchema.map((credentialSchema) => credentialSchema.id);
return [this.credentialSchema.id];
}
get credentialSubjectIds() {
if (Array.isArray(this.credentialSubject)) return this.credentialSubject.map((credentialSubject) => credentialSubject.id).filter((v) => v !== void 0);
return this.credentialSubject.id ? [this.credentialSubject.id] : [];
}
get contexts() {
return asArray(this.context);
}
toJSON() {
return JsonTransformer.toJSON(this);
}
};
__decorate([
Expose({ name: "@context" }),
IsCredentialJsonLdContext({ credentialContext: CREDENTIALS_CONTEXT_V2_URL }),
__decorateMetadata("design:type", typeof Array === "undefined" ? Object : Array)
], W3cV2Credential.prototype, "context", void 0);
__decorate([
IsOptional(),
IsUri(),
__decorateMetadata("design:type", String)
], W3cV2Credential.prototype, "id", void 0);
__decorate([IsCredentialType(), __decorateMetadata("design:type", Object)], W3cV2Credential.prototype, "type", void 0);
__decorate([
IsOptional(),
IsStringOrInstanceOrArrayOfInstances({
classType: W3cV2LocalizedValue,
allowEmptyArray: true
}),
W3cV2LocalizedValueTransformer(),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "name", void 0);
__decorate([
IsOptional(),
IsStringOrInstanceOrArrayOfInstances({
classType: W3cV2LocalizedValue,
allowEmptyArray: true
}),
W3cV2LocalizedValueTransformer(),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "description", void 0);
__decorate([
IsW3cV2Issuer(),
W3cV2IssuerTransformer(),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "issuer", void 0);
__decorate([
Type(() => W3cV2CredentialSubject),
ValidateNested({ each: true }),
IsInstanceOrArrayOfInstances({ classType: W3cV2CredentialSubject }),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "credentialSubject", void 0);
__decorate([
IsRFC3339(),
IsOptional(),
__decorateMetadata("design:type", String)
], W3cV2Credential.prototype, "validFrom", void 0);
__decorate([
IsRFC3339(),
IsOptional(),
__decorateMetadata("design:type", String)
], W3cV2Credential.prototype, "validUntil", void 0);
__decorate([
IsOptional(),
Type(() => W3cV2CredentialStatus),
ValidateNested({ each: true }),
IsInstanceOrArrayOfInstances({
classType: W3cV2CredentialStatus,
allowEmptyArray: true
}),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "credentialStatus", void 0);
__decorate([
IsOptional(),
Type(() => W3cV2CredentialSchema),
ValidateNested({ each: true }),
IsInstanceOrArrayOfInstances({
classType: W3cV2CredentialSchema,
allowEmptyArray: true
}),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "credentialSchema", void 0);
__decorate([
IsOptional(),
Type(() => W3cV2RefreshService),
ValidateNested({ each: true }),
IsInstanceOrArrayOfInstances({
classType: W3cV2RefreshService,
allowEmptyArray: true
}),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "refreshService", void 0);
__decorate([
IsOptional(),
Type(() => W3cV2Evidence),
ValidateNested({ each: true }),
IsInstanceOrArrayOfInstances({
classType: W3cV2Evidence,
allowEmptyArray: true
}),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "evidence", void 0);
__decorate([
IsOptional(),
Type(() => W3cV2TermsOfUse),
ValidateNested({ each: true }),
IsInstanceOrArrayOfInstances({
classType: W3cV2TermsOfUse,
allowEmptyArray: true
}),
__decorateMetadata("design:type", Object)
], W3cV2Credential.prototype, "termsOfUse", void 0);
__decorate([IsNever(), __decorateMetadata("design:type", void 0)], W3cV2Credential.prototype, "vc", void 0);
__decorate([IsNever(), __decorateMetadata("design:type", void 0)], W3cV2Credential.prototype, "vp", void 0);
//#endregion
export { W3cV2Credential };
//# sourceMappingURL=W3cV2Credential.mjs.map