@openfn/language-mojatax 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,39 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "properties": {
4
+ "baseUrl": {
5
+ "title": "Base URL",
6
+ "type": "string",
7
+ "description": "Mojatax base URL",
8
+ "format": "uri",
9
+ "minLength": 1,
10
+ "examples": [
11
+ "https://vfd-staging.mojatax.com"
12
+ ]
13
+ },
14
+ "clientId": {
15
+ "title": "Client Id",
16
+ "type": "string",
17
+ "description": "Your Mojatax client id",
18
+ "examples": [
19
+ "the-long-uuid-provided-by-mojatax"
20
+ ]
21
+ },
22
+ "password": {
23
+ "title": "Password",
24
+ "type": "string",
25
+ "description": "Your Mojatax password",
26
+ "writeOnly": true,
27
+ "examples": [
28
+ "@some(!)Str0ngp4ss0w0rd"
29
+ ]
30
+ }
31
+ },
32
+ "type": "object",
33
+ "additionalProperties": true,
34
+ "required": [
35
+ "baseUrl",
36
+ "password",
37
+ "clientId"
38
+ ]
39
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,191 @@
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
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
39
+ merge: () => import_language_common3.merge,
40
+ post: () => post,
41
+ request: () => request2,
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
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
59
+ merge: () => import_language_common3.merge,
60
+ post: () => post,
61
+ request: () => request2,
62
+ sourceValue: () => import_language_common3.sourceValue
63
+ });
64
+ var import_util2 = require("@openfn/language-common/util");
65
+
66
+ // src/Utils.js
67
+ var import_language_common = require("@openfn/language-common");
68
+ var import_util = require("@openfn/language-common/util");
69
+ var import_node_path = __toESM(require("path"), 1);
70
+ var authorize = (state) => {
71
+ const auth = state.configuration ?? {};
72
+ if (auth.access_token) {
73
+ return state;
74
+ }
75
+ const { clientId: client_id, password } = auth;
76
+ const headers = {};
77
+ if (client_id && password) {
78
+ Object.assign(headers, {
79
+ "Content-Type": "application/json"
80
+ });
81
+ const body = {
82
+ client_id,
83
+ password
84
+ };
85
+ const options = {
86
+ body,
87
+ headers,
88
+ method: "POST",
89
+ parseAs: "json",
90
+ baseUrl: auth.baseUrl
91
+ };
92
+ return (0, import_util.request)("POST", "/api/v1/auth/clientLogin", options).then(
93
+ (response) => {
94
+ return {
95
+ ...state,
96
+ configuration: {
97
+ ...state.configuration,
98
+ access_token: response.body.access_token
99
+ }
100
+ };
101
+ }
102
+ );
103
+ } else {
104
+ throw new Error(
105
+ "Invalid authorization credentials. Include clientId and password in state.configuration"
106
+ );
107
+ }
108
+ };
109
+ var prepareNextState = (state, response, callback = (s) => s) => {
110
+ const { body, ...responseWithoutBody } = response;
111
+ const nextState = {
112
+ ...(0, import_language_common.composeNextState)(state, response.body),
113
+ response: responseWithoutBody
114
+ };
115
+ return callback(nextState);
116
+ };
117
+ var request = (configuration, method, path, options) => {
118
+ const { baseUrl, access_token } = configuration ?? {};
119
+ const { data, params = {} } = options;
120
+ const headers = {
121
+ Authorization: `Bearer ${access_token}`,
122
+ "content-type": "application/json"
123
+ };
124
+ (0, import_util.assertRelativeUrl)(path);
125
+ const opts = {
126
+ parseAs: "json",
127
+ body: data,
128
+ query: params,
129
+ baseUrl,
130
+ headers
131
+ };
132
+ const safePath = import_node_path.default.join("/api/v1", path);
133
+ return (0, import_util.request)(method, safePath, opts).then(import_util.logResponse);
134
+ };
135
+
136
+ // src/Adaptor.js
137
+ var import_language_common2 = require("@openfn/language-common");
138
+ var import_language_common3 = require("@openfn/language-common");
139
+ function execute(...operations) {
140
+ const initialState = {
141
+ references: [],
142
+ data: null
143
+ };
144
+ return (state) => {
145
+ return (0, import_language_common2.execute)(
146
+ authorize,
147
+ ...operations
148
+ )({
149
+ ...initialState,
150
+ ...state
151
+ });
152
+ };
153
+ }
154
+ function post(path, data, options, callback) {
155
+ return request2("POST", `/client/${path}`, data, options, callback);
156
+ }
157
+ function request2(method, path, body, options = {}, callback = (s) => s) {
158
+ return async (state) => {
159
+ const [resolvedMethod, resolvedPath, resolvedData, resolvedoptions] = (0, import_util2.expandReferences)(state, method, path, body, options);
160
+ const response = await request(
161
+ state.configuration,
162
+ resolvedMethod,
163
+ resolvedPath,
164
+ {
165
+ data: resolvedData,
166
+ ...resolvedoptions
167
+ }
168
+ );
169
+ return prepareNextState(state, response, callback);
170
+ };
171
+ }
172
+
173
+ // src/index.js
174
+ var src_default = Adaptor_exports;
175
+ // Annotate the CommonJS export names for ESM import in node:
176
+ 0 && (module.exports = {
177
+ cursor,
178
+ dataPath,
179
+ dataValue,
180
+ dateFns,
181
+ each,
182
+ execute,
183
+ field,
184
+ fields,
185
+ fn,
186
+ lastReferenceValue,
187
+ merge,
188
+ post,
189
+ request,
190
+ sourceValue
191
+ });
package/dist/index.js ADDED
@@ -0,0 +1,168 @@
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
+ lastReferenceValue: () => lastReferenceValue,
20
+ merge: () => merge,
21
+ post: () => post,
22
+ request: () => request2,
23
+ sourceValue: () => sourceValue
24
+ });
25
+ import { expandReferences } from "@openfn/language-common/util";
26
+
27
+ // src/Utils.js
28
+ import { composeNextState } from "@openfn/language-common";
29
+ import {
30
+ request as commonRequest,
31
+ assertRelativeUrl,
32
+ logResponse
33
+ } from "@openfn/language-common/util";
34
+ import nodepath from "path";
35
+ var authorize = (state) => {
36
+ const auth = state.configuration ?? {};
37
+ if (auth.access_token) {
38
+ return state;
39
+ }
40
+ const { clientId: client_id, password } = auth;
41
+ const headers = {};
42
+ if (client_id && password) {
43
+ Object.assign(headers, {
44
+ "Content-Type": "application/json"
45
+ });
46
+ const body = {
47
+ client_id,
48
+ password
49
+ };
50
+ const options = {
51
+ body,
52
+ headers,
53
+ method: "POST",
54
+ parseAs: "json",
55
+ baseUrl: auth.baseUrl
56
+ };
57
+ return commonRequest("POST", "/api/v1/auth/clientLogin", options).then(
58
+ (response) => {
59
+ return {
60
+ ...state,
61
+ configuration: {
62
+ ...state.configuration,
63
+ access_token: response.body.access_token
64
+ }
65
+ };
66
+ }
67
+ );
68
+ } else {
69
+ throw new Error(
70
+ "Invalid authorization credentials. Include clientId and password in state.configuration"
71
+ );
72
+ }
73
+ };
74
+ var prepareNextState = (state, response, callback = (s) => s) => {
75
+ const { body, ...responseWithoutBody } = response;
76
+ const nextState = {
77
+ ...composeNextState(state, response.body),
78
+ response: responseWithoutBody
79
+ };
80
+ return callback(nextState);
81
+ };
82
+ var request = (configuration, method, path, options) => {
83
+ const { baseUrl, access_token } = configuration ?? {};
84
+ const { data, params = {} } = options;
85
+ const headers = {
86
+ Authorization: `Bearer ${access_token}`,
87
+ "content-type": "application/json"
88
+ };
89
+ assertRelativeUrl(path);
90
+ const opts = {
91
+ parseAs: "json",
92
+ body: data,
93
+ query: params,
94
+ baseUrl,
95
+ headers
96
+ };
97
+ const safePath = nodepath.join("/api/v1", path);
98
+ return commonRequest(method, safePath, opts).then(logResponse);
99
+ };
100
+
101
+ // src/Adaptor.js
102
+ import { execute as commonExecute } from "@openfn/language-common";
103
+ import {
104
+ dataPath,
105
+ dataValue,
106
+ dateFns,
107
+ cursor,
108
+ each,
109
+ field,
110
+ fields,
111
+ fn,
112
+ lastReferenceValue,
113
+ merge,
114
+ sourceValue
115
+ } from "@openfn/language-common";
116
+ function execute(...operations) {
117
+ const initialState = {
118
+ references: [],
119
+ data: null
120
+ };
121
+ return (state) => {
122
+ return commonExecute(
123
+ authorize,
124
+ ...operations
125
+ )({
126
+ ...initialState,
127
+ ...state
128
+ });
129
+ };
130
+ }
131
+ function post(path, data, options, callback) {
132
+ return request2("POST", `/client/${path}`, data, options, callback);
133
+ }
134
+ function request2(method, path, body, options = {}, callback = (s) => s) {
135
+ return async (state) => {
136
+ const [resolvedMethod, resolvedPath, resolvedData, resolvedoptions] = expandReferences(state, method, path, body, options);
137
+ const response = await request(
138
+ state.configuration,
139
+ resolvedMethod,
140
+ resolvedPath,
141
+ {
142
+ data: resolvedData,
143
+ ...resolvedoptions
144
+ }
145
+ );
146
+ return prepareNextState(state, response, callback);
147
+ };
148
+ }
149
+
150
+ // src/index.js
151
+ var src_default = Adaptor_exports;
152
+ export {
153
+ cursor,
154
+ dataPath,
155
+ dataValue,
156
+ dateFns,
157
+ src_default as default,
158
+ each,
159
+ execute,
160
+ field,
161
+ fields,
162
+ fn,
163
+ lastReferenceValue,
164
+ merge,
165
+ post,
166
+ request2 as request,
167
+ sourceValue
168
+ };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@openfn/language-mojatax",
3
+ "version": "1.0.0",
4
+ "description": "OpenFn mojatax 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.0.3"
24
+ },
25
+ "devDependencies": {
26
+ "assertion-error": "2.0.0",
27
+ "chai": "4.3.6",
28
+ "deep-eql": "4.1.1",
29
+ "esno": "^0.16.3",
30
+ "mocha": "^10.7.3",
31
+ "rimraf": "3.0.2",
32
+ "undici": "^5.22.1"
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 mojatax",
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,91 @@
1
+ /**
2
+ * State object
3
+ * @typedef {Object} MojataxHttpState
4
+ * @property data - The response body (as JSON)
5
+ * @property response - The HTTP response from the Mojatax server (excluding the body)
6
+ * @property references - An array of all previous data objects used in the Job
7
+ **/
8
+ /**
9
+ * Options provided to Mojatax HTTP request
10
+ * @typedef {Object} MojataxRequestOptions
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} query - An object of query parameters to be encoded into the URL.
15
+ * @property {object} headers - An object of headers to append to the request.
16
+ * @property {string} parseAs - Parse the response body as json, text or stream. By default will use the response headers.
17
+ * @property {number} timeout - Request timeout in ms. Default: 300 seconds.
18
+ * @property {object} tls - TLS/SSL authentication options. See https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions
19
+ */
20
+ /**
21
+ * Execute a sequence of operations.
22
+ * Wraps `language-common/execute` to make working with this API easier.
23
+ * @example
24
+ * execute(
25
+ * create('foo'),
26
+ * delete('bar')
27
+ * )(state)
28
+ * @private
29
+ * @param {Operations} operations - Operations to be performed.
30
+ * @returns {Operation}
31
+ */
32
+ export function execute(...operations: Operations): Operation;
33
+ /**
34
+ * Make a POST request to any Mojatax endpoint.
35
+ * @example <caption>Make a POST request to create an invoice</caption>
36
+ * post("CreateInvoice", {
37
+ * invoice_id: 'PID092',
38
+ * customerId: '102',
39
+ * items: [],
40
+ * });
41
+ * @function
42
+ * @public
43
+ * @param {string} path - Path to resource
44
+ * @param {object} data - Object which will be attached to the request body
45
+ * @param {MojataxRequestOptions} options - Optional request options
46
+ * @param {function} [callback] - Optional callback to handle the response
47
+ * @returns {Operation}
48
+ * @state {MojataxHttpState}
49
+ */
50
+ export function post(path: string, data: object, options: MojataxRequestOptions, callback?: Function): Operation;
51
+ /**
52
+ * Make a general HTTP request against the Mojatax server.
53
+ * @example <caption>Make a POST request to create an invoice</caption>
54
+ * request("POST", "/client/CreateInvoice", {
55
+ * invoice_id: 'PID092',
56
+ * customerId: '102',
57
+ * items: [],
58
+ * });
59
+ * @function
60
+ * @public
61
+ * @param {string} method - HTTP method to use
62
+ * @param {string} path - Path to resource
63
+ * @param {object} data - Object which will be attached to the POST body
64
+ * @param {MojataxRequestOptions} options - Optional request options
65
+ * @param {function} [callback] - Optional callback to handle the response
66
+ * @returns {Operation}
67
+ * @state {MojataxHttpState}
68
+ */
69
+ export function request(method: string, path: string, body: any, options?: MojataxRequestOptions, callback?: Function): Operation;
70
+ /**
71
+ * State object
72
+ */
73
+ export type MojataxHttpState = {
74
+ /**
75
+ * - The response body (as JSON)
76
+ */
77
+ data: any;
78
+ /**
79
+ * - The HTTP response from the Mojatax server (excluding the body)
80
+ */
81
+ response: any;
82
+ /**
83
+ * - An array of all previous data objects used in the Job
84
+ */
85
+ references: any;
86
+ };
87
+ /**
88
+ * Options provided to Mojatax HTTP request
89
+ */
90
+ export type MojataxRequestOptions = any;
91
+ export { dataPath, dataValue, dateFns, cursor, each, field, fields, fn, lastReferenceValue, merge, sourceValue } from "@openfn/language-common";
@@ -0,0 +1,3 @@
1
+ export function authorize(state: any): any;
2
+ export function prepareNextState(state: any, response: any, callback?: (s: any) => any): any;
3
+ export function request(configuration: any, 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";