@openfn/language-wigal-sms 0.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/dist/index.cjs ADDED
@@ -0,0 +1,232 @@
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
+ cursor: () => import_language_common3.cursor,
29
+ dataPath: () => import_language_common3.dataPath,
30
+ dataValue: () => import_language_common3.dataValue,
31
+ dateFns: () => import_language_common3.dateFns,
32
+ default: () => src_default,
33
+ each: () => import_language_common3.each,
34
+ execute: () => execute,
35
+ field: () => import_language_common3.field,
36
+ fields: () => import_language_common3.fields,
37
+ fn: () => import_language_common3.fn,
38
+ fnIf: () => import_language_common3.fnIf,
39
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
40
+ merge: () => import_language_common3.merge,
41
+ sendSms: () => sendSms,
42
+ sourceValue: () => import_language_common3.sourceValue
43
+ });
44
+ module.exports = __toCommonJS(src_exports);
45
+
46
+ // src/Adaptor.js
47
+ var Adaptor_exports = {};
48
+ __export(Adaptor_exports, {
49
+ cursor: () => import_language_common3.cursor,
50
+ dataPath: () => import_language_common3.dataPath,
51
+ dataValue: () => import_language_common3.dataValue,
52
+ dateFns: () => import_language_common3.dateFns,
53
+ each: () => import_language_common3.each,
54
+ execute: () => execute,
55
+ field: () => import_language_common3.field,
56
+ fields: () => import_language_common3.fields,
57
+ fn: () => import_language_common3.fn,
58
+ fnIf: () => import_language_common3.fnIf,
59
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
60
+ merge: () => import_language_common3.merge,
61
+ sendSms: () => sendSms,
62
+ sourceValue: () => import_language_common3.sourceValue
63
+ });
64
+ var import_language_common2 = require("@openfn/language-common");
65
+
66
+ // src/util.js
67
+ var import_undici = __toESM(require("undici"), 1);
68
+ var import_node_path = __toESM(require("path"), 1);
69
+ var import_util = require("@openfn/language-common/util");
70
+ var import_language_common = require("@openfn/language-common");
71
+ var client;
72
+ var getClient = (baseUrl) => {
73
+ if (baseUrl) {
74
+ const url = new URL(baseUrl);
75
+ client = new import_undici.default.Client(url.origin);
76
+ }
77
+ return client;
78
+ };
79
+ var setMockClient = (mockClient) => {
80
+ client = mockClient;
81
+ };
82
+ var prepareNextState = async (state, response) => {
83
+ const { body, ...responseWithoutBody } = response;
84
+ const result = await body.json();
85
+ if (!state.references) {
86
+ state.references = [];
87
+ }
88
+ const nextState = {
89
+ ...(0, import_language_common.composeNextState)(state, result),
90
+ response: responseWithoutBody
91
+ };
92
+ return nextState;
93
+ };
94
+ var request = (path, options) => {
95
+ return async (state) => {
96
+ const { baseUrl, apiKey, username } = state.configuration;
97
+ getClient(baseUrl);
98
+ const basePath = baseUrl ? new URL(baseUrl).pathname : "/";
99
+ const [resolvedPath, resolvedoptions] = (0, import_util.expandReferences)(
100
+ state,
101
+ path,
102
+ options
103
+ );
104
+ const {
105
+ data,
106
+ headers = {
107
+ "content-type": "application/json",
108
+ "API-KEY": apiKey,
109
+ USERNAME: username
110
+ },
111
+ method = "POST",
112
+ query,
113
+ ...otherOptions
114
+ } = resolvedoptions;
115
+ const safePath = resolvedPath ? import_node_path.default.join(basePath, resolvedPath) : basePath;
116
+ const args = {
117
+ path: safePath,
118
+ data,
119
+ headers: {
120
+ ...headers
121
+ },
122
+ method,
123
+ query,
124
+ ...otherOptions
125
+ };
126
+ if (data) {
127
+ args.body = JSON.stringify(data);
128
+ }
129
+ const response = await client.request(args);
130
+ if (response.statusCode >= 400) {
131
+ await handleError(response, path);
132
+ }
133
+ (0, import_util.logResponse)(response);
134
+ return prepareNextState(state, response);
135
+ };
136
+ };
137
+ var handleError = async (response, path) => {
138
+ const error = await response.body.json();
139
+ if (response.statusCode === 404) {
140
+ (0, import_util.throwError)("NOT_FOUND", {
141
+ description: "The server returned an error, see details message",
142
+ error: error.message,
143
+ fix: `Kindly check the path: ${path} well and try again.`
144
+ });
145
+ }
146
+ if (response.statusCode === 403) {
147
+ (0, import_util.throwError)("FORBIDDEN", {
148
+ description: "The server returned an error, see details message",
149
+ error: error.message,
150
+ fix: `Make sure the API-Key,Username and senderid are correct and exists in the header of the request`
151
+ });
152
+ }
153
+ };
154
+
155
+ // src/mock.js
156
+ var import_undici2 = require("undici");
157
+ var wigalSmsResponse = {
158
+ status: "ACCEPTED",
159
+ message: "Message Accepted For Processing"
160
+ };
161
+ function createServer(url = "https://frogapi.wigal.com.gh") {
162
+ const agent = new import_undici2.MockAgent();
163
+ const mockPool = agent.get(url);
164
+ const sendSms2 = (req) => {
165
+ return {
166
+ statusCode: 200,
167
+ responseOptions: {
168
+ headers: { "Content-Type": "application/json" }
169
+ },
170
+ data: JSON.stringify(wigalSmsResponse)
171
+ };
172
+ };
173
+ mockPool.intercept({ method: "post", path: "/api/v3/sms/send" }).reply(sendSms2).persist();
174
+ return {
175
+ agent,
176
+ request: ({ method, path, data, ...rest }) => {
177
+ const opts = {
178
+ method,
179
+ path,
180
+ origin: url,
181
+ ...rest
182
+ };
183
+ if (data) {
184
+ opts.body = JSON.stringify(data);
185
+ }
186
+ return mockPool.request(opts);
187
+ }
188
+ };
189
+ }
190
+
191
+ // src/Adaptor.js
192
+ var import_language_common3 = require("@openfn/language-common");
193
+ function execute(...operations) {
194
+ const initialState = {
195
+ references: [],
196
+ data: null,
197
+ configuration: {}
198
+ };
199
+ return (state) => {
200
+ if (!state.configuration.baseUrl) {
201
+ const client2 = createServer();
202
+ setMockClient(client2);
203
+ }
204
+ return (0, import_language_common2.execute)(...operations)({ ...initialState, ...state });
205
+ };
206
+ }
207
+ function sendSms(data) {
208
+ return request("/api/v3/sms/send", {
209
+ method: "POST",
210
+ data
211
+ });
212
+ }
213
+
214
+ // src/index.js
215
+ var src_default = Adaptor_exports;
216
+ // Annotate the CommonJS export names for ESM import in node:
217
+ 0 && (module.exports = {
218
+ cursor,
219
+ dataPath,
220
+ dataValue,
221
+ dateFns,
222
+ each,
223
+ execute,
224
+ field,
225
+ fields,
226
+ fn,
227
+ fnIf,
228
+ lastReferenceValue,
229
+ merge,
230
+ sendSms,
231
+ sourceValue
232
+ });
package/dist/index.js ADDED
@@ -0,0 +1,210 @@
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
+ cursor: () => cursor,
11
+ dataPath: () => dataPath,
12
+ dataValue: () => dataValue,
13
+ dateFns: () => dateFns,
14
+ each: () => each,
15
+ execute: () => execute,
16
+ field: () => field,
17
+ fields: () => fields,
18
+ fn: () => fn,
19
+ fnIf: () => fnIf,
20
+ lastReferenceValue: () => lastReferenceValue,
21
+ merge: () => merge,
22
+ sendSms: () => sendSms,
23
+ sourceValue: () => sourceValue
24
+ });
25
+ import { execute as commonExecute } from "@openfn/language-common";
26
+
27
+ // src/util.js
28
+ import undici from "undici";
29
+ import nodepath from "path";
30
+ import {
31
+ throwError,
32
+ logResponse,
33
+ expandReferences
34
+ } from "@openfn/language-common/util";
35
+ import { composeNextState } from "@openfn/language-common";
36
+ var client;
37
+ var getClient = (baseUrl) => {
38
+ if (baseUrl) {
39
+ const url = new URL(baseUrl);
40
+ client = new undici.Client(url.origin);
41
+ }
42
+ return client;
43
+ };
44
+ var setMockClient = (mockClient) => {
45
+ client = mockClient;
46
+ };
47
+ var prepareNextState = async (state, response) => {
48
+ const { body, ...responseWithoutBody } = response;
49
+ const result = await body.json();
50
+ if (!state.references) {
51
+ state.references = [];
52
+ }
53
+ const nextState = {
54
+ ...composeNextState(state, result),
55
+ response: responseWithoutBody
56
+ };
57
+ return nextState;
58
+ };
59
+ var request = (path, options) => {
60
+ return async (state) => {
61
+ const { baseUrl, apiKey, username } = state.configuration;
62
+ getClient(baseUrl);
63
+ const basePath = baseUrl ? new URL(baseUrl).pathname : "/";
64
+ const [resolvedPath, resolvedoptions] = expandReferences(
65
+ state,
66
+ path,
67
+ options
68
+ );
69
+ const {
70
+ data,
71
+ headers = {
72
+ "content-type": "application/json",
73
+ "API-KEY": apiKey,
74
+ USERNAME: username
75
+ },
76
+ method = "POST",
77
+ query,
78
+ ...otherOptions
79
+ } = resolvedoptions;
80
+ const safePath = resolvedPath ? nodepath.join(basePath, resolvedPath) : basePath;
81
+ const args = {
82
+ path: safePath,
83
+ data,
84
+ headers: {
85
+ ...headers
86
+ },
87
+ method,
88
+ query,
89
+ ...otherOptions
90
+ };
91
+ if (data) {
92
+ args.body = JSON.stringify(data);
93
+ }
94
+ const response = await client.request(args);
95
+ if (response.statusCode >= 400) {
96
+ await handleError(response, path);
97
+ }
98
+ logResponse(response);
99
+ return prepareNextState(state, response);
100
+ };
101
+ };
102
+ var handleError = async (response, path) => {
103
+ const error = await response.body.json();
104
+ if (response.statusCode === 404) {
105
+ throwError("NOT_FOUND", {
106
+ description: "The server returned an error, see details message",
107
+ error: error.message,
108
+ fix: `Kindly check the path: ${path} well and try again.`
109
+ });
110
+ }
111
+ if (response.statusCode === 403) {
112
+ throwError("FORBIDDEN", {
113
+ description: "The server returned an error, see details message",
114
+ error: error.message,
115
+ fix: `Make sure the API-Key,Username and senderid are correct and exists in the header of the request`
116
+ });
117
+ }
118
+ };
119
+
120
+ // src/mock.js
121
+ import { MockAgent } from "undici";
122
+ var wigalSmsResponse = {
123
+ status: "ACCEPTED",
124
+ message: "Message Accepted For Processing"
125
+ };
126
+ function createServer(url = "https://frogapi.wigal.com.gh") {
127
+ const agent = new MockAgent();
128
+ const mockPool = agent.get(url);
129
+ const sendSms2 = (req) => {
130
+ return {
131
+ statusCode: 200,
132
+ responseOptions: {
133
+ headers: { "Content-Type": "application/json" }
134
+ },
135
+ data: JSON.stringify(wigalSmsResponse)
136
+ };
137
+ };
138
+ mockPool.intercept({ method: "post", path: "/api/v3/sms/send" }).reply(sendSms2).persist();
139
+ return {
140
+ agent,
141
+ request: ({ method, path, data, ...rest }) => {
142
+ const opts = {
143
+ method,
144
+ path,
145
+ origin: url,
146
+ ...rest
147
+ };
148
+ if (data) {
149
+ opts.body = JSON.stringify(data);
150
+ }
151
+ return mockPool.request(opts);
152
+ }
153
+ };
154
+ }
155
+
156
+ // src/Adaptor.js
157
+ import {
158
+ fn,
159
+ fnIf,
160
+ each,
161
+ merge,
162
+ field,
163
+ fields,
164
+ cursor,
165
+ dateFns,
166
+ dataPath,
167
+ dataValue,
168
+ sourceValue,
169
+ lastReferenceValue
170
+ } from "@openfn/language-common";
171
+ function execute(...operations) {
172
+ const initialState = {
173
+ references: [],
174
+ data: null,
175
+ configuration: {}
176
+ };
177
+ return (state) => {
178
+ if (!state.configuration.baseUrl) {
179
+ const client2 = createServer();
180
+ setMockClient(client2);
181
+ }
182
+ return commonExecute(...operations)({ ...initialState, ...state });
183
+ };
184
+ }
185
+ function sendSms(data) {
186
+ return request("/api/v3/sms/send", {
187
+ method: "POST",
188
+ data
189
+ });
190
+ }
191
+
192
+ // src/index.js
193
+ var src_default = Adaptor_exports;
194
+ export {
195
+ cursor,
196
+ dataPath,
197
+ dataValue,
198
+ dateFns,
199
+ src_default as default,
200
+ each,
201
+ execute,
202
+ field,
203
+ fields,
204
+ fn,
205
+ fnIf,
206
+ lastReferenceValue,
207
+ merge,
208
+ sendSms,
209
+ sourceValue
210
+ };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@openfn/language-wigal-sms",
3
+ "version": "0.1.0",
4
+ "description": "OpenFn wigal-sms 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
+ "undici": "^5.28.4",
24
+ "@openfn/language-common": "2.3.0"
25
+ },
26
+ "devDependencies": {
27
+ "assertion-error": "2.0.0",
28
+ "chai": "4.3.6",
29
+ "deep-eql": "4.1.1",
30
+ "esno": "^0.16.3",
31
+ "mocha": "^10.7.3",
32
+ "rimraf": "3.0.2"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/openfn/adaptors.git"
37
+ },
38
+ "types": "types/index.d.ts",
39
+ "main": "dist/index.cjs",
40
+ "scripts": {
41
+ "build": "pnpm clean && build-adaptor wigal-sms",
42
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
43
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
44
+ "clean": "rimraf dist types docs",
45
+ "pack": "pnpm pack --pack-destination ../../dist",
46
+ "lint": "eslint src"
47
+ }
48
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * SMS Request Object
3
+ * @typedef {Object} SMSRequestObject
4
+ * @public
5
+ * @property {string} senderid - The senderID used for sending message. Approved SenderIDs only
6
+ * @property {array} destinations - An array of objects containing the destination phone number and message to be sent
7
+ * @property {string} message - The message to be sent to the destinations
8
+ * @property {string} smstype - The type of message to be sent. Default is 'text'
9
+ */
10
+ /**
11
+ * State object
12
+ * @typedef {Object} SendSMSState
13
+ * @property data - the parsed response body. containt status and message response
14
+ * @property response - the response from the Wigal SMS server, including headers, statusCode etc
15
+ * @property references - an array of all previous data objects used in the Job
16
+ **/
17
+ /**
18
+ * Executes an operation.
19
+ * @function
20
+ * @private
21
+ * @param {Operation} operations - Operations
22
+ * @returns {State}
23
+ */
24
+ export function execute(...operations: Operation): State;
25
+ /**
26
+ * Send SMS using Wigal SMS Gateway API
27
+ *
28
+ * @example <caption>Send General SMS message</caption>
29
+ * sendSMS({
30
+ * senderid: "Stevkky",
31
+ * destinations: [{ destination: "0552825710" }],
32
+ * message: "This is a sample message for SMS sending via Wigal FROG API.",
33
+ * smstype: "text",
34
+ * });
35
+ * @example <caption>Send Personalized SMS message</caption>
36
+ * sendSMS({
37
+ * senderid: "Stevkky",
38
+ * destinations: [
39
+ * {
40
+ * destination: "0542709440",
41
+ * message: "Hello Joe your order is ready",
42
+ * msgid: "MGS1010101",
43
+ * smstype: "text",
44
+ * },
45
+ * ],
46
+ * });
47
+ * @param {SMSRequestObject} data - SMS payload to push to Wigal. This includes the message, phone number, etc
48
+ * @returns {Operation}
49
+ * @state {SendSMSState}
50
+ */
51
+ export function sendSms(data: SMSRequestObject): Operation;
52
+ /**
53
+ * SMS Request Object
54
+ */
55
+ export type SMSRequestObject = any;
56
+ /**
57
+ * State object
58
+ */
59
+ export type SendSMSState = {
60
+ /**
61
+ * - the parsed response body. containt status and message response
62
+ */
63
+ data: any;
64
+ /**
65
+ * - the response from the Wigal SMS server, including headers, statusCode etc
66
+ */
67
+ response: any;
68
+ /**
69
+ * - an array of all previous data objects used in the Job
70
+ */
71
+ references: any;
72
+ };
73
+ export { fn, fnIf, each, merge, field, fields, cursor, dateFns, dataPath, dataValue, sourceValue, lastReferenceValue } from "@openfn/language-common";
@@ -0,0 +1,3 @@
1
+ export default Adaptor;
2
+ export * from "./Adaptor";
3
+ import * as Adaptor from "./Adaptor";
@@ -0,0 +1,10 @@
1
+ export function createServer(url?: string): {
2
+ agent: MockAgent<MockAgent.Options>;
3
+ request: ({ method, path, data, ...rest }: {
4
+ [x: string]: any;
5
+ method: any;
6
+ path: any;
7
+ data: any;
8
+ }) => Promise<import("undici/types/dispatcher").default.ResponseData>;
9
+ };
10
+ import { MockAgent } from "undici/types";
@@ -0,0 +1,4 @@
1
+ export function setMockClient(mockClient: any): void;
2
+ export function prepareNextState(state: any, response: any): Promise<any>;
3
+ export function request(path: any, options: any): (state: any) => Promise<any>;
4
+ export function handleError(response: any, path: any): Promise<void>;