@getvouch/sdk 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -2
- package/dist/lib/vouch.d.ts +8 -0
- package/dist/lib/vouch.js +19 -3
- package/dist/lib/vouch.test.js +60 -3
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://docs.getvouch.io/introduction)
|
|
4
4
|
|
|
5
|
-
This is the vouch SDK, a TypeScript library for interacting with vouch. Currently, it allows you to generate
|
|
5
|
+
This is the vouch SDK, a TypeScript library for interacting with vouch. Currently, it allows you to generate links to start the web proof flow or direct users to a widget page.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -47,7 +47,7 @@ const url = vouch.getStartUrl({
|
|
|
47
47
|
});
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
#### Adding inputs
|
|
51
51
|
|
|
52
52
|
[Learn more about inputs](https://docs.getvouch.io/getting-started/handling-inputs).
|
|
53
53
|
|
|
@@ -74,3 +74,17 @@ const url = vouch.getStartUrl({
|
|
|
74
74
|
|
|
75
75
|
The inputs will be encoded to base64, so you'd see something like this inside the URL:
|
|
76
76
|
`inputs=eyJuYW1lIjoiSm9obiBEb2UiLCJtaW5Gb2xsb3dlcnNDb3VudCI6MTMzN30`
|
|
77
|
+
|
|
78
|
+
### Get widget URL
|
|
79
|
+
|
|
80
|
+
Generate a link to a widget page where users can choose from available data sources.
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const url = vouch.getWidgetUrl({
|
|
84
|
+
widgetId: "41b9a0c3-1234-5678-9abc-def012345678", // Your widget ID. Must be a UUIDv4.
|
|
85
|
+
redirectBackUrl: "https://example.com/callback", // Return destination after proof completion.
|
|
86
|
+
requestId, // (Optional) If not provided, a new request ID will be generated.
|
|
87
|
+
customerId: "1be03be8-5014-413c-835a-feddf4020da2", // (Optional) Your unique customer ID. Defaults to demo customer ID.
|
|
88
|
+
webhookUrl: "https://example.com/webhook", // (Optional) Proof delivery endpoint.
|
|
89
|
+
});
|
|
90
|
+
```
|
package/dist/lib/vouch.d.ts
CHANGED
|
@@ -9,8 +9,16 @@ export interface VouchUrlParams {
|
|
|
9
9
|
inputs?: Record<string, unknown>;
|
|
10
10
|
webhookUrl?: string;
|
|
11
11
|
}
|
|
12
|
+
export interface WidgetUrlParams {
|
|
13
|
+
widgetId: string;
|
|
14
|
+
redirectBackUrl: string;
|
|
15
|
+
customerId?: string;
|
|
16
|
+
requestId?: string;
|
|
17
|
+
webhookUrl?: string;
|
|
18
|
+
}
|
|
12
19
|
export declare class Vouch {
|
|
13
20
|
host: URL;
|
|
14
21
|
constructor({ vouchHost }?: Config);
|
|
15
22
|
getStartUrl({ customerId, datasourceId, requestId, redirectBackUrl, inputs, webhookUrl, }: VouchUrlParams): URL;
|
|
23
|
+
getWidgetUrl({ widgetId, redirectBackUrl, customerId, requestId, webhookUrl, }: WidgetUrlParams): URL;
|
|
16
24
|
}
|
package/dist/lib/vouch.js
CHANGED
|
@@ -2,7 +2,7 @@ import { validatedHost } from "../utils/validated-host";
|
|
|
2
2
|
import { validatedUuidV4 } from "../utils/validated-uuid";
|
|
3
3
|
import { encodeInputs } from "../utils/encode-inputs";
|
|
4
4
|
const DEFAULT_VOUCH_HOST = "https://app.getvouch.io";
|
|
5
|
-
const
|
|
5
|
+
const DEMO_CUSTOMER_ID = "1be03be8-5014-413c-835a-feddf4020da2";
|
|
6
6
|
export class Vouch {
|
|
7
7
|
constructor({ vouchHost = DEFAULT_VOUCH_HOST } = {}) {
|
|
8
8
|
this.host = validatedHost(vouchHost);
|
|
@@ -14,7 +14,7 @@ export class Vouch {
|
|
|
14
14
|
: crypto.randomUUID(),
|
|
15
15
|
customerId: customerId
|
|
16
16
|
? validatedUuidV4(customerId, "customerId")
|
|
17
|
-
:
|
|
17
|
+
: DEMO_CUSTOMER_ID,
|
|
18
18
|
datasourceId: validatedUuidV4(datasourceId, "datasourceId"),
|
|
19
19
|
redirectBackUrl,
|
|
20
20
|
});
|
|
@@ -24,6 +24,22 @@ export class Vouch {
|
|
|
24
24
|
if (webhookUrl) {
|
|
25
25
|
params.set("webhookUrl", webhookUrl);
|
|
26
26
|
}
|
|
27
|
-
return new URL(`/
|
|
27
|
+
return new URL(`/prove?${params.toString()}`, this.host);
|
|
28
|
+
}
|
|
29
|
+
getWidgetUrl({ widgetId, redirectBackUrl, customerId, requestId, webhookUrl, }) {
|
|
30
|
+
const params = new URLSearchParams({
|
|
31
|
+
requestId: requestId
|
|
32
|
+
? validatedUuidV4(requestId, "requestId")
|
|
33
|
+
: crypto.randomUUID(),
|
|
34
|
+
customerId: customerId
|
|
35
|
+
? validatedUuidV4(customerId, "customerId")
|
|
36
|
+
: DEMO_CUSTOMER_ID,
|
|
37
|
+
redirectBackUrl,
|
|
38
|
+
});
|
|
39
|
+
if (webhookUrl) {
|
|
40
|
+
params.set("webhookUrl", webhookUrl);
|
|
41
|
+
}
|
|
42
|
+
const path = `/widget/${validatedUuidV4(widgetId, "widgetId")}`;
|
|
43
|
+
return new URL(`${path}?${params.toString()}`, this.host);
|
|
28
44
|
}
|
|
29
45
|
}
|
package/dist/lib/vouch.test.js
CHANGED
|
@@ -19,7 +19,7 @@ describe("Vouch SDK", () => {
|
|
|
19
19
|
redirectBackUrl: "https://example.com/redirect",
|
|
20
20
|
};
|
|
21
21
|
const url = vouch.getStartUrl(params);
|
|
22
|
-
expect(url.toString()).toBe(`https://app.getvouch.io/
|
|
22
|
+
expect(url.toString()).toBe(`https://app.getvouch.io/prove?requestId=${params.requestId}&customerId=${params.customerId}&datasourceId=${params.datasourceId}&redirectBackUrl=${encodeURIComponent(params.redirectBackUrl)}`);
|
|
23
23
|
});
|
|
24
24
|
it("adds inputs", () => {
|
|
25
25
|
const params = {
|
|
@@ -30,7 +30,7 @@ describe("Vouch SDK", () => {
|
|
|
30
30
|
inputs: { key: "value" },
|
|
31
31
|
};
|
|
32
32
|
const url = vouch.getStartUrl(params);
|
|
33
|
-
expect(url.toString()).toBe(`https://app.getvouch.io/
|
|
33
|
+
expect(url.toString()).toBe(`https://app.getvouch.io/prove?requestId=${params.requestId}&customerId=${params.customerId}&datasourceId=${params.datasourceId}&redirectBackUrl=${encodeURIComponent(params.redirectBackUrl)}&inputs=eyJrZXkiOiJ2YWx1ZSJ9`);
|
|
34
34
|
});
|
|
35
35
|
it("adds webhookUrl", () => {
|
|
36
36
|
const params = {
|
|
@@ -41,7 +41,7 @@ describe("Vouch SDK", () => {
|
|
|
41
41
|
webhookUrl: "https://example.com/webhook",
|
|
42
42
|
};
|
|
43
43
|
const url = vouch.getStartUrl(params);
|
|
44
|
-
expect(url.toString()).toBe(`https://app.getvouch.io/
|
|
44
|
+
expect(url.toString()).toBe(`https://app.getvouch.io/prove?requestId=${params.requestId}&customerId=${params.customerId}&datasourceId=${params.datasourceId}&redirectBackUrl=${encodeURIComponent(params.redirectBackUrl)}&webhookUrl=${encodeURIComponent(params.webhookUrl)}`);
|
|
45
45
|
});
|
|
46
46
|
it("generates requestId", () => {
|
|
47
47
|
var _a;
|
|
@@ -76,4 +76,61 @@ describe("Vouch SDK", () => {
|
|
|
76
76
|
});
|
|
77
77
|
});
|
|
78
78
|
});
|
|
79
|
+
describe("getWidgetUrl", () => {
|
|
80
|
+
let vouch;
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
vouch = new Vouch();
|
|
83
|
+
});
|
|
84
|
+
it("constructs correct URL", () => {
|
|
85
|
+
const params = {
|
|
86
|
+
widgetId: crypto.randomUUID(),
|
|
87
|
+
requestId: crypto.randomUUID(),
|
|
88
|
+
customerId: crypto.randomUUID(),
|
|
89
|
+
redirectBackUrl: "https://example.com/redirect",
|
|
90
|
+
webhookUrl: "https://example.com/webhook",
|
|
91
|
+
};
|
|
92
|
+
const url = vouch.getWidgetUrl(params);
|
|
93
|
+
expect(url.toString()).toBe(`https://app.getvouch.io/widget/${params.widgetId}?requestId=${params.requestId}&customerId=${params.customerId}&redirectBackUrl=${encodeURIComponent(params.redirectBackUrl)}&webhookUrl=${encodeURIComponent(params.webhookUrl)}`);
|
|
94
|
+
});
|
|
95
|
+
it("generates requestId", () => {
|
|
96
|
+
const params = {
|
|
97
|
+
widgetId: crypto.randomUUID(),
|
|
98
|
+
redirectBackUrl: "https://example.com/redirect",
|
|
99
|
+
};
|
|
100
|
+
const url = vouch.getWidgetUrl(params);
|
|
101
|
+
const generatedRequestId = url.searchParams.get("requestId");
|
|
102
|
+
expect(generatedRequestId).not.toBeNull();
|
|
103
|
+
expect(() => validatedUuidV4(generatedRequestId !== null && generatedRequestId !== void 0 ? generatedRequestId : "")).not.toThrow();
|
|
104
|
+
});
|
|
105
|
+
it("sets default customerId", () => {
|
|
106
|
+
const params = {
|
|
107
|
+
widgetId: crypto.randomUUID(),
|
|
108
|
+
redirectBackUrl: "https://example.com/redirect",
|
|
109
|
+
};
|
|
110
|
+
const url = vouch.getWidgetUrl(params);
|
|
111
|
+
expect(url.searchParams.get("customerId")).toBe("1be03be8-5014-413c-835a-feddf4020da2");
|
|
112
|
+
});
|
|
113
|
+
it("validates UUIDs", () => {
|
|
114
|
+
const params = {
|
|
115
|
+
widgetId: crypto.randomUUID(),
|
|
116
|
+
requestId: crypto.randomUUID(),
|
|
117
|
+
customerId: crypto.randomUUID(),
|
|
118
|
+
redirectBackUrl: "https://example.com/redirect",
|
|
119
|
+
};
|
|
120
|
+
expect(() => vouch.getWidgetUrl(Object.assign(Object.assign({}, params), { widgetId: "invalid-uuid" }))).toThrow("Invalid UUIDv4 for widgetId: invalid-uuid");
|
|
121
|
+
expect(() => vouch.getWidgetUrl(Object.assign(Object.assign({}, params), { requestId: "invalid-uuid" }))).toThrow("Invalid UUIDv4 for requestId: invalid-uuid");
|
|
122
|
+
expect(() => vouch.getWidgetUrl(Object.assign(Object.assign({}, params), { customerId: "invalid-uuid" }))).toThrow("Invalid UUIDv4 for customerId: invalid-uuid");
|
|
123
|
+
});
|
|
124
|
+
it("handles custom host", () => {
|
|
125
|
+
const customVouch = new Vouch({ vouchHost: "https://example.com" });
|
|
126
|
+
const params = {
|
|
127
|
+
widgetId: crypto.randomUUID(),
|
|
128
|
+
requestId: crypto.randomUUID(),
|
|
129
|
+
customerId: crypto.randomUUID(),
|
|
130
|
+
redirectBackUrl: "https://example.com/redirect",
|
|
131
|
+
};
|
|
132
|
+
const url = customVouch.getWidgetUrl(params);
|
|
133
|
+
expect(url.toString()).toBe(`https://example.com/widget/${params.widgetId}?requestId=${params.requestId}&customerId=${params.customerId}&redirectBackUrl=${encodeURIComponent(params.redirectBackUrl)}`);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
79
136
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getvouch/sdk",
|
|
3
3
|
"author": "vouch",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"main": "dist/index.js",
|
|
5
6
|
"types": "dist/index.d.ts",
|
|
6
7
|
"repository": {
|
|
@@ -8,7 +9,7 @@
|
|
|
8
9
|
"url": "git+https://github.com/vlayer-xyz/vouch.git#main"
|
|
9
10
|
},
|
|
10
11
|
"private": false,
|
|
11
|
-
"version": "0.1.
|
|
12
|
+
"version": "0.1.4",
|
|
12
13
|
"description": "vouch SDK",
|
|
13
14
|
"keywords": [
|
|
14
15
|
"webproof",
|