forked from openwallet-foundation/credo-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
76 lines (68 loc) · 2.49 KB
/
helpers.ts
File metadata and controls
76 lines (68 loc) · 2.49 KB
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
import { Transform } from 'class-transformer';
import { validateOrReject } from 'class-validator';
import { ConnectionInvitationMessage } from './protocols/connections/ConnectionInvitationMessage';
import { JsonEncoder } from './utils/JsonEncoder';
import { JsonTransformer } from './utils/JsonTransformer';
/**
* Create a `ConnectionInvitationMessage` instance from the `c_i` parameter of an URL
*
* @param invitationUrl invitation url containing c_i parameter
*
* @throws Error when url can not be decoded to JSON, or decoded message is not a valid `ConnectionInvitationMessage`
*/
export async function decodeInvitationFromUrl(invitationUrl: string): Promise<ConnectionInvitationMessage> {
// TODO: properly extract c_i param from invitation URL
const [, encodedInvitation] = invitationUrl.split('c_i=');
const invitationJson = JsonEncoder.fromBase64(encodedInvitation);
const invitation = JsonTransformer.fromJSON(invitationJson, ConnectionInvitationMessage);
// TODO: should validation happen here?
await validateOrReject(invitation);
return invitation;
}
/**
* Create an invitation url from this instance
*
* @param invitation invitation message
* @param domain domain name to use for invitation url
*/
export function encodeInvitationToUrl(
invitation: ConnectionInvitationMessage,
domain = 'https://example.com/ssi'
): string {
const invitationJson = JsonTransformer.toJSON(invitation);
const encodedInvitation = JsonEncoder.toBase64URL(invitationJson);
const invitationUrl = `${domain}?c_i=${encodedInvitation}`;
return invitationUrl;
}
/**
* Provide a default value for a parameter when using class-transformer
*
* Class transfomer doesn't use the default value of a property when transforming an
* object using `plainToClass`. This decorator allows to set a default value when no value is
* present during transformation.
*
* @param defaultValue the default value to use when there is no value present during transformation
* @see https://github.com/typestack/class-transformer/issues/129#issuecomment-425843700
*
* @example
* import { plainToClass } from 'class-transformer'
*
* class Test {
* // doesn't work
* myProp = true;
*
* // does work
* @Default(true)
* myDefaultProp: boolean;
* }
*
* plainToClass(Test, {})
* // results in
* {
* "myProp": undefined,
* "myDefaultProp": true
* }
*/
export function Default<T>(defaultValue: T) {
return Transform((value: T | null | undefined) => (value !== null && value !== undefined ? value : defaultValue));
}