Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daktela init #25

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/appmixer/daktela/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Daktela

## Generate module

```sh
appmixer init openapi ./daktela/openapi.yml ./daktela/
```

## Manual Updates

Apply the following manual updates since those are not covered by the OpenAPI generator.

### auth.js
Keep as is. Auth flow is `pwd`.

### `httpRequest()` methods
Keep retrieving from context.

```js
query.append('accessToken', context.auth.token);
```
59 changes: 59 additions & 0 deletions src/appmixer/daktela/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

module.exports = {

type: 'pwd',

definition: {

auth: {
username: {
type: 'text',
name: 'Username',
tooltip: 'Provide your username.'
},
password: {
type: 'password',
name: 'Password',
tooltip: 'Provide your password.'
},
instance: {
type: 'text',
name: 'Instance',
tooltip: 'Provide your instance. Example: https://yourcompany.daktela.com'
}
},

requestProfileInfo: async context => {

const { result } = await daktelaValidate(context);

return { name: result.user.title || result.user.alias };
},

accountNameFromProfileInfo: 'name',

validate: async context => {

const { result } = await daktelaValidate(context);

return { token: result.accessToken };
}
}
};

async function daktelaValidate(context) {

const { data } = await context.httpRequest({
url: `https://${context.instance || context.auth.instance}.daktela.com/api/v6/login.json`,
method: 'POST',
data: {
// When called from auth.js we have username and password in context.
// When called from other components we have username and password in context.auth.
username: context.username || context.auth.username,
password: context.password || context.auth.password
}
});

return data;
}
9 changes: 9 additions & 0 deletions src/appmixer/daktela/bundle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "appmixer.daktela",
"version": "1.0.0",
"changelog": {
"1.0.0": [
"Daktela"
]
}
}
91 changes: 91 additions & 0 deletions src/appmixer/daktela/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

module.exports = {

jsonata: require('jsonata'),

jsonPointer: require('json-pointer'),

jmespath: require('jmespath'),

FormData: require('form-data'),

getBaseUrl: function(context) {

let url = 'https://{instance}.daktela.com';
url = url.replaceAll('{instance}', context.auth?.instance || context.config?.instance || 'https://democz.daktela.com');
return url;
},

setProperty: function(obj, path, value) {

if (!obj || typeof obj !== 'object' || !path) {
throw new Error('Invalid input');
}

if (typeof value === 'undefined') return;

const pathArray = Array.isArray(path) ? path : path.split('.');
const pathLength = pathArray.length;

for (let i = 0; i < pathLength - 1; i++) {
const key = pathArray[i];
if (!obj.hasOwnProperty(key) || typeof obj[key] !== 'object') {
obj[key] = {};
}
obj = obj[key];
}

obj[pathArray[pathLength - 1]] = value;
},

setProperties: function(obj, mapping) {

Object.keys(mapping || {}).forEach(path => {
this.setProperty(obj, path, mapping[path]);
});
},

replaceRuntimeExpressions: async function(template, context, response, request) {

if (template === '$request.body') {
return request.data;
}

let result = typeof template === 'string' ? template : JSON.stringify(template);

result = result.replace(/{\$webhookUrl}/g, context.getWebhookUrl());
result = result.replace(/{\$baseUrl}/g, this.getBaseUrl(context));

result = result.replace(/{\$response.body#([^}]*)}/g, (match, pointer) => {
return this.jsonPointer.get(response.data, pointer);
});

result = result.replace(/{\$parameters\.([^}]*)}/g, (match, pointer) => {
return this.jsonPointer.get(context.properties, '/' + pointer);
});

result = result.replace(/{\$connection.profile\.([^}]*)}/g, (match, pointer) => {
return this.jsonPointer.get(context.auth.profileInfo, '/' + pointer);
});
result = result.replace(/{\$connection.profile#([^}]*)}/g, (match, pointer) => {
return this.jsonPointer.get(context.auth.profileInfo, pointer);
});

const responseTransformPromises = [];
const responseTransformRegex = /{\$response.transform#(.*(?<!\\))}/g;
result.replace(responseTransformRegex, (match, exp) => {
const expression = this.jsonata(exp);
responseTransformPromises.push(expression.evaluate(response));
});
const replacements = await Promise.all(responseTransformPromises);
result = result.replace(responseTransformRegex, () => replacements.shift());

result = result.replace(/{\$response.header\.([^}]*)}/g, (match, pointer) => {
return this.jsonPointer.get(response.headers, '/' + pointer);
});

return typeof template === 'string' ? result : JSON.parse(result);
}

};
Loading
Loading