mezon-sdk 2.7.1 → 2.7.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.
- package/README.md +143 -143
- package/api.ts +333 -0
- package/build.mjs +44 -44
- package/client.ts +198 -327
- package/dist/api.d.ts +53 -0
- package/dist/client.d.ts +60 -74
- package/dist/index.d.ts +18 -18
- package/dist/mezon-sdk.cjs.js +10303 -0
- package/dist/mezon-sdk.esm.mjs +10283 -0
- package/dist/mezon-sdk.iife.js +10305 -0
- package/dist/mezon-sdk.umd.js +10979 -0
- package/dist/session.d.ts +52 -52
- package/dist/socket.d.ts +696 -0
- package/dist/utils.d.ts +3 -3
- package/dist/web_socket_adapter.d.ts +83 -0
- package/index.ts +19 -19
- package/package.json +46 -49
- package/rollup.config.js +40 -40
- package/session.ts +107 -107
- package/socket.ts +1526 -0
- package/tsconfig.json +10 -10
- package/utils.ts +47 -47
- package/web_socket_adapter.ts +159 -0
- package/api.gen.ts +0 -783
- package/dist/api.gen.d.ts +0 -157
- package/dist/satori-js.cjs.js +0 -1456
- package/dist/satori-js.esm.mjs +0 -1436
- package/dist/satori-js.iife.js +0 -1458
- package/dist/satori-js.umd.js +0 -1956
- package/tsconfig.base.json +0 -33
package/README.md
CHANGED
|
@@ -1,143 +1,143 @@
|
|
|
1
|
-
|
|
2
|
-
========================
|
|
3
|
-
|
|
4
|
-
> JavaScript client for
|
|
5
|
-
|
|
6
|
-
This client implements the full API for interacting with
|
|
7
|
-
|
|
8
|
-
Full documentation is online - https://
|
|
9
|
-
|
|
10
|
-
## Getting Started
|
|
11
|
-
|
|
12
|
-
You'll need access to an instance of the
|
|
13
|
-
|
|
14
|
-
1. Import the client into your project. It's [available on NPM](https://www.npmjs/package/mezon-sdk).
|
|
15
|
-
|
|
16
|
-
```shell
|
|
17
|
-
npm install mezon-sdk
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
You'll now see the code in the "node_modules" folder and package listed in your "package.json".
|
|
21
|
-
|
|
22
|
-
2. Use the connection credentials to build a client object.
|
|
23
|
-
|
|
24
|
-
```js
|
|
25
|
-
import {Client} from "mezon-sdk";
|
|
26
|
-
const client = new Client("apiKey");
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## Usage
|
|
30
|
-
|
|
31
|
-
The client object has many method to execute various features in the server.
|
|
32
|
-
|
|
33
|
-
### Authenticate
|
|
34
|
-
|
|
35
|
-
To authenticate with the
|
|
36
|
-
|
|
37
|
-
```js
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
client.authenticate(
|
|
41
|
-
.then(session => {
|
|
42
|
-
_session = session;
|
|
43
|
-
console.info("Authenticated:", session);
|
|
44
|
-
}).catch(error => {
|
|
45
|
-
console.error("Error:", error);
|
|
46
|
-
});
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### Sessions
|
|
50
|
-
|
|
51
|
-
When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a `Session` object.
|
|
52
|
-
|
|
53
|
-
```js
|
|
54
|
-
console.info(session.token); // raw JWT token
|
|
55
|
-
console.info(session.refreshToken); // refresh token
|
|
56
|
-
console.info("Session has expired?", session.isexpired(Date.now() / 1000));
|
|
57
|
-
const expiresAt = session.expires_at;
|
|
58
|
-
console.warn("Session will expire at:", new Date(expiresAt * 1000).toISOString());
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
It is recommended to store the auth token from the session and check at startup if it has expired. If the token has expired you must reauthenticate. The expiry time of the token can be changed as a setting in the server.
|
|
62
|
-
|
|
63
|
-
```js
|
|
64
|
-
// Assume we've stored the auth token in browser Web Storage.
|
|
65
|
-
const authtoken = window.localStorage.getItem("satori_authtoken");
|
|
66
|
-
const refreshtoken = window.localStorage.getItem("satori_refreshtoken");
|
|
67
|
-
|
|
68
|
-
let session = satorijs.Session.restore(authtoken, refreshtoken);
|
|
69
|
-
|
|
70
|
-
// Check whether a session is close to expiry.
|
|
71
|
-
|
|
72
|
-
const unixTimeInFuture = Date.now() + 8.64e+7; // one day from now
|
|
73
|
-
|
|
74
|
-
if (session.isexpired(unixTimeInFuture / 1000)) {
|
|
75
|
-
try
|
|
76
|
-
{
|
|
77
|
-
session = await client.sessionRefresh(session);
|
|
78
|
-
}
|
|
79
|
-
catch (e)
|
|
80
|
-
{
|
|
81
|
-
console.info("Session can no longer be refreshed. Must reauthenticate!");
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
### Requests
|
|
87
|
-
|
|
88
|
-
The client includes lots of builtin APIs for various featyures of the
|
|
89
|
-
|
|
90
|
-
Most requests are sent with a session object which authorizes the client.
|
|
91
|
-
|
|
92
|
-
```js
|
|
93
|
-
const flags = await client.getFlags(session);
|
|
94
|
-
console.info("Flags:", flags);
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
## Contribute
|
|
98
|
-
|
|
99
|
-
The development roadmap is managed as GitHub issues and pull requests are welcome. If you're interested in enhancing the code please open an issue to discuss the changes or drop in and discuss it in the [community forum](https://forum.
|
|
100
|
-
|
|
101
|
-
### Source Builds
|
|
102
|
-
|
|
103
|
-
Ensure you are using Node v18>.
|
|
104
|
-
|
|
105
|
-
The codebase is multi-package monorepo written in TypeScript and can be built with [esbuild](https://github.com/evanw/esbuild). All dependencies are managed with Yarn.
|
|
106
|
-
|
|
107
|
-
To build from source, install dependencies and build the `
|
|
108
|
-
|
|
109
|
-
```shell
|
|
110
|
-
npm install --workspace=mezon-sdk && npm run build --workspace=mezon-sdk
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
### Run Tests
|
|
114
|
-
|
|
115
|
-
To run tests you will need access to an instance of the
|
|
116
|
-
|
|
117
|
-
Tests are run against each workspace bundle; if you have made source code changes, you should `npm run build --workspace=<workspace>` prior to running tests.
|
|
118
|
-
|
|
119
|
-
```shell
|
|
120
|
-
npm run test --workspace=mezon-sdk-test
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
### Release Process
|
|
124
|
-
|
|
125
|
-
To release onto NPM if you have access to the "@
|
|
126
|
-
|
|
127
|
-
```shell
|
|
128
|
-
npm run build --workspace=<workspace> && npm publish --access=public --workspace=<workspace>
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
### Generate Docs
|
|
132
|
-
|
|
133
|
-
API docs are generated with typedoc and deployed to GitHub pages.
|
|
134
|
-
|
|
135
|
-
To run typedoc:
|
|
136
|
-
|
|
137
|
-
```
|
|
138
|
-
npm install && npm run docs
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### License
|
|
142
|
-
|
|
143
|
-
This project is licensed under the [Apache-2 License](https://github.com/
|
|
1
|
+
Mezon JavaScript Client
|
|
2
|
+
========================
|
|
3
|
+
|
|
4
|
+
> JavaScript client for Mezon server written in TypeScript. For browser and React Native projects.
|
|
5
|
+
|
|
6
|
+
This client implements the full API for interacting with Mezon server. It's written in TypeScript with minimal dependencies to be compatible with all modern browsers and React Native.
|
|
7
|
+
|
|
8
|
+
Full documentation is online - https://mezon.ai/docs/javascript-client-guide
|
|
9
|
+
|
|
10
|
+
## Getting Started
|
|
11
|
+
|
|
12
|
+
You'll need access to an instance of the Mezon server before you can connect with the client.
|
|
13
|
+
|
|
14
|
+
1. Import the client into your project. It's [available on NPM](https://www.npmjs/package/mezon-sdk).
|
|
15
|
+
|
|
16
|
+
```shell
|
|
17
|
+
npm install mezon-sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
You'll now see the code in the "node_modules" folder and package listed in your "package.json".
|
|
21
|
+
|
|
22
|
+
2. Use the connection credentials to build a client object.
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import {Client} from "mezon-sdk";
|
|
26
|
+
const client = new Client("apiKey");
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
The client object has many method to execute various features in the server.
|
|
32
|
+
|
|
33
|
+
### Authenticate
|
|
34
|
+
|
|
35
|
+
To authenticate with the Mezon server you must provide an identifier for the user.
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const appId = "<AppId>";
|
|
39
|
+
|
|
40
|
+
client.authenticate(appId)
|
|
41
|
+
.then(session => {
|
|
42
|
+
_session = session;
|
|
43
|
+
console.info("Authenticated:", session);
|
|
44
|
+
}).catch(error => {
|
|
45
|
+
console.error("Error:", error);
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Sessions
|
|
50
|
+
|
|
51
|
+
When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a `Session` object.
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
console.info(session.token); // raw JWT token
|
|
55
|
+
console.info(session.refreshToken); // refresh token
|
|
56
|
+
console.info("Session has expired?", session.isexpired(Date.now() / 1000));
|
|
57
|
+
const expiresAt = session.expires_at;
|
|
58
|
+
console.warn("Session will expire at:", new Date(expiresAt * 1000).toISOString());
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
It is recommended to store the auth token from the session and check at startup if it has expired. If the token has expired you must reauthenticate. The expiry time of the token can be changed as a setting in the server.
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
// Assume we've stored the auth token in browser Web Storage.
|
|
65
|
+
const authtoken = window.localStorage.getItem("satori_authtoken");
|
|
66
|
+
const refreshtoken = window.localStorage.getItem("satori_refreshtoken");
|
|
67
|
+
|
|
68
|
+
let session = satorijs.Session.restore(authtoken, refreshtoken);
|
|
69
|
+
|
|
70
|
+
// Check whether a session is close to expiry.
|
|
71
|
+
|
|
72
|
+
const unixTimeInFuture = Date.now() + 8.64e+7; // one day from now
|
|
73
|
+
|
|
74
|
+
if (session.isexpired(unixTimeInFuture / 1000)) {
|
|
75
|
+
try
|
|
76
|
+
{
|
|
77
|
+
session = await client.sessionRefresh(session);
|
|
78
|
+
}
|
|
79
|
+
catch (e)
|
|
80
|
+
{
|
|
81
|
+
console.info("Session can no longer be refreshed. Must reauthenticate!");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Requests
|
|
87
|
+
|
|
88
|
+
The client includes lots of builtin APIs for various featyures of the Mezon server. These can be accessed with the methods which return Promise objects.
|
|
89
|
+
|
|
90
|
+
Most requests are sent with a session object which authorizes the client.
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
const flags = await client.getFlags(session);
|
|
94
|
+
console.info("Flags:", flags);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Contribute
|
|
98
|
+
|
|
99
|
+
The development roadmap is managed as GitHub issues and pull requests are welcome. If you're interested in enhancing the code please open an issue to discuss the changes or drop in and discuss it in the [community forum](https://forum.mezon.ai).
|
|
100
|
+
|
|
101
|
+
### Source Builds
|
|
102
|
+
|
|
103
|
+
Ensure you are using Node v18>.
|
|
104
|
+
|
|
105
|
+
The codebase is multi-package monorepo written in TypeScript and can be built with [esbuild](https://github.com/evanw/esbuild). All dependencies are managed with Yarn.
|
|
106
|
+
|
|
107
|
+
To build from source, install dependencies and build the `mezon-sdk` package:
|
|
108
|
+
|
|
109
|
+
```shell
|
|
110
|
+
npm install --workspace=mezon-sdk && npm run build --workspace=mezon-sdk
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Run Tests
|
|
114
|
+
|
|
115
|
+
To run tests you will need access to an instance of the Mezon server.
|
|
116
|
+
|
|
117
|
+
Tests are run against each workspace bundle; if you have made source code changes, you should `npm run build --workspace=<workspace>` prior to running tests.
|
|
118
|
+
|
|
119
|
+
```shell
|
|
120
|
+
npm run test --workspace=mezon-sdk-test
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Release Process
|
|
124
|
+
|
|
125
|
+
To release onto NPM if you have access to the "@mezon" organization you can use NPM.
|
|
126
|
+
|
|
127
|
+
```shell
|
|
128
|
+
npm run build --workspace=<workspace> && npm publish --access=public --workspace=<workspace>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Generate Docs
|
|
132
|
+
|
|
133
|
+
API docs are generated with typedoc and deployed to GitHub pages.
|
|
134
|
+
|
|
135
|
+
To run typedoc:
|
|
136
|
+
|
|
137
|
+
```
|
|
138
|
+
npm install && npm run docs
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### License
|
|
142
|
+
|
|
143
|
+
This project is licensed under the [Apache-2 License](https://github.com/mezon/mezon/blob/master/LICENSE).
|
package/api.ts
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
// tslint:disable
|
|
2
|
+
/* Code generated by openapi-gen/main.go. DO NOT EDIT. */
|
|
3
|
+
|
|
4
|
+
import { buildFetchOptions } from './utils';
|
|
5
|
+
import { encode } from 'js-base64';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/** A user's session used to authenticate messages. */
|
|
9
|
+
export interface ApiSession {
|
|
10
|
+
//True if the corresponding account was just created, false otherwise.
|
|
11
|
+
created?: boolean;
|
|
12
|
+
//Refresh token that can be used for session token renewal.
|
|
13
|
+
refresh_token?: string;
|
|
14
|
+
//Authentication credentials.
|
|
15
|
+
token?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Log out a session, invalidate a refresh token, or log out all sessions/refresh tokens for a user. */
|
|
19
|
+
export interface ApiAuthenticateLogoutRequest {
|
|
20
|
+
//Refresh token to invalidate.
|
|
21
|
+
refresh_token?: string;
|
|
22
|
+
//Session token to log out.
|
|
23
|
+
token?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Authenticate against the server with a refresh token. */
|
|
27
|
+
export interface ApiAuthenticateRefreshRequest {
|
|
28
|
+
//Refresh token.
|
|
29
|
+
refresh_token?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Authenticate against the server with a device ID. */
|
|
33
|
+
export interface ApiAuthenticateRequest {
|
|
34
|
+
//The App account details.
|
|
35
|
+
account?: ApiAccountApp;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/** Send a app token to the server. Used with authenticate/link/unlink. */
|
|
40
|
+
export interface ApiAccountApp {
|
|
41
|
+
//
|
|
42
|
+
appid?: string;
|
|
43
|
+
//
|
|
44
|
+
appname?: string;
|
|
45
|
+
//The account token when create apps to access their profile API.
|
|
46
|
+
token?: string;
|
|
47
|
+
//Extra information that will be bundled in the session token.
|
|
48
|
+
vars?: Record<string, string>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
/** The request to update the status of a message. */
|
|
53
|
+
export interface ApiUpdateMessageRequest {
|
|
54
|
+
//The time the message was consumed by the identity.
|
|
55
|
+
consume_time?: string;
|
|
56
|
+
//The identifier of the messages.
|
|
57
|
+
id?: string;
|
|
58
|
+
//The time the message was read at the client.
|
|
59
|
+
read_time?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
export class MezonApi {
|
|
64
|
+
|
|
65
|
+
constructor(readonly apiKey: string, readonly basePath: string, readonly timeoutMs: number) {}
|
|
66
|
+
|
|
67
|
+
/** A healthcheck which load balancers can use to check the service. */
|
|
68
|
+
mezonHealthcheck(bearerToken: string,
|
|
69
|
+
options: any = {}): Promise<any> {
|
|
70
|
+
|
|
71
|
+
const urlPath = "/healthcheck";
|
|
72
|
+
const queryParams = new Map<string, any>();
|
|
73
|
+
|
|
74
|
+
let bodyJson : string = "";
|
|
75
|
+
|
|
76
|
+
const fullUrl = this.buildFullUrl(this.basePath, urlPath, queryParams);
|
|
77
|
+
const fetchOptions = buildFetchOptions("GET", options, bodyJson);
|
|
78
|
+
if (bearerToken) {
|
|
79
|
+
fetchOptions.headers["Authorization"] = "Bearer " + bearerToken;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return Promise.race([
|
|
83
|
+
fetch(fullUrl, fetchOptions).then((response) => {
|
|
84
|
+
if (response.status == 204) {
|
|
85
|
+
return response;
|
|
86
|
+
} else if (response.status >= 200 && response.status < 300) {
|
|
87
|
+
return response.json();
|
|
88
|
+
} else {
|
|
89
|
+
throw response;
|
|
90
|
+
}
|
|
91
|
+
}),
|
|
92
|
+
new Promise((_, reject) =>
|
|
93
|
+
setTimeout(reject, this.timeoutMs, "Request timed out.")
|
|
94
|
+
),
|
|
95
|
+
]);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A readycheck which load balancers can use to check the service. */
|
|
99
|
+
mezonReadycheck(bearerToken: string,
|
|
100
|
+
options: any = {}): Promise<any> {
|
|
101
|
+
|
|
102
|
+
const urlPath = "/readycheck";
|
|
103
|
+
const queryParams = new Map<string, any>();
|
|
104
|
+
|
|
105
|
+
let bodyJson : string = "";
|
|
106
|
+
|
|
107
|
+
const fullUrl = this.buildFullUrl(this.basePath, urlPath, queryParams);
|
|
108
|
+
const fetchOptions = buildFetchOptions("GET", options, bodyJson);
|
|
109
|
+
if (bearerToken) {
|
|
110
|
+
fetchOptions.headers["Authorization"] = "Bearer " + bearerToken;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return Promise.race([
|
|
114
|
+
fetch(fullUrl, fetchOptions).then((response) => {
|
|
115
|
+
if (response.status == 204) {
|
|
116
|
+
return response;
|
|
117
|
+
} else if (response.status >= 200 && response.status < 300) {
|
|
118
|
+
return response.json();
|
|
119
|
+
} else {
|
|
120
|
+
throw response;
|
|
121
|
+
}
|
|
122
|
+
}),
|
|
123
|
+
new Promise((_, reject) =>
|
|
124
|
+
setTimeout(reject, this.timeoutMs, "Request timed out.")
|
|
125
|
+
),
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Authenticate a app with a token against the server. */
|
|
130
|
+
mezonAuthenticate(basicAuthUsername: string,
|
|
131
|
+
basicAuthPassword: string,
|
|
132
|
+
body:ApiAuthenticateRequest,
|
|
133
|
+
options: any = {}): Promise<ApiSession> {
|
|
134
|
+
|
|
135
|
+
if (body === null || body === undefined) {
|
|
136
|
+
throw new Error("'body' is a required parameter but is null or undefined.");
|
|
137
|
+
}
|
|
138
|
+
const urlPath = "/v2/apps/authenticate/token";
|
|
139
|
+
const queryParams = new Map<string, any>();
|
|
140
|
+
|
|
141
|
+
let bodyJson : string = "";
|
|
142
|
+
bodyJson = JSON.stringify(body || {});
|
|
143
|
+
|
|
144
|
+
const fullUrl = this.buildFullUrl(this.basePath, urlPath, queryParams);
|
|
145
|
+
const fetchOptions = buildFetchOptions("POST", options, bodyJson);
|
|
146
|
+
if (basicAuthUsername) {
|
|
147
|
+
fetchOptions.headers["Authorization"] = "Basic " + encode(basicAuthUsername + ":" + basicAuthPassword);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return Promise.race([
|
|
151
|
+
fetch(fullUrl, fetchOptions).then((response) => {
|
|
152
|
+
if (response.status == 204) {
|
|
153
|
+
return response;
|
|
154
|
+
} else if (response.status >= 200 && response.status < 300) {
|
|
155
|
+
return response.json();
|
|
156
|
+
} else {
|
|
157
|
+
throw response;
|
|
158
|
+
}
|
|
159
|
+
}),
|
|
160
|
+
new Promise((_, reject) =>
|
|
161
|
+
setTimeout(reject, this.timeoutMs, "Request timed out.")
|
|
162
|
+
),
|
|
163
|
+
]);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Log out a session, invalidate a refresh token, or log out all sessions/refresh tokens for a user. */
|
|
167
|
+
mezonAuthenticateLogout(bearerToken: string,
|
|
168
|
+
body:ApiAuthenticateLogoutRequest,
|
|
169
|
+
options: any = {}): Promise<any> {
|
|
170
|
+
|
|
171
|
+
if (body === null || body === undefined) {
|
|
172
|
+
throw new Error("'body' is a required parameter but is null or undefined.");
|
|
173
|
+
}
|
|
174
|
+
const urlPath = "/v2/apps/authenticate/logout";
|
|
175
|
+
const queryParams = new Map<string, any>();
|
|
176
|
+
|
|
177
|
+
let bodyJson : string = "";
|
|
178
|
+
bodyJson = JSON.stringify(body || {});
|
|
179
|
+
|
|
180
|
+
const fullUrl = this.buildFullUrl(this.basePath, urlPath, queryParams);
|
|
181
|
+
const fetchOptions = buildFetchOptions("POST", options, bodyJson);
|
|
182
|
+
if (bearerToken) {
|
|
183
|
+
fetchOptions.headers["Authorization"] = "Bearer " + bearerToken;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return Promise.race([
|
|
187
|
+
fetch(fullUrl, fetchOptions).then((response) => {
|
|
188
|
+
if (response.status == 204) {
|
|
189
|
+
return response;
|
|
190
|
+
} else if (response.status >= 200 && response.status < 300) {
|
|
191
|
+
return response.json();
|
|
192
|
+
} else {
|
|
193
|
+
throw response;
|
|
194
|
+
}
|
|
195
|
+
}),
|
|
196
|
+
new Promise((_, reject) =>
|
|
197
|
+
setTimeout(reject, this.timeoutMs, "Request timed out.")
|
|
198
|
+
),
|
|
199
|
+
]);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Refresh a user's session using a refresh token retrieved from a previous authentication request. */
|
|
203
|
+
mezonAuthenticateRefresh(basicAuthUsername: string,
|
|
204
|
+
basicAuthPassword: string,
|
|
205
|
+
body:ApiAuthenticateRefreshRequest,
|
|
206
|
+
options: any = {}): Promise<ApiSession> {
|
|
207
|
+
|
|
208
|
+
if (body === null || body === undefined) {
|
|
209
|
+
throw new Error("'body' is a required parameter but is null or undefined.");
|
|
210
|
+
}
|
|
211
|
+
const urlPath = "/v2/apps/authenticate/refresh";
|
|
212
|
+
const queryParams = new Map<string, any>();
|
|
213
|
+
|
|
214
|
+
let bodyJson : string = "";
|
|
215
|
+
bodyJson = JSON.stringify(body || {});
|
|
216
|
+
|
|
217
|
+
const fullUrl = this.buildFullUrl(this.basePath, urlPath, queryParams);
|
|
218
|
+
const fetchOptions = buildFetchOptions("POST", options, bodyJson);
|
|
219
|
+
if (basicAuthUsername) {
|
|
220
|
+
fetchOptions.headers["Authorization"] = "Basic " + encode(basicAuthUsername + ":" + basicAuthPassword);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return Promise.race([
|
|
224
|
+
fetch(fullUrl, fetchOptions).then((response) => {
|
|
225
|
+
if (response.status == 204) {
|
|
226
|
+
return response;
|
|
227
|
+
} else if (response.status >= 200 && response.status < 300) {
|
|
228
|
+
return response.json();
|
|
229
|
+
} else {
|
|
230
|
+
throw response;
|
|
231
|
+
}
|
|
232
|
+
}),
|
|
233
|
+
new Promise((_, reject) =>
|
|
234
|
+
setTimeout(reject, this.timeoutMs, "Request timed out.")
|
|
235
|
+
),
|
|
236
|
+
]);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Deletes a message for an identity. */
|
|
240
|
+
mezonDeleteMessage(bearerToken: string,
|
|
241
|
+
id:string,
|
|
242
|
+
options: any = {}): Promise<any> {
|
|
243
|
+
|
|
244
|
+
if (id === null || id === undefined) {
|
|
245
|
+
throw new Error("'id' is a required parameter but is null or undefined.");
|
|
246
|
+
}
|
|
247
|
+
const urlPath = "/v2/apps/message/{id}"
|
|
248
|
+
.replace("{id}", encodeURIComponent(String(id)));
|
|
249
|
+
const queryParams = new Map<string, any>();
|
|
250
|
+
|
|
251
|
+
let bodyJson : string = "";
|
|
252
|
+
|
|
253
|
+
const fullUrl = this.buildFullUrl(this.basePath, urlPath, queryParams);
|
|
254
|
+
const fetchOptions = buildFetchOptions("DELETE", options, bodyJson);
|
|
255
|
+
if (bearerToken) {
|
|
256
|
+
fetchOptions.headers["Authorization"] = "Bearer " + bearerToken;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return Promise.race([
|
|
260
|
+
fetch(fullUrl, fetchOptions).then((response) => {
|
|
261
|
+
if (response.status == 204) {
|
|
262
|
+
return response;
|
|
263
|
+
} else if (response.status >= 200 && response.status < 300) {
|
|
264
|
+
return response.json();
|
|
265
|
+
} else {
|
|
266
|
+
throw response;
|
|
267
|
+
}
|
|
268
|
+
}),
|
|
269
|
+
new Promise((_, reject) =>
|
|
270
|
+
setTimeout(reject, this.timeoutMs, "Request timed out.")
|
|
271
|
+
),
|
|
272
|
+
]);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Updates a message for an identity. */
|
|
276
|
+
mezonUpdateMessage(bearerToken: string,
|
|
277
|
+
id:string,
|
|
278
|
+
body:ApiUpdateMessageRequest,
|
|
279
|
+
options: any = {}): Promise<any> {
|
|
280
|
+
|
|
281
|
+
if (id === null || id === undefined) {
|
|
282
|
+
throw new Error("'id' is a required parameter but is null or undefined.");
|
|
283
|
+
}
|
|
284
|
+
if (body === null || body === undefined) {
|
|
285
|
+
throw new Error("'body' is a required parameter but is null or undefined.");
|
|
286
|
+
}
|
|
287
|
+
const urlPath = "/v2/apps/message/{id}"
|
|
288
|
+
.replace("{id}", encodeURIComponent(String(id)));
|
|
289
|
+
const queryParams = new Map<string, any>();
|
|
290
|
+
|
|
291
|
+
let bodyJson : string = "";
|
|
292
|
+
bodyJson = JSON.stringify(body || {});
|
|
293
|
+
|
|
294
|
+
const fullUrl = this.buildFullUrl(this.basePath, urlPath, queryParams);
|
|
295
|
+
const fetchOptions = buildFetchOptions("PUT", options, bodyJson);
|
|
296
|
+
if (bearerToken) {
|
|
297
|
+
fetchOptions.headers["Authorization"] = "Bearer " + bearerToken;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return Promise.race([
|
|
301
|
+
fetch(fullUrl, fetchOptions).then((response) => {
|
|
302
|
+
if (response.status == 204) {
|
|
303
|
+
return response;
|
|
304
|
+
} else if (response.status >= 200 && response.status < 300) {
|
|
305
|
+
return response.json();
|
|
306
|
+
} else {
|
|
307
|
+
throw response;
|
|
308
|
+
}
|
|
309
|
+
}),
|
|
310
|
+
new Promise((_, reject) =>
|
|
311
|
+
setTimeout(reject, this.timeoutMs, "Request timed out.")
|
|
312
|
+
),
|
|
313
|
+
]);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
buildFullUrl(basePath: string, fragment: string, queryParams: Map<string, any>) {
|
|
317
|
+
let fullPath = basePath + fragment + "?";
|
|
318
|
+
|
|
319
|
+
for (let [k, v] of queryParams) {
|
|
320
|
+
if (v instanceof Array) {
|
|
321
|
+
fullPath += v.reduce((prev: any, curr: any) => {
|
|
322
|
+
return prev + encodeURIComponent(k) + "=" + encodeURIComponent(curr) + "&";
|
|
323
|
+
}, "");
|
|
324
|
+
} else {
|
|
325
|
+
if (v != null) {
|
|
326
|
+
fullPath += encodeURIComponent(k) + "=" + encodeURIComponent(v) + "&";
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return fullPath;
|
|
332
|
+
}
|
|
333
|
+
};
|