All files / src/profiles/profileSchemas person.ts

100% Statements 27/27
0% Branches 0/3
100% Functions 16/16
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187  1x   1x 1x 1x 1x           1x                                                                                                                                                                                     1x   3x 3x           2x 2x       1x 1x       1x 1x       3x                               43x       4x       4x       4x       4x       4x       4x       4x       4x       4x       4x      
// @ts-ignore: Could not find a declaration file for module
import * as inspector from 'schema-inspector'
 
import { Profile } from '../profile'
import { extractProfile } from '../profileTokens'
import { getPersonFromLegacyFormat } from './personLegacy'
import {
  getName, getFamilyName, getGivenName, getAvatarUrl, getDescription,
  getVerifiedAccounts, getAddress, getBirthDate,
  getConnections, getOrganizations
} from './personUtils'
 
const schemaDefinition = {
  type: 'object',
  strict: false,
  properties: {
    '@context': { type: 'string', optional: true },
    '@type': { type: 'string' },
    '@id': { type: 'string', optional: true },
    name: { type: 'string', optional: true },
    givenName: { type: 'string', optional: true },
    familyName: { type: 'string', optional: true },
    description: { type: 'string', optional: true },
    image: {
      type: 'array',
      optional: true,
      items: {
        type: 'object',
        properties: {
          '@type': { type: 'string' },
          name: { type: 'string', optional: true },
          contentUrl: { type: 'string', optional: true }
        }
      }
    },
    website: {
      type: 'array',
      optional: true,
      items: {
        type: 'object',
        properties: {
          '@type': { type: 'string' },
          url: { type: 'string', optional: true }
        }
      }
    },
    account: {
      type: 'array',
      optional: true,
      items: {
        type: 'object',
        properties: {
          '@type': { type: 'string' },
          service: { type: 'string', optional: true },
          identifier: { type: 'string', optional: true },
          proofType: { type: 'string', optional: true },
          proofUrl: { type: 'string', optional: true },
          proofMessage: { type: 'string', optional: true },
          proofSignature: { type: 'string', optional: true }
        }
      }
    },
    worksFor: {
      type: 'array',
      optional: true,
      items: {
        type: 'object',
        properties: {
          '@type': { type: 'string' },
          '@id': { type: 'string', optional: true }
        }
      }
    },
    knows: {
      type: 'array',
      optional: true,
      items: {
        type: 'object',
        properties: {
          '@type': { type: 'string' },
          '@id': { type: 'string', optional: true }
        }
      }
    },
    address: {
      type: 'object',
      optional: true,
      properties: {
        '@type': { type: 'string' },
        streetAddress: { type: 'string', optional: true },
        addressLocality: { type: 'string', optional: true },
        postalCode: { type: 'string', optional: true },
        addressCountry: { type: 'string', optional: true }
      }
    },
    birthDate: { type: 'string', optional: true },
    taxID: { type: 'string', optional: true }
  }
}
 
/**
 * @ignore
 */
export class Person extends Profile {
  constructor(profile = {}) {
    super(profile)
    this._profile = Object.assign({}, {
      '@type': 'Person'
    }, this._profile)
  }
 
  static validateSchema(profile: any, strict = false) {
    schemaDefinition.strict = strict
    return inspector.validate(schemaDefinition, profile)
  }
 
  static fromToken(token: string, publicKeyOrAddress: string | null = null): Person {
    const profile = extractProfile(token, publicKeyOrAddress)
    return new Person(profile)
  }
 
  static fromLegacyFormat(legacyProfile: any) {
    const profile = getPersonFromLegacyFormat(legacyProfile)
    return new Person(profile)
  }
 
  toJSON() {
    return {
      profile: this.profile(),
      name: this.name(),
      givenName: this.givenName(),
      familyName: this.familyName(),
      description: this.description(),
      avatarUrl: this.avatarUrl(),
      verifiedAccounts: this.verifiedAccounts(),
      address: this.address(),
      birthDate: this.birthDate(),
      connections: this.connections(),
      organizations: this.organizations()
    }
  }
 
  profile() {
    return Object.assign({}, this._profile)
  }
 
  name() {
    return getName(this.profile())
  }
 
  givenName() {
    return getGivenName(this.profile())
  }
 
  familyName() {
    return getFamilyName(this.profile())
  }
 
  description() {
    return getDescription(this.profile())
  }
 
  avatarUrl() {
    return getAvatarUrl(this.profile())
  }
 
  verifiedAccounts(verifications?: any[]) {
    return getVerifiedAccounts(this.profile(), verifications)
  }
 
  address() {
    return getAddress(this.profile())
  }
 
  birthDate() {
    return getBirthDate(this.profile())
  }
 
  connections() {
    return getConnections(this.profile())
  }
 
  organizations() {
    return getOrganizations(this.profile())
  }
}