@areumtecnologia/meta 1.0.0 → 1.1.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 CHANGED
@@ -1,9 +1,9 @@
1
1
  const GraphClient = require('./lib/graphClient');
2
- const FlowService = require('./lib/flowService');
3
2
  const TokenService = require('./lib/tokenService');
3
+ const WhatsApp = require('./lib/whatsapp');
4
4
 
5
5
  module.exports = {
6
6
  GraphClient,
7
- FlowService,
7
+ WhatsApp,
8
8
  TokenService
9
9
  };
@@ -6,8 +6,11 @@ class GraphClient {
6
6
  throw new Error('Meta accessToken é obrigatório');
7
7
  }
8
8
 
9
+ this.accessToken = accessToken;
10
+ this.apiVersion = apiVersion;
11
+ this.baseURL = `https://graph.facebook.com/${apiVersion}`;
9
12
  this.client = axios.create({
10
- baseURL: `https://graph.facebook.com/${apiVersion}`,
13
+ baseURL: this.baseURL,
11
14
  headers: {
12
15
  Authorization: `Bearer ${accessToken}`,
13
16
  'Content-Type': 'application/json'
@@ -15,16 +18,29 @@ class GraphClient {
15
18
  });
16
19
  }
17
20
 
18
- async get(url, params = {}) {
19
- return this.client.get(url, { params });
21
+ async get(url, params = {}, config = {}) {
22
+ return this.client.get(url, { params, ...config });
20
23
  }
21
24
 
22
- async post(url, data = {}) {
23
- return this.client.post(url, data);
25
+ async post(url, data = {}, config = {}) {
26
+ return this.client.post(url, data, config);
24
27
  }
25
28
 
26
- async delete(url) {
27
- return this.client.delete(url);
29
+ async put(url, data = {}, config = {}) {
30
+ return await axios.post(
31
+ `${this.baseURL}/${url}`,
32
+ data,
33
+ {
34
+ headers: {
35
+ Authorization: `Bearer ${this.accessToken}`,
36
+ // NÃO coloque Content-Type manualmente
37
+ }
38
+ }
39
+ );
40
+ }
41
+
42
+ async delete(url, config = {}) {
43
+ return this.client.delete(url, config);
28
44
  }
29
45
  }
30
46
 
@@ -0,0 +1,129 @@
1
+ const MetaApiError = require('../metaApiError');
2
+ /**
3
+ * Classe para gerenciar flows
4
+ *
5
+ */
6
+ class FlowService {
7
+
8
+ constructor(graphClient, wabaId) {
9
+ if (!wabaId) {
10
+ throw new Error('wabaId é obrigatório');
11
+ }
12
+
13
+ this.client = graphClient;
14
+ this.wabaId = wabaId;
15
+ }
16
+
17
+ /**
18
+ * CREATE FLOW
19
+ */
20
+ async create({ name, flowJson, categories = ["OTHER"], publish = false, endpointUri = null }) {
21
+ try {
22
+ const res = await this.client.post(`/${this.wabaId}/flows`, {
23
+ name,
24
+ flow_json: flowJson,
25
+ categories,
26
+ publish,
27
+ endpoint_uri: endpointUri
28
+ });
29
+
30
+ return res.data;
31
+ } catch (err) {
32
+ throw MetaApiError.fromAxios(err);
33
+ }
34
+ }
35
+
36
+ /**
37
+ * READ FLOW
38
+ */
39
+ async get(flowId) {
40
+ try {
41
+ if (flowId) {
42
+ const res = await this.client.get(`/${flowId}?fields=id,name,categories,preview,status,validation_errors,json_version,data_api_version,endpoint_uri,whatsapp_business_account,application,health_status`);
43
+ return res.data;
44
+ }
45
+ const res = await this.client.get(`/${this.wabaId}/flows`);
46
+ return res.data;
47
+ } catch (err) {
48
+ throw MetaApiError.fromAxios(err);
49
+ }
50
+ }
51
+
52
+
53
+ /**
54
+ * Updating a Flow's Flow JSON
55
+ */
56
+ async updateFlowJson(flowId, flowJson) {
57
+ try {
58
+ const form = new FormData();
59
+ form.append("name", "flow.json");
60
+ form.append("asset_type", "FLOW_JSON");
61
+ const blob = new Blob(
62
+ [JSON.stringify(flowJson)],
63
+ { type: "application/json" }
64
+ );
65
+
66
+ form.append("file", blob, "flow.json");
67
+ const res = await this.client.put(`/${flowId}/assets`, form);
68
+ return res.data;
69
+ } catch (err) {
70
+ throw MetaApiError.fromAxios(err);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * UPDATE FLOW (NÃO PODE ATUALIZAR O FLOW JSON, APENAS METADADOS)
76
+ */
77
+ async update(flowId, { name, categories, endpointUri, flowJson }) {
78
+ try {
79
+ const res = { metadata: null, json: null };
80
+ if (!flowId) {
81
+ throw new Error('flowId é obrigatório');
82
+ }
83
+ if (name || categories || endpointUri) {
84
+ const rs = await this.client.post(`/${flowId}`, {
85
+ name,
86
+ categories,
87
+ endpoint_uri: endpointUri
88
+ });
89
+ res.metadata = rs.data;
90
+ }
91
+ if (flowJson) {
92
+ res.json = await this.updateFlowJson(flowId, flowJson);
93
+ }
94
+
95
+ return res;
96
+ } catch (err) {
97
+ throw MetaApiError.fromAxios(err);
98
+ }
99
+ }
100
+
101
+ /**
102
+ * PUBLISH FLOW
103
+ */
104
+ async publish(flowId) {
105
+ try {
106
+ const res = await this.client.post(`/${flowId}`, {
107
+ publish: true
108
+ });
109
+
110
+ return res.data;
111
+ } catch (err) {
112
+ throw MetaApiError.fromAxios(err);
113
+ }
114
+ }
115
+
116
+ /**
117
+ * DELETE FLOW
118
+ */
119
+ async delete(flowId) {
120
+ try {
121
+ const res = await this.client.delete(`/${flowId}`);
122
+ return res.data;
123
+ } catch (err) {
124
+ throw MetaApiError.fromAxios(err);
125
+ }
126
+ }
127
+ }
128
+
129
+ module.exports = FlowService;
@@ -0,0 +1,5 @@
1
+ const FlowService = require('./flowService');
2
+
3
+ module.exports = {
4
+ FlowService
5
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@areumtecnologia/meta",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Um wrapper para a api da Meta",
5
5
  "license": "ISC",
6
6
  "author": "Áreum Tecnologia",
@@ -10,6 +10,7 @@
10
10
  "test": "node ./test/app.js"
11
11
  },
12
12
  "dependencies": {
13
+ "@areumtecnologia/meta": "^1.0.0",
13
14
  "axios": "^1.13.5"
14
15
  }
15
16
  }
@@ -1,86 +0,0 @@
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;