@defarm/sdk 0.1.0 → 0.1.2
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 +24 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +8 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +8 -1
- package/dist/modules.d.ts +18 -1
- package/dist/modules.js +34 -0
- package/dist/types.d.ts +31 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,6 +17,30 @@ const sdk = new DefarmSdk({ gatewayBaseUrl: "https://gateway.defarm.net" });
|
|
|
17
17
|
const auth = await sdk.auth.login("email", "senha");
|
|
18
18
|
sdk.setAccessToken(auth.access_token);
|
|
19
19
|
|
|
20
|
+
const circuits = await sdk.circuits.list();
|
|
21
|
+
console.log(circuits);
|
|
22
|
+
|
|
23
|
+
const disclosure = await sdk.disclosures.create({
|
|
24
|
+
item_id: "<item_id>",
|
|
25
|
+
preset: "finance_basic",
|
|
26
|
+
audience: "bank_partner",
|
|
27
|
+
});
|
|
28
|
+
console.log(disclosure.receipt_id);
|
|
29
|
+
|
|
30
|
+
const receipts = await sdk.receipts.list({ receipt_type: "disclosure" });
|
|
31
|
+
console.log(receipts.length);
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Autenticação por API key (parceiro)
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { DefarmSdk } from "@defarm/sdk";
|
|
38
|
+
|
|
39
|
+
const sdk = new DefarmSdk({
|
|
40
|
+
gatewayBaseUrl: "https://gateway.defarm.net",
|
|
41
|
+
apiKey: process.env.DEFARM_API_KEY,
|
|
42
|
+
});
|
|
43
|
+
|
|
20
44
|
const circuits = await sdk.circuits.list();
|
|
21
45
|
console.log(circuits);
|
|
22
46
|
```
|
package/dist/client.d.ts
CHANGED
|
@@ -2,7 +2,9 @@ import { HttpMethod, SdkConfig } from "./types.js";
|
|
|
2
2
|
export declare class DefarmHttpClient {
|
|
3
3
|
private readonly config;
|
|
4
4
|
private accessToken?;
|
|
5
|
+
private apiKey?;
|
|
5
6
|
constructor(config: SdkConfig);
|
|
6
7
|
setAccessToken(token?: string): void;
|
|
8
|
+
setApiKey(apiKey?: string): void;
|
|
7
9
|
request<T>(method: HttpMethod, path: string, body?: unknown): Promise<T>;
|
|
8
10
|
}
|
package/dist/client.js
CHANGED
|
@@ -3,15 +3,20 @@ import { DefarmApiError } from "./types.js";
|
|
|
3
3
|
export class DefarmHttpClient {
|
|
4
4
|
config;
|
|
5
5
|
accessToken;
|
|
6
|
+
apiKey;
|
|
6
7
|
constructor(config) {
|
|
7
8
|
this.config = {
|
|
8
9
|
timeoutMs: 20000,
|
|
9
10
|
...config,
|
|
10
11
|
};
|
|
12
|
+
this.apiKey = config.apiKey;
|
|
11
13
|
}
|
|
12
14
|
setAccessToken(token) {
|
|
13
15
|
this.accessToken = token;
|
|
14
16
|
}
|
|
17
|
+
setApiKey(apiKey) {
|
|
18
|
+
this.apiKey = apiKey;
|
|
19
|
+
}
|
|
15
20
|
async request(method, path, body) {
|
|
16
21
|
const url = `${this.config.gatewayBaseUrl.replace(/\/$/, "")}${path}`;
|
|
17
22
|
const headers = {
|
|
@@ -20,6 +25,9 @@ export class DefarmHttpClient {
|
|
|
20
25
|
if (this.accessToken) {
|
|
21
26
|
headers.authorization = `Bearer ${this.accessToken}`;
|
|
22
27
|
}
|
|
28
|
+
if (this.apiKey) {
|
|
29
|
+
headers["x-api-key"] = this.apiKey;
|
|
30
|
+
}
|
|
23
31
|
const response = await undiciRequest(url, {
|
|
24
32
|
method,
|
|
25
33
|
headers,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DefarmHttpClient } from "./client.js";
|
|
2
|
-
import { AuthApi, CircuitsApi, EventsApi, ItemsApi, WorkspaceApi } from "./modules.js";
|
|
2
|
+
import { AuthApi, CircuitsApi, DisclosuresApi, EventsApi, ItemsApi, ReceiptsApi, WorkspaceApi } from "./modules.js";
|
|
3
3
|
import { SdkConfig } from "./types.js";
|
|
4
4
|
export * from "./types.js";
|
|
5
5
|
export declare class DefarmSdk {
|
|
@@ -8,7 +8,10 @@ export declare class DefarmSdk {
|
|
|
8
8
|
readonly circuits: CircuitsApi;
|
|
9
9
|
readonly items: ItemsApi;
|
|
10
10
|
readonly events: EventsApi;
|
|
11
|
+
readonly disclosures: DisclosuresApi;
|
|
12
|
+
readonly receipts: ReceiptsApi;
|
|
11
13
|
readonly http: DefarmHttpClient;
|
|
12
14
|
constructor(config: SdkConfig);
|
|
13
15
|
setAccessToken(token?: string): void;
|
|
16
|
+
setApiKey(apiKey?: string): void;
|
|
14
17
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DefarmHttpClient } from "./client.js";
|
|
2
|
-
import { AuthApi, CircuitsApi, EventsApi, ItemsApi, WorkspaceApi } from "./modules.js";
|
|
2
|
+
import { AuthApi, CircuitsApi, DisclosuresApi, EventsApi, ItemsApi, ReceiptsApi, WorkspaceApi } from "./modules.js";
|
|
3
3
|
export * from "./types.js";
|
|
4
4
|
export class DefarmSdk {
|
|
5
5
|
auth;
|
|
@@ -7,6 +7,8 @@ export class DefarmSdk {
|
|
|
7
7
|
circuits;
|
|
8
8
|
items;
|
|
9
9
|
events;
|
|
10
|
+
disclosures;
|
|
11
|
+
receipts;
|
|
10
12
|
http;
|
|
11
13
|
constructor(config) {
|
|
12
14
|
this.http = new DefarmHttpClient(config);
|
|
@@ -15,8 +17,13 @@ export class DefarmSdk {
|
|
|
15
17
|
this.circuits = new CircuitsApi(this.http);
|
|
16
18
|
this.items = new ItemsApi(this.http);
|
|
17
19
|
this.events = new EventsApi(this.http);
|
|
20
|
+
this.disclosures = new DisclosuresApi(this.http);
|
|
21
|
+
this.receipts = new ReceiptsApi(this.http);
|
|
18
22
|
}
|
|
19
23
|
setAccessToken(token) {
|
|
20
24
|
this.http.setAccessToken(token);
|
|
21
25
|
}
|
|
26
|
+
setApiKey(apiKey) {
|
|
27
|
+
this.http.setApiKey(apiKey);
|
|
28
|
+
}
|
|
22
29
|
}
|
package/dist/modules.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DefarmHttpClient } from "./client.js";
|
|
2
|
-
import { AuthResponse, Circuit, Event, Item } from "./types.js";
|
|
2
|
+
import { AuthResponse, Circuit, DisclosureRequest, DisclosureResponse, Event, Item, ReceiptSummary } from "./types.js";
|
|
3
3
|
export declare class AuthApi {
|
|
4
4
|
private readonly http;
|
|
5
5
|
constructor(http: DefarmHttpClient);
|
|
@@ -43,3 +43,20 @@ export declare class EventsApi {
|
|
|
43
43
|
add(payload: Record<string, unknown>): Promise<unknown>;
|
|
44
44
|
update(id: string, payload: Record<string, unknown>): Promise<unknown>;
|
|
45
45
|
}
|
|
46
|
+
export declare class DisclosuresApi {
|
|
47
|
+
private readonly http;
|
|
48
|
+
constructor(http: DefarmHttpClient);
|
|
49
|
+
create(payload: DisclosureRequest): Promise<DisclosureResponse>;
|
|
50
|
+
}
|
|
51
|
+
export declare class ReceiptsApi {
|
|
52
|
+
private readonly http;
|
|
53
|
+
constructor(http: DefarmHttpClient);
|
|
54
|
+
list(params?: {
|
|
55
|
+
receipt_type?: string;
|
|
56
|
+
circuit_id?: string;
|
|
57
|
+
item_id?: string;
|
|
58
|
+
limit?: number;
|
|
59
|
+
offset?: number;
|
|
60
|
+
}): Promise<ReceiptSummary[]>;
|
|
61
|
+
show(id: string): Promise<unknown>;
|
|
62
|
+
}
|
package/dist/modules.js
CHANGED
|
@@ -89,3 +89,37 @@ export class EventsApi {
|
|
|
89
89
|
return this.http.request("PUT", `/api/events/${id}/status`, payload);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
+
export class DisclosuresApi {
|
|
93
|
+
http;
|
|
94
|
+
constructor(http) {
|
|
95
|
+
this.http = http;
|
|
96
|
+
}
|
|
97
|
+
async create(payload) {
|
|
98
|
+
return this.http.request("POST", "/api/disclosures", payload);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
export class ReceiptsApi {
|
|
102
|
+
http;
|
|
103
|
+
constructor(http) {
|
|
104
|
+
this.http = http;
|
|
105
|
+
}
|
|
106
|
+
async list(params) {
|
|
107
|
+
const qs = new URLSearchParams();
|
|
108
|
+
if (params?.receipt_type)
|
|
109
|
+
qs.set("receipt_type", params.receipt_type);
|
|
110
|
+
if (params?.circuit_id)
|
|
111
|
+
qs.set("circuit_id", params.circuit_id);
|
|
112
|
+
if (params?.item_id)
|
|
113
|
+
qs.set("item_id", params.item_id);
|
|
114
|
+
if (params?.limit != null)
|
|
115
|
+
qs.set("limit", String(params.limit));
|
|
116
|
+
if (params?.offset != null)
|
|
117
|
+
qs.set("offset", String(params.offset));
|
|
118
|
+
const suffix = qs.toString() ? `?${qs.toString()}` : "";
|
|
119
|
+
const res = await this.http.request("GET", `/api/receipts${suffix}`);
|
|
120
|
+
return Array.isArray(res) ? res : res.receipts || [];
|
|
121
|
+
}
|
|
122
|
+
async show(id) {
|
|
123
|
+
return this.http.request("GET", `/api/receipts/${id}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
|
3
3
|
export interface SdkConfig {
|
|
4
4
|
gatewayBaseUrl: string;
|
|
5
5
|
timeoutMs?: number;
|
|
6
|
+
apiKey?: string;
|
|
6
7
|
}
|
|
7
8
|
export interface AuthTokens {
|
|
8
9
|
access_token: string;
|
|
@@ -50,6 +51,36 @@ export interface Event {
|
|
|
50
51
|
circuit_id?: string;
|
|
51
52
|
payload?: Record<string, unknown>;
|
|
52
53
|
}
|
|
54
|
+
export interface DisclosureRequest {
|
|
55
|
+
item_id: string;
|
|
56
|
+
preset: "finance_basic" | "audit_basic" | "public_basic" | string;
|
|
57
|
+
audience?: string;
|
|
58
|
+
expires_in_days?: number;
|
|
59
|
+
}
|
|
60
|
+
export interface DisclosureResponse {
|
|
61
|
+
receipt_id: string;
|
|
62
|
+
preset: string;
|
|
63
|
+
audience?: string;
|
|
64
|
+
proof_hash: string;
|
|
65
|
+
expires_at?: string;
|
|
66
|
+
disclosed_payload: Record<string, unknown>;
|
|
67
|
+
}
|
|
68
|
+
export interface ReceiptSummary {
|
|
69
|
+
receipt_id: string;
|
|
70
|
+
receipt_type: "ingestion" | "disclosure" | string;
|
|
71
|
+
status: string;
|
|
72
|
+
created_at: string;
|
|
73
|
+
completed_at?: string;
|
|
74
|
+
circuit_id?: string;
|
|
75
|
+
item_id?: string;
|
|
76
|
+
preset?: string;
|
|
77
|
+
audience?: string;
|
|
78
|
+
proof_hash?: string;
|
|
79
|
+
rows_total?: number;
|
|
80
|
+
items_created?: number;
|
|
81
|
+
items_updated?: number;
|
|
82
|
+
events_created?: number;
|
|
83
|
+
}
|
|
53
84
|
export declare class DefarmApiError extends Error {
|
|
54
85
|
status: number;
|
|
55
86
|
details?: unknown;
|