@openfn/language-facebook 0.2.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/dist/index.cjs ADDED
@@ -0,0 +1,137 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+
25
+ // src/index.js
26
+ var src_exports = {};
27
+ __export(src_exports, {
28
+ alterState: () => import_language_common2.alterState,
29
+ dataPath: () => import_language_common2.dataPath,
30
+ dataValue: () => import_language_common2.dataValue,
31
+ default: () => src_default,
32
+ each: () => import_language_common2.each,
33
+ execute: () => execute,
34
+ field: () => import_language_common2.field,
35
+ fields: () => import_language_common2.fields,
36
+ fn: () => import_language_common2.fn,
37
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
38
+ merge: () => import_language_common2.merge,
39
+ postMessage: () => postMessage,
40
+ sourceValue: () => import_language_common2.sourceValue
41
+ });
42
+ module.exports = __toCommonJS(src_exports);
43
+
44
+ // src/Adaptor.js
45
+ var Adaptor_exports = {};
46
+ __export(Adaptor_exports, {
47
+ alterState: () => import_language_common2.alterState,
48
+ dataPath: () => import_language_common2.dataPath,
49
+ dataValue: () => import_language_common2.dataValue,
50
+ each: () => import_language_common2.each,
51
+ execute: () => execute,
52
+ field: () => import_language_common2.field,
53
+ fields: () => import_language_common2.fields,
54
+ fn: () => import_language_common2.fn,
55
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
56
+ merge: () => import_language_common2.merge,
57
+ postMessage: () => postMessage,
58
+ sourceValue: () => import_language_common2.sourceValue
59
+ });
60
+ var import_language_common = require("@openfn/language-common");
61
+ var import_request = __toESM(require("request"), 1);
62
+ var import_language_common2 = require("@openfn/language-common");
63
+ function execute(...operations) {
64
+ const initialState = {
65
+ references: [],
66
+ data: null
67
+ };
68
+ return (state) => {
69
+ return (0, import_language_common.execute)(...operations)({
70
+ ...initialState,
71
+ ...state
72
+ });
73
+ };
74
+ }
75
+ function postMessage(params) {
76
+ return (state) => {
77
+ function assembleError({ response, error }) {
78
+ if (response && [200, 201, 202, 204].indexOf(response.statusCode) > -1)
79
+ return false;
80
+ if (error)
81
+ return error;
82
+ return new Error(`Server responded with ${response.statusCode}`);
83
+ }
84
+ const { accessToken } = state.configuration;
85
+ const url = `https://graph.facebook.com/v2.6/me/messages?access_token=${accessToken}`;
86
+ const { headers, message, recipient } = (0, import_language_common.expandReferences)(params)(state);
87
+ const body = {
88
+ messaging_type: "UPDATE",
89
+ recipient,
90
+ message
91
+ };
92
+ return new Promise((resolve, reject) => {
93
+ console.log("Request body:");
94
+ console.log("\n" + JSON.stringify(body, null, 4) + "\n");
95
+ import_request.default.post(
96
+ {
97
+ url,
98
+ headers,
99
+ json: body
100
+ },
101
+ function(error, response, body2) {
102
+ error = assembleError({ error, response });
103
+ if (error) {
104
+ reject(error);
105
+ console.log(response);
106
+ } else {
107
+ console.log("Printing response...\n");
108
+ console.log(JSON.stringify(response, null, 4) + "\n");
109
+ console.log("Post Message succeeded.");
110
+ resolve(body2);
111
+ }
112
+ }
113
+ );
114
+ }).then((response) => {
115
+ const nextState = (0, import_language_common.composeNextState)(state, response);
116
+ return nextState;
117
+ });
118
+ };
119
+ }
120
+
121
+ // src/index.js
122
+ var src_default = Adaptor_exports;
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ alterState,
126
+ dataPath,
127
+ dataValue,
128
+ each,
129
+ execute,
130
+ field,
131
+ fields,
132
+ fn,
133
+ lastReferenceValue,
134
+ merge,
135
+ postMessage,
136
+ sourceValue
137
+ });
package/dist/index.js ADDED
@@ -0,0 +1,115 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/Adaptor.js
8
+ var Adaptor_exports = {};
9
+ __export(Adaptor_exports, {
10
+ alterState: () => alterState,
11
+ dataPath: () => dataPath,
12
+ dataValue: () => dataValue,
13
+ each: () => each,
14
+ execute: () => execute,
15
+ field: () => field,
16
+ fields: () => fields,
17
+ fn: () => fn,
18
+ lastReferenceValue: () => lastReferenceValue,
19
+ merge: () => merge,
20
+ postMessage: () => postMessage,
21
+ sourceValue: () => sourceValue
22
+ });
23
+ import {
24
+ execute as commonExecute,
25
+ expandReferences,
26
+ composeNextState
27
+ } from "@openfn/language-common";
28
+ import request from "request";
29
+ import {
30
+ field,
31
+ fields,
32
+ sourceValue,
33
+ fn,
34
+ alterState,
35
+ each,
36
+ merge,
37
+ dataPath,
38
+ dataValue,
39
+ lastReferenceValue
40
+ } from "@openfn/language-common";
41
+ function execute(...operations) {
42
+ const initialState = {
43
+ references: [],
44
+ data: null
45
+ };
46
+ return (state) => {
47
+ return commonExecute(...operations)({
48
+ ...initialState,
49
+ ...state
50
+ });
51
+ };
52
+ }
53
+ function postMessage(params) {
54
+ return (state) => {
55
+ function assembleError({ response, error }) {
56
+ if (response && [200, 201, 202, 204].indexOf(response.statusCode) > -1)
57
+ return false;
58
+ if (error)
59
+ return error;
60
+ return new Error(`Server responded with ${response.statusCode}`);
61
+ }
62
+ const { accessToken } = state.configuration;
63
+ const url = `https://graph.facebook.com/v2.6/me/messages?access_token=${accessToken}`;
64
+ const { headers, message, recipient } = expandReferences(params)(state);
65
+ const body = {
66
+ messaging_type: "UPDATE",
67
+ recipient,
68
+ message
69
+ };
70
+ return new Promise((resolve, reject) => {
71
+ console.log("Request body:");
72
+ console.log("\n" + JSON.stringify(body, null, 4) + "\n");
73
+ request.post(
74
+ {
75
+ url,
76
+ headers,
77
+ json: body
78
+ },
79
+ function(error, response, body2) {
80
+ error = assembleError({ error, response });
81
+ if (error) {
82
+ reject(error);
83
+ console.log(response);
84
+ } else {
85
+ console.log("Printing response...\n");
86
+ console.log(JSON.stringify(response, null, 4) + "\n");
87
+ console.log("Post Message succeeded.");
88
+ resolve(body2);
89
+ }
90
+ }
91
+ );
92
+ }).then((response) => {
93
+ const nextState = composeNextState(state, response);
94
+ return nextState;
95
+ });
96
+ };
97
+ }
98
+
99
+ // src/index.js
100
+ var src_default = Adaptor_exports;
101
+ export {
102
+ alterState,
103
+ dataPath,
104
+ dataValue,
105
+ src_default as default,
106
+ each,
107
+ execute,
108
+ field,
109
+ fields,
110
+ fn,
111
+ lastReferenceValue,
112
+ merge,
113
+ postMessage,
114
+ sourceValue
115
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@openfn/language-facebook",
3
+ "version": "0.2.0",
4
+ "description": "An Language Package for Facebook Messenger API",
5
+ "main": "dist/index.cjs",
6
+ "author": "Open Function Group",
7
+ "license": "LGPLv3",
8
+ "files": [
9
+ "dist/",
10
+ "types/",
11
+ "ast.json",
12
+ "configuration-schema.json"
13
+ ],
14
+ "dependencies": {
15
+ "@openfn/language-common": "^1.7.5",
16
+ "request": "^2.88.2"
17
+ },
18
+ "devDependencies": {
19
+ "@openfn/buildtools": "^1.0.2",
20
+ "@openfn/simple-ast": "0.4.1",
21
+ "assertion-error": "^2.0.0",
22
+ "chai": "^4.3.7",
23
+ "deep-eql": "^4.1.2",
24
+ "esdoc-es7-plugin": "0.0.3",
25
+ "esno": "^0.16.3",
26
+ "mocha": "^10.1.0",
27
+ "nock": "^13.2.9",
28
+ "rimraf": "^3.0.2"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/openfn/adaptors.git"
33
+ },
34
+ "type": "module",
35
+ "exports": {
36
+ ".": {
37
+ "import": "./dist/index.js",
38
+ "require": "./dist/index.cjs"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "types": "types/index.d.ts",
43
+ "scripts": {
44
+ "build": "pnpm clean && build-adaptor facebook",
45
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
46
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
47
+ "clean": "rimraf dist types docs",
48
+ "pack": "pnpm pack --pack-destination ../../dist"
49
+ }
50
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Execute a sequence of operations.
3
+ * Wraps `@openfn/language-common/execute`, and prepends initial state for http.
4
+ * @example
5
+ * execute(
6
+ * create('foo'),
7
+ * delete('bar')
8
+ * )(state)
9
+ * @function
10
+ * @param {Operations} operations - Operations to be performed.
11
+ * @returns {Operation}
12
+ */
13
+ export function execute(...operations: Operations): Operation;
14
+ /**
15
+ * Post a message
16
+ * @public
17
+ * @example
18
+ * postMessage({
19
+ * "recipient": {
20
+ * "id": "your-psid"
21
+ * },
22
+ * "message": {
23
+ * "text": "your-message"
24
+ * }
25
+ * })
26
+ * @function
27
+ * @param {object} params - data to make the fetch
28
+ * @returns {Operation}
29
+ */
30
+ export function postMessage(params: object): Operation;
31
+ export { field, fields, sourceValue, fn, alterState, each, merge, dataPath, dataValue, lastReferenceValue } from "@openfn/language-common";
@@ -0,0 +1,3 @@
1
+ export default Adaptor;
2
+ export * from "./Adaptor";
3
+ import * as Adaptor from "./Adaptor";