Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve types for profiles #1381

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/profile/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export {
verifyProfileToken,
extractProfile,
} from './profileTokens';

export { PublicProfile, PublicPersonProfile } from './types';
5 changes: 3 additions & 2 deletions packages/profile/src/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as inspector from 'schema-inspector';

import { Logger } from '@stacks/common';
import { createFetchFn, FetchFn } from '@stacks/network';
import { PublicPersonProfile } from './types';

const schemaDefinition: { [key: string]: any } = {
type: 'object',
Expand Down Expand Up @@ -165,7 +166,7 @@ const personSchemaDefinition = {
* @ignore
*/
export class Person extends Profile {
constructor(profile = {}) {
constructor(profile: PublicPersonProfile = { '@type': 'Person' }) {
super(profile);
this._profile = Object.assign(
{},
Expand All @@ -182,7 +183,7 @@ export class Person extends Profile {
}

static fromToken(token: string, publicKeyOrAddress: string | null = null): Person {
const profile = extractProfile(token, publicKeyOrAddress);
const profile = extractProfile(token, publicKeyOrAddress) as PublicPersonProfile;
return new Person(profile);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/profile/src/profileSchemas/personLegacy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ProfileType } from "../types";

/**
*
* @param serviceName
Expand Down Expand Up @@ -27,7 +29,7 @@ function formatAccount(serviceName: string, data: any) {
*/
export function getPersonFromLegacyFormat(profile: any) {
const profileData: {
['@type']: string;
['@type']: ProfileType;
account?: any[];
name?: string;
description?: string;
Expand Down
73 changes: 73 additions & 0 deletions packages/profile/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const PERSON_TYPE = 'Person';
const CONTEXT = 'http://schema.org';
const IMAGE_TYPE = 'ImageObject';

export type ProfileType = typeof PERSON_TYPE;

export interface ProfileImage {
'@type': typeof IMAGE_TYPE;
name?: string;
contentUrl?: string;
[k: string]: unknown;
}

export interface PublicProfileBase {
'@type'?: ProfileType;
'@context'?: typeof CONTEXT;
apps?: {
[origin: string]: string;
};
appsMeta?: {
[origin: string]: {
publicKey: string;
storage: string;
};
};
}

export interface PublicPersonProfile extends PublicProfileBase {
'@type': typeof PERSON_TYPE;
name?: string;
givenName?: string;
familyName?: string;
description?: string;
image?: ProfileImage[];
website?: {
'@type'?: string;
url?: string;
[k: string]: unknown;
}[];
account?: {
'@type'?: string;
service?: string;
identifier?: string;
proofType?: string;
proofUrl?: string;
proofMessage?: string;
proofSignature?: string;
[k: string]: unknown;
}[];
worksFor?: {
'@type'?: string;
'@id'?: string;
[k: string]: unknown;
}[];
knows?: {
'@type'?: string;
'@id'?: string;
[k: string]: unknown;
}[];
address?: {
'@type'?: string;
streetAddress?: string;
addressLocality?: string;
postalCode?: string;
addressCountry?: string;
[k: string]: unknown;
};
birthDate?: string;
taxID?: string;
[k: string]: unknown;
}

export type PublicProfile = PublicPersonProfile;