@aloma.io/integration-sdk 3.6.6 → 3.7.1

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.
Files changed (32) hide show
  1. package/build/builder/index.d.mts +1 -1
  2. package/build/builder/index.mjs +12 -16
  3. package/build/builder/runtime-context.mjs +3 -2
  4. package/build/cli.mjs +15 -4
  5. package/build/internal/dispatcher/index.d.mts +11 -17
  6. package/build/internal/dispatcher/index.mjs +12 -1
  7. package/build/internal/fetcher/fetcher.d.mts +15 -0
  8. package/build/internal/fetcher/fetcher.mjs +97 -0
  9. package/build/internal/fetcher/index.d.mts +1 -0
  10. package/build/internal/fetcher/index.mjs +1 -0
  11. package/build/internal/fetcher/oauth-fetcher.d.mts +32 -0
  12. package/build/internal/fetcher/oauth-fetcher.mjs +114 -0
  13. package/build/internal/index.d.mts +8 -7
  14. package/build/internal/index.mjs +14 -241
  15. package/build/internal/util/index.d.mts +5 -0
  16. package/build/internal/util/index.mjs +45 -0
  17. package/build/internal/util/jwe/index.d.mts +5 -8
  18. package/build/internal/util/jwe/index.mjs +3 -0
  19. package/build/transform/index.d.mts +5 -0
  20. package/build/transform/index.mjs +79 -0
  21. package/package.json +1 -1
  22. package/src/builder/index.mts +13 -17
  23. package/src/builder/runtime-context.mts +4 -1
  24. package/src/cli.mts +19 -4
  25. package/src/internal/dispatcher/{index.mjs → index.mts} +14 -2
  26. package/src/internal/fetcher/fetcher.mts +126 -0
  27. package/src/internal/fetcher/oauth-fetcher.mts +147 -0
  28. package/src/internal/{index.mjs → index.mts} +18 -317
  29. package/src/internal/util/index.mts +57 -0
  30. package/src/internal/util/jwe/{index.mjs → index.mts} +7 -4
  31. /package/src/internal/util/jwe/{cli.mjs → cli.mts} +0 -0
  32. /package/src/{builder/transform → transform}/index.mts +0 -0
@@ -7,5 +7,5 @@ export declare class Builder {
7
7
  build(): Promise<RuntimeContext>;
8
8
  private checkIcon;
9
9
  private parsePackageJson;
10
- private discoverTypes;
10
+ private loadTypes;
11
11
  }
@@ -1,17 +1,13 @@
1
1
  import fs from "node:fs";
2
- import parseTypes from "./transform/index.mjs";
3
- import RuntimeContext from "./runtime-context.mjs";
4
- import { fileURLToPath } from "node:url";
5
2
  import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { notEmpty } from "../internal/util/index.mjs";
5
+ import RuntimeContext from "./runtime-context.mjs";
6
+ const offset = '/../../../../../';
6
7
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
- const notEmpty = (what, name) => {
8
- if (!what?.trim())
9
- throw new Error(`${name} cannot be empty`);
10
- return what;
11
- };
12
8
  export class Builder {
13
9
  data = {
14
- controller: "./build/controller/.controller-for-types.mts",
10
+ controller: "./build/.controller.json",
15
11
  };
16
12
  config(arg) {
17
13
  this.data.config = arg;
@@ -27,29 +23,29 @@ export class Builder {
27
23
  }
28
24
  async build() {
29
25
  await this.parsePackageJson();
30
- await this.discoverTypes();
26
+ await this.loadTypes();
31
27
  await this.checkIcon();
32
28
  // @ts-ignore
33
- const Controller = (await import(__dirname + "/../../../../../build/controller/index.mjs")).default;
29
+ const Controller = (await import(__dirname + offset + "build/controller/index.mjs")).default;
34
30
  return new RuntimeContext(new Controller(), this.data);
35
31
  }
36
32
  async checkIcon() {
37
33
  const data = this.data;
38
- const root = __dirname + "/../../../../../";
34
+ const root = __dirname + offset;
39
35
  data.icon = `${root}/logo.png`;
40
36
  }
41
37
  async parsePackageJson() {
42
38
  const data = this.data;
43
- const packageJson = JSON.parse(fs.readFileSync(__dirname + "/../../../../../package.json", {
39
+ const packageJson = JSON.parse(fs.readFileSync(__dirname + offset + "package.json", {
44
40
  encoding: "utf-8",
45
41
  }));
46
42
  notEmpty((data.id = packageJson.connectorId), "id");
47
43
  notEmpty((data.version = packageJson.version), "version");
48
44
  }
49
- async discoverTypes() {
45
+ async loadTypes() {
50
46
  notEmpty(this.data.controller, "controller");
51
- const content = fs.readFileSync(this.data.controller);
52
- const { text, methods } = await parseTypes(this.data.controller);
47
+ const content = fs.readFileSync(this.data.controller, { encoding: 'utf-8' });
48
+ const { text, methods } = JSON.parse(content);
53
49
  this.data.types = text;
54
50
  this.data.methods = methods;
55
51
  }
@@ -1,6 +1,6 @@
1
+ import fs from "node:fs";
1
2
  import { AbstractController } from "../controller/index.mjs";
2
3
  import { Connector } from "../internal/index.mjs";
3
- import fs from "node:fs";
4
4
  export default class RuntimeContext {
5
5
  controller;
6
6
  data;
@@ -10,8 +10,9 @@ export default class RuntimeContext {
10
10
  }
11
11
  async start() {
12
12
  const controller = this.controller;
13
- if (!(controller instanceof AbstractController))
13
+ if (!(controller instanceof AbstractController)) {
14
14
  throw new Error("the controller needs to extend AbstractController");
15
+ }
15
16
  const data = this.data;
16
17
  let icon;
17
18
  try {
package/build/cli.mjs CHANGED
@@ -1,11 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  import { Command } from "commander";
3
+ import ChildProcess from "node:child_process";
3
4
  import fs from "node:fs";
4
- import { fileURLToPath } from "node:url";
5
5
  import path from "node:path";
6
- import JWE from "./internal/util/jwe/index.mjs";
6
+ import { fileURLToPath } from "node:url";
7
7
  import util from "node:util";
8
- import ChildProcess from "node:child_process";
8
+ import { notEmpty } from "./internal/util/index.mjs";
9
+ import JWE from "./internal/util/jwe/index.mjs";
10
+ import parseTypes from "./transform/index.mjs";
9
11
  const exec = util.promisify(ChildProcess.exec);
10
12
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
11
13
  const files = [
@@ -87,8 +89,17 @@ program
87
89
  .command("build")
88
90
  .description("Build the current connector project")
89
91
  .action(async (str, options) => {
90
- const { stdout, stderr } = await exec(`rm -rf build; mkdir -p build/controller; cp ./src/controller/index.mts ./build/controller/.controller-for-types.mts;`);
92
+ const { stdout, stderr } = await exec(`rm -rf build; mkdir -p build`);
91
93
  if (stdout)
92
94
  console.log(stdout);
95
+ new Extractor().extract('./src/controller/index.mts', './build/.controller.json');
93
96
  });
97
+ class Extractor {
98
+ async extract(source, target) {
99
+ notEmpty(source, "source");
100
+ fs.readFileSync(source);
101
+ const { text, methods } = await parseTypes(source);
102
+ fs.writeFileSync(target, JSON.stringify({ text, methods }), { encoding: 'utf-8' });
103
+ }
104
+ }
94
105
  program.parse();
@@ -1,29 +1,21 @@
1
- export class Dispatcher {
2
- _config: {
3
- fields: {};
4
- };
1
+ export default class Dispatcher {
2
+ private _config;
3
+ private _main;
4
+ _oauth: any;
5
+ private _types;
6
+ private _resolvers;
7
+ constructor();
5
8
  main(what: any): this;
6
- _main: any;
7
9
  oauth(arg: any): this;
8
- _oauth: any;
9
10
  types(what: any): this;
10
- _types: any;
11
- config({ fields, oauth, description, summary }: {
12
- fields: any;
13
- oauth: any;
14
- description: any;
15
- summary: any;
16
- }): this;
11
+ config({ fields, oauth, description, summary }: any): this;
17
12
  resolvers(what: any): this;
18
- _resolvers: any;
19
13
  endpoint(what: any, notOptional: any): this;
20
14
  startOAuth(): void;
21
15
  finishOAuth(): void;
22
16
  build(): {
23
17
  introspect: () => any;
24
- configSchema: () => {
25
- fields: {};
26
- };
18
+ configSchema: () => any;
27
19
  execute: ({ query, variables }: {
28
20
  query: any;
29
21
  variables: any;
@@ -31,4 +23,6 @@ export class Dispatcher {
31
23
  processPacket: (packet: any) => Promise<any>;
32
24
  start: (transport: any) => Promise<void>;
33
25
  };
26
+ onConfig(arg0: any): void;
34
27
  }
28
+ export { Dispatcher };
@@ -1,4 +1,9 @@
1
- class Dispatcher {
1
+ export default class Dispatcher {
2
+ _config;
3
+ _main;
4
+ _oauth;
5
+ _types;
6
+ _resolvers;
2
7
  constructor() {
3
8
  this._config = { fields: {} };
4
9
  }
@@ -152,11 +157,14 @@ ${arg.configurableClientScope}
152
157
  const processPacket = async (packet) => {
153
158
  switch (packet.method()) {
154
159
  case "connector.introspect":
160
+ // @ts-ignore
155
161
  const intro = await introspect({});
156
162
  return { configSchema: local._config, introspect: intro };
157
163
  case "connector.start-oauth":
164
+ // @ts-ignore
158
165
  return await local.startOAuth(packet.args());
159
166
  case "connector.finish-oauth":
167
+ // @ts-ignore
160
168
  return await local.finishOAuth(packet.args());
161
169
  case "connector.query":
162
170
  const ret = await execute(packet.args());
@@ -178,5 +186,8 @@ ${arg.configurableClientScope}
178
186
  start,
179
187
  };
180
188
  }
189
+ onConfig(arg0) {
190
+ throw new Error("Method not implemented.");
191
+ }
181
192
  }
182
193
  export { Dispatcher };
@@ -0,0 +1,15 @@
1
+ export default class Fetcher {
2
+ retry: number;
3
+ baseUrl: any;
4
+ onResponse: any;
5
+ customize0: any;
6
+ constructor({ retry, baseUrl, onResponse, customize }: {
7
+ retry?: number | undefined;
8
+ baseUrl: any;
9
+ onResponse: any;
10
+ customize: any;
11
+ });
12
+ customize(options?: {}, args?: {}): Promise<void>;
13
+ onError(e: any, url: string, options: any, retries: number, args: any, rateLimit?: any): Promise<unknown>;
14
+ fetch(url: string, options: any, retries: number, args?: any): Promise<any>;
15
+ }
@@ -0,0 +1,97 @@
1
+ import { unwrap } from "../util/index.mjs";
2
+ export default class Fetcher {
3
+ retry;
4
+ baseUrl;
5
+ onResponse;
6
+ customize0;
7
+ constructor({ retry = 5, baseUrl, onResponse, customize }) {
8
+ this.retry = retry;
9
+ this.baseUrl = baseUrl;
10
+ this.onResponse = onResponse;
11
+ if (customize)
12
+ this.customize0 = customize;
13
+ }
14
+ async customize(options = {}, args = {}) {
15
+ if (this.customize0)
16
+ await this.customize0(options, args);
17
+ }
18
+ async onError(e, url, options, retries, args, rateLimit) {
19
+ var local = this;
20
+ return new Promise((resolve, reject) => {
21
+ setTimeout(async () => {
22
+ try {
23
+ resolve(await local.fetch(url, options, retries, args));
24
+ }
25
+ catch (e) {
26
+ reject(e);
27
+ }
28
+ }, rateLimit ? 10000 : 500);
29
+ });
30
+ }
31
+ async fetch(url, options = {}, retries, args = {}) {
32
+ var local = this, baseUrl = local.baseUrl;
33
+ if (retries == null)
34
+ retries = local.retry;
35
+ let theURL = !baseUrl
36
+ ? url
37
+ : `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(/\/\/+/gi, "/");
38
+ try {
39
+ options.url = url;
40
+ await local.customize(options, args);
41
+ url = options.url;
42
+ delete options.url;
43
+ theURL = !baseUrl
44
+ ? url
45
+ : `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(/\/\/+/gi, "/");
46
+ if (!options?.headers || !options?.headers?.Accept) {
47
+ options.headers = {
48
+ ...options.headers,
49
+ Accept: "application/json",
50
+ };
51
+ }
52
+ if (!options?.headers || !options?.headers?.["Content-type"]) {
53
+ options.headers = {
54
+ ...options.headers,
55
+ "Content-type": "application/json",
56
+ };
57
+ }
58
+ if (!(options?.method === "GET" || options?.method === "HEAD") &&
59
+ options?.body &&
60
+ !(typeof options.body === "string") &&
61
+ options?.headers?.["Content-type"] === "application/json") {
62
+ options.body = JSON.stringify(options.body);
63
+ }
64
+ const timeout = Math.min(options?.timeout || 30 * 60 * 1000, 30 * 60 * 1000);
65
+ const ret = await fetch(theURL, {
66
+ ...options,
67
+ signal: AbortSignal.timeout(timeout),
68
+ });
69
+ const status = await ret.status;
70
+ if (status > 399) {
71
+ const text = await ret.text();
72
+ const e = new Error(status + " " + text);
73
+ e.status = status;
74
+ throw e;
75
+ }
76
+ if (local.onResponse) {
77
+ await local.onResponse(ret);
78
+ }
79
+ return unwrap(ret, options);
80
+ }
81
+ catch (e) {
82
+ // too many requests
83
+ if (e.status === 429) {
84
+ return local.onError(e, url, options, retries, args, true);
85
+ }
86
+ // bad request
87
+ if (e.status === 400 || e.status === 422) {
88
+ throw e;
89
+ }
90
+ --retries;
91
+ console.log(theURL, e);
92
+ if (retries <= 0)
93
+ throw e;
94
+ return local.onError(e, url, options, retries, args);
95
+ }
96
+ }
97
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ import Fetcher from "./fetcher.mjs";
2
+ declare class OAuthFetcher extends Fetcher {
3
+ oauth: any;
4
+ _getToken: any;
5
+ constructor({ oauth, retry, getToken, baseUrl, onResponse, customize }: {
6
+ oauth: any;
7
+ retry?: number | undefined;
8
+ getToken: any;
9
+ baseUrl: any;
10
+ onResponse: any;
11
+ customize: any;
12
+ });
13
+ getToken(force: any): Promise<any>;
14
+ onError(e: any, url: any, options: any, retries: any, args: any, rateLimit: any): Promise<unknown>;
15
+ periodicRefresh(): Promise<void>;
16
+ customize(options: any, args?: any): Promise<void>;
17
+ }
18
+ export declare class OAuth {
19
+ _data: any;
20
+ saveOAuthResult: any;
21
+ obtainViaRefreshToken: any;
22
+ clients: any[];
23
+ constructor(data: any, saveOAuthResult: any, getRefreshToken: any);
24
+ data(): any;
25
+ accessToken(): any;
26
+ refreshToken(): any;
27
+ update(accessToken: any, refreshToken: any): Promise<void>;
28
+ periodicRefresh(): Promise<void>;
29
+ invalidate(err: any): Promise<void>;
30
+ getClient(arg?: any): OAuthFetcher;
31
+ }
32
+ export {};
@@ -0,0 +1,114 @@
1
+ import Fetcher from "./fetcher.mjs";
2
+ class OAuthFetcher extends Fetcher {
3
+ oauth;
4
+ _getToken;
5
+ constructor({ oauth, retry = 5, getToken, baseUrl, onResponse, customize }) {
6
+ super({ retry, baseUrl, onResponse, customize });
7
+ this.oauth = oauth;
8
+ this._getToken = getToken;
9
+ }
10
+ async getToken(force) {
11
+ var local = this, oauth = local.oauth;
12
+ if (local._getToken)
13
+ return local._getToken(force);
14
+ if (!force && oauth.accessToken())
15
+ return oauth.accessToken();
16
+ const refreshToken = oauth.refreshToken();
17
+ try {
18
+ if (!refreshToken) {
19
+ throw new Error("have no access_token and no refresh_token");
20
+ }
21
+ const ret = await oauth.obtainViaRefreshToken(oauth.refreshToken());
22
+ if (ret.access_token) {
23
+ oauth.update(ret.access_token, ret.refresh_token);
24
+ return ret.access_token;
25
+ }
26
+ else {
27
+ throw new Error("could not obtain access token via refresh token");
28
+ }
29
+ }
30
+ catch (e) {
31
+ oauth.invalidate(e);
32
+ throw e;
33
+ }
34
+ }
35
+ async onError(e, url, options, retries, args, rateLimit) {
36
+ var local = this;
37
+ return new Promise((resolve, reject) => {
38
+ setTimeout(async () => {
39
+ try {
40
+ resolve(await local.fetch(url, options, retries, {
41
+ forceTokenRefresh: e.status === 401,
42
+ }));
43
+ }
44
+ catch (e) {
45
+ reject(e);
46
+ }
47
+ }, rateLimit ? 10000 : 500);
48
+ });
49
+ }
50
+ async periodicRefresh() {
51
+ const local = this, oauth = local.oauth;
52
+ console.log("refreshing oauth token, have token", !!oauth.refreshToken());
53
+ if (!oauth.refreshToken())
54
+ return;
55
+ await local.getToken(true);
56
+ console.log("refreshed oauth token");
57
+ }
58
+ async customize(options, args = {}) {
59
+ const local = this;
60
+ if (this.customize0)
61
+ await this.customize0(options, args);
62
+ const token = await local.getToken(args.forceTokenRefresh);
63
+ options.headers = {
64
+ ...options.headers,
65
+ Authorization: `Bearer ${token}`,
66
+ };
67
+ }
68
+ }
69
+ export class OAuth {
70
+ _data;
71
+ saveOAuthResult;
72
+ obtainViaRefreshToken;
73
+ clients;
74
+ constructor(data, saveOAuthResult, getRefreshToken) {
75
+ this._data = data || {};
76
+ this.saveOAuthResult = saveOAuthResult;
77
+ this.obtainViaRefreshToken = getRefreshToken;
78
+ this.clients = [];
79
+ }
80
+ data() {
81
+ return this._data;
82
+ }
83
+ accessToken() {
84
+ return this._data.access_token;
85
+ }
86
+ refreshToken() {
87
+ return this._data.refresh_token;
88
+ }
89
+ async update(accessToken, refreshToken) {
90
+ this._data.access_token = accessToken;
91
+ if (refreshToken) {
92
+ this._data.refresh_token = refreshToken;
93
+ }
94
+ await this.saveOAuthResult(this._data);
95
+ }
96
+ async periodicRefresh() {
97
+ const clients = this.clients;
98
+ console.log("refreshing oauth clients", clients.length);
99
+ for (let i = 0; i < clients.length; ++i) {
100
+ const client = clients[0];
101
+ await client.periodicRefresh();
102
+ }
103
+ }
104
+ async invalidate(err) {
105
+ if (true)
106
+ return;
107
+ //if (this._data.access_token === "invalid") return;
108
+ }
109
+ getClient(arg = {}) {
110
+ const client = new OAuthFetcher({ ...arg, oauth: this });
111
+ this.clients.push(client);
112
+ return client;
113
+ }
114
+ }
@@ -1,16 +1,17 @@
1
- export class Connector {
1
+ import { Dispatcher } from "./dispatcher/index.mjs";
2
+ declare class Connector {
3
+ id: any;
4
+ version: any;
5
+ name: any;
6
+ icon: any;
7
+ dispatcher?: Dispatcher;
2
8
  constructor({ version, id, name, icon }: {
3
9
  version: any;
4
10
  id: any;
5
11
  name: any;
6
12
  icon: any;
7
13
  });
8
- id: any;
9
- version: any;
10
- name: any;
11
- icon: any;
12
14
  configure(): Dispatcher;
13
- dispatcher: Dispatcher | undefined;
14
15
  run(): Promise<void>;
15
16
  }
16
- import { Dispatcher } from "./dispatcher/index.mjs";
17
+ export { Connector };