@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 +30 -10
- package/dist/index.js +36 -32
- package/package.json +13 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,34 @@
|
|
|
1
|
-
|
|
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
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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<
|
|
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
|
|
3
|
-
|
|
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.
|
|
6
|
+
exports.AfiliadoSDK = void 0;
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
13
8
|
class AfiliadoSDK {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
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.
|
|
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": [
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
8
10
|
"scripts": {
|
|
9
11
|
"build": "tsc",
|
|
10
12
|
"prepublishOnly": "npm run build"
|
|
11
13
|
},
|
|
12
|
-
"keywords": [
|
|
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
|
+
}
|