@areumtecnologia/meta 1.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/index.js +9 -0
- package/lib/flowService.js +86 -0
- package/lib/graphClient.js +31 -0
- package/lib/metaApiError.js +22 -0
- package/lib/tokenService.js +20 -0
- package/package.json +15 -0
package/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const MetaApiError = require('./metaApiError');
|
|
2
|
+
|
|
3
|
+
class FlowService {
|
|
4
|
+
constructor(graphClient, wabaId) {
|
|
5
|
+
if (!wabaId) {
|
|
6
|
+
throw new Error('wabaId é obrigatório');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
this.client = graphClient;
|
|
10
|
+
this.wabaId = wabaId;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* CREATE FLOW
|
|
15
|
+
*/
|
|
16
|
+
async create({ name, categories, flowJson, publish = false }) {
|
|
17
|
+
try {
|
|
18
|
+
const res = await this.client.post(`/${this.wabaId}/flows`, {
|
|
19
|
+
name,
|
|
20
|
+
categories,
|
|
21
|
+
flow_json: flowJson,
|
|
22
|
+
publish
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return res.data;
|
|
26
|
+
} catch (err) {
|
|
27
|
+
throw MetaApiError.fromAxios(err);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* READ FLOW
|
|
33
|
+
*/
|
|
34
|
+
async get(flowId) {
|
|
35
|
+
try {
|
|
36
|
+
const res = await this.client.get(`/${flowId}`);
|
|
37
|
+
return res.data;
|
|
38
|
+
} catch (err) {
|
|
39
|
+
throw MetaApiError.fromAxios(err);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* UPDATE FLOW
|
|
45
|
+
*/
|
|
46
|
+
async update(flowId, flowJson) {
|
|
47
|
+
try {
|
|
48
|
+
const res = await this.client.post(`/${flowId}`, {
|
|
49
|
+
flow_json: flowJson
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return res.data;
|
|
53
|
+
} catch (err) {
|
|
54
|
+
throw MetaApiError.fromAxios(err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* PUBLISH FLOW
|
|
60
|
+
*/
|
|
61
|
+
async publish(flowId) {
|
|
62
|
+
try {
|
|
63
|
+
const res = await this.client.post(`/${flowId}`, {
|
|
64
|
+
publish: true
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return res.data;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
throw MetaApiError.fromAxios(err);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* DELETE FLOW
|
|
75
|
+
*/
|
|
76
|
+
async delete(flowId) {
|
|
77
|
+
try {
|
|
78
|
+
const res = await this.client.delete(`/${flowId}`);
|
|
79
|
+
return res.data;
|
|
80
|
+
} catch (err) {
|
|
81
|
+
throw MetaApiError.fromAxios(err);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = FlowService;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
|
|
3
|
+
class GraphClient {
|
|
4
|
+
constructor({ accessToken, apiVersion = 'v24.0' }) {
|
|
5
|
+
if (!accessToken) {
|
|
6
|
+
throw new Error('Meta accessToken é obrigatório');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
this.client = axios.create({
|
|
10
|
+
baseURL: `https://graph.facebook.com/${apiVersion}`,
|
|
11
|
+
headers: {
|
|
12
|
+
Authorization: `Bearer ${accessToken}`,
|
|
13
|
+
'Content-Type': 'application/json'
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async get(url, params = {}) {
|
|
19
|
+
return this.client.get(url, { params });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async post(url, data = {}) {
|
|
23
|
+
return this.client.post(url, data);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async delete(url) {
|
|
27
|
+
return this.client.delete(url);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = GraphClient;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class MetaApiError extends Error {
|
|
2
|
+
constructor(message, status, data) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'MetaApiError';
|
|
5
|
+
this.status = status;
|
|
6
|
+
this.data = data;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static fromAxios(err) {
|
|
10
|
+
if (err.response) {
|
|
11
|
+
return new MetaApiError(
|
|
12
|
+
err.response.data?.error?.message || 'Erro na API da Meta',
|
|
13
|
+
err.response.status,
|
|
14
|
+
err.response.data
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return new MetaApiError(err.message, 500, null);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = MetaApiError;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class TokenService {
|
|
2
|
+
constructor({ accessToken, expiresAt = null }) {
|
|
3
|
+
this.accessToken = accessToken;
|
|
4
|
+
this.expiresAt = expiresAt;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
isExpired() {
|
|
8
|
+
if (!this.expiresAt) return false;
|
|
9
|
+
return Date.now() >= this.expiresAt;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
getToken() {
|
|
13
|
+
if (this.isExpired()) {
|
|
14
|
+
throw new Error('Access token expirado');
|
|
15
|
+
}
|
|
16
|
+
return this.accessToken;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = TokenService;
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@areumtecnologia/meta",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Um wrapper para a api da Meta",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "Áreum Tecnologia",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "node ./test/app.js"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"axios": "^1.13.5"
|
|
14
|
+
}
|
|
15
|
+
}
|