@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 CHANGED
@@ -1,16 +1,18 @@
1
- # Vouch SDK
1
+ # vouch SDK
2
2
 
3
- 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.
3
+ [![Static Badge](https://img.shields.io/badge/vouch-read_docs-green?style=for-the-badge)](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 Vouch SDK, you can use npm or other package managers.
9
+ To install the vouch SDK, you can use npm or other package managers.
8
10
 
9
11
  ```bash
10
- npm install @vouch/vouch-sdk
11
- yarn add @vouch/vouch-sdk
12
- pnpm add @vouch/vouch-sdk
13
- bun add @vouch/vouch-sdk
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 "@vouch/vouch-sdk";
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 "@vouch/vouch-sdk";
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 Vouch flow](https://docs.getvouch.io/getting-started/first-steps#redirect-user-to-vouch).
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`
@@ -6,7 +6,7 @@ export interface VouchUrlParams {
6
6
  redirectBackUrl: string;
7
7
  customerId?: string;
8
8
  requestId?: string;
9
- inputs?: string;
9
+ inputs?: Record<string, unknown>;
10
10
  webhookUrl?: string;
11
11
  }
12
12
  export declare class Vouch {
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);
@@ -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: "eyJrZXkiOiAidmFsdWUifQ",
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=${params.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,3 @@
1
+ export declare function toBase64InBrowser(encodedText: string): string;
2
+ export declare function toBase64(encodedText: string): string;
3
+ export declare function encodeInputs(inputs?: Record<string, unknown>): string;
@@ -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": "Vouch",
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.0",
12
- "description": "Vouch SDK",
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",
package/tsconfig.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "target": "ES2017",
5
5
  "types": ["vitest/globals"],
6
6
  "moduleDetection": "force",
7
- "moduleResolution": "node",
7
+ "moduleResolution": "bundler",
8
8
  "declaration": true,
9
9
  "outDir": "./dist",
10
10
  "rootDir": "./src",