@gesagro/jaspion-sdk 1.0.11 → 2.0.0

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,7 +1,66 @@
1
+ # @gesagro/jaspion-sdk
2
+
3
+ SDK para o Jaspion.
4
+
5
+ O SDK é agnóstico de transporte: ele não faz requisições HTTP diretamente.
6
+ Em vez disso, você fornece uma implementação de `HttpClient`, que pode ser
7
+ construída sobre qualquer biblioteca HTTP.
8
+
9
+ ## Instalação
10
+
1
11
  ```bash
2
- npm version x.x.x
12
+ npm install @gesagro/jaspion-sdk
3
13
  ```
4
14
 
5
- ```bash
6
- npm publish
15
+ ## Uso com axios
16
+
17
+ ### 1. Implemente o `HttpClient`
18
+
19
+ O contrato é simples: receber um `HttpRequest` e devolver um `HttpResponse`.
20
+
21
+ ```ts
22
+ import axios, { type AxiosInstance } from 'axios'
23
+ import type { HttpClient, HttpRequest, HttpResponse } from '@gesagro/jaspion-sdk'
24
+
25
+ export class AxiosHttpClient implements HttpClient {
26
+ constructor(private readonly instance: AxiosInstance) {}
27
+
28
+ async request<T>({
29
+ method,
30
+ path,
31
+ body,
32
+ headers,
33
+ }: HttpRequest): Promise<HttpResponse<T>> {
34
+ const response = await this.instance.request<T>({
35
+ method,
36
+ url: path,
37
+ data: body,
38
+ headers,
39
+ })
40
+
41
+ return {
42
+ status: response.status,
43
+ data: response.data,
44
+ }
45
+ }
46
+ }
47
+ ```
48
+
49
+ ### 2. Instancie o `Jaspion`
50
+
51
+ A `baseURL` e a autenticação ficam na instância do axios, mantendo o SDK
52
+ focado apenas nos recursos da API.
53
+
54
+ ```ts
55
+ import axios from 'axios'
56
+ import { Jaspion } from '@gesagro/jaspion-sdk'
57
+ import { AxiosHttpClient } from './axios-http-client'
58
+
59
+ const instance = axios.create({
60
+ baseURL: 'https://api.jaspion.com',
61
+ })
62
+
63
+ const jaspion = new Jaspion({
64
+ http: new AxiosHttpClient(instance),
65
+ })
7
66
  ```
package/dist/index.d.mts CHANGED
@@ -1,9 +1,16 @@
1
- declare class HttpClient {
2
- private readonly baseUrl;
3
- private headers;
4
- constructor(baseUrl: string);
5
- setHeader(key: string, value: string): void;
6
- post(path: string, body: unknown): Promise<void>;
1
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
2
+ interface HttpRequest {
3
+ method: HttpMethod;
4
+ path: string;
5
+ body?: unknown;
6
+ headers?: Record<string, string>;
7
+ }
8
+ interface HttpResponse<T = unknown> {
9
+ status: number;
10
+ data: T;
11
+ }
12
+ interface HttpClient {
13
+ request<T = unknown>(request: HttpRequest): Promise<HttpResponse<T>>;
7
14
  }
8
15
 
9
16
  interface CsvReport {
@@ -1424,12 +1431,12 @@ declare class PigMaster {
1424
1431
  constructor(http: HttpClient);
1425
1432
  }
1426
1433
 
1427
- declare class JaspionClient {
1428
- private readonly http;
1434
+ interface JaspionConfig {
1435
+ http: HttpClient;
1436
+ }
1437
+ declare class Jaspion {
1429
1438
  readonly pigMaster: PigMaster;
1430
- constructor(http: HttpClient);
1431
- setAccessToken(token: string): void;
1439
+ constructor({ http }: JaspionConfig);
1432
1440
  }
1433
- declare function jaspion(baseUrl: string): JaspionClient;
1434
1441
 
1435
- export { jaspion };
1442
+ export { type HttpClient, type HttpMethod, type HttpRequest, type HttpResponse, Jaspion, type JaspionConfig };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,16 @@
1
- declare class HttpClient {
2
- private readonly baseUrl;
3
- private headers;
4
- constructor(baseUrl: string);
5
- setHeader(key: string, value: string): void;
6
- post(path: string, body: unknown): Promise<void>;
1
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
2
+ interface HttpRequest {
3
+ method: HttpMethod;
4
+ path: string;
5
+ body?: unknown;
6
+ headers?: Record<string, string>;
7
+ }
8
+ interface HttpResponse<T = unknown> {
9
+ status: number;
10
+ data: T;
11
+ }
12
+ interface HttpClient {
13
+ request<T = unknown>(request: HttpRequest): Promise<HttpResponse<T>>;
7
14
  }
8
15
 
9
16
  interface CsvReport {
@@ -1424,12 +1431,12 @@ declare class PigMaster {
1424
1431
  constructor(http: HttpClient);
1425
1432
  }
1426
1433
 
1427
- declare class JaspionClient {
1428
- private readonly http;
1434
+ interface JaspionConfig {
1435
+ http: HttpClient;
1436
+ }
1437
+ declare class Jaspion {
1429
1438
  readonly pigMaster: PigMaster;
1430
- constructor(http: HttpClient);
1431
- setAccessToken(token: string): void;
1439
+ constructor({ http }: JaspionConfig);
1432
1440
  }
1433
- declare function jaspion(baseUrl: string): JaspionClient;
1434
1441
 
1435
- export { jaspion };
1442
+ export { type HttpClient, type HttpMethod, type HttpRequest, type HttpResponse, Jaspion, type JaspionConfig };