@proveanything/smartlinks 1.0.3 → 1.0.5
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 +109 -59
- package/dist/api/appConfiguration.d.ts +11 -0
- package/dist/api/appConfiguration.js +17 -0
- package/dist/api/collection.d.ts +10 -0
- package/dist/api/collection.js +16 -0
- package/dist/api/index.d.ts +4 -0
- package/dist/api/index.js +6 -0
- package/dist/api/product.d.ts +18 -0
- package/dist/api/product.js +28 -0
- package/dist/api/proof.d.ts +11 -0
- package/dist/api/proof.js +17 -0
- package/dist/http.d.ts +18 -0
- package/dist/http.js +54 -0
- package/dist/index.d.ts +3 -113
- package/dist/index.js +5 -105
- package/dist/types/appConfiguration.d.ts +11 -0
- package/dist/types/appConfiguration.js +1 -0
- package/dist/types/collection.d.ts +13 -0
- package/dist/types/collection.js +1 -0
- package/dist/types/error.d.ts +9 -0
- package/dist/types/error.js +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.js +7 -0
- package/dist/types/product.d.ts +13 -0
- package/dist/types/product.js +1 -0
- package/dist/types/proof.d.ts +19 -0
- package/dist/types/proof.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @proveanything/smartlinks
|
|
2
2
|
|
|
3
|
-
An official JavaScript/TypeScript client SDK for the Smartlinks API. This package provides
|
|
3
|
+
An official JavaScript/TypeScript client SDK for the Smartlinks API. This package provides simple, namespaced functions to fetch Collection, Product, Proof, and App Configuration data in both browser and Node.js environments.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -13,20 +13,38 @@ yarn add @proveanything/smartlinks
|
|
|
13
13
|
## Quickstart
|
|
14
14
|
|
|
15
15
|
```ts
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
initializeApi,
|
|
18
|
+
collection,
|
|
19
|
+
product,
|
|
20
|
+
proof,
|
|
21
|
+
appConfiguration,
|
|
22
|
+
} from "@proveanything/smartlinks";
|
|
17
23
|
|
|
18
24
|
async function main() {
|
|
19
|
-
//
|
|
20
|
-
|
|
25
|
+
// Initialize once (provide base URL and optional API key/bearer token)
|
|
26
|
+
initializeApi({
|
|
27
|
+
baseURL: "https://smartlinks.app/api/v1",
|
|
28
|
+
apiKey: "YOUR_API_KEY_HERE", // optional
|
|
29
|
+
bearerToken: "YOUR_BEARER_TOKEN", // optional
|
|
30
|
+
});
|
|
21
31
|
|
|
22
32
|
try {
|
|
23
33
|
// Fetch a collection by ID
|
|
24
|
-
const
|
|
25
|
-
console.log("Collection:",
|
|
34
|
+
const coll = await collection.get("abc123");
|
|
35
|
+
console.log("Collection:", coll);
|
|
26
36
|
|
|
27
37
|
// Fetch a product item by collection ID & product ID
|
|
28
|
-
const
|
|
29
|
-
console.log("Product Item:",
|
|
38
|
+
const prod = await product.get("abc123", "prod789");
|
|
39
|
+
console.log("Product Item:", prod);
|
|
40
|
+
|
|
41
|
+
// Fetch a proof by collection ID & proof ID
|
|
42
|
+
const prf = await proof.get("abc123", "proof456");
|
|
43
|
+
console.log("Proof:", prf);
|
|
44
|
+
|
|
45
|
+
// Fetch an app configuration by collection ID & app ID
|
|
46
|
+
const cfg = await appConfiguration.get("abc123", "app789");
|
|
47
|
+
console.log("App Configuration:", cfg);
|
|
30
48
|
} catch (err) {
|
|
31
49
|
console.error("Error fetching data:", err);
|
|
32
50
|
}
|
|
@@ -37,19 +55,24 @@ main();
|
|
|
37
55
|
|
|
38
56
|
## API Reference
|
|
39
57
|
|
|
40
|
-
###
|
|
58
|
+
### Initialization
|
|
41
59
|
|
|
42
60
|
```ts
|
|
43
|
-
|
|
61
|
+
initializeApi(options: { baseURL: string; apiKey?: string; bearerToken?: string }): void
|
|
44
62
|
```
|
|
45
63
|
|
|
46
64
|
- **Parameters:**
|
|
47
|
-
- `baseURL` (`string`, required): The root URL of the Smartlinks API, e.g. `https://smartlinks.app/api/v1`.
|
|
48
|
-
- `apiKey`
|
|
65
|
+
- `baseURL` (`string`, required): The root URL of the Smartlinks API, e.g. `https://smartlinks.app/api/v1`.
|
|
66
|
+
- `apiKey` (`string`, optional): Your API key for the `X-API-Key` header.
|
|
67
|
+
- `bearerToken` (`string`, optional): Your Bearer token for the `AUTHORIZATION` header.
|
|
68
|
+
|
|
69
|
+
All subsequent calls to the API functions will use these settings.
|
|
49
70
|
|
|
50
71
|
---
|
|
51
72
|
|
|
52
|
-
|
|
73
|
+
### Namespace: `collection`
|
|
74
|
+
|
|
75
|
+
#### `collection.get(collectionId: string): Promise<CollectionResponse>`
|
|
53
76
|
|
|
54
77
|
Fetches a single collection by its ID.
|
|
55
78
|
|
|
@@ -69,20 +92,21 @@ Fetches a single collection by its ID.
|
|
|
69
92
|
|
|
70
93
|
- **Example:**
|
|
71
94
|
```ts
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
console.log("Fetched collection:", collection);
|
|
95
|
+
const coll = await collection.get("abc123");
|
|
96
|
+
console.log("Fetched collection:", coll.title);
|
|
75
97
|
```
|
|
76
98
|
|
|
77
99
|
---
|
|
78
100
|
|
|
79
|
-
|
|
101
|
+
### Namespace: `product`
|
|
102
|
+
|
|
103
|
+
#### `product.get(collectionId: string, productId: string): Promise<ProductResponse>`
|
|
80
104
|
|
|
81
105
|
Fetches a single product item within a collection.
|
|
82
106
|
|
|
83
107
|
- **Parameters:**
|
|
84
108
|
- `collectionId` (`string`, required): The parent collection’s ID.
|
|
85
|
-
- `productId`
|
|
109
|
+
- `productId` (`string`, required): The product item’s ID.
|
|
86
110
|
- **Returns:**
|
|
87
111
|
A `Promise` that resolves to a `ProductResponse` object:
|
|
88
112
|
|
|
@@ -97,93 +121,119 @@ Fetches a single product item within a collection.
|
|
|
97
121
|
|
|
98
122
|
- **Example:**
|
|
99
123
|
```ts
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
console.log("Fetched product:", product);
|
|
124
|
+
const prod = await product.get("abc123", "prod789");
|
|
125
|
+
console.log("Fetched product:", prod.name);
|
|
103
126
|
```
|
|
104
127
|
|
|
105
128
|
---
|
|
106
129
|
|
|
107
|
-
|
|
130
|
+
### Namespace: `proof`
|
|
108
131
|
|
|
109
|
-
|
|
132
|
+
#### `proof.get(collectionId: string, proofId: string): Promise<ProofResponse>`
|
|
133
|
+
|
|
134
|
+
Fetches a single proof by collection ID and proof ID.
|
|
110
135
|
|
|
111
136
|
- **Parameters:**
|
|
112
137
|
- `collectionId` (`string`, required): The parent collection’s ID.
|
|
113
|
-
- `
|
|
138
|
+
- `proofId` (`string`, required): The proof’s ID.
|
|
114
139
|
- **Returns:**
|
|
115
|
-
A `Promise` that resolves to
|
|
140
|
+
A `Promise` that resolves to a `ProofResponse` object:
|
|
116
141
|
|
|
117
142
|
```ts
|
|
118
|
-
export interface
|
|
143
|
+
export interface ProofResponse {
|
|
144
|
+
collectionId: string;
|
|
145
|
+
createdAt: string;
|
|
119
146
|
id: string;
|
|
120
|
-
|
|
121
|
-
|
|
147
|
+
productId: string;
|
|
148
|
+
tokenId: string;
|
|
149
|
+
userId: string;
|
|
150
|
+
values: Record<string, any>;
|
|
122
151
|
}
|
|
123
152
|
```
|
|
124
153
|
|
|
125
154
|
- **Example:**
|
|
126
155
|
```ts
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
console.log("Fetched app configuration:", config);
|
|
156
|
+
const prf = await proof.get("abc123", "proof456");
|
|
157
|
+
console.log("Fetched proof:", prf.id);
|
|
130
158
|
```
|
|
131
159
|
|
|
132
160
|
---
|
|
133
161
|
|
|
134
|
-
|
|
162
|
+
### Namespace: `appConfiguration`
|
|
135
163
|
|
|
136
|
-
|
|
164
|
+
#### `appConfiguration.get(collectionId: string, appId: string): Promise<AppConfigurationResponse>`
|
|
137
165
|
|
|
138
|
-
|
|
139
|
-
import { ApiClient } from "@proveanything/smartlinks";
|
|
166
|
+
Fetches a single app configuration by collection ID and app ID.
|
|
140
167
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
168
|
+
- **Parameters:**
|
|
169
|
+
- `collectionId` (`string`, required): The parent collection’s ID.
|
|
170
|
+
- `appId` (`string`, required): The app configuration’s ID.
|
|
171
|
+
- **Returns:**
|
|
172
|
+
A `Promise` that resolves to an `AppConfigurationResponse` object:
|
|
144
173
|
|
|
145
|
-
|
|
174
|
+
```ts
|
|
175
|
+
export interface AppConfigurationResponse {
|
|
176
|
+
id: string;
|
|
177
|
+
name: string;
|
|
178
|
+
settings?: Record<string, any>;
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
- **Example:**
|
|
183
|
+
```ts
|
|
184
|
+
const cfg = await appConfiguration.get("abc123", "app789");
|
|
185
|
+
console.log("Fetched app configuration:", cfg.name);
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
---
|
|
146
189
|
|
|
147
190
|
## Error Handling
|
|
148
191
|
|
|
149
|
-
All methods throw an `Error` when the server responds with a non-2xx status. The thrown error message
|
|
192
|
+
All methods throw an `Error` when the server responds with a non-2xx status. The thrown error message will include the numeric error code and message from the API. Example:
|
|
150
193
|
|
|
151
194
|
```ts
|
|
152
|
-
import {
|
|
153
|
-
|
|
154
|
-
async function fetchData() {
|
|
155
|
-
const client = new ApiClient("https://smartlinks.app/api/v1", "INVALID_KEY");
|
|
195
|
+
import { product } from "@proveanything/smartlinks";
|
|
156
196
|
|
|
197
|
+
async function fetchProduct() {
|
|
157
198
|
try {
|
|
158
|
-
await
|
|
199
|
+
await product.get("abc123", "invalidProdId");
|
|
159
200
|
} catch (err) {
|
|
160
|
-
// err.message might be: "Error
|
|
201
|
+
// err.message might be: "Error 404: Not Found"
|
|
161
202
|
console.error("Request failed:", err);
|
|
162
203
|
}
|
|
163
204
|
}
|
|
164
205
|
|
|
165
|
-
|
|
206
|
+
fetchProduct();
|
|
166
207
|
```
|
|
167
208
|
|
|
168
|
-
|
|
209
|
+
---
|
|
169
210
|
|
|
170
|
-
|
|
211
|
+
## Types
|
|
171
212
|
|
|
172
|
-
|
|
173
|
-
- [`examples/browser-demo.html`](examples/browser-demo.html)
|
|
174
|
-
- [`examples/react-demo.tsx`](examples/react-demo.tsx)
|
|
213
|
+
You can import any of the response interfaces directly:
|
|
175
214
|
|
|
176
|
-
|
|
215
|
+
```ts
|
|
216
|
+
import {
|
|
217
|
+
CollectionResponse,
|
|
218
|
+
ProductResponse,
|
|
219
|
+
ProofResponse,
|
|
220
|
+
AppConfigurationResponse,
|
|
221
|
+
} from "@proveanything/smartlinks/types";
|
|
222
|
+
```
|
|
177
223
|
|
|
178
|
-
|
|
179
|
-
You can find the full API contract in [`openapi.yaml`](openapi.yaml) at the root of this package.
|
|
224
|
+
---
|
|
180
225
|
|
|
181
226
|
## Changelog
|
|
182
227
|
|
|
183
228
|
### 1.0.0
|
|
184
229
|
|
|
185
|
-
- Initial release:
|
|
186
|
-
- `
|
|
187
|
-
-
|
|
188
|
-
|
|
230
|
+
- Initial release:
|
|
231
|
+
- `initializeApi` function to configure baseURL and auth.
|
|
232
|
+
- Namespaced modules:
|
|
233
|
+
- `collection.get(collectionId)`
|
|
234
|
+
- `product.get(collectionId, productId)`
|
|
235
|
+
- `proof.get(collectionId, proofId)`
|
|
236
|
+
- `appConfiguration.get(collectionId, appId)`
|
|
237
|
+
- Full TypeScript typings and JSDoc.
|
|
238
|
+
- Browser/Node fetch support via `cross-fetch`.
|
|
189
239
|
- Error handling via thrown `Error` objects.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AppConfigurationResponse } from "../types/appConfiguration";
|
|
2
|
+
export declare namespace appConfiguration {
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves a single App Configuration by Collection ID and App ID.
|
|
5
|
+
* @param collectionId – Identifier of the parent collection
|
|
6
|
+
* @param appId – Identifier of the app configuration
|
|
7
|
+
* @returns Promise resolving to an AppConfigurationResponse object
|
|
8
|
+
* @throws ErrorResponse if the request fails
|
|
9
|
+
*/
|
|
10
|
+
function get(collectionId: string, appId: string): Promise<AppConfigurationResponse>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/api/appConfiguration.ts
|
|
2
|
+
import { request } from "../http";
|
|
3
|
+
export var appConfiguration;
|
|
4
|
+
(function (appConfiguration) {
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves a single App Configuration by Collection ID and App ID.
|
|
7
|
+
* @param collectionId – Identifier of the parent collection
|
|
8
|
+
* @param appId – Identifier of the app configuration
|
|
9
|
+
* @returns Promise resolving to an AppConfigurationResponse object
|
|
10
|
+
* @throws ErrorResponse if the request fails
|
|
11
|
+
*/
|
|
12
|
+
async function get(collectionId, appId) {
|
|
13
|
+
const path = `/public/collection/${encodeURIComponent(collectionId)}/app/${encodeURIComponent(appId)}`;
|
|
14
|
+
return request(path);
|
|
15
|
+
}
|
|
16
|
+
appConfiguration.get = get;
|
|
17
|
+
})(appConfiguration || (appConfiguration = {}));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CollectionResponse } from "../types/collection";
|
|
2
|
+
export declare namespace collection {
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves a single Collection by its ID.
|
|
5
|
+
* @param collectionId – Identifier of the collection
|
|
6
|
+
* @returns Promise resolving to a CollectionResponse object
|
|
7
|
+
* @throws ErrorResponse if the request fails
|
|
8
|
+
*/
|
|
9
|
+
function get(collectionId: string): Promise<CollectionResponse>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/api/collection.ts
|
|
2
|
+
import { request } from "../http";
|
|
3
|
+
export var collection;
|
|
4
|
+
(function (collection) {
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves a single Collection by its ID.
|
|
7
|
+
* @param collectionId – Identifier of the collection
|
|
8
|
+
* @returns Promise resolving to a CollectionResponse object
|
|
9
|
+
* @throws ErrorResponse if the request fails
|
|
10
|
+
*/
|
|
11
|
+
async function get(collectionId) {
|
|
12
|
+
const path = `/public/collection/${encodeURIComponent(collectionId)}`;
|
|
13
|
+
return request(path);
|
|
14
|
+
}
|
|
15
|
+
collection.get = get;
|
|
16
|
+
})(collection || (collection = {}));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// src/api/index.ts
|
|
2
|
+
// Re-export all resource namespaces so the consumer can import them in one line.
|
|
3
|
+
export { collection } from "./collection";
|
|
4
|
+
export { product } from "./product";
|
|
5
|
+
export { proof } from "./proof";
|
|
6
|
+
export { appConfiguration } from "./appConfiguration";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ProductResponse } from "../types/product";
|
|
2
|
+
export declare namespace product {
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves a single Product Item by Collection ID and Product ID.
|
|
5
|
+
* @param collectionId – Identifier of the parent collection
|
|
6
|
+
* @param productId – Identifier of the product item
|
|
7
|
+
* @returns Promise resolving to a ProductResponse object
|
|
8
|
+
* @throws ErrorResponse if the request fails
|
|
9
|
+
*/
|
|
10
|
+
function get(collectionId: string, productId: string): Promise<ProductResponse>;
|
|
11
|
+
/**
|
|
12
|
+
* Retrieves all Product Items for a Collection.
|
|
13
|
+
* @param collectionId – Identifier of the parent collection
|
|
14
|
+
* @returns Promise resolving to an array of ProductResponse objects
|
|
15
|
+
* @throws ErrorResponse if the request fails
|
|
16
|
+
*/
|
|
17
|
+
function getAll(collectionId: string): Promise<ProductResponse[]>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/api/product.ts
|
|
2
|
+
import { request } from "../http";
|
|
3
|
+
export var product;
|
|
4
|
+
(function (product) {
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves a single Product Item by Collection ID and Product ID.
|
|
7
|
+
* @param collectionId – Identifier of the parent collection
|
|
8
|
+
* @param productId – Identifier of the product item
|
|
9
|
+
* @returns Promise resolving to a ProductResponse object
|
|
10
|
+
* @throws ErrorResponse if the request fails
|
|
11
|
+
*/
|
|
12
|
+
async function get(collectionId, productId) {
|
|
13
|
+
const path = `/public/collection/${encodeURIComponent(collectionId)}/product/${encodeURIComponent(productId)}`;
|
|
14
|
+
return request(path);
|
|
15
|
+
}
|
|
16
|
+
product.get = get;
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves all Product Items for a Collection.
|
|
19
|
+
* @param collectionId – Identifier of the parent collection
|
|
20
|
+
* @returns Promise resolving to an array of ProductResponse objects
|
|
21
|
+
* @throws ErrorResponse if the request fails
|
|
22
|
+
*/
|
|
23
|
+
async function getAll(collectionId) {
|
|
24
|
+
const path = `/public/collection/${encodeURIComponent(collectionId)}/product`;
|
|
25
|
+
return request(path);
|
|
26
|
+
}
|
|
27
|
+
product.getAll = getAll;
|
|
28
|
+
})(product || (product = {}));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ProofResponse } from "../types/proof";
|
|
2
|
+
export declare namespace proof {
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves a single Proof by Collection ID and Proof ID.
|
|
5
|
+
* @param collectionId – Identifier of the parent collection
|
|
6
|
+
* @param proofId – Identifier of the proof
|
|
7
|
+
* @returns Promise resolving to a ProofResponse object
|
|
8
|
+
* @throws ErrorResponse if the request fails
|
|
9
|
+
*/
|
|
10
|
+
function get(collectionId: string, proofId: string): Promise<ProofResponse>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/api/proof.ts
|
|
2
|
+
import { request } from "../http";
|
|
3
|
+
export var proof;
|
|
4
|
+
(function (proof) {
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves a single Proof by Collection ID and Proof ID.
|
|
7
|
+
* @param collectionId – Identifier of the parent collection
|
|
8
|
+
* @param proofId – Identifier of the proof
|
|
9
|
+
* @returns Promise resolving to a ProofResponse object
|
|
10
|
+
* @throws ErrorResponse if the request fails
|
|
11
|
+
*/
|
|
12
|
+
async function get(collectionId, proofId) {
|
|
13
|
+
const path = `/public/collection/${encodeURIComponent(collectionId)}/proof/${encodeURIComponent(proofId)}`;
|
|
14
|
+
return request(path);
|
|
15
|
+
}
|
|
16
|
+
proof.get = get;
|
|
17
|
+
})(proof || (proof = {}));
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Call this once (e.g. at app startup) to configure baseURL/auth.
|
|
3
|
+
*
|
|
4
|
+
* @param options.baseURL - The root URL of the Smartlinks API (e.g. "https://smartlinks.app/api/v1")
|
|
5
|
+
* @param options.apiKey - (Optional) API key for X-API-Key header
|
|
6
|
+
* @param options.bearerToken - (Optional) Bearer token for AUTHORIZATION header
|
|
7
|
+
*/
|
|
8
|
+
export declare function initializeApi(options: {
|
|
9
|
+
baseURL: string;
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
bearerToken?: string;
|
|
12
|
+
}): void;
|
|
13
|
+
/**
|
|
14
|
+
* Internal helper that performs a GET request to \`\${baseURL}\${path}\`,
|
|
15
|
+
* injecting headers for apiKey or bearerToken if present.
|
|
16
|
+
* Returns the parsed JSON as T, or throws an Error.
|
|
17
|
+
*/
|
|
18
|
+
export declare function request<T>(path: string): Promise<T>;
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/http.ts
|
|
2
|
+
// This module replaces the ApiClient constructor. It keeps baseURL, apiKey, bearerToken
|
|
3
|
+
// in module-scope variables, and provides a shared `request<T>(path)` helper that will
|
|
4
|
+
// be used by all namespaced files (collection.ts, product.ts, etc.).
|
|
5
|
+
let baseURL = null;
|
|
6
|
+
let apiKey = undefined;
|
|
7
|
+
let bearerToken = undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Call this once (e.g. at app startup) to configure baseURL/auth.
|
|
10
|
+
*
|
|
11
|
+
* @param options.baseURL - The root URL of the Smartlinks API (e.g. "https://smartlinks.app/api/v1")
|
|
12
|
+
* @param options.apiKey - (Optional) API key for X-API-Key header
|
|
13
|
+
* @param options.bearerToken - (Optional) Bearer token for AUTHORIZATION header
|
|
14
|
+
*/
|
|
15
|
+
export function initializeApi(options) {
|
|
16
|
+
baseURL = options.baseURL.replace(/\/+\$/, ""); // trim trailing slash
|
|
17
|
+
apiKey = options.apiKey;
|
|
18
|
+
bearerToken = options.bearerToken;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Internal helper that performs a GET request to \`\${baseURL}\${path}\`,
|
|
22
|
+
* injecting headers for apiKey or bearerToken if present.
|
|
23
|
+
* Returns the parsed JSON as T, or throws an Error.
|
|
24
|
+
*/
|
|
25
|
+
export async function request(path) {
|
|
26
|
+
if (!baseURL) {
|
|
27
|
+
throw new Error("HTTP client is not initialized. Call initializeApi(...) first.");
|
|
28
|
+
}
|
|
29
|
+
const url = `${baseURL}${path}`;
|
|
30
|
+
const headers = {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
};
|
|
33
|
+
if (apiKey) {
|
|
34
|
+
headers["X-API-Key"] = apiKey;
|
|
35
|
+
}
|
|
36
|
+
if (bearerToken) {
|
|
37
|
+
headers["AUTHORIZATION"] = `Bearer ${bearerToken}`;
|
|
38
|
+
}
|
|
39
|
+
const response = await fetch(url, {
|
|
40
|
+
method: "GET",
|
|
41
|
+
headers,
|
|
42
|
+
});
|
|
43
|
+
if (!response.ok) {
|
|
44
|
+
// Try to parse ErrorResponse; if that fails, throw generic
|
|
45
|
+
try {
|
|
46
|
+
const errBody = (await response.json());
|
|
47
|
+
throw new Error(`Error ${errBody.code}: ${errBody.message}`);
|
|
48
|
+
}
|
|
49
|
+
catch (_a) {
|
|
50
|
+
throw new Error(`Request to ${url} failed with status ${response.status}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return (await response.json());
|
|
54
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,113 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
export interface CollectionResponse {
|
|
5
|
-
/** Unique identifier for the collection */
|
|
6
|
-
id: string;
|
|
7
|
-
/** Machine‐readable name of the collection */
|
|
8
|
-
name: string;
|
|
9
|
-
/** Human‐readable title of the collection */
|
|
10
|
-
title: string;
|
|
11
|
-
/** URL to the collection’s logo image */
|
|
12
|
-
logoImage: string;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Represents a Product Item object.
|
|
16
|
-
*/
|
|
17
|
-
export interface ProductResponse {
|
|
18
|
-
/** Unique identifier for the product */
|
|
19
|
-
id: string;
|
|
20
|
-
/** Name of the product */
|
|
21
|
-
name: string;
|
|
22
|
-
/** Detailed description of the product */
|
|
23
|
-
description: string;
|
|
24
|
-
/** URL to the product’s hero image */
|
|
25
|
-
heroImage: string;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Represents an App Configuration object.
|
|
29
|
-
*/
|
|
30
|
-
export interface AppConfigurationResponse {
|
|
31
|
-
/** Unique identifier for the app configuration */
|
|
32
|
-
id: string;
|
|
33
|
-
/** Name of the app configuration */
|
|
34
|
-
name: string;
|
|
35
|
-
/** Key‐value pairs representing configuration settings */
|
|
36
|
-
settings?: Record<string, any>;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Represents a standardized error response.
|
|
40
|
-
*/
|
|
41
|
-
export interface ErrorResponse {
|
|
42
|
-
/** Numeric error code */
|
|
43
|
-
code: number;
|
|
44
|
-
/** Human‐readable error message */
|
|
45
|
-
message: string;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* ApiClient for the Smartlinks API.
|
|
49
|
-
* Supports both browser (native fetch) and Node (using cross-fetch).
|
|
50
|
-
*/
|
|
51
|
-
export declare class ApiClient {
|
|
52
|
-
private baseURL;
|
|
53
|
-
private apiKey?;
|
|
54
|
-
private bearerToken?;
|
|
55
|
-
/**
|
|
56
|
-
* Creates an instance of ApiClient.
|
|
57
|
-
* @param baseURL - The base URL of the Smartlinks API (e.g., https://smartlinks.app/api/v1)
|
|
58
|
-
* @param apiKey - (Optional) API key for X-API-Key header
|
|
59
|
-
* @param bearerToken - (Optional) Bearer token for AUTHORIZATION header
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* // With both API key and bearer token
|
|
63
|
-
* const client = new ApiClient(
|
|
64
|
-
* 'https://smartlinks.app/api/v1',
|
|
65
|
-
* 'your-api-key',
|
|
66
|
-
* 'your-bearer-token'
|
|
67
|
-
* );
|
|
68
|
-
*
|
|
69
|
-
* // With only API key
|
|
70
|
-
* const client = new ApiClient(
|
|
71
|
-
* 'https://smartlinks.app/api/v1',
|
|
72
|
-
* 'your-api-key'
|
|
73
|
-
* );
|
|
74
|
-
*
|
|
75
|
-
* // With only bearer token
|
|
76
|
-
* const client = new ApiClient(
|
|
77
|
-
* 'https://smartlinks.app/api/v1',
|
|
78
|
-
* undefined,
|
|
79
|
-
* 'your-bearer-token'
|
|
80
|
-
* );
|
|
81
|
-
*/
|
|
82
|
-
constructor(baseURL: string, apiKey?: string, bearerToken?: string);
|
|
83
|
-
/**
|
|
84
|
-
* Retrieves a single Collection by its ID.
|
|
85
|
-
* @param collectionId - Identifier of the collection
|
|
86
|
-
* @returns Promise resolving to a CollectionResponse object
|
|
87
|
-
* @throws ErrorResponse if the request fails
|
|
88
|
-
*/
|
|
89
|
-
getCollection(collectionId: string): Promise<CollectionResponse>;
|
|
90
|
-
/**
|
|
91
|
-
* Retrieves a single Product Item by Collection ID and Product ID.
|
|
92
|
-
* @param collectionId - Identifier of the parent collection
|
|
93
|
-
* @param productId - Identifier of the product item
|
|
94
|
-
* @returns Promise resolving to a ProductResponse object
|
|
95
|
-
* @throws ErrorResponse if the request fails
|
|
96
|
-
*/
|
|
97
|
-
getProductItem(collectionId: string, productId: string): Promise<ProductResponse>;
|
|
98
|
-
/**
|
|
99
|
-
* Retrieves a single App Configuration by Collection ID and App ID.
|
|
100
|
-
* @param collectionId - Identifier of the parent collection
|
|
101
|
-
* @param appId - Identifier of the app configuration
|
|
102
|
-
* @returns Promise resolving to an AppConfigurationResponse object
|
|
103
|
-
* @throws ErrorResponse if the request fails
|
|
104
|
-
*/
|
|
105
|
-
getAppConfiguration(collectionId: string, appId: string): Promise<AppConfigurationResponse>;
|
|
106
|
-
/**
|
|
107
|
-
* Internal helper to perform a GET request and parse JSON.
|
|
108
|
-
* @param path - The path (relative to baseURL) to request
|
|
109
|
-
* @returns Promise resolving to the parsed JSON of type T
|
|
110
|
-
* @throws Error if network error or a non-2xx response is returned
|
|
111
|
-
*/
|
|
112
|
-
private request;
|
|
113
|
-
}
|
|
1
|
+
export { initializeApi, request } from "./http";
|
|
2
|
+
export * from "./api";
|
|
3
|
+
export * from "./types";
|
package/dist/index.js
CHANGED
|
@@ -1,105 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* ApiClient for the Smartlinks API.
|
|
7
|
-
* Supports both browser (native fetch) and Node (using cross-fetch).
|
|
8
|
-
*/
|
|
9
|
-
export class ApiClient {
|
|
10
|
-
/**
|
|
11
|
-
* Creates an instance of ApiClient.
|
|
12
|
-
* @param baseURL - The base URL of the Smartlinks API (e.g., https://smartlinks.app/api/v1)
|
|
13
|
-
* @param apiKey - (Optional) API key for X-API-Key header
|
|
14
|
-
* @param bearerToken - (Optional) Bearer token for AUTHORIZATION header
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* // With both API key and bearer token
|
|
18
|
-
* const client = new ApiClient(
|
|
19
|
-
* 'https://smartlinks.app/api/v1',
|
|
20
|
-
* 'your-api-key',
|
|
21
|
-
* 'your-bearer-token'
|
|
22
|
-
* );
|
|
23
|
-
*
|
|
24
|
-
* // With only API key
|
|
25
|
-
* const client = new ApiClient(
|
|
26
|
-
* 'https://smartlinks.app/api/v1',
|
|
27
|
-
* 'your-api-key'
|
|
28
|
-
* );
|
|
29
|
-
*
|
|
30
|
-
* // With only bearer token
|
|
31
|
-
* const client = new ApiClient(
|
|
32
|
-
* 'https://smartlinks.app/api/v1',
|
|
33
|
-
* undefined,
|
|
34
|
-
* 'your-bearer-token'
|
|
35
|
-
* );
|
|
36
|
-
*/
|
|
37
|
-
constructor(baseURL, apiKey, bearerToken) {
|
|
38
|
-
this.baseURL = baseURL.replace(/\/+$/, ''); // Trim trailing slash
|
|
39
|
-
this.apiKey = apiKey;
|
|
40
|
-
this.bearerToken = bearerToken;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Retrieves a single Collection by its ID.
|
|
44
|
-
* @param collectionId - Identifier of the collection
|
|
45
|
-
* @returns Promise resolving to a CollectionResponse object
|
|
46
|
-
* @throws ErrorResponse if the request fails
|
|
47
|
-
*/
|
|
48
|
-
async getCollection(collectionId) {
|
|
49
|
-
const path = `/public/collection/${encodeURIComponent(collectionId)}`;
|
|
50
|
-
return this.request(path);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Retrieves a single Product Item by Collection ID and Product ID.
|
|
54
|
-
* @param collectionId - Identifier of the parent collection
|
|
55
|
-
* @param productId - Identifier of the product item
|
|
56
|
-
* @returns Promise resolving to a ProductResponse object
|
|
57
|
-
* @throws ErrorResponse if the request fails
|
|
58
|
-
*/
|
|
59
|
-
async getProductItem(collectionId, productId) {
|
|
60
|
-
const path = `/public/collection/${encodeURIComponent(collectionId)}/product/${encodeURIComponent(productId)}`;
|
|
61
|
-
return this.request(path);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Retrieves a single App Configuration by Collection ID and App ID.
|
|
65
|
-
* @param collectionId - Identifier of the parent collection
|
|
66
|
-
* @param appId - Identifier of the app configuration
|
|
67
|
-
* @returns Promise resolving to an AppConfigurationResponse object
|
|
68
|
-
* @throws ErrorResponse if the request fails
|
|
69
|
-
*/
|
|
70
|
-
async getAppConfiguration(collectionId, appId) {
|
|
71
|
-
const path = `/public/collection/${encodeURIComponent(collectionId)}/app/${encodeURIComponent(appId)}`;
|
|
72
|
-
return this.request(path);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Internal helper to perform a GET request and parse JSON.
|
|
76
|
-
* @param path - The path (relative to baseURL) to request
|
|
77
|
-
* @returns Promise resolving to the parsed JSON of type T
|
|
78
|
-
* @throws Error if network error or a non-2xx response is returned
|
|
79
|
-
*/
|
|
80
|
-
async request(path) {
|
|
81
|
-
const url = `${this.baseURL}${path}`;
|
|
82
|
-
const headers = {
|
|
83
|
-
'Content-Type': 'application/json',
|
|
84
|
-
};
|
|
85
|
-
if (this.apiKey) {
|
|
86
|
-
headers['X-API-Key'] = this.apiKey;
|
|
87
|
-
}
|
|
88
|
-
if (this.bearerToken) {
|
|
89
|
-
headers['AUTHORIZATION'] = `Bearer ${this.bearerToken}`;
|
|
90
|
-
}
|
|
91
|
-
const response = await fetch(url, { method: 'GET', headers });
|
|
92
|
-
if (!response.ok) {
|
|
93
|
-
// Attempt to parse error body; if it fails, throw generic
|
|
94
|
-
let errorBody;
|
|
95
|
-
try {
|
|
96
|
-
errorBody = (await response.json());
|
|
97
|
-
}
|
|
98
|
-
catch (_a) {
|
|
99
|
-
throw new Error(`Request failed with status ${response.status}`);
|
|
100
|
-
}
|
|
101
|
-
throw new Error(`Error ${errorBody.code}: ${errorBody.message}`);
|
|
102
|
-
}
|
|
103
|
-
return (await response.json());
|
|
104
|
-
}
|
|
105
|
-
}
|
|
1
|
+
// src/index.ts
|
|
2
|
+
// Top-level entrypoint of the npm package. Re-export initializeApi + all namespaces.
|
|
3
|
+
export { initializeApi, request } from "./http";
|
|
4
|
+
export * from "./api";
|
|
5
|
+
export * from "./types";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents an App Configuration object.
|
|
3
|
+
*/
|
|
4
|
+
export interface AppConfigurationResponse {
|
|
5
|
+
/** Unique identifier for the app configuration */
|
|
6
|
+
id: string;
|
|
7
|
+
/** Name of the app configuration */
|
|
8
|
+
name: string;
|
|
9
|
+
/** Key-value pairs representing configuration settings */
|
|
10
|
+
settings?: Record<string, any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a Collection object.
|
|
3
|
+
*/
|
|
4
|
+
export interface CollectionResponse {
|
|
5
|
+
/** Unique identifier for the collection */
|
|
6
|
+
id: string;
|
|
7
|
+
/** Machine-readable name of the collection */
|
|
8
|
+
name: string;
|
|
9
|
+
/** Human-readable title of the collection */
|
|
10
|
+
title: string;
|
|
11
|
+
/** URL to the collection’s logo image */
|
|
12
|
+
logoImage: string;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a Product Item object.
|
|
3
|
+
*/
|
|
4
|
+
export interface ProductResponse {
|
|
5
|
+
/** Unique identifier for the product */
|
|
6
|
+
id: string;
|
|
7
|
+
/** Name of the product */
|
|
8
|
+
name: string;
|
|
9
|
+
/** Detailed description of the product */
|
|
10
|
+
description: string;
|
|
11
|
+
/** URL to the product’s hero image */
|
|
12
|
+
heroImage: string;
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a Proof object.
|
|
3
|
+
*/
|
|
4
|
+
export interface ProofResponse {
|
|
5
|
+
/** Unique identifier for the collection */
|
|
6
|
+
collectionId: string;
|
|
7
|
+
/** Creation timestamp */
|
|
8
|
+
createdAt: string;
|
|
9
|
+
/** Unique identifier for the proof */
|
|
10
|
+
id: string;
|
|
11
|
+
/** Unique identifier for the product */
|
|
12
|
+
productId: string;
|
|
13
|
+
/** Unique identifier for the token */
|
|
14
|
+
tokenId: string;
|
|
15
|
+
/** Unique identifier for the user */
|
|
16
|
+
userId: string;
|
|
17
|
+
/** Arbitrary key-value pairs for proof values */
|
|
18
|
+
values: Record<string, any>;
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|