@nmshd/connector-tui 0.0.1-beta.1 → 0.0.1-beta.2

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,50 @@
1
+ import { ConnectorClient } from "@nmshd/connector-sdk";
2
+ import chalk from "chalk";
3
+ import { readFile } from "fs/promises";
4
+ import prompts from "prompts";
5
+ import { ConnectorTUIBaseWithMixins } from "./mixins";
6
+ export class ConnectorTUI extends ConnectorTUIBaseWithMixins {
7
+ static async create(baseUrl, apiKey) {
8
+ const client = ConnectorClient.create({ baseUrl, apiKey });
9
+ const address = (await client.account.getIdentityInfo()).result.address;
10
+ return new ConnectorTUI(client, address);
11
+ }
12
+ async run() {
13
+ const connectorVersionInfo = await this.checkConnectorVersion();
14
+ if (!connectorVersionInfo)
15
+ return;
16
+ await this.showStartupMessage(connectorVersionInfo);
17
+ while (true) {
18
+ const result = await prompts({
19
+ type: "select",
20
+ name: "action",
21
+ message: "What do you want to do?",
22
+ choices: this.choices,
23
+ });
24
+ if (!result.action)
25
+ break;
26
+ try {
27
+ await result.action.apply(this);
28
+ }
29
+ catch (error) {
30
+ console.log(chalk.red("An Error occurred: "), error);
31
+ }
32
+ }
33
+ }
34
+ async checkConnectorVersion() {
35
+ const connectorVersionInfo = await this.connectorClient.monitoring.getVersion();
36
+ if (!connectorVersionInfo.version.startsWith("3.")) {
37
+ console.log(`This TUI is made for Enmeshed V2 connectors (starting with version 3.0.0 of the connector). Current version: ${connectorVersionInfo.version}`);
38
+ return;
39
+ }
40
+ return connectorVersionInfo;
41
+ }
42
+ async showStartupMessage(connectorVersionInfo) {
43
+ const jsonString = (await readFile(new URL("../package.json", import.meta.url))).toString();
44
+ const packageJson = JSON.parse(jsonString);
45
+ console.log(`Welcome to the ${chalk.blue("Enmeshed V2 TUI")}!`);
46
+ console.log(`TUI Version: ${chalk.yellow(packageJson.version)}`);
47
+ console.log(`Connector version: ${chalk.yellow(connectorVersionInfo.version)}`);
48
+ console.log("");
49
+ }
50
+ }
@@ -0,0 +1,83 @@
1
+ import { ConnectorRelationshipStatus, } from "@nmshd/connector-sdk";
2
+ import prompts from "prompts";
3
+ export class ConnectorTUIBase {
4
+ connectorClient;
5
+ connectorAddress;
6
+ choices = [];
7
+ constructor(connectorClient, connectorAddress) {
8
+ this.connectorClient = connectorClient;
9
+ this.connectorAddress = connectorAddress;
10
+ }
11
+ async selectRelationship(prompt, status = ConnectorRelationshipStatus.ACTIVE) {
12
+ const choices = await this.getRelationshipChoices(status, true);
13
+ if (!choices)
14
+ return;
15
+ const recipientsResult = await prompts({ message: prompt, type: "select", name: "recipient", choices });
16
+ return recipientsResult.recipient;
17
+ }
18
+ async selectRelationships(prompt, status = ConnectorRelationshipStatus.ACTIVE) {
19
+ const choices = await this.getRelationshipChoices(status, false);
20
+ if (!choices)
21
+ return;
22
+ const recipientsResult = await prompts({ message: prompt, type: "multiselect", name: "recipients", choices });
23
+ const recipients = recipientsResult.recipients;
24
+ if (!recipients || recipients.length === 0) {
25
+ console.log("No recipients selected");
26
+ return;
27
+ }
28
+ return recipients;
29
+ }
30
+ async getRelationshipChoices(status, returnRelationship) {
31
+ const relationships = (await this.connectorClient.relationships.getRelationships({ status })).result;
32
+ if (relationships.length === 0) {
33
+ console.log(`No relationships with status '${status}' found`);
34
+ return;
35
+ }
36
+ const choices = await Promise.all(relationships.map((relationship) => this.renderRelationship(relationship, returnRelationship)));
37
+ return choices;
38
+ }
39
+ async renderRelationship(relationship, returnRelationship) {
40
+ const value = returnRelationship ? relationship : relationship.peer;
41
+ const response = await this.connectorClient.relationships.getAttributesForRelationship(relationship.id);
42
+ const relationshipAttributes = response.result.filter((a) => a.content.owner === relationship.peer);
43
+ const displayName = relationshipAttributes.find((a) => a.content.value["@type"] === "DisplayName");
44
+ if (displayName) {
45
+ return { title: `${relationship.peer} (${displayName.content.value.value})`, value };
46
+ }
47
+ const surname = relationshipAttributes.find((a) => a.content.value["@type"] === "Surname");
48
+ const givenName = relationshipAttributes.find((a) => a.content.value["@type"] === "GivenName");
49
+ if (surname || givenName) {
50
+ const name = `${surname?.content.value.value || ""} ${givenName?.content.value.value || ""}`.trim();
51
+ return {
52
+ title: `${relationship.peer} (${name})`,
53
+ value,
54
+ };
55
+ }
56
+ return { title: relationship.peer, value };
57
+ }
58
+ async selectFile(prompt) {
59
+ const choices = await this.getFileChoices();
60
+ if (!choices)
61
+ return;
62
+ const result = await prompts({ message: prompt, type: "select", name: "file", choices: choices });
63
+ return result.file;
64
+ }
65
+ async selectFiles(prompt) {
66
+ const choices = await this.getFileChoices();
67
+ if (!choices)
68
+ return;
69
+ const result = await prompts({ message: prompt, type: "multiselect", name: "files", choices: choices });
70
+ return result.files;
71
+ }
72
+ async getFileChoices() {
73
+ const files = (await this.connectorClient.files.getFiles()).result;
74
+ if (files.length === 0) {
75
+ console.log("No files found");
76
+ return;
77
+ }
78
+ return files.map((f) => this.renderFile(f));
79
+ }
80
+ renderFile(file) {
81
+ return { title: file.title, value: file };
82
+ }
83
+ }
package/dist/index.js CHANGED
@@ -1,45 +1,12 @@
1
1
  import chalk from "chalk";
2
- import { readFile } from "fs/promises";
3
- import prompts from "prompts";
4
- import { acceptPendingRelationships, CONNECTOR_CLIENT, getAttributesOfContact, sendMail, sendRequestByMessage, shareRequestByTemplate, sync, uploadFile, } from "./functionality";
5
- const connectorVersionInfo = await CONNECTOR_CLIENT.monitoring.getVersion();
6
- if (!connectorVersionInfo.version.startsWith("3.")) {
7
- console.log(`This TUI is made for Enmeshed V2 connectors (starting with version 3.0.0 of the connector). Current version: ${connectorVersionInfo.version}`);
2
+ import dotenv from "dotenv";
3
+ import { ConnectorTUI } from "./ConnectorTUI";
4
+ dotenv.config();
5
+ if (typeof process.env.BASE_URL === "undefined" || typeof process.env.API_KEY === "undefined") {
6
+ console.log(chalk.red("Please provide a BASE_URL and API_KEY in your .env file or as environment variables."));
8
7
  process.exit(1);
9
8
  }
10
- const jsonString = (await readFile(new URL("../package.json", import.meta.url))).toString();
11
- const packageJson = JSON.parse(jsonString);
12
- console.log(`Welcome to the ${chalk.blue("Enmeshed V2 TUI")}!`);
13
- console.log(`TUI Version: ${chalk.yellow(packageJson.version)}`);
14
- console.log(`Connector version: ${chalk.yellow(connectorVersionInfo.version)}`);
15
- console.log("");
16
- while (true) {
17
- const result = await prompts({
18
- type: "select",
19
- name: "action",
20
- message: "What do you want to do?",
21
- choices: [
22
- { title: "Sync", value: sync },
23
- { title: "Accept Pending Relationships", value: acceptPendingRelationships },
24
- { title: "Upload File", value: uploadFile },
25
- { title: "Send Mail", value: sendMail },
26
- { title: "Send Request By Message", value: sendRequestByMessage },
27
- { title: "Share Request By Template", value: shareRequestByTemplate },
28
- { title: "Get Attributes of Contact", value: getAttributesOfContact },
29
- { title: "Exit", value: "exit" },
30
- ],
31
- });
32
- if (!result.action)
33
- break;
34
- if (typeof result.action === "function") {
35
- try {
36
- await result.action();
37
- }
38
- catch (error) {
39
- console.log(chalk.red("An Error occurred: "), error);
40
- }
41
- }
42
- else {
43
- break;
44
- }
45
- }
9
+ const baseUrl = process.env.BASE_URL;
10
+ const apiKey = process.env.API_KEY;
11
+ const tui = await ConnectorTUI.create(baseUrl, apiKey);
12
+ await tui.run();
@@ -0,0 +1,27 @@
1
+ import { ConnectorRelationshipStatus } from "@nmshd/connector-sdk";
2
+ import prompts from "prompts";
3
+ export function AddAcceptPendingRelationships(Base) {
4
+ return class Sync extends Base {
5
+ constructor(...args) {
6
+ super(...args);
7
+ this.choices.push({ title: "Accept Pending Relationships", value: this.acceptPendingRelationships });
8
+ }
9
+ async acceptPendingRelationships() {
10
+ const result = await prompts({
11
+ message: "Do you want to sync before accepting pending relationships?",
12
+ type: "confirm",
13
+ name: "sync",
14
+ });
15
+ if (result.sync) {
16
+ await this.connectorClient.account.sync();
17
+ }
18
+ const pendingRelationships = (await this.connectorClient.relationships.getRelationships({
19
+ status: ConnectorRelationshipStatus.PENDING,
20
+ })).result;
21
+ for (const relationship of pendingRelationships) {
22
+ console.log(`Accepting relationship ${relationship.id}`);
23
+ await this.connectorClient.relationships.acceptRelationshipChange(relationship.id, relationship.changes[0].id);
24
+ }
25
+ }
26
+ };
27
+ }
@@ -0,0 +1,11 @@
1
+ export function AddExit(Base) {
2
+ return class Sync extends Base {
3
+ constructor(...args) {
4
+ super(...args);
5
+ this.choices.push({ title: "Exit", value: this.exit });
6
+ }
7
+ async exit() {
8
+ process.exit(0);
9
+ }
10
+ };
11
+ }
@@ -0,0 +1,16 @@
1
+ export function AddGetAttributesOfContact(Base) {
2
+ return class Sync extends Base {
3
+ constructor(...args) {
4
+ super(...args);
5
+ this.choices.push({ title: "Get Attributes Of Contact", value: this.getAttributesOfContact });
6
+ }
7
+ async getAttributesOfContact() {
8
+ const relationship = await this.selectRelationship("From which contact do you want to get the attributes?");
9
+ if (!relationship)
10
+ return console.log("No recipient selected");
11
+ const attributeResult = await this.connectorClient.relationships.getAttributesForRelationship(relationship.id);
12
+ const attributes = attributeResult.result.filter((a) => a.content.owner === relationship.peer);
13
+ attributes.map((attribute) => console.log(`${attribute.id}: ${attribute.content.value["@type"]} = ${attribute.content.value.value}`));
14
+ }
15
+ };
16
+ }
@@ -0,0 +1,40 @@
1
+ import prompts from "prompts";
2
+ export function AddSendMail(Base) {
3
+ return class SendMail extends Base {
4
+ constructor(...args) {
5
+ super(...args);
6
+ this.choices.push({ title: "Send Mail", value: this.sendMail });
7
+ }
8
+ async sendMail() {
9
+ const recipients = await this.selectRelationships("Which recipient(s) do you want to send the message to?");
10
+ if (!recipients)
11
+ return;
12
+ const attachments = await this.selectFiles("Which file(s) do you want to send?");
13
+ const result = await prompts([
14
+ {
15
+ message: "Whats the subject?",
16
+ type: "text",
17
+ name: "subject",
18
+ initial: "A Subject",
19
+ },
20
+ {
21
+ message: "Whats the body?",
22
+ type: "text",
23
+ name: "body",
24
+ initial: "A Body",
25
+ },
26
+ ]);
27
+ const content = {
28
+ "@type": "Mail",
29
+ to: recipients,
30
+ subject: result.subject,
31
+ body: result.body,
32
+ };
33
+ const sendMessageResult = await this.connectorClient.messages.sendMessage({ recipients, content, attachments });
34
+ if (sendMessageResult.isError) {
35
+ return console.error("Error while sending message", sendMessageResult.error);
36
+ }
37
+ console.log(`Message sent to [${recipients.join(", ")}]`);
38
+ }
39
+ };
40
+ }
@@ -0,0 +1,204 @@
1
+ import prompts from "prompts";
2
+ export function AddSendRequestByMessage(Base) {
3
+ return class Sync extends Base {
4
+ constructor(...args) {
5
+ super(...args);
6
+ this.choices.push({ title: "Send Request By Message", value: this.sendRequestByMessage });
7
+ }
8
+ async sendRequestByMessage() {
9
+ const recipient = await this.selectRelationship("Which relationship do you want to send the request to?");
10
+ if (!recipient)
11
+ return console.log("No recipient selected");
12
+ const peer = recipient.peer;
13
+ const whatRequest = await prompts({
14
+ message: "What kind of request do you want to send?",
15
+ type: "select",
16
+ name: "requestMethod",
17
+ choices: [
18
+ {
19
+ title: "ReadRelationshipAttributeRequest",
20
+ value: this.createReadRelationshipAttributeRequest.bind(this),
21
+ },
22
+ {
23
+ title: "ReadIdentityAttributeRequest",
24
+ value: this.createReadIdentityAttributeRequest.bind(this),
25
+ },
26
+ {
27
+ title: "ProposeAttributeRequest",
28
+ value: this.createProposeAttributeRequest.bind(this),
29
+ },
30
+ {
31
+ title: "CreateRelationshipAttributeRequest",
32
+ value: this.createCreateRelationshipAttributeRequest.bind(this),
33
+ },
34
+ ],
35
+ });
36
+ if (!whatRequest.requestMethod)
37
+ return console.log("No request method selected");
38
+ const response = await whatRequest.requestMethod(peer);
39
+ if (response.isError) {
40
+ return console.error("Error while creating LocalRequest", response.error);
41
+ }
42
+ const messageResult = await this.connectorClient.messages.sendMessage({
43
+ recipients: [peer],
44
+ content: response.result.content,
45
+ });
46
+ if (messageResult.isError) {
47
+ return console.error("Error while sending message", messageResult.error);
48
+ }
49
+ console.log(`Request sent to '${peer}'`);
50
+ }
51
+ async createReadRelationshipAttributeRequest(peer) {
52
+ const result = await prompts([
53
+ {
54
+ message: "Whats the attribute type you would like to query?",
55
+ type: "text",
56
+ name: "attributeType",
57
+ initial: "An attribute type",
58
+ },
59
+ {
60
+ message: "Whats the key of the attribute you would like to query?",
61
+ type: "text",
62
+ name: "key",
63
+ initial: "Key of RelationshipAttribute",
64
+ },
65
+ {
66
+ message: "Whats the third party address?",
67
+ type: "text",
68
+ name: "thirdParty",
69
+ initial: "Third party address",
70
+ },
71
+ ]);
72
+ return await this.connectorClient.outgoingRequests.createRequest({
73
+ peer,
74
+ content: {
75
+ items: [
76
+ {
77
+ "@type": "ReadAttributeRequestItem",
78
+ mustBeAccepted: true,
79
+ query: {
80
+ "@type": "RelationshipAttributeQuery",
81
+ owner: "",
82
+ valueType: result.attributeType,
83
+ attributeCreationHints: {
84
+ title: `A ${result.attributeType} attribute`,
85
+ confidentiality: "public",
86
+ },
87
+ key: result.key,
88
+ thirdParty: result.thirdParty ? result.thirdParty : undefined,
89
+ },
90
+ },
91
+ ],
92
+ },
93
+ });
94
+ }
95
+ async createReadIdentityAttributeRequest(peer) {
96
+ const result = await prompts([
97
+ {
98
+ message: "Whats the attribute type you would like to query?",
99
+ type: "text",
100
+ name: "attributeType",
101
+ initial: "An attribute type",
102
+ },
103
+ ]);
104
+ return await this.connectorClient.outgoingRequests.createRequest({
105
+ peer,
106
+ content: {
107
+ items: [
108
+ {
109
+ "@type": "ReadAttributeRequestItem",
110
+ mustBeAccepted: true,
111
+ query: {
112
+ "@type": "IdentityAttributeQuery",
113
+ valueType: result.attributeType,
114
+ },
115
+ },
116
+ ],
117
+ },
118
+ });
119
+ }
120
+ async createProposeAttributeRequest(peer) {
121
+ const result = await prompts([
122
+ {
123
+ message: "Whats the attribute type you would like to create?",
124
+ type: "text",
125
+ name: "attributeType",
126
+ initial: "An attribute type",
127
+ },
128
+ {
129
+ message: "Whats the value of your Attribute?",
130
+ type: "text",
131
+ name: "value",
132
+ initial: "Value",
133
+ },
134
+ ]);
135
+ return await this.connectorClient.outgoingRequests.createRequest({
136
+ peer,
137
+ content: {
138
+ items: [
139
+ {
140
+ "@type": "ProposeAttributeRequestItem",
141
+ mustBeAccepted: true,
142
+ query: {
143
+ "@type": "IdentityAttributeQuery",
144
+ valueType: result.attributeType,
145
+ },
146
+ attribute: {
147
+ "@type": "IdentityAttribute",
148
+ owner: "",
149
+ value: {
150
+ "@type": result.attributeType,
151
+ value: result.value,
152
+ },
153
+ },
154
+ },
155
+ ],
156
+ },
157
+ });
158
+ }
159
+ async createCreateRelationshipAttributeRequest(peer) {
160
+ const result = await prompts([
161
+ {
162
+ message: "Whats the title of the RelationshipAttribute you would like to create?",
163
+ type: "text",
164
+ name: "title",
165
+ initial: "Title of RelationshipAttribute",
166
+ },
167
+ {
168
+ message: "Whats the key of the RelationshipAttribute you would like to create?",
169
+ type: "text",
170
+ name: "key",
171
+ initial: "Key of RelationshipAttribute",
172
+ },
173
+ {
174
+ message: "Whats the value of your Attribute?",
175
+ type: "text",
176
+ name: "value",
177
+ initial: "Value",
178
+ },
179
+ ]);
180
+ return await this.connectorClient.outgoingRequests.createRequest({
181
+ peer,
182
+ content: {
183
+ items: [
184
+ {
185
+ "@type": "CreateRelationshipAttributeRequestItem",
186
+ mustBeAccepted: true,
187
+ attribute: {
188
+ "@type": "RelationshipAttribute",
189
+ owner: "",
190
+ key: result.key,
191
+ confidentiality: "public",
192
+ value: {
193
+ "@type": "ProprietaryString",
194
+ value: result.value,
195
+ title: result.title,
196
+ },
197
+ },
198
+ },
199
+ ],
200
+ },
201
+ });
202
+ }
203
+ };
204
+ }
@@ -0,0 +1,109 @@
1
+ import { DateTime } from "luxon";
2
+ import qrcode from "qrcode-terminal";
3
+ export function AddShareRequestByTemplate(Base) {
4
+ return class Sync extends Base {
5
+ constructor(...args) {
6
+ super(...args);
7
+ this.choices.push({ title: "Share Request By Template", value: this.shareRequestByTemplate });
8
+ }
9
+ async shareRequestByTemplate() {
10
+ const name = process.env.CONNECTOR_DISPLAY_NAME || "ConnectorV2 Demo";
11
+ const displayName = await this.getOrCreateConnectorDisplayName(name);
12
+ const request = {
13
+ items: [
14
+ {
15
+ "@type": "RequestItemGroup",
16
+ mustBeAccepted: true,
17
+ title: "Shared Attributes",
18
+ items: [
19
+ {
20
+ "@type": "ShareAttributeRequestItem",
21
+ mustBeAccepted: true,
22
+ attribute: { ...displayName.content, owner: "" },
23
+ sourceAttributeId: displayName.id,
24
+ },
25
+ ],
26
+ },
27
+ {
28
+ "@type": "RequestItemGroup",
29
+ mustBeAccepted: true,
30
+ title: "Requested Attributes",
31
+ items: [
32
+ {
33
+ "@type": "ReadAttributeRequestItem",
34
+ mustBeAccepted: true,
35
+ query: {
36
+ "@type": "IdentityAttributeQuery",
37
+ valueType: "Surname",
38
+ },
39
+ },
40
+ {
41
+ "@type": "ReadAttributeRequestItem",
42
+ mustBeAccepted: true,
43
+ query: {
44
+ "@type": "IdentityAttributeQuery",
45
+ valueType: "GivenName",
46
+ },
47
+ },
48
+ {
49
+ "@type": "ReadAttributeRequestItem",
50
+ mustBeAccepted: false,
51
+ query: {
52
+ "@type": "IdentityAttributeQuery",
53
+ valueType: "EMailAddress",
54
+ },
55
+ },
56
+ ],
57
+ },
58
+ ],
59
+ };
60
+ await this.createQRCodeForRelationshipTemplate(request, name);
61
+ }
62
+ async getOrCreateConnectorDisplayName(displayName) {
63
+ const response = await this.connectorClient.attributes.getValidAttributes({
64
+ content: {
65
+ owner: this.connectorAddress,
66
+ value: {
67
+ "@type": "DisplayName",
68
+ },
69
+ },
70
+ shareInfo: {
71
+ peer: "!",
72
+ },
73
+ });
74
+ if (response.result.length > 0) {
75
+ return response.result[0];
76
+ }
77
+ const createAttributeResponse = await this.connectorClient.attributes.createAttribute({
78
+ content: {
79
+ "@type": "IdentityAttribute",
80
+ owner: this.connectorAddress,
81
+ value: {
82
+ "@type": "DisplayName",
83
+ value: displayName,
84
+ },
85
+ },
86
+ });
87
+ return createAttributeResponse.result;
88
+ }
89
+ async createQRCodeForRelationshipTemplate(request, name) {
90
+ const templateBody = {
91
+ "@type": "RelationshipTemplateBody",
92
+ title: `Kontaktanfrage mit ${name}`,
93
+ metadata: {
94
+ webSessionId: "12345",
95
+ },
96
+ onNewRelationship: request,
97
+ };
98
+ const template = await this.connectorClient.relationshipTemplates.createOwnRelationshipTemplate({
99
+ content: templateBody,
100
+ expiresAt: DateTime.now().plus({ days: 2 }).toISO(),
101
+ });
102
+ const templateId = template.result.id;
103
+ const tokenResponse = await this.connectorClient.relationshipTemplates.createTokenForOwnRelationshipTemplate(templateId);
104
+ const url = `nmshd://qr#${tokenResponse.result.truncatedReference}`;
105
+ console.log(url);
106
+ qrcode.generate(url, { small: true });
107
+ }
108
+ };
109
+ }
@@ -0,0 +1,16 @@
1
+ export function AddSync(Base) {
2
+ return class Sync extends Base {
3
+ constructor(...args) {
4
+ super(...args);
5
+ this.choices.push({ title: "Sync", value: this.sync });
6
+ }
7
+ async sync() {
8
+ const syncRequest = await this.connectorClient.account.sync();
9
+ if (syncRequest.isError) {
10
+ console.error(syncRequest.error);
11
+ return;
12
+ }
13
+ console.log(syncRequest.result);
14
+ }
15
+ };
16
+ }
@@ -0,0 +1,50 @@
1
+ import fs from "fs";
2
+ import { DateTime } from "luxon";
3
+ import path from "path";
4
+ import prompts from "prompts";
5
+ import qrcode from "qrcode-terminal";
6
+ import url from "url";
7
+ export function AddUploadFile(Base) {
8
+ return class Sync extends Base {
9
+ assetFolder = path.resolve(url.fileURLToPath(new URL(".", import.meta.url)), "../../__assets__");
10
+ constructor(...args) {
11
+ super(...args);
12
+ this.choices.push({ title: "Upload File", value: this.uploadFile });
13
+ }
14
+ async uploadFile() {
15
+ const filenames = await fs.promises.readdir(this.assetFolder);
16
+ const result = await prompts([
17
+ {
18
+ message: "Which file do you want to upload?",
19
+ type: "select",
20
+ name: "filename",
21
+ choices: filenames.map((filename) => ({ title: filename, value: filename })),
22
+ },
23
+ {
24
+ message: "Whats the files title?",
25
+ type: "text",
26
+ name: "title",
27
+ initial: "aFile",
28
+ },
29
+ ]);
30
+ const title = result.title;
31
+ const expiresAt = DateTime.local().plus({ days: 1 }).toISO();
32
+ const filename = result.filename;
33
+ if (!title || !filename)
34
+ return console.log("No file / title selected");
35
+ const file = await fs.promises.readFile(path.resolve(this.assetFolder, filename));
36
+ const uploadedFile = await this.connectorClient.files.uploadOwnFile({ title, expiresAt, file, filename });
37
+ const render = await prompts({
38
+ name: "yesno",
39
+ message: "Do you want to render a QR Code for this file?",
40
+ type: "confirm",
41
+ initial: true,
42
+ });
43
+ if (render.yesno) {
44
+ const url = `nmshd://qr#${uploadedFile.result.truncatedReference}`;
45
+ console.log(url);
46
+ qrcode.generate(url, { small: true });
47
+ }
48
+ }
49
+ };
50
+ }
@@ -0,0 +1,29 @@
1
+ import { ConnectorTUIBase } from "../ConnectorTUIBase";
2
+ import { AddAcceptPendingRelationships } from "./AddAcceptPendingRelationships";
3
+ import { AddExit } from "./AddExit";
4
+ import { AddGetAttributesOfContact } from "./AddGetAttributesOfContact";
5
+ import { AddSendMail } from "./AddSendMail";
6
+ import { AddSendRequestByMessage } from "./AddSendRequestByMessage";
7
+ import { AddShareRequestByTemplate } from "./AddShareRequestByTemplate";
8
+ import { AddSync } from "./AddSync";
9
+ import { AddUploadFile } from "./AddUploadFile";
10
+ export class ConnectorTUIBaseWithMixins
11
+ //
12
+ extends AddExit(
13
+ //
14
+ AddAcceptPendingRelationships(
15
+ //
16
+ AddGetAttributesOfContact(
17
+ //
18
+ AddSendMail(
19
+ //
20
+ AddSendRequestByMessage(
21
+ //
22
+ AddShareRequestByTemplate(
23
+ //
24
+ AddSync(
25
+ //
26
+ AddUploadFile(
27
+ //
28
+ ConnectorTUIBase)))))))) {
29
+ }
@@ -0,0 +1,9 @@
1
+ export * from "./AddAcceptPendingRelationships";
2
+ export * from "./AddExit";
3
+ export * from "./AddGetAttributesOfContact";
4
+ export * from "./AddSendMail";
5
+ export * from "./AddSendRequestByMessage";
6
+ export * from "./AddShareRequestByTemplate";
7
+ export * from "./AddSync";
8
+ export * from "./AddUploadFile";
9
+ export * from "./ConnectorTUIBaseWithMixins";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nmshd/connector-tui",
3
- "version": "0.0.1-beta.1",
3
+ "version": "0.0.1-beta.2",
4
4
  "repository": "github:nmshd/connector-tui",
5
5
  "license": "MIT",
6
6
  "author": "j&s-soft GmbH",
@@ -1,21 +0,0 @@
1
- import { ConnectorRelationshipStatus } from "@nmshd/connector-sdk";
2
- import prompts from "prompts";
3
- import { CONNECTOR_CLIENT } from "./globals";
4
- function acceptRelationship(r) {
5
- console.log(`Accepting relationship ${r.id}`);
6
- return CONNECTOR_CLIENT.relationships.acceptRelationshipChange(r.id, r.changes[0].id);
7
- }
8
- export async function acceptPendingRelationships() {
9
- const result = await prompts({
10
- message: "Do you want to sync before accepting pending relationships?",
11
- type: "confirm",
12
- name: "sync",
13
- });
14
- if (result.sync) {
15
- await CONNECTOR_CLIENT.account.sync();
16
- }
17
- const pendingRelationships = (await CONNECTOR_CLIENT.relationships.getRelationships({
18
- status: ConnectorRelationshipStatus.PENDING,
19
- })).result;
20
- await Promise.all(pendingRelationships.map(acceptRelationship));
21
- }
@@ -1,10 +0,0 @@
1
- import { CONNECTOR_CLIENT } from "./globals";
2
- import { selectRelationship } from "./selectors";
3
- export async function getAttributesOfContact() {
4
- const relationship = await selectRelationship("From which contact do you want to get the attributes?");
5
- if (!relationship)
6
- return console.log("No recipient selected");
7
- const attributeResult = await CONNECTOR_CLIENT.relationships.getAttributesForRelationship(relationship.id);
8
- const attributes = attributeResult.result.filter((a) => a.content.owner === relationship.peer);
9
- attributes.map((attribute) => console.log(`${attribute.id}: ${attribute.content.value["@type"]} = ${attribute.content.value.value}`));
10
- }
@@ -1,8 +0,0 @@
1
- import { ConnectorClient } from "@nmshd/connector-sdk";
2
- import dotenv from "dotenv";
3
- dotenv.config();
4
- export const CONNECTOR_CLIENT = ConnectorClient.create({
5
- baseUrl: process.env.BASE_URL,
6
- apiKey: process.env.API_KEY,
7
- });
8
- export const CONNECTOR_ADDRESS = (await CONNECTOR_CLIENT.account.getIdentityInfo()).result.address;
@@ -1,8 +0,0 @@
1
- export { acceptPendingRelationships } from "./acceptPendingRelationships";
2
- export { getAttributesOfContact } from "./getAttributesOfContact";
3
- export { CONNECTOR_CLIENT } from "./globals";
4
- export { sendMail } from "./sendMail";
5
- export { sendRequestByMessage } from "./sendRequestByMessage";
6
- export { shareRequestByTemplate } from "./shareRequestByTemplate";
7
- export { sync } from "./sync";
8
- export { uploadFile } from "./uploadFile";
@@ -1,27 +0,0 @@
1
- import prompts from "prompts";
2
- import { CONNECTOR_CLIENT } from "../globals";
3
- function renderFile(file) {
4
- return { title: file.title, value: file };
5
- }
6
- async function getChoices() {
7
- const files = (await CONNECTOR_CLIENT.files.getFiles()).result;
8
- if (files.length === 0) {
9
- console.log("No files found");
10
- return;
11
- }
12
- return files.map((f) => renderFile(f));
13
- }
14
- export async function selectFile(prompt) {
15
- const choices = await getChoices();
16
- if (!choices)
17
- return;
18
- const result = await prompts({ message: prompt, type: "select", name: "file", choices: choices });
19
- return result.file;
20
- }
21
- export async function selectFiles(prompt) {
22
- const choices = await getChoices();
23
- if (!choices)
24
- return;
25
- const result = await prompts({ message: prompt, type: "multiselect", name: "files", choices: choices });
26
- return result.files;
27
- }
@@ -1,2 +0,0 @@
1
- export * from "./files";
2
- export * from "./relationships";
@@ -1,50 +0,0 @@
1
- import { ConnectorRelationshipStatus } from "@nmshd/connector-sdk";
2
- import prompts from "prompts";
3
- import { CONNECTOR_CLIENT } from "../globals";
4
- async function renderRelationship(relationship, returnRelationship) {
5
- const value = returnRelationship ? relationship : relationship.peer;
6
- const response = await CONNECTOR_CLIENT.relationships.getAttributesForRelationship(relationship.id);
7
- const relationshipAttributes = response.result.filter((a) => a.content.owner === relationship.peer);
8
- const displayName = relationshipAttributes.find((a) => a.content.value["@type"] === "DisplayName");
9
- if (displayName) {
10
- return { title: `${relationship.peer} (${displayName.content.value.value})`, value };
11
- }
12
- const surname = relationshipAttributes.find((a) => a.content.value["@type"] === "Surname");
13
- const givenName = relationshipAttributes.find((a) => a.content.value["@type"] === "GivenName");
14
- if (surname || givenName) {
15
- const name = `${surname?.content.value.value || ""} ${givenName?.content.value.value || ""}`.trim();
16
- return {
17
- title: `${relationship.peer} (${name})`,
18
- value,
19
- };
20
- }
21
- return { title: relationship.peer, value };
22
- }
23
- async function getChoices(status, returnRelationship) {
24
- const relationships = (await CONNECTOR_CLIENT.relationships.getRelationships({ status })).result;
25
- if (relationships.length === 0) {
26
- console.log(`No relationships with status '${status}' found`);
27
- return;
28
- }
29
- const choices = await Promise.all(relationships.map((r) => renderRelationship(r, returnRelationship)));
30
- return choices;
31
- }
32
- export async function selectRelationship(prompt, status = ConnectorRelationshipStatus.ACTIVE) {
33
- const choices = await getChoices(status, true);
34
- if (!choices)
35
- return;
36
- const recipientsResult = await prompts({ message: prompt, type: "select", name: "recipient", choices });
37
- return recipientsResult.recipient;
38
- }
39
- export async function selectRelationships(prompt, status = ConnectorRelationshipStatus.ACTIVE) {
40
- const choices = await getChoices(status, false);
41
- if (!choices)
42
- return;
43
- const recipientsResult = await prompts({ message: prompt, type: "multiselect", name: "recipients", choices });
44
- const recipients = recipientsResult.recipients;
45
- if (!recipients || recipients.length === 0) {
46
- console.log("No recipients selected");
47
- return;
48
- }
49
- return recipients;
50
- }
@@ -1,34 +0,0 @@
1
- import prompts from "prompts";
2
- import { CONNECTOR_CLIENT } from "./globals";
3
- import { selectFiles, selectRelationships } from "./selectors";
4
- export async function sendMail() {
5
- const recipients = await selectRelationships("Which recipient(s) do you want to send the message to?");
6
- if (!recipients)
7
- return;
8
- const attachments = await selectFiles("Which file(s) do you want to send?");
9
- const result = await prompts([
10
- {
11
- message: "Whats the subject?",
12
- type: "text",
13
- name: "subject",
14
- initial: "A Subject",
15
- },
16
- {
17
- message: "Whats the body?",
18
- type: "text",
19
- name: "body",
20
- initial: "A Body",
21
- },
22
- ]);
23
- const content = {
24
- "@type": "Mail",
25
- to: recipients,
26
- subject: result.subject,
27
- body: result.body,
28
- };
29
- const sendMessageResult = await CONNECTOR_CLIENT.messages.sendMessage({ recipients, content, attachments });
30
- if (sendMessageResult.isError) {
31
- return console.error("Error while sending message", sendMessageResult.error);
32
- }
33
- console.log(`Message sent to [${recipients.join(", ")}]`);
34
- }
@@ -1,186 +0,0 @@
1
- import prompts from "prompts";
2
- import { CONNECTOR_CLIENT } from "./globals";
3
- import { selectRelationship } from "./selectors";
4
- export async function sendRequestByMessage() {
5
- const recipient = await selectRelationship("Which relationship do you want to send the request to?");
6
- if (!recipient)
7
- return console.log("No recipient selected");
8
- const peer = recipient.peer;
9
- const whatRequest = await prompts({
10
- message: "What kind of request do you want to send?",
11
- type: "select",
12
- name: "requestMethod",
13
- choices: [
14
- { title: "ReadRelationshipAttributeRequest", value: createReadRelationshipAttributeRequest },
15
- { title: "ReadIdentityAttributeRequest", value: createReadIdentityAttributeRequest },
16
- { title: "ProposeAttributeRequest", value: createProposeAttributeRequest },
17
- { title: "CreateRelationshipAttributeRequest", value: createCreateRelationshipAttributeRequest },
18
- ],
19
- });
20
- if (!whatRequest.requestMethod)
21
- return console.log("No request method selected");
22
- const response = await whatRequest.requestMethod(peer);
23
- if (response.isError) {
24
- return console.error("Error while creating LocalRequest", response.error);
25
- }
26
- const messageResult = await CONNECTOR_CLIENT.messages.sendMessage({
27
- recipients: [peer],
28
- content: response.result.content,
29
- });
30
- if (messageResult.isError) {
31
- return console.error("Error while sending message", messageResult.error);
32
- }
33
- console.log(`Request sent to '${peer}'`);
34
- }
35
- async function createReadRelationshipAttributeRequest(peer) {
36
- const result = await prompts([
37
- {
38
- message: "Whats the attribute type you would like to query?",
39
- type: "text",
40
- name: "attributeType",
41
- initial: "An attribute type",
42
- },
43
- {
44
- message: "Whats the key of the attribute you would like to query?",
45
- type: "text",
46
- name: "key",
47
- initial: "Key of RelationshipAttribute",
48
- },
49
- {
50
- message: "Whats the third party address?",
51
- type: "text",
52
- name: "thirdParty",
53
- initial: "Third party address",
54
- },
55
- ]);
56
- return await CONNECTOR_CLIENT.outgoingRequests.createRequest({
57
- peer,
58
- content: {
59
- items: [
60
- {
61
- "@type": "ReadAttributeRequestItem",
62
- mustBeAccepted: true,
63
- query: {
64
- "@type": "RelationshipAttributeQuery",
65
- owner: "",
66
- valueType: result.attributeType,
67
- attributeCreationHints: {
68
- title: `A ${result.attributeType} attribute`,
69
- confidentiality: "public",
70
- },
71
- key: result.key,
72
- thirdParty: result.thirdParty ? result.thirdParty : undefined,
73
- },
74
- },
75
- ],
76
- },
77
- });
78
- }
79
- async function createReadIdentityAttributeRequest(peer) {
80
- const result = await prompts([
81
- {
82
- message: "Whats the attribute type you would like to query?",
83
- type: "text",
84
- name: "attributeType",
85
- initial: "An attribute type",
86
- },
87
- ]);
88
- return await CONNECTOR_CLIENT.outgoingRequests.createRequest({
89
- peer,
90
- content: {
91
- items: [
92
- {
93
- "@type": "ReadAttributeRequestItem",
94
- mustBeAccepted: true,
95
- query: {
96
- "@type": "IdentityAttributeQuery",
97
- valueType: result.attributeType,
98
- },
99
- },
100
- ],
101
- },
102
- });
103
- }
104
- async function createProposeAttributeRequest(peer) {
105
- const result = await prompts([
106
- {
107
- message: "Whats the attribute type you would like to create?",
108
- type: "text",
109
- name: "attributeType",
110
- initial: "An attribute type",
111
- },
112
- {
113
- message: "Whats the value of your Attribute?",
114
- type: "text",
115
- name: "value",
116
- initial: "Value",
117
- },
118
- ]);
119
- return await CONNECTOR_CLIENT.outgoingRequests.createRequest({
120
- peer,
121
- content: {
122
- items: [
123
- {
124
- "@type": "ProposeAttributeRequestItem",
125
- mustBeAccepted: true,
126
- query: {
127
- "@type": "IdentityAttributeQuery",
128
- valueType: result.attributeType,
129
- },
130
- attribute: {
131
- "@type": "IdentityAttribute",
132
- owner: "",
133
- value: {
134
- "@type": result.attributeType,
135
- value: result.value,
136
- },
137
- },
138
- },
139
- ],
140
- },
141
- });
142
- }
143
- async function createCreateRelationshipAttributeRequest(peer) {
144
- const result = await prompts([
145
- {
146
- message: "Whats the title of the RelationshipAttribute you would like to create?",
147
- type: "text",
148
- name: "title",
149
- initial: "Title of RelationshipAttribute",
150
- },
151
- {
152
- message: "Whats the key of the RelationshipAttribute you would like to create?",
153
- type: "text",
154
- name: "key",
155
- initial: "Key of RelationshipAttribute",
156
- },
157
- {
158
- message: "Whats the value of your Attribute?",
159
- type: "text",
160
- name: "value",
161
- initial: "Value",
162
- },
163
- ]);
164
- return await CONNECTOR_CLIENT.outgoingRequests.createRequest({
165
- peer,
166
- content: {
167
- items: [
168
- {
169
- "@type": "CreateRelationshipAttributeRequestItem",
170
- mustBeAccepted: true,
171
- attribute: {
172
- "@type": "RelationshipAttribute",
173
- owner: "",
174
- key: result.key,
175
- confidentiality: "public",
176
- value: {
177
- "@type": "ProprietaryString",
178
- value: result.value,
179
- title: result.title,
180
- },
181
- },
182
- },
183
- ],
184
- },
185
- });
186
- }
@@ -1,102 +0,0 @@
1
- import { DateTime } from "luxon";
2
- import qrcode from "qrcode-terminal";
3
- import { CONNECTOR_ADDRESS, CONNECTOR_CLIENT } from "./globals";
4
- export async function shareRequestByTemplate() {
5
- const name = process.env.CONNECTOR_DISPLAY_NAME || "ConnectorV2 Demo";
6
- const displayName = await getOrCreateConnectorDisplayName(name);
7
- const request = {
8
- items: [
9
- {
10
- "@type": "RequestItemGroup",
11
- mustBeAccepted: true,
12
- title: "Shared Attributes",
13
- items: [
14
- {
15
- "@type": "ShareAttributeRequestItem",
16
- mustBeAccepted: true,
17
- attribute: { ...displayName.content, owner: "" },
18
- sourceAttributeId: displayName.id,
19
- },
20
- ],
21
- },
22
- {
23
- "@type": "RequestItemGroup",
24
- mustBeAccepted: true,
25
- title: "Requested Attributes",
26
- items: [
27
- {
28
- "@type": "ReadAttributeRequestItem",
29
- mustBeAccepted: true,
30
- query: {
31
- "@type": "IdentityAttributeQuery",
32
- valueType: "Surname",
33
- },
34
- },
35
- {
36
- "@type": "ReadAttributeRequestItem",
37
- mustBeAccepted: true,
38
- query: {
39
- "@type": "IdentityAttributeQuery",
40
- valueType: "GivenName",
41
- },
42
- },
43
- {
44
- "@type": "ReadAttributeRequestItem",
45
- mustBeAccepted: false,
46
- query: {
47
- "@type": "IdentityAttributeQuery",
48
- valueType: "EMailAddress",
49
- },
50
- },
51
- ],
52
- },
53
- ],
54
- };
55
- await createQRCodeForRelationshipTemplate(request, name);
56
- }
57
- async function getOrCreateConnectorDisplayName(displayName) {
58
- const response = await CONNECTOR_CLIENT.attributes.getValidAttributes({
59
- content: {
60
- owner: CONNECTOR_ADDRESS,
61
- value: {
62
- "@type": "DisplayName",
63
- },
64
- },
65
- shareInfo: {
66
- peer: "!",
67
- },
68
- });
69
- if (response.result.length > 0) {
70
- return response.result[0];
71
- }
72
- const createAttributeResponse = await CONNECTOR_CLIENT.attributes.createAttribute({
73
- content: {
74
- "@type": "IdentityAttribute",
75
- owner: CONNECTOR_ADDRESS,
76
- value: {
77
- "@type": "DisplayName",
78
- value: displayName,
79
- },
80
- },
81
- });
82
- return createAttributeResponse.result;
83
- }
84
- async function createQRCodeForRelationshipTemplate(request, name) {
85
- const templateBody = {
86
- "@type": "RelationshipTemplateBody",
87
- title: `Kontaktanfrage mit ${name}`,
88
- metadata: {
89
- webSessionId: "12345",
90
- },
91
- onNewRelationship: request,
92
- };
93
- const template = await CONNECTOR_CLIENT.relationshipTemplates.createOwnRelationshipTemplate({
94
- content: templateBody,
95
- expiresAt: DateTime.now().plus({ days: 2 }).toISO(),
96
- });
97
- const templateId = template.result.id;
98
- const tokenResponse = await CONNECTOR_CLIENT.relationshipTemplates.createTokenForOwnRelationshipTemplate(templateId);
99
- const url = `nmshd://qr#${tokenResponse.result.truncatedReference}`;
100
- console.log(url);
101
- qrcode.generate(url, { small: true });
102
- }
@@ -1,9 +0,0 @@
1
- import { CONNECTOR_CLIENT } from "./globals";
2
- export async function sync() {
3
- const syncRequest = await CONNECTOR_CLIENT.account.sync();
4
- if (syncRequest.isError) {
5
- console.error(syncRequest.error);
6
- return;
7
- }
8
- console.log(syncRequest.result);
9
- }
@@ -1,43 +0,0 @@
1
- import fs from "fs";
2
- import { DateTime } from "luxon";
3
- import path from "path";
4
- import qrcode from "qrcode-terminal";
5
- import url from "url";
6
- import { CONNECTOR_CLIENT } from "./globals";
7
- import prompts from "prompts";
8
- const assetFolder = path.resolve(url.fileURLToPath(new URL(".", import.meta.url)), "../../__assets__");
9
- export async function uploadFile() {
10
- const filenames = await fs.promises.readdir(assetFolder);
11
- const result = await prompts([
12
- {
13
- message: "Which file do you want to upload?",
14
- type: "select",
15
- name: "filename",
16
- choices: filenames.map((filename) => ({ title: filename, value: filename })),
17
- },
18
- {
19
- message: "Whats the files title?",
20
- type: "text",
21
- name: "title",
22
- initial: "aFile",
23
- },
24
- ]);
25
- const title = result.title;
26
- const expiresAt = DateTime.local().plus({ days: 1 }).toISO();
27
- const filename = result.filename;
28
- if (!title || !filename)
29
- return console.log("No file / title selected");
30
- const file = await fs.promises.readFile(path.resolve(assetFolder, filename));
31
- const uploadedFile = await CONNECTOR_CLIENT.files.uploadOwnFile({ title, expiresAt, file, filename });
32
- const render = await prompts({
33
- name: "yesno",
34
- message: "Do you want to render a QR Code for this file?",
35
- type: "confirm",
36
- initial: true,
37
- });
38
- if (render.yesno) {
39
- const url = `nmshd://qr#${uploadedFile.result.truncatedReference}`;
40
- console.log(url);
41
- qrcode.generate(url, { small: true });
42
- }
43
- }