@openfn/language-whatsapp 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.
@@ -0,0 +1,34 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "properties": {
4
+ "apiToken": {
5
+ "title": "API Token",
6
+ "type": "string",
7
+ "description": "The whatsapp API token from Meta",
8
+ "writeOnly": true,
9
+ "minLength": 1,
10
+ "examples": ["EAAZAq6ZBYUd64BO83b46fn2MdraueL5s9I5IRZCtWUpYsfmEZARN2HDmL8BE5UhFf98EuTnpkhZBWTR9xippoN1a9MzY8Giw3pjSp55YMAF1nowqsy9aLODz48dm8TpXk8ZADUt6qX3vnYJzcn4OH9jrOIiW5ivt3By0beSjbX0E32ZCUMQpS9xRirepvc7qgGKurWQKIcPLPVSGjXQlZCSZBmLdCL8IW07qfjISupZA3HIniORAZDZD"]
11
+ },
12
+ "apiVersion": {
13
+ "title": "API Version",
14
+ "type": "string",
15
+ "description": "The whatsapp API version from Meta",
16
+ "examples": ["v22.0"]
17
+ },
18
+ "baseUrl": {
19
+ "title": "Base URL",
20
+ "type": "string",
21
+ "description": "The whatsapp server base URL",
22
+ "examples": ["https://graph.facebook.com"]
23
+ },
24
+ "phoneNumberId": {
25
+ "title": "Phone Number ID",
26
+ "type": "string",
27
+ "description": "The whatsapp phone number ID from Meta",
28
+ "examples": ["720348101158165"]
29
+ }
30
+ },
31
+ "type": "object",
32
+ "additionalProperties": true,
33
+ "required": ["phoneNumberId", "apiToken"]
34
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,146 @@
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
+ combine: () => import_language_common2.combine,
29
+ cursor: () => import_language_common2.cursor,
30
+ dataPath: () => import_language_common2.dataPath,
31
+ dataValue: () => import_language_common2.dataValue,
32
+ dateFns: () => import_language_common2.dateFns,
33
+ default: () => src_default,
34
+ each: () => import_language_common2.each,
35
+ field: () => import_language_common2.field,
36
+ fields: () => import_language_common2.fields,
37
+ fn: () => import_language_common2.fn,
38
+ fnIf: () => import_language_common2.fnIf,
39
+ group: () => import_language_common2.group,
40
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
41
+ merge: () => import_language_common2.merge,
42
+ request: () => request2,
43
+ scrubEmojis: () => import_language_common2.scrubEmojis,
44
+ sourceValue: () => import_language_common2.sourceValue,
45
+ util: () => import_language_common2.util
46
+ });
47
+ module.exports = __toCommonJS(src_exports);
48
+
49
+ // src/Adaptor.js
50
+ var Adaptor_exports = {};
51
+ __export(Adaptor_exports, {
52
+ combine: () => import_language_common2.combine,
53
+ cursor: () => import_language_common2.cursor,
54
+ dataPath: () => import_language_common2.dataPath,
55
+ dataValue: () => import_language_common2.dataValue,
56
+ dateFns: () => import_language_common2.dateFns,
57
+ each: () => import_language_common2.each,
58
+ field: () => import_language_common2.field,
59
+ fields: () => import_language_common2.fields,
60
+ fn: () => import_language_common2.fn,
61
+ fnIf: () => import_language_common2.fnIf,
62
+ group: () => import_language_common2.group,
63
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
64
+ merge: () => import_language_common2.merge,
65
+ request: () => request2,
66
+ scrubEmojis: () => import_language_common2.scrubEmojis,
67
+ sourceValue: () => import_language_common2.sourceValue,
68
+ util: () => import_language_common2.util
69
+ });
70
+ var import_util2 = require("@openfn/language-common/util");
71
+
72
+ // src/Utils.js
73
+ var import_language_common = require("@openfn/language-common");
74
+ var import_util = require("@openfn/language-common/util");
75
+ var import_node_path = __toESM(require("path"), 1);
76
+ var prepareNextState = (state, response) => {
77
+ const { body, ...responseWithoutBody } = response;
78
+ if (!state.references) {
79
+ state.references = [];
80
+ }
81
+ return {
82
+ ...(0, import_language_common.composeNextState)(state, response.body),
83
+ response: responseWithoutBody
84
+ };
85
+ };
86
+ var request = (configuration = {}, method, path, options) => {
87
+ const { phoneNumberId, apiToken, baseUrl = "https://graph.facebook.com", apiVersion = "" } = configuration;
88
+ const { headers = {} } = options;
89
+ const errors = {
90
+ 404: "Page not found"
91
+ };
92
+ const opts = {
93
+ parseAs: "json",
94
+ errors,
95
+ baseUrl,
96
+ ...options,
97
+ headers: {
98
+ "content-type": "application/json",
99
+ "Authorization": `Bearer ${apiToken}`,
100
+ ...headers
101
+ }
102
+ };
103
+ const safePath = import_node_path.default.join(apiVersion, phoneNumberId, path);
104
+ return (0, import_util.request)(method, safePath, opts);
105
+ };
106
+
107
+ // src/Adaptor.js
108
+ var import_language_common2 = require("@openfn/language-common");
109
+ function request2(method, path, body, options = {}) {
110
+ return async (state) => {
111
+ const [resolvedMethod, resolvedPath, resolvedBody, resolvedoptions] = (0, import_util2.expandReferences)(state, method, path, body, options);
112
+ const response = await request(
113
+ state.configuration,
114
+ resolvedMethod,
115
+ resolvedPath,
116
+ {
117
+ body: resolvedBody,
118
+ ...resolvedoptions
119
+ }
120
+ );
121
+ return prepareNextState(state, response);
122
+ };
123
+ }
124
+
125
+ // src/index.js
126
+ var src_default = Adaptor_exports;
127
+ // Annotate the CommonJS export names for ESM import in node:
128
+ 0 && (module.exports = {
129
+ combine,
130
+ cursor,
131
+ dataPath,
132
+ dataValue,
133
+ dateFns,
134
+ each,
135
+ field,
136
+ fields,
137
+ fn,
138
+ fnIf,
139
+ group,
140
+ lastReferenceValue,
141
+ merge,
142
+ request,
143
+ scrubEmojis,
144
+ sourceValue,
145
+ util
146
+ });
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
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
+ combine: () => combine,
11
+ cursor: () => cursor,
12
+ dataPath: () => dataPath,
13
+ dataValue: () => dataValue,
14
+ dateFns: () => dateFns,
15
+ each: () => each,
16
+ field: () => field,
17
+ fields: () => fields,
18
+ fn: () => fn,
19
+ fnIf: () => fnIf,
20
+ group: () => group,
21
+ lastReferenceValue: () => lastReferenceValue,
22
+ merge: () => merge,
23
+ request: () => request2,
24
+ scrubEmojis: () => scrubEmojis,
25
+ sourceValue: () => sourceValue,
26
+ util: () => util
27
+ });
28
+ import { expandReferences } from "@openfn/language-common/util";
29
+
30
+ // src/Utils.js
31
+ import { composeNextState } from "@openfn/language-common";
32
+ import {
33
+ request as commonRequest
34
+ } from "@openfn/language-common/util";
35
+ import nodepath from "path";
36
+ var prepareNextState = (state, response) => {
37
+ const { body, ...responseWithoutBody } = response;
38
+ if (!state.references) {
39
+ state.references = [];
40
+ }
41
+ return {
42
+ ...composeNextState(state, response.body),
43
+ response: responseWithoutBody
44
+ };
45
+ };
46
+ var request = (configuration = {}, method, path, options) => {
47
+ const { phoneNumberId, apiToken, baseUrl = "https://graph.facebook.com", apiVersion = "" } = configuration;
48
+ const { headers = {} } = options;
49
+ const errors = {
50
+ 404: "Page not found"
51
+ };
52
+ const opts = {
53
+ parseAs: "json",
54
+ errors,
55
+ baseUrl,
56
+ ...options,
57
+ headers: {
58
+ "content-type": "application/json",
59
+ "Authorization": `Bearer ${apiToken}`,
60
+ ...headers
61
+ }
62
+ };
63
+ const safePath = nodepath.join(apiVersion, phoneNumberId, path);
64
+ return commonRequest(method, safePath, opts);
65
+ };
66
+
67
+ // src/Adaptor.js
68
+ import {
69
+ combine,
70
+ cursor,
71
+ dataPath,
72
+ dataValue,
73
+ dateFns,
74
+ each,
75
+ field,
76
+ fields,
77
+ fn,
78
+ fnIf,
79
+ group,
80
+ lastReferenceValue,
81
+ merge,
82
+ scrubEmojis,
83
+ sourceValue,
84
+ util
85
+ } from "@openfn/language-common";
86
+ function request2(method, path, body, options = {}) {
87
+ return async (state) => {
88
+ const [resolvedMethod, resolvedPath, resolvedBody, resolvedoptions] = expandReferences(state, method, path, body, options);
89
+ const response = await request(
90
+ state.configuration,
91
+ resolvedMethod,
92
+ resolvedPath,
93
+ {
94
+ body: resolvedBody,
95
+ ...resolvedoptions
96
+ }
97
+ );
98
+ return prepareNextState(state, response);
99
+ };
100
+ }
101
+
102
+ // src/index.js
103
+ var src_default = Adaptor_exports;
104
+ export {
105
+ combine,
106
+ cursor,
107
+ dataPath,
108
+ dataValue,
109
+ dateFns,
110
+ src_default as default,
111
+ each,
112
+ field,
113
+ fields,
114
+ fn,
115
+ fnIf,
116
+ group,
117
+ lastReferenceValue,
118
+ merge,
119
+ request2 as request,
120
+ scrubEmojis,
121
+ sourceValue,
122
+ util
123
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@openfn/language-whatsapp",
3
+ "version": "1.0.0",
4
+ "description": "OpenFn whatsapp adaptor",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/index.js",
9
+ "types": "./types/index.d.ts",
10
+ "require": "./dist/index.cjs"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "author": "Open Function Group",
15
+ "license": "LGPLv3",
16
+ "files": [
17
+ "dist/",
18
+ "types/",
19
+ "ast.json",
20
+ "configuration-schema.json"
21
+ ],
22
+ "dependencies": {
23
+ "@openfn/language-common": "2.5.0"
24
+ },
25
+ "devDependencies": {
26
+ "assertion-error": "2.0.0",
27
+ "chai": "4.3.6",
28
+ "deep-eql": "4.1.1",
29
+ "mocha": "^10.7.3",
30
+ "rimraf": "3.0.2",
31
+ "undici": "^5.22.1"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/openfn/adaptors.git"
36
+ },
37
+ "types": "types/index.d.ts",
38
+ "main": "dist/index.cjs",
39
+ "scripts": {
40
+ "build": "pnpm clean && build-adaptor whatsapp",
41
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
42
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
43
+ "clean": "rimraf dist types docs",
44
+ "pack": "pnpm pack --pack-destination ../../dist",
45
+ "lint": "eslint src"
46
+ }
47
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * State object
3
+ * @typedef {Object} HttpState
4
+ * @property data - the parsed response body
5
+ * @property response - the response from the HTTP server, including headers, statusCode, body, etc
6
+ * @property references - an array of all previous data objects used in the Job
7
+ **/
8
+ /**
9
+ * Options provided to the HTTP request
10
+ * @typedef {Object} RequestOptions
11
+ * @public
12
+ * @property {object|string} body - body data to append to the request. JSON will be converted to a string (but a content-type header will not be attached to the request).
13
+ * @property {object} errors - Map of errorCodes -> error messages, ie, `{ 404: 'Resource not found;' }`. Pass `false` to suppress errors for this code.
14
+ * @property {object} form - Pass a JSON object to be serialised into a multipart HTML form (as FormData) in the body.
15
+ * @property {object} query - An object of query parameters to be encoded into the URL.
16
+ * @property {object} headers - An object of headers to append to the request.
17
+ * @property {string} parseAs - Parse the response body as json, text or stream. By default will use the response headers.
18
+ * @property {number} timeout - Request timeout in ms. Default: 300 seconds.
19
+ * @property {object} tls - TLS/SSL authentication options. See https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions
20
+ */
21
+ /**
22
+ * Make a HTTP request to the WhatsApp base URL, guided by the configuration
23
+ * @example
24
+ * <caption>Send a whatsapp message</caption>
25
+ * request(
26
+ * 'POST',
27
+ * 'messages',
28
+ * {
29
+ * to: '254712345678',
30
+ * body: 'Hello, world!',
31
+ * type: 'template',
32
+ * template: {
33
+ * name: 'hello_world',
34
+ * language: { code: 'en_US' }
35
+ * },
36
+ * messaging_product: 'whatsapp'
37
+ * });
38
+ * @function
39
+ * @public
40
+ * @param {string} method - HTTP method to use
41
+ * @param {string} path - Path to resource
42
+ * @param {object} body - Object which will be attached to the request body
43
+ * @param {RequestOptions} options - Additional request options
44
+ * @returns {Operation}
45
+ * @state {HttpState}
46
+ */
47
+ export function request(method: string, path: string, body: object, options?: RequestOptions): Operation;
48
+ /**
49
+ * State object
50
+ */
51
+ export type HttpState = {
52
+ /**
53
+ * - the parsed response body
54
+ */
55
+ data: any;
56
+ /**
57
+ * - the response from the HTTP server, including headers, statusCode, body, etc
58
+ */
59
+ response: any;
60
+ /**
61
+ * - an array of all previous data objects used in the Job
62
+ */
63
+ references: any;
64
+ };
65
+ /**
66
+ * Options provided to the HTTP request
67
+ */
68
+ export type RequestOptions = any;
69
+ export { combine, cursor, dataPath, dataValue, dateFns, each, field, fields, fn, fnIf, group, lastReferenceValue, merge, scrubEmojis, sourceValue, util } from "@openfn/language-common";
@@ -0,0 +1,2 @@
1
+ export function prepareNextState(state: any, response: any): any;
2
+ export function request(configuration: {}, method: any, path: any, options: any): any;
@@ -0,0 +1,3 @@
1
+ export default Adaptor;
2
+ export * from "./Adaptor";
3
+ import * as Adaptor from "./Adaptor";