@getvouch/sdk 0.1.0 → 0.1.1
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 +40 -10
- package/dist/lib/vouch.d.ts +1 -1
- package/dist/lib/vouch.js +2 -1
- package/dist/lib/vouch.test.js +2 -2
- package/dist/utils/encode-inputs.d.ts +3 -0
- package/dist/utils/encode-inputs.js +25 -0
- package/dist/utils/encode-inputs.test.d.ts +1 -0
- package/dist/utils/encode-inputs.test.js +40 -0
- package/package.json +4 -3
- package/tsconfig.json +1 -1
package/README.md
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# vouch SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://docs.getvouch.io/introduction)
|
|
4
|
+
|
|
5
|
+
This is the vouch SDK, a TypeScript library for interacting with vouch. Currently, it allows you to generate a link to start the web proof flow.
|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
|
-
To install the
|
|
9
|
+
To install the vouch SDK, you can use npm or other package managers.
|
|
8
10
|
|
|
9
11
|
```bash
|
|
10
|
-
npm install @
|
|
11
|
-
yarn add @
|
|
12
|
-
pnpm add @
|
|
13
|
-
bun add @
|
|
12
|
+
npm install @getvouch/sdk
|
|
13
|
+
yarn add @getvouch/sdk
|
|
14
|
+
pnpm add @getvouch/sdk
|
|
15
|
+
bun add @getvouch/sdk
|
|
14
16
|
```
|
|
15
17
|
|
|
16
18
|
## Usage
|
|
@@ -18,14 +20,14 @@ bun add @vouch/vouch-sdk
|
|
|
18
20
|
Create vouch instance:
|
|
19
21
|
|
|
20
22
|
```typescript
|
|
21
|
-
import { Vouch } from "@
|
|
23
|
+
import { Vouch } from "@getvouch/sdk";
|
|
22
24
|
const vouch = new Vouch();
|
|
23
25
|
```
|
|
24
26
|
|
|
25
27
|
You may provide a configuration object with a set of optional parameters:
|
|
26
28
|
|
|
27
29
|
```typescript
|
|
28
|
-
import { Vouch } from "@
|
|
30
|
+
import { Vouch } from "@getvouch/sdk";
|
|
29
31
|
const vouch = new Vouch({
|
|
30
32
|
vouchHost: "https://app.getvouch.io", // The base URL for the Vouch API. Can be a string or an URL object. Defaults to "https://app.getvouch.io".
|
|
31
33
|
});
|
|
@@ -33,7 +35,7 @@ const vouch = new Vouch({
|
|
|
33
35
|
|
|
34
36
|
### Get vouch start URL
|
|
35
37
|
|
|
36
|
-
[Generate a link to start the
|
|
38
|
+
[Generate a link to start the vouch flow](https://docs.getvouch.io/getting-started/first-steps#redirect-user-to-vouch).
|
|
37
39
|
|
|
38
40
|
```typescript
|
|
39
41
|
const url = vouch.getStartUrl({
|
|
@@ -44,3 +46,31 @@ const url = vouch.getStartUrl({
|
|
|
44
46
|
webhookUrl: `https://docs.getvouch.io/api/web-proof/${requestId}`, // (Optional) Proof delivery endpoint.
|
|
45
47
|
});
|
|
46
48
|
```
|
|
49
|
+
|
|
50
|
+
### Adding inputs
|
|
51
|
+
|
|
52
|
+
[Learn more about inputs](https://docs.getvouch.io/getting-started/handling-inputs).
|
|
53
|
+
|
|
54
|
+
Suppose, you want to use a datasource with the following inputs schema:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
{
|
|
58
|
+
name: string;
|
|
59
|
+
minFollowersCount: number;
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
You'd then need to provide the inputs when generating the start URL:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
const url = vouch.getStartUrl({
|
|
67
|
+
/// ...similar to the previous example
|
|
68
|
+
inputs: {
|
|
69
|
+
name: "John Doe",
|
|
70
|
+
minFollowersCount: 1337,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The inputs will be encoded to base64, so you'd see something like this inside the URL:
|
|
76
|
+
`inputs=eyJuYW1lIjoiSm9obiBEb2UiLCJtaW5Gb2xsb3dlcnNDb3VudCI6MTMzN30`
|
package/dist/lib/vouch.d.ts
CHANGED
package/dist/lib/vouch.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { validatedHost } from "@/utils/validated-host";
|
|
2
2
|
import { validatedUuidV4 } from "@/utils/validated-uuid";
|
|
3
|
+
import { encodeInputs } from "@/utils/encode-inputs";
|
|
3
4
|
const DEFAULT_VOUCH_HOST = "https://app.getvouch.io";
|
|
4
5
|
const DEFAULT_CUSTOMER_ID = "1be03be8-5014-413c-835a-feddf4020da2";
|
|
5
6
|
export class Vouch {
|
|
@@ -18,7 +19,7 @@ export class Vouch {
|
|
|
18
19
|
redirectBackUrl,
|
|
19
20
|
});
|
|
20
21
|
if (inputs) {
|
|
21
|
-
params.set("inputs", inputs);
|
|
22
|
+
params.set("inputs", encodeInputs(inputs));
|
|
22
23
|
}
|
|
23
24
|
if (webhookUrl) {
|
|
24
25
|
params.set("webhookUrl", webhookUrl);
|
package/dist/lib/vouch.test.js
CHANGED
|
@@ -27,10 +27,10 @@ describe("Vouch SDK", () => {
|
|
|
27
27
|
datasourceId: crypto.randomUUID(),
|
|
28
28
|
customerId: crypto.randomUUID(),
|
|
29
29
|
redirectBackUrl: "https://example.com/redirect",
|
|
30
|
-
inputs: "
|
|
30
|
+
inputs: { key: "value" },
|
|
31
31
|
};
|
|
32
32
|
const url = vouch.getStartUrl(params);
|
|
33
|
-
expect(url.toString()).toBe(`https://app.getvouch.io/start?requestId=${params.requestId}&customerId=${params.customerId}&datasourceId=${params.datasourceId}&redirectBackUrl=${encodeURIComponent(params.redirectBackUrl)}&inputs
|
|
33
|
+
expect(url.toString()).toBe(`https://app.getvouch.io/start?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 = {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function toBase64InBrowser(encodedText) {
|
|
2
|
+
const bytes = new TextEncoder().encode(encodedText);
|
|
3
|
+
return btoa(String.fromCharCode(...bytes))
|
|
4
|
+
.replace(/=/g, "")
|
|
5
|
+
.replace(/\+/g, "-")
|
|
6
|
+
.replace(/\//g, "_");
|
|
7
|
+
}
|
|
8
|
+
export function toBase64(encodedText) {
|
|
9
|
+
if (typeof Buffer !== "undefined") {
|
|
10
|
+
return Buffer.from(encodedText, "utf-8").toString("base64url");
|
|
11
|
+
}
|
|
12
|
+
return toBase64InBrowser(encodedText);
|
|
13
|
+
}
|
|
14
|
+
export function encodeInputs(inputs) {
|
|
15
|
+
if (!inputs) {
|
|
16
|
+
return toBase64("{}");
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const jsonString = JSON.stringify(inputs);
|
|
20
|
+
return toBase64(jsonString);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
throw new Error(`Failed to encode inputs: ${String(error)}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { encodeInputs, toBase64, toBase64InBrowser } from "./encode-inputs";
|
|
2
|
+
import { fromBase64 } from "common";
|
|
3
|
+
import { expect } from "vitest";
|
|
4
|
+
describe("Encode inputs", () => {
|
|
5
|
+
describe("toBase64", () => {
|
|
6
|
+
it("should convert toBase64", () => {
|
|
7
|
+
const result = toBase64("Hello, World!");
|
|
8
|
+
expect(result).toBe("SGVsbG8sIFdvcmxkIQ");
|
|
9
|
+
});
|
|
10
|
+
it("is url safe", () => {
|
|
11
|
+
const result = toBase64("ûïÿÿÿ");
|
|
12
|
+
expect(result).toBe("w7vDr8O_w7_Dvw");
|
|
13
|
+
expect(fromBase64(result)).toBe("ûïÿÿÿ");
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
describe("toBase64InBrowser", () => {
|
|
17
|
+
it("should convert toBase64", () => {
|
|
18
|
+
const result = toBase64InBrowser("Hello, World!");
|
|
19
|
+
expect(result).toBe("SGVsbG8sIFdvcmxkIQ");
|
|
20
|
+
});
|
|
21
|
+
it("is url safe", () => {
|
|
22
|
+
const result = toBase64InBrowser("ûïÿÿÿ");
|
|
23
|
+
expect(result).toBe("w7vDr8O_w7_Dvw");
|
|
24
|
+
expect(fromBase64(result)).toBe("ûïÿÿÿ");
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe("input encoding", () => {
|
|
28
|
+
it("encodes empty JSON object when no inputs were provided", () => {
|
|
29
|
+
const result = toBase64("{}");
|
|
30
|
+
expect(encodeInputs()).toBe(result);
|
|
31
|
+
expect(encodeInputs({})).toBe(result);
|
|
32
|
+
});
|
|
33
|
+
it("stringifies object and encodes it to base64", () => {
|
|
34
|
+
const inputs = { name: "John Doe", age: 30, city: "New York" };
|
|
35
|
+
expect(encodeInputs(inputs)).toBe(
|
|
36
|
+
// toBase64({"name":"John Doe","age":30,"city":"New York"})
|
|
37
|
+
"eyJuYW1lIjoiSm9obiBEb2UiLCJhZ2UiOjMwLCJjaXR5IjoiTmV3IFlvcmsifQ");
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getvouch/sdk",
|
|
3
|
-
"author": "
|
|
3
|
+
"author": "vouch",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"repository": {
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"url": "git+https://github.com/vlayer-xyz/vouch.git#main"
|
|
9
9
|
},
|
|
10
10
|
"private": false,
|
|
11
|
-
"version": "0.1.
|
|
12
|
-
"description": "
|
|
11
|
+
"version": "0.1.1",
|
|
12
|
+
"description": "vouch SDK",
|
|
13
13
|
"keywords": [
|
|
14
14
|
"webproof",
|
|
15
15
|
"sdk",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"format:fix": "prettier --write ."
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
+
"common": "workspace:*",
|
|
27
28
|
"eslint": "^9.24.0",
|
|
28
29
|
"eslint-config-prettier": "^10.1.1",
|
|
29
30
|
"eslint-plugin-prettier": "^5.2.6",
|