mezon-sdk 2.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.
- package/README.md +143 -0
- package/api.gen.ts +783 -0
- package/build.mjs +45 -0
- package/client.ts +327 -0
- package/dist/api.gen.d.ts +157 -0
- package/dist/client.d.ts +74 -0
- package/dist/index.d.ts +18 -0
- package/dist/satori-js.cjs.js +1456 -0
- package/dist/satori-js.esm.mjs +1436 -0
- package/dist/satori-js.iife.js +1458 -0
- package/dist/satori-js.umd.js +1956 -0
- package/dist/session.d.ts +52 -0
- package/dist/utils.d.ts +3 -0
- package/index.ts +20 -0
- package/package.json +49 -0
- package/rollup.config.js +40 -0
- package/session.ts +107 -0
- package/tsconfig.base.json +33 -0
- package/tsconfig.json +10 -0
- package/utils.ts +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
Satori JavaScript Client
|
|
2
|
+
========================
|
|
3
|
+
|
|
4
|
+
> JavaScript client for Satori server written in TypeScript. For browser and React Native projects.
|
|
5
|
+
|
|
6
|
+
This client implements the full API for interacting with Satori 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://heroiclabs.com/docs/javascript-client-guide
|
|
9
|
+
|
|
10
|
+
## Getting Started
|
|
11
|
+
|
|
12
|
+
You'll need access to an instance of the Satori 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 Satori server you must provide an identifier for the user.
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const userId = "<UniqueUserId>";
|
|
39
|
+
|
|
40
|
+
client.authenticate(userId)
|
|
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 Satori 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.heroiclabs.com).
|
|
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 `satori-js` 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 Satori 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 "@heroiclabs" 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/heroiclabs/nakama-js/blob/master/LICENSE).
|