@hexagramio/saga-ts 0.9.293-1 → 0.9.293-3
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/cjs/commands/users.d.ts +151 -0
- package/dist/cjs/commands/users.d.ts.map +1 -0
- package/dist/cjs/commands/users.js +186 -0
- package/dist/cjs/commands/users.js.map +1 -0
- package/dist/cjs/index.d.ts +7 -115
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +20 -201
- package/dist/cjs/index.js.map +1 -1
- package/dist/commands/users.d.ts +151 -0
- package/dist/commands/users.d.ts.map +1 -0
- package/dist/commands/users.js +170 -0
- package/dist/commands/users.js.map +1 -0
- package/dist/index.d.ts +7 -115
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -195
- package/dist/index.js.map +1 -1
- package/package.json +3 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,116 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import { ReadMessage, ReadProperty } from "./lib/shared_types";
|
|
9
|
-
/**
|
|
10
|
-
* The APIError is thrown when a HTTPCommand or SocketCommands fails to execute
|
|
11
|
-
*/
|
|
12
|
-
export declare class APIError extends Error {
|
|
13
|
-
/**
|
|
14
|
-
* a saga status code as explained in the actual saga documentation,
|
|
15
|
-
* very much aligned to HTTP Status codes
|
|
16
|
-
*/
|
|
17
|
-
statusCode: number;
|
|
18
|
-
/**
|
|
19
|
-
* The path of the HTTPCommand or SocketCommand that caused the error
|
|
20
|
-
*/
|
|
21
|
-
path?: string;
|
|
22
|
-
/**
|
|
23
|
-
* Details on what field were not permitted
|
|
24
|
-
*/
|
|
25
|
-
errors?: Record<string, string>;
|
|
26
|
-
/**
|
|
27
|
-
* Create a
|
|
28
|
-
* @param message the error message
|
|
29
|
-
* @param statusCode a saga status code as explained in the actual saga documentation, very much aligned to HTTP Status codes
|
|
30
|
-
* @param errors list of field names and the reason why the field wasn't permitted
|
|
31
|
-
* @param path The path of the HTTPCommand or SocketCommand that caused the error
|
|
32
|
-
*/
|
|
33
|
-
constructor(message: string, statusCode: number, errors?: Record<string, string>, path?: string);
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Encapsulates authentication tokens. An authentication object is returned i.e. from {@link LoginUserCommand} It's required for most HTTP Commands.
|
|
37
|
-
*/
|
|
38
|
-
export interface Authentication {
|
|
39
|
-
/**
|
|
40
|
-
* Required access token
|
|
41
|
-
*/
|
|
42
|
-
accessToken: string;
|
|
43
|
-
/**
|
|
44
|
-
* Used to retrieve a new access token using {@link RefreshTokenCommand}
|
|
45
|
-
*/
|
|
46
|
-
refreshToken: string;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Interface to represent the various mutations of a HTTP Command profile.
|
|
50
|
-
*/
|
|
51
|
-
export interface HTTPCommand<T> {
|
|
52
|
-
method: "GET" | "PUT" | "POST" | "DELETE";
|
|
53
|
-
path: string;
|
|
54
|
-
params?: URLSearchParams;
|
|
55
|
-
data?: any;
|
|
56
|
-
accessToken?: string;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Send the HTTP Command to the host with the given baseURL
|
|
60
|
-
* @param baseURL the baseURL of the HTTP Api, i.e 'https://example.api.hexagram.io'
|
|
61
|
-
* @param command the actual command to send
|
|
62
|
-
* @return a promise containing the response from the command
|
|
63
|
-
* @throws an APIError if the operations fails
|
|
64
|
-
*/
|
|
65
|
-
export declare const sendHTTPCommand: <T>(baseURL: URL, command: HTTPCommand<T>) => Promise<T>;
|
|
66
|
-
/**
|
|
67
|
-
* The events a socket session can receive.
|
|
68
|
-
*/
|
|
69
|
-
export interface EventMap {
|
|
70
|
-
"/properties": ReadProperty;
|
|
71
|
-
"/messages": ReadMessage;
|
|
72
|
-
"authenticated": any;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* A socket join command
|
|
76
|
-
*/
|
|
77
|
-
export interface SocketJoinCommand {
|
|
78
|
-
method: "/users" | "/bots" | "/globals" | "/jobs/runnable_calls" | "/jobs/versions" | "/scripts/runnable_calls" | "/scripts/versions" | "/system" | "/npm_packages";
|
|
79
|
-
id: string;
|
|
80
|
-
join: boolean;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* The discriminating union type of socket commands,
|
|
84
|
-
* for it just contains the join commands, but the
|
|
85
|
-
* intent is to hold other socket commands as well
|
|
86
|
-
*/
|
|
87
|
-
export type SocketCommand = SocketJoinCommand;
|
|
88
|
-
/**
|
|
89
|
-
* Class that wraps saga socket connection logic and manages realtime saga socket events. The
|
|
90
|
-
*/
|
|
91
|
-
export declare class SocketSession {
|
|
92
|
-
private socket;
|
|
93
|
-
private readonly maxRetries;
|
|
94
|
-
private commandStack;
|
|
95
|
-
private ready;
|
|
96
|
-
/**
|
|
97
|
-
* Set connection parameters.
|
|
98
|
-
* @param host the host to connect to
|
|
99
|
-
* @param accessToken the access token to authenticate
|
|
100
|
-
* @param errorNotify callback function that will be called when errors occur
|
|
101
|
-
*/
|
|
102
|
-
constructor(host: URL, accessToken: string, errorNotify: (error: APIError) => any, options?: {
|
|
103
|
-
delayAuthentication?: number;
|
|
104
|
-
triggerDisconnectAndConnect?: number;
|
|
105
|
-
});
|
|
106
|
-
private emitStackedCommands;
|
|
107
|
-
on<E extends keyof EventMap>(type: E, listener: (ev: EventMap[E]) => undefined): void;
|
|
108
|
-
off<E extends keyof EventMap>(type: E, listener?: (ev: EventMap[E]) => undefined): void;
|
|
109
|
-
/**
|
|
110
|
-
* Send commands to the server
|
|
111
|
-
* @param command
|
|
112
|
-
* @throws APIError if error ack is send back from server
|
|
113
|
-
*/
|
|
114
|
-
emitCommand(command: SocketCommand): Promise<unknown>;
|
|
115
|
-
}
|
|
1
|
+
export * from './senders/http';
|
|
2
|
+
export * from './senders/socket';
|
|
3
|
+
export * from './commands/bots';
|
|
4
|
+
export * from './commands/users';
|
|
5
|
+
export * from './commands/system';
|
|
6
|
+
export * from './commands/test_data';
|
|
7
|
+
export * from './shared/types';
|
|
116
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,sBAAsB,CAAA;AACpC,cAAc,gBAAgB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,196 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
-
});
|
|
14
|
-
};
|
|
15
|
-
import axios from 'axios';
|
|
16
|
-
import socketio from 'socket.io-client';
|
|
17
|
-
import parseLinkHeader from "parse-link-header";
|
|
18
|
-
import Debug from "debug";
|
|
19
|
-
const debug = Debug("saga-ts");
|
|
20
|
-
/**
|
|
21
|
-
* The APIError is thrown when a HTTPCommand or SocketCommands fails to execute
|
|
22
|
-
*/
|
|
23
|
-
export class APIError extends Error {
|
|
24
|
-
/**
|
|
25
|
-
* Create a
|
|
26
|
-
* @param message the error message
|
|
27
|
-
* @param statusCode a saga status code as explained in the actual saga documentation, very much aligned to HTTP Status codes
|
|
28
|
-
* @param errors list of field names and the reason why the field wasn't permitted
|
|
29
|
-
* @param path The path of the HTTPCommand or SocketCommand that caused the error
|
|
30
|
-
*/
|
|
31
|
-
constructor(message, statusCode, errors, path) {
|
|
32
|
-
super(message);
|
|
33
|
-
this.statusCode = statusCode;
|
|
34
|
-
this.errors = errors;
|
|
35
|
-
this.path = path;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Send the HTTP Command to the host with the given baseURL
|
|
40
|
-
* @param baseURL the baseURL of the HTTP Api, i.e 'https://example.api.hexagram.io'
|
|
41
|
-
* @param command the actual command to send
|
|
42
|
-
* @return a promise containing the response from the command
|
|
43
|
-
* @throws an APIError if the operations fails
|
|
44
|
-
*/
|
|
45
|
-
export const sendHTTPCommand = (baseURL, command) => __awaiter(void 0, void 0, void 0, function* () {
|
|
46
|
-
var _a, _b, _c, _d, _e;
|
|
47
|
-
const headers = { 'Content-Type': 'application/json' };
|
|
48
|
-
if (command.accessToken) {
|
|
49
|
-
headers.Authorization = `Bearer ${command.accessToken}`;
|
|
50
|
-
}
|
|
51
|
-
const response = yield axios({
|
|
52
|
-
headers,
|
|
53
|
-
baseURL: baseURL.toString(),
|
|
54
|
-
validateStatus: () => true,
|
|
55
|
-
method: command.method,
|
|
56
|
-
url: command.path,
|
|
57
|
-
params: command.params,
|
|
58
|
-
data: command.data
|
|
59
|
-
});
|
|
60
|
-
if (response.status !== 200) {
|
|
61
|
-
throw new APIError(response.data.toString(), response.status, (_a = response.data) === null || _a === void 0 ? void 0 : _a.errors, command.path);
|
|
62
|
-
}
|
|
63
|
-
else if (response.headers['link'] || response.headers['link'] === '') {
|
|
64
|
-
const parsed = parseLinkHeader(response.headers['link']);
|
|
65
|
-
const list = (url) => {
|
|
66
|
-
return {
|
|
67
|
-
method: "GET",
|
|
68
|
-
path: url.pathname,
|
|
69
|
-
params: url.searchParams,
|
|
70
|
-
accessToken: command.accessToken
|
|
71
|
-
};
|
|
72
|
-
};
|
|
73
|
-
return {
|
|
74
|
-
result: response.data,
|
|
75
|
-
prevCommand: ((_b = parsed === null || parsed === void 0 ? void 0 : parsed.prev) === null || _b === void 0 ? void 0 : _b.url) ? list(new URL((_c = parsed === null || parsed === void 0 ? void 0 : parsed.prev) === null || _c === void 0 ? void 0 : _c.url)) : undefined,
|
|
76
|
-
nextCommand: ((_d = parsed === null || parsed === void 0 ? void 0 : parsed.next) === null || _d === void 0 ? void 0 : _d.url) ? list(new URL((_e = parsed === null || parsed === void 0 ? void 0 : parsed.next) === null || _e === void 0 ? void 0 : _e.url)) : undefined
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
return response.data;
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
/**
|
|
84
|
-
* Class that wraps saga socket connection logic and manages realtime saga socket events. The
|
|
85
|
-
*/
|
|
86
|
-
export class SocketSession {
|
|
87
|
-
/**
|
|
88
|
-
* Set connection parameters.
|
|
89
|
-
* @param host the host to connect to
|
|
90
|
-
* @param accessToken the access token to authenticate
|
|
91
|
-
* @param errorNotify callback function that will be called when errors occur
|
|
92
|
-
*/
|
|
93
|
-
constructor(host, accessToken, errorNotify, options) {
|
|
94
|
-
this.maxRetries = 100;
|
|
95
|
-
this.commandStack = [];
|
|
96
|
-
this.ready = false;
|
|
97
|
-
this.socket = socketio(host.toString(), {
|
|
98
|
-
transports: ['websocket', 'polling'],
|
|
99
|
-
reconnection: true,
|
|
100
|
-
reconnectionDelay: 1000,
|
|
101
|
-
reconnectionDelayMax: 5000,
|
|
102
|
-
reconnectionAttempts: this.maxRetries,
|
|
103
|
-
})
|
|
104
|
-
.on('connect', () => {
|
|
105
|
-
debug('connect');
|
|
106
|
-
if ((options === null || options === void 0 ? void 0 : options.delayAuthentication) && (options === null || options === void 0 ? void 0 : options.delayAuthentication) > 0) {
|
|
107
|
-
setTimeout(() => this.socket.emit('authentication', { accessToken }), options.delayAuthentication);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
this.socket.emit('authentication', { accessToken });
|
|
111
|
-
}
|
|
112
|
-
if ((options === null || options === void 0 ? void 0 : options.triggerDisconnectAndConnect) && options.triggerDisconnectAndConnect > 0) {
|
|
113
|
-
setTimeout(() => {
|
|
114
|
-
options.triggerDisconnectAndConnect = 0;
|
|
115
|
-
this.socket.disconnect();
|
|
116
|
-
this.socket.connect();
|
|
117
|
-
this.emitStackedCommands();
|
|
118
|
-
}, options.triggerDisconnectAndConnect);
|
|
119
|
-
}
|
|
120
|
-
//this.socket.emit('authentication', {accessToken});
|
|
121
|
-
})
|
|
122
|
-
.on('disconnect', err => {
|
|
123
|
-
this.ready = false;
|
|
124
|
-
debug(`disconnect ${err}`);
|
|
125
|
-
})
|
|
126
|
-
.on('error', err => {
|
|
127
|
-
debug(`error ${err}`);
|
|
128
|
-
errorNotify(new APIError(err.message, 500));
|
|
129
|
-
})
|
|
130
|
-
.on('unauthorized', (err) => {
|
|
131
|
-
debug('unauthorized');
|
|
132
|
-
errorNotify(new APIError(err.message, 401));
|
|
133
|
-
})
|
|
134
|
-
.on('authenticated', () => {
|
|
135
|
-
debug('authenticated');
|
|
136
|
-
this.ready = true;
|
|
137
|
-
this.emitStackedCommands();
|
|
138
|
-
});
|
|
139
|
-
this.socket.io.on('reconnect', () => {
|
|
140
|
-
debug('reconnect');
|
|
141
|
-
this.emitStackedCommands();
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
emitStackedCommands() {
|
|
145
|
-
debug(`emitStackedCommands ${this.commandStack.length}`);
|
|
146
|
-
for (const command of this.commandStack) {
|
|
147
|
-
debug(`send queued ${command.method} ${command.join} ${command.id}`);
|
|
148
|
-
this.socket.emit(`${command.method}/${command.join ? "join" : "leave"}`, command.id, (response) => {
|
|
149
|
-
debug(`queued command response ${response.status_code}`);
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
this.commandStack = this.commandStack.filter(command => command.join !== undefined);
|
|
153
|
-
}
|
|
154
|
-
on(type, listener) {
|
|
155
|
-
//@ts-ignore
|
|
156
|
-
this.socket.on(type, listener);
|
|
157
|
-
}
|
|
158
|
-
off(type, listener) {
|
|
159
|
-
//@ts-ignore
|
|
160
|
-
this.socket.off(type, listener);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Send commands to the server
|
|
164
|
-
* @param command
|
|
165
|
-
* @throws APIError if error ack is send back from server
|
|
166
|
-
*/
|
|
167
|
-
emitCommand(command) {
|
|
168
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
return new Promise((resolve, reject) => {
|
|
170
|
-
if (!this.ready) {
|
|
171
|
-
debug(`command queued ${command.method} ${command.join} ${command.id}`);
|
|
172
|
-
this.commandStack.push(command);
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
debug(`sending ${command.method}`);
|
|
176
|
-
const path = `${command.method}/${command.join ? "join" : "leave"}`;
|
|
177
|
-
this.socket.emit(path, command.id, (response) => {
|
|
178
|
-
debug(`received ack ${response.status_code} for ${command.method} `);
|
|
179
|
-
if (response.status_code !== 200) {
|
|
180
|
-
reject(new APIError(response.body.name.toString(), response.status_code, response.body.errors, path));
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
183
|
-
//we only need to queue messages that join/leave
|
|
184
|
-
//for replay when the socket reconnects
|
|
185
|
-
if (command.join !== undefined) {
|
|
186
|
-
this.commandStack.push(command);
|
|
187
|
-
}
|
|
188
|
-
resolve(response);
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
}
|
|
1
|
+
export * from './senders/http';
|
|
2
|
+
export * from './senders/socket';
|
|
3
|
+
export * from './commands/bots';
|
|
4
|
+
export * from './commands/users';
|
|
5
|
+
export * from './commands/system';
|
|
6
|
+
export * from './commands/test_data';
|
|
7
|
+
export * from './shared/types';
|
|
196
8
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,mBAAmB,CAAA;AACjC,cAAc,sBAAsB,CAAA;AACpC,cAAc,gBAAgB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hexagramio/saga-ts",
|
|
3
|
-
"version": "0.9.293-
|
|
3
|
+
"version": "0.9.293-3",
|
|
4
4
|
"description": "Saga SDK",
|
|
5
|
-
"main": "./dist/index.js",
|
|
6
5
|
"types": "./dist/index.d.ts",
|
|
7
|
-
|
|
8
6
|
"exports": {
|
|
9
7
|
".": "./dist/index.js",
|
|
10
|
-
"
|
|
8
|
+
"./": "./dist/"
|
|
11
9
|
},
|
|
12
10
|
"scripts": {
|
|
13
11
|
"test": "jest --forceExit",
|
|
@@ -47,4 +45,4 @@
|
|
|
47
45
|
"license": "MIT",
|
|
48
46
|
"homepage": "https://hexagram.io",
|
|
49
47
|
"author": "Hexagram <admin@hexagram.io> (https:/hexagram.io/)"
|
|
50
|
-
}
|
|
48
|
+
}
|