/
types.ts
85 lines (73 loc) · 3.21 KB
/
types.ts
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
import { ModelAliases } from '@glazed/types';
import { z } from 'zod';
import { StreamID } from '@ceramicnetwork/streamid';
import { VC, IDXCredential, IDXCredentialValidator } from '@learncard/types';
import { ResolutionExtension } from '../vc-resolution';
/** @group IDXPlugin */
export type CeramicIDXArgs = {
modelData: ModelAliases;
credentialAlias: string;
ceramicEndpoint: string;
defaultContentFamily: string;
};
/** @group IDXPlugin */
export type CeramicURI = `lc:ceramic:${string}`;
/** @group IDXPlugin */
export const CeramicURIValidator = z
.string()
.refine(
string => string.split(':').length === 3 && string.split(':')[0] === 'lc',
'URI must be of the form lc:${storage}:${url}'
)
.refine(
string => string.split(':')[1] === 'ceramic',
'URI must use storage type ceramic (i.e. must be lc:ceramic:${streamID})'
);
/** @group IDXPlugin */
export type IDXPluginMethods<URI extends string = ''> = {
getCredentialsListFromIdx: <Metadata extends Record<string, any> = Record<never, never>>(
alias?: string
) => Promise<CredentialsList<Metadata>>;
publishContentToCeramic: (cred: any) => Promise<CeramicURI>;
readContentFromCeramic: (streamId: string) => Promise<any>;
getVerifiableCredentialFromIdx: (id: string) => Promise<VC | undefined>;
getVerifiableCredentialsFromIdx: () => Promise<VC[]>;
addVerifiableCredentialInIdx: <Metadata extends Record<string, any> = Record<never, never>>(
cred: IDXCredential<Metadata>
) => Promise<CeramicURI>;
removeVerifiableCredentialInIdx: (title: string) => Promise<StreamID>;
} & ResolutionExtension<URI | CeramicURI>;
/** @group IDXPlugin */
export type IDXPluginDependentMethods<URI extends string = ''> = {
getKey: () => string;
} & ResolutionExtension<URI>;
/** @group IDXPlugin */
export type CredentialsList<Metadata extends Record<string, any> = Record<never, never>> = {
credentials: Array<IDXCredential<Metadata>>;
};
/** @group IDXPlugin */
export const CredentialsListValidator: z.ZodType<CredentialsList> = z
.object({ credentials: IDXCredentialValidator.array() })
.strict();
// Below types are temporary! They will be removed in the future when we are confident that everyone
// has moved on to the new schema
/** @group IDXPlugin */
export type BackwardsCompatIDXCredential<
Metadata extends Record<string, any> = Record<never, never>
> = { [key: string]: any; id: string; title: string; storageType?: 'ceramic' } & Metadata;
/** @group IDXPlugin */
export const BackwardsCompatIDXCredentialValidator: z.ZodType<BackwardsCompatIDXCredential> = z
.object({ id: z.string(), title: z.string(), storageType: z.literal('ceramic').optional() })
.catchall(z.any());
/** @group IDXPlugin */
export type BackwardsCompatCredentialsList<
Metadata extends Record<string, any> = Record<never, never>
> = {
credentials: Array<IDXCredential<Metadata> | BackwardsCompatIDXCredential<Metadata>>;
};
/** @group IDXPlugin */
export const BackwardsCompatCredentialsListValidator: z.ZodType<BackwardsCompatCredentialsList> = z
.object({
credentials: IDXCredentialValidator.or(BackwardsCompatIDXCredentialValidator).array(),
})
.strict();