{"version":3,"file":"W3cV2Credential.mjs","names":[],"sources":["../../../../../src/modules/vc/models/credential/W3cV2Credential.ts"],"sourcesContent":["import { Expose, plainToClassFromExist, Type } from 'class-transformer'\nimport { IsOptional, IsRFC3339, ValidateNested } from 'class-validator'\nimport type { JsonObject, SingleOrArray } from '../../../../types'\nimport { asArray, JsonTransformer, mapSingleOrArray } from '../../../../utils'\nimport {\n  IsInstanceOrArrayOfInstances,\n  IsNever,\n  IsStringOrInstanceOrArrayOfInstances,\n  IsUri,\n} from '../../../../utils/validators'\nimport { CREDENTIALS_CONTEXT_V2_URL } from '../../constants'\nimport { IsCredentialJsonLdContext, IsCredentialType } from '../../validators'\nimport { W3cV2CredentialSchema, type W3cV2CredentialSchemaOptions } from './W3cV2CredentialSchema'\nimport { W3cV2CredentialStatus, type W3cV2CredentialStatusOptions } from './W3cV2CredentialStatus'\nimport { W3cV2CredentialSubject, type W3cV2CredentialSubjectOptions } from './W3cV2CredentialSubject'\nimport { W3cV2Evidence, type W3cV2EvidenceOptions } from './W3cV2Evidence'\nimport { IsW3cV2Issuer, W3cV2Issuer, type W3cV2IssuerOptions, W3cV2IssuerTransformer } from './W3cV2Issuer'\nimport type { W3cV2JsonCredential } from './W3cV2JsonCredential'\nimport {\n  W3cV2LocalizedValue,\n  type W3cV2LocalizedValueOptions,\n  W3cV2LocalizedValueTransformer,\n} from './W3cV2LocalizedValue'\nimport { W3cV2RefreshService, type W3cV2RefreshServiceOptions } from './W3cV2RefreshService'\nimport { W3cV2TermsOfUse, type W3cV2TermsOfUseOptions } from './W3cV2TermsOfUse'\n\nexport interface W3cV2CredentialOptions {\n  context?: Array<string | JsonObject>\n  id?: string\n  type: Array<string>\n  name?: string | SingleOrArray<W3cV2LocalizedValueOptions>\n  description?: string | SingleOrArray<W3cV2LocalizedValueOptions>\n  issuer: string | W3cV2IssuerOptions\n  credentialSubject: SingleOrArray<W3cV2CredentialSubjectOptions>\n  validFrom?: string\n  validUntil?: string\n  credentialStatus?: SingleOrArray<W3cV2CredentialStatusOptions>\n  credentialSchema?: SingleOrArray<W3cV2CredentialSchemaOptions>\n  refreshService?: SingleOrArray<W3cV2RefreshServiceOptions>\n  termsOfUse?: SingleOrArray<W3cV2TermsOfUseOptions>\n  evidence?: SingleOrArray<W3cV2EvidenceOptions>\n  [property: string]: unknown\n}\n\nexport class W3cV2Credential {\n  public constructor(options: W3cV2CredentialOptions) {\n    if (options) {\n      const {\n        context,\n        id,\n        type,\n        name,\n        description,\n        issuer,\n        credentialSubject,\n        validFrom,\n        validUntil,\n        credentialStatus,\n        credentialSchema,\n        refreshService,\n        termsOfUse,\n        evidence,\n        ...rest\n      } = options\n\n      plainToClassFromExist(this, rest)\n\n      this.context = context ?? [CREDENTIALS_CONTEXT_V2_URL]\n      this.id = id\n      this.type = type || ['VerifiableCredential']\n\n      if (name) {\n        this.name =\n          typeof name === 'string'\n            ? name\n            : mapSingleOrArray(name, (n) => (n instanceof W3cV2LocalizedValue ? n : new W3cV2LocalizedValue(n)))\n      }\n\n      if (description) {\n        this.description =\n          typeof description === 'string'\n            ? description\n            : mapSingleOrArray(description, (n) => (n instanceof W3cV2LocalizedValue ? n : new W3cV2LocalizedValue(n)))\n      }\n\n      this.issuer = typeof issuer === 'string' || issuer instanceof W3cV2Issuer ? issuer : new W3cV2Issuer(issuer)\n\n      this.credentialSubject = mapSingleOrArray(credentialSubject, (subject) =>\n        subject instanceof W3cV2CredentialSubject\n          ? subject\n          : // NOTE: type assertion is needed here since TS cannot properly infer the type.\n            new W3cV2CredentialSubject(subject as W3cV2CredentialSubjectOptions)\n      )\n\n      this.validFrom = validFrom\n      this.validUntil = validUntil\n\n      if (credentialStatus) {\n        this.credentialStatus = mapSingleOrArray(credentialStatus, (status) =>\n          status instanceof W3cV2CredentialStatus ? status : new W3cV2CredentialStatus(status)\n        )\n      }\n\n      if (credentialSchema) {\n        this.credentialSchema = mapSingleOrArray(credentialSchema, (schema) =>\n          schema instanceof W3cV2CredentialSchema ? schema : new W3cV2CredentialSchema(schema)\n        )\n      }\n\n      if (refreshService) {\n        this.refreshService = mapSingleOrArray(refreshService, (service) =>\n          service instanceof W3cV2RefreshService ? service : new W3cV2RefreshService(service)\n        )\n      }\n\n      if (termsOfUse) {\n        this.termsOfUse = mapSingleOrArray(termsOfUse, (tos) =>\n          tos instanceof W3cV2TermsOfUse ? tos : new W3cV2TermsOfUse(tos)\n        )\n      }\n\n      if (evidence) {\n        this.evidence = mapSingleOrArray(evidence, (evidence) =>\n          evidence instanceof W3cV2Evidence ? evidence : new W3cV2Evidence(evidence)\n        )\n      }\n    }\n  }\n\n  @Expose({ name: '@context' })\n  @IsCredentialJsonLdContext({ credentialContext: CREDENTIALS_CONTEXT_V2_URL })\n  public context!: Array<string | JsonObject>\n\n  @IsOptional()\n  @IsUri()\n  public id?: string\n\n  @IsCredentialType()\n  public type!: SingleOrArray<string>\n\n  @IsOptional()\n  @IsStringOrInstanceOrArrayOfInstances({ classType: W3cV2LocalizedValue, allowEmptyArray: true })\n  @W3cV2LocalizedValueTransformer()\n  public name?: string | SingleOrArray<W3cV2LocalizedValue>\n\n  @IsOptional()\n  @IsStringOrInstanceOrArrayOfInstances({ classType: W3cV2LocalizedValue, allowEmptyArray: true })\n  @W3cV2LocalizedValueTransformer()\n  public description?: string | SingleOrArray<W3cV2LocalizedValue>\n\n  @IsW3cV2Issuer()\n  @W3cV2IssuerTransformer()\n  public issuer!: string | W3cV2Issuer\n\n  @Type(() => W3cV2CredentialSubject)\n  @ValidateNested({ each: true })\n  @IsInstanceOrArrayOfInstances({ classType: W3cV2CredentialSubject })\n  public credentialSubject!: SingleOrArray<W3cV2CredentialSubject>\n\n  @IsRFC3339()\n  @IsOptional()\n  public validFrom?: string\n\n  @IsRFC3339()\n  @IsOptional()\n  public validUntil?: string\n\n  @IsOptional()\n  @Type(() => W3cV2CredentialStatus)\n  @ValidateNested({ each: true })\n  @IsInstanceOrArrayOfInstances({ classType: W3cV2CredentialStatus, allowEmptyArray: true })\n  public credentialStatus?: SingleOrArray<W3cV2CredentialStatus>\n\n  @IsOptional()\n  @Type(() => W3cV2CredentialSchema)\n  @ValidateNested({ each: true })\n  @IsInstanceOrArrayOfInstances({ classType: W3cV2CredentialSchema, allowEmptyArray: true })\n  public credentialSchema?: SingleOrArray<W3cV2CredentialSchema>\n\n  @IsOptional()\n  @Type(() => W3cV2RefreshService)\n  @ValidateNested({ each: true })\n  @IsInstanceOrArrayOfInstances({ classType: W3cV2RefreshService, allowEmptyArray: true })\n  public refreshService?: SingleOrArray<W3cV2RefreshService>\n\n  @IsOptional()\n  @Type(() => W3cV2Evidence)\n  @ValidateNested({ each: true })\n  @IsInstanceOrArrayOfInstances({ classType: W3cV2Evidence, allowEmptyArray: true })\n  public evidence?: SingleOrArray<W3cV2Evidence>\n\n  @IsOptional()\n  @Type(() => W3cV2TermsOfUse)\n  @ValidateNested({ each: true })\n  @IsInstanceOrArrayOfInstances({ classType: W3cV2TermsOfUse, allowEmptyArray: true })\n  public termsOfUse?: SingleOrArray<W3cV2TermsOfUse>\n\n  @IsNever()\n  public vc?: never\n\n  @IsNever()\n  public vp?: never;\n\n  [property: string]: unknown\n\n  public get issuerId(): string {\n    return this.issuer instanceof W3cV2Issuer ? this.issuer.id : this.issuer\n  }\n\n  public get credentialSchemaIds(): string[] {\n    if (!this.credentialSchema) {\n      return []\n    }\n\n    if (Array.isArray(this.credentialSchema)) {\n      return this.credentialSchema.map((credentialSchema) => credentialSchema.id)\n    }\n\n    return [this.credentialSchema.id]\n  }\n\n  public get credentialSubjectIds(): string[] {\n    if (Array.isArray(this.credentialSubject)) {\n      return this.credentialSubject\n        .map((credentialSubject) => credentialSubject.id)\n        .filter((v): v is string => v !== undefined)\n    }\n\n    return this.credentialSubject.id ? [this.credentialSubject.id] : []\n  }\n\n  public get contexts(): Array<string | JsonObject> {\n    return asArray(this.context)\n  }\n\n  public toJSON(): W3cV2JsonCredential {\n    return JsonTransformer.toJSON(this) as W3cV2JsonCredential\n  }\n}\n"],"mappings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