@afiliado/sdk 1.0.0 → 1.0.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/dist/index.d.ts CHANGED
@@ -1,14 +1,34 @@
1
- declare class AfiliadoSDK {
1
+ export interface ParsedOffer {
2
+ id: string;
3
+ link: string;
4
+ title: string;
5
+ description: string;
6
+ image: string;
7
+ price_discount: number;
8
+ price_original: number;
9
+ percent_discount: number;
10
+ has_discount: boolean;
11
+ category: string;
12
+ cod: string;
13
+ marketplace: 'amazon' | 'shopee' | 'mercadolivre' | 'unknown';
14
+ visits: number;
15
+ clicks: number;
16
+ created_at: string;
17
+ active: boolean;
18
+ shortlink: string;
19
+ slug: string;
20
+ }
21
+ export declare class AfiliadoSDK {
2
22
  private apiKey;
3
- private baseUrl;
4
- constructor(apiKey: string);
5
- /**
6
- * Transforma uma URL bruta em um objeto de produto com link de afiliado.
7
- */
8
- parse({ url, tags }: {
23
+ private userId;
24
+ private domain;
25
+ constructor({ apiKey, userId }: {
26
+ apiKey: string;
27
+ userId: string;
28
+ });
29
+ parse({ baseUrl, url, tags }: {
30
+ baseUrl?: string;
9
31
  url: string;
10
32
  tags?: Record<string, string>;
11
- }): Promise<any>;
33
+ }): Promise<ParsedOffer>;
12
34
  }
13
- export declare const afiliado: AfiliadoSDK;
14
- export {};
package/dist/index.js CHANGED
@@ -1,41 +1,45 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
4
  };
11
5
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.afiliado = void 0;
6
+ exports.AfiliadoSDK = void 0;
7
+ const crypto_1 = __importDefault(require("crypto"));
13
8
  class AfiliadoSDK {
14
- constructor(apiKey) {
15
- // Em produção, use a URL real: https://api.afiliado.me/v1
16
- this.baseUrl = '/api/v1';
9
+ apiKey;
10
+ userId;
11
+ domain = `${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/api/v1`;
12
+ constructor({ apiKey, userId }) {
13
+ if (!apiKey || !userId)
14
+ throw new Error("AfiliadoSDK: Credenciais ausentes.");
17
15
  this.apiKey = apiKey;
16
+ this.userId = userId;
18
17
  }
19
- /**
20
- * Transforma uma URL bruta em um objeto de produto com link de afiliado.
21
- */
22
- parse(_a) {
23
- return __awaiter(this, arguments, void 0, function* ({ url, tags }) {
24
- const res = yield fetch(`${this.baseUrl}/parse`, {
25
- method: 'POST',
26
- headers: {
27
- 'Authorization': `Bearer ${this.apiKey}`,
28
- 'Content-Type': 'application/json',
29
- },
30
- body: JSON.stringify({ url, tags }),
31
- });
32
- if (!res.ok) {
33
- const error = yield res.json();
34
- throw new Error(error.error || 'Falha ao processar API');
35
- }
36
- return res.json();
18
+ async parse({ baseUrl, url, tags }) {
19
+ const fetchUrl = baseUrl ? `${baseUrl}/parse` : `${this.domain}/parse`;
20
+ const bodyString = JSON.stringify({ url, tags });
21
+ const timestamp = Date.now().toString();
22
+ const signaturePayload = `${timestamp}.${bodyString}`;
23
+ const signature = crypto_1.default
24
+ .createHmac('sha256', this.apiKey)
25
+ .update(signaturePayload)
26
+ .digest('hex');
27
+ const res = await fetch(fetchUrl, {
28
+ method: 'POST',
29
+ headers: {
30
+ 'x-customer-id': this.userId,
31
+ 'x-timestamp': timestamp,
32
+ 'x-signature': signature,
33
+ 'x-api-key': this.apiKey,
34
+ 'Content-Type': 'application/json',
35
+ },
36
+ body: bodyString,
37
37
  });
38
+ if (!res.ok) {
39
+ const error = await res.json();
40
+ throw new Error(error.error || 'Falha na validação de segurança da API');
41
+ }
42
+ return res.json();
38
43
  }
39
44
  }
40
- // Exporta uma instância pronta para uso se houver uma KEY no ambiente
41
- exports.afiliado = new AfiliadoSDK(process.env.AFILIADO_API_KEY || '');
45
+ exports.AfiliadoSDK = AfiliadoSDK;
package/package.json CHANGED
@@ -1,18 +1,27 @@
1
1
  {
2
2
  "name": "@afiliado/sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "SDK oficial para integração com o motor de links do afiliado.me",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
- "files": ["dist"],
7
+ "files": [
8
+ "dist"
9
+ ],
8
10
  "scripts": {
9
11
  "build": "tsc",
10
12
  "prepublishOnly": "npm run build"
11
13
  },
12
- "keywords": ["affiliate", "marketing", "sdk", "nextjs"],
14
+ "keywords": [
15
+ "affiliate",
16
+ "marketing",
17
+ "sdk",
18
+ "nextjs"
19
+ ],
13
20
  "author": "Afiliado.me",
14
21
  "license": "MIT",
15
22
  "devDependencies": {
23
+ "@types/node": "^25.2.3",
24
+ "crypto": "^1.0.1",
16
25
  "typescript": "^5.0.0"
17
26
  }
18
- }
27
+ }