{"version":3,"file":"W3cCredential.mjs","names":[],"sources":["../../../../../src/modules/vc/models/credential/W3cCredential.ts"],"sourcesContent":["import { Expose, Type } from 'class-transformer'\nimport { IsInstance, IsOptional, IsRFC3339, ValidateNested } from 'class-validator'\nimport type { JsonObject, SingleOrArray } from '../../../../types'\nimport { asArray, JsonTransformer, mapSingleOrArray } from '../../../../utils'\nimport { IsInstanceOrArrayOfInstances, IsUri } from '../../../../utils/validators'\nimport { CREDENTIALS_CONTEXT_V1_URL } from '../../constants'\nimport { IsCredentialJsonLdContext, IsCredentialType } from '../../validators'\nimport { W3cCredentialSchema } from './W3cCredentialSchema'\nimport { W3cCredentialStatus } from './W3cCredentialStatus'\nimport type { W3cCredentialSubjectOptions } from './W3cCredentialSubject'\nimport { IsW3cCredentialSubject, W3cCredentialSubject, W3cCredentialSubjectTransformer } from './W3cCredentialSubject'\nimport type { W3cIssuerOptions } from './W3cIssuer'\nimport { IsW3cIssuer, W3cIssuer, W3cIssuerTransformer } from './W3cIssuer'\n\nexport interface W3cCredentialOptions {\n  context?: Array<string | JsonObject>\n  id?: string\n  type: Array<string>\n  issuer: string | W3cIssuerOptions\n  issuanceDate: string\n  expirationDate?: string\n  credentialSubject: SingleOrArray<W3cCredentialSubjectOptions>\n  credentialStatus?: W3cCredentialStatus\n  credentialSchema?: SingleOrArray<W3cCredentialSchema>\n}\n\nexport class W3cCredential {\n  public constructor(options: W3cCredentialOptions) {\n    if (options) {\n      this.context = options.context ?? [CREDENTIALS_CONTEXT_V1_URL]\n      this.id = options.id\n      this.type = options.type || ['VerifiableCredential']\n      this.issuer =\n        typeof options.issuer === 'string' || options.issuer instanceof W3cIssuer\n          ? options.issuer\n          : new W3cIssuer(options.issuer)\n      this.issuanceDate = options.issuanceDate\n      this.expirationDate = options.expirationDate\n      this.credentialSubject = mapSingleOrArray(options.credentialSubject, (subject) =>\n        subject instanceof W3cCredentialSubject ? subject : new W3cCredentialSubject(subject)\n      )\n\n      if (options.credentialStatus) {\n        this.credentialStatus =\n          options.credentialStatus instanceof W3cCredentialStatus\n            ? options.credentialStatus\n            : new W3cCredentialStatus(options.credentialStatus)\n      }\n\n      if (options.credentialSchema) {\n        this.credentialSchema = mapSingleOrArray(options.credentialSchema, (schema) =>\n          schema instanceof W3cCredentialSchema ? schema : new W3cCredentialSchema(schema)\n        )\n      }\n    }\n  }\n\n  @Expose({ name: '@context' })\n  @IsCredentialJsonLdContext()\n  public context!: Array<string | JsonObject>\n\n  @IsOptional()\n  @IsUri()\n  public id?: string\n\n  @IsCredentialType()\n  public type!: Array<string>\n\n  @W3cIssuerTransformer()\n  @IsW3cIssuer()\n  public issuer!: string | W3cIssuer\n\n  @IsRFC3339()\n  public issuanceDate!: string\n\n  @IsRFC3339()\n  @IsOptional()\n  public expirationDate?: string\n\n  @IsW3cCredentialSubject({ each: true })\n  @W3cCredentialSubjectTransformer()\n  public credentialSubject!: SingleOrArray<W3cCredentialSubject>\n\n  @IsOptional()\n  @Type(() => W3cCredentialSchema)\n  @ValidateNested({ each: true })\n  @IsInstanceOrArrayOfInstances({ classType: W3cCredentialSchema, allowEmptyArray: true })\n  public credentialSchema?: SingleOrArray<W3cCredentialSchema>\n\n  @IsOptional()\n  @Type(() => W3cCredentialStatus)\n  @ValidateNested({ each: true })\n  @IsInstance(W3cCredentialStatus)\n  public credentialStatus?: W3cCredentialStatus\n\n  public get issuerId(): string {\n    return this.issuer instanceof W3cIssuer ? this.issuer.id : this.issuer\n  }\n\n  public get credentialSchemaIds(): string[] {\n    if (!this.credentialSchema) return []\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 static fromJson(json: Record<string, unknown>) {\n    return JsonTransformer.fromJSON(json, W3cCredential)\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AA0BA,IAAa,gBAAb,MAAa,cAAc;CACzB,AAAO,YAAY,SAA+B;AAChD,MAAI,SAAS;AACX,QAAK,UAAU,QAAQ,WAAW,CAAC,2BAA2B;AAC9D,QAAK,KAAK,QAAQ;AAClB,QAAK,OAAO,QAAQ,QAAQ,CAAC,uBAAuB;AACpD,QAAK,SACH,OAAO,QAAQ,WAAW,YAAY,QAAQ,kBAAkB,YAC5D,QAAQ,SACR,IAAI,UAAU,QAAQ,OAAO;AACnC,QAAK,eAAe,QAAQ;AAC5B,QAAK,iBAAiB,QAAQ;AAC9B,QAAK,oBAAoB,iBAAiB,QAAQ,oBAAoB,YACpE,mBAAmB,uBAAuB,UAAU,IAAI,qBAAqB,QAAQ,CACtF;AAED,OAAI,QAAQ,iBACV,MAAK,mBACH,QAAQ,4BAA4B,sBAChC,QAAQ,mBACR,IAAI,oBAAoB,QAAQ,iBAAiB;AAGzD,OAAI,QAAQ,iBACV,MAAK,mBAAmB,iBAAiB,QAAQ,mBAAmB,WAClE,kBAAkB,sBAAsB,SAAS,IAAI,oBAAoB,OAAO,CACjF;;;CA2CP,IAAW,WAAmB;AAC5B,SAAO,KAAK,kBAAkB,YAAY,KAAK,OAAO,KAAK,KAAK;;CAGlE,IAAW,sBAAgC;AACzC,MAAI,CAAC,KAAK,iBAAkB,QAAO,EAAE;AAErC,MAAI,MAAM,QAAQ,KAAK,iBAAiB,CACtC,QAAO,KAAK,iBAAiB,KAAK,qBAAqB,iBAAiB,GAAG;AAG7E,SAAO,CAAC,KAAK,iBAAiB,GAAG;;CAGnC,IAAW,uBAAiC;AAC1C,MAAI,MAAM,QAAQ,KAAK,kBAAkB,CACvC,QAAO,KAAK,kBACT,KAAK,sBAAsB,kBAAkB,GAAG,CAChD,QAAQ,MAAmB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