@accelbyte/sdk 3.0.8 → 4.0.0
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 +572 -2
- package/dist/cjs/node/index.cjs +901 -826
- package/dist/cjs/node/index.cjs.map +1 -1
- package/dist/es/browser/index.browser.js +839 -787
- package/dist/es/browser/index.browser.js.map +1 -1
- package/dist/es/node/index.node.js +845 -789
- package/dist/es/node/index.node.js.map +1 -1
- package/dist/index.d.ts +295 -237
- package/package.json +40 -12
package/README.md
CHANGED
|
@@ -1,4 +1,574 @@
|
|
|
1
|
-
# AccelByte
|
|
1
|
+
# AccelByte Gaming Services (AGS) TypeScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Note**: The AccelByte Gaming Service (AGS Typescript SDK is still under development. Some Extend SDK features may not be available yet.
|
|
4
4
|
|
|
5
|
+
The AGS TypeScript SDK is a versatile library for building web applications using AccelByte API services. It is platform-agnostic, making it suitable for both browser and server environments. Developed with TypeScript, the SDK emphasizes strict type safety, providing robust runtime type-checking to help you confidently work with AccelByte Gaming Service (AGS) type definitions.
|
|
6
|
+
|
|
7
|
+
This guide will walk you through getting started with the AGS TypeScript SDK, including installation and usage instructions.
|
|
8
|
+
|
|
9
|
+
## Installing AGS TypeScript SDK
|
|
10
|
+
|
|
11
|
+
We recommend installing the TypeScript SDK via npm using your preferred package manager. Open your terminal and run this command:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @accelbyte/sdk @accelbyte/sdk-iam @accelbyte/sdk-basic # Add more SDK packages as needed
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
>**Important:** You must specifically install `@accelbyte/sdk` in your project as it has been moved to `peerDependencies` in other `@accelbyte/sdk-*` packages. This ensures that the core SDK is shared across all related modules.
|
|
18
|
+
|
|
19
|
+
*Optional*: If you plan to use our generated React Query hooks, you'll also need to install `@tanstack/query`. Note that we currently only support v4.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @tanstack/react-query@4.36.1
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Refer to the [AccelByte APIs](#accelbyte-apis) section to see the full list of services and SDK offered by AccelByte.
|
|
26
|
+
|
|
27
|
+
## Using AGS Typescript SDK
|
|
28
|
+
|
|
29
|
+
To begin using the AGS TypeScript SDK, you'll need to instantiate it with some basic configuration, including an IAM Client ID (`clientId`) and other core settings. Depending on your application, you may want to store environment variables in a `.env` file and load them using your development tools (e.g., via `process.env.ENV_KEY`).
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import { AccelByte } from '@accelbyte/sdk';
|
|
33
|
+
|
|
34
|
+
const sdk = await AccelByte.SDK({
|
|
35
|
+
coreConfig: {
|
|
36
|
+
baseURL: "<Your publisher base URL, e.g. <https://prod.gamingservices.accelbyte.io>>",
|
|
37
|
+
clientId: "<Your publisher Client ID, e.g. 32_char_guid>",
|
|
38
|
+
redirectURI: "<Your publisher Redirect URL, e.g. <https://prod.gamingservices.accelbyte.io>>",
|
|
39
|
+
namespace: "<Your publisher namespace, e.g. 'accelbyte'>"
|
|
40
|
+
},
|
|
41
|
+
axiosConfig: {
|
|
42
|
+
interceptors: [
|
|
43
|
+
{
|
|
44
|
+
type: 'response',
|
|
45
|
+
name: 'disconnected',
|
|
46
|
+
onError: e => {
|
|
47
|
+
const error = e as AxiosError;
|
|
48
|
+
const { config, response } = error;
|
|
49
|
+
if (!response) {
|
|
50
|
+
console.warn(`sdk:ERR_INTERNET_DISCONNECTED ${config?.baseURL}${config?.url}. ${error.message}\n`);
|
|
51
|
+
}
|
|
52
|
+
return Promise.reject(error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
],
|
|
56
|
+
request: {
|
|
57
|
+
withCredentials: true // If true, cookies will be sent automatically with requests.
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Configuration breakdown
|
|
64
|
+
|
|
65
|
+
>**Note:** When setting up IAM Clients, ensure that you include an additional Redirect URI with the format `{studio-namespace}.prod.gamingservices.accelbyte.io` (e.g., `accelbyte.prod.gamingservices.accelbyte.io`).
|
|
66
|
+
|
|
67
|
+
- **coreConfig**:
|
|
68
|
+
- `baseURL`: The base URL for your AGS deployment. This is the starting point for all API requests.
|
|
69
|
+
- `clientId`: The client ID. This is required for authentication with the IAM service. For more information about creating IAM clients, refer to the [AGS Documentation Portal](https://docs.accelbyte.io/gaming-services/services/access/authorization/manage-access-control-for-applications/#create-an-iam-client).
|
|
70
|
+
- `redirectURI`: The URL to which users will be redirected after logging in.
|
|
71
|
+
- `namespace`: The namespace for your AGS deployment, which helps in scoping API requests.
|
|
72
|
+
|
|
73
|
+
- **axiosConfig** (optional):
|
|
74
|
+
- `interceptors`: Custom Axios interceptors to manage request and response behaviors. For example, you can add an interceptor to handle network errors.
|
|
75
|
+
- `request`: Configuration for Axios request, such as setting `withCredentials` to `true` to include cookies automatically. For more information, refer to the [Axios documentation](https://axios-http.com/docs/req_config). Using cookies can help prevent unauthorized access, protect against Cross-Site Request Forgery (CSRF) attacks, and ensure that resources are only accessed by legitimate referrers.
|
|
76
|
+
|
|
77
|
+
## Interacting with AGS
|
|
78
|
+
|
|
79
|
+
This is a sample code snippet demonstrating how to use the SDK to interact with AGS:
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
import { IamUserAuthorizationClient } from '@accelbyte/sdk-iam';
|
|
83
|
+
import { UserProfileApi } from '@accelbyte/sdk-basic';
|
|
84
|
+
// Log in to IAM using an authorization code
|
|
85
|
+
const token = await new IamUserAuthorizationClient(sdk).loginWithAuthorizationCode({ code, codeVerifier });
|
|
86
|
+
// Retrieve the current user profile
|
|
87
|
+
const userProfile = await UsersAdminApi(sdk).getUsersMe_v3();
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Integrating React Query
|
|
91
|
+
|
|
92
|
+
>**Note:** This is a browser-only integration.
|
|
93
|
+
|
|
94
|
+
If you're using `@tanstack/react-query` to manage server state, the AGS SDK offers seamless integration through generated queries and mutations. This allows you to efficiently fetch, update, and manage data while leveraging React Query's powerful features like caching, automatic retries, and query invalidation.
|
|
95
|
+
|
|
96
|
+
### Query example
|
|
97
|
+
|
|
98
|
+
To retrieve data using a generated query, you can use one of the SDK's hooks. The query key used in invalidation can be found within the generated queries.
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
import { useUsersAdminApi_GetUsersMe_v3 } from '@accelbyte/sdk-iam';
|
|
102
|
+
|
|
103
|
+
const usersQuery = useUsersAdminApi_GetUsersMe_v3(
|
|
104
|
+
sdk,
|
|
105
|
+
{ coreConfig: { namespace }, axiosConfig: {} },
|
|
106
|
+
{
|
|
107
|
+
retry: 2, // Retry the request twice before failing
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
console.log(usersQuery.data);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Each generated query comes with unique query keys that you can use for query invalidation when it's necessary to refresh the data. You can view these keys by hovering over the generated query, as shown below:
|
|
115
|
+
|
|
116
|
+

|
|
117
|
+
|
|
118
|
+
### Mutation Example
|
|
119
|
+
|
|
120
|
+
To perform data mutations (e.g., creating, updating, or deleting resources), use one of the generated mutation hooks:
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
import { useUsersAdminApi_UpdateUser_v3 } from '@accelbyte/sdk-iam';
|
|
124
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
125
|
+
|
|
126
|
+
const queryClient = useQueryClient();
|
|
127
|
+
|
|
128
|
+
const updateUserMutation = useUsersAdminApi_UpdateUserMutation_v3(
|
|
129
|
+
sdk,
|
|
130
|
+
{
|
|
131
|
+
onSuccess: (data) => {
|
|
132
|
+
console.log('User data updated:', data);
|
|
133
|
+
// Invalidate the query to refetch the updated data
|
|
134
|
+
queryClient.invalidateQueries({queryKey: [Key_UsersAdmin.User_v3]);
|
|
135
|
+
},
|
|
136
|
+
onError: (error) => console.error('Error updating user data:', error),
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
updateUserMutation.mutate({ namespace, data: {/* updated user data */} });
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
In the example above, after a successful mutation, the corresponding query is invalidated using `queryClient.invalidateQueries({queryKey: [Key_UsersAdmin.User_v3] })`. This triggers a refetch to ensure the UI reflects the latest data.
|
|
144
|
+
|
|
145
|
+
These hooks provide a robust way to interact with AGS, making it easier to handle data fetching and state management within your React applications.
|
|
146
|
+
|
|
147
|
+
## Custom Network Calls
|
|
148
|
+
|
|
149
|
+
The SDK exposes public endpoints for common operations, but if you need to make a custom network call, you can do so using the SDK's network capabilities:
|
|
150
|
+
|
|
151
|
+
```js
|
|
152
|
+
async function customNetworkCall() {
|
|
153
|
+
try {
|
|
154
|
+
const url = '/iam/v3/public/users/me';
|
|
155
|
+
const network = Network.create(sdk.assembly().axiosConfig.request);
|
|
156
|
+
return await network.get(url);
|
|
157
|
+
} catch (err) {
|
|
158
|
+
console.error(err);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
This flexibility allows you to extend the SDK for specific use cases beyond the provided API methods.
|
|
164
|
+
|
|
165
|
+
## AGS SDK API
|
|
166
|
+
|
|
167
|
+
### AGS SDK Constructor
|
|
168
|
+
|
|
169
|
+
When creating an instance of the AGS SDK using `AccelByte.SDK(config)`, you need to provide a configuration object that adheres to the `SdkConstructorParam` interface.
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
export interface SdkConstructorParam {
|
|
173
|
+
coreConfig: MakeOptional<CoreConfig, 'useSchemaValidation'>
|
|
174
|
+
axiosConfig?: AxiosConfig
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
This configuration consists of two main sections: `coreConfig` and `axiosConfig`. Here's a detailed breakdown of each section:
|
|
179
|
+
|
|
180
|
+
#### coreConfig
|
|
181
|
+
|
|
182
|
+
This is a required object that contains the core configuration for the SDK. It includes essential settings like `clientId`, `redirectURI`, `baseURL`, and `namespace`.
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
export interface CoreConfig {
|
|
186
|
+
clientId: string;
|
|
187
|
+
redirectURI: string;
|
|
188
|
+
baseURL: string;
|
|
189
|
+
namespace: string;
|
|
190
|
+
useSchemaValidation?: boolean;
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
The following table describes each parameter's function, type, and default value.
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
| Parameter | Type | Description | Default Value |
|
|
198
|
+
|-----------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|
|
|
199
|
+
| `clientId` (required) | string | The client ID associated with your SDK. You can obtain this from the AGS Admin Portal under OAuth Clients **Game Setup** > **Games and Apps** > **IAM Clients**. | |
|
|
200
|
+
| `redirectURI ` (required) | string | The URI to redirect to after a successful login. This value is crucial for generating a valid login URL to IAM and should match the redirect URI configured in the Admin Portal. | |
|
|
201
|
+
| `baseURL` (required) | string | The URI to redirect to after a successful login. This value is crucial for generating a valid login URL to IAM and should match the redirect URI configured in the Admin Portal. | |
|
|
202
|
+
| `namespace` (required) | string | The namespace associated with your AGS deployment. | |
|
|
203
|
+
| `useSchemaValidation` | boolean | Determines whether the SDK enforces the Zod schema validation at runtime. Setting this to `false` will bypass validation, which might be useful to prevent throwing errors if there are schema mismatches between the Zod model and the backend response. | true |
|
|
204
|
+
|
|
205
|
+
#### axiosConfig (optional)
|
|
206
|
+
|
|
207
|
+
This object allows you to customize the Axios instance used by the SDK, such as adding interceptors or modifying request configurations.
|
|
208
|
+
|
|
209
|
+
```js
|
|
210
|
+
export interface AxiosConfig {
|
|
211
|
+
interceptors?: Interceptor[];
|
|
212
|
+
request?: AxiosRequestConfig;
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
- `interceptors` (`Interceptor[]`, optional): An array of interceptor objects that can modify requests or responses. These interceptors provide hooks for adding custom behavior before a request is sent or after a response is received.
|
|
217
|
+
|
|
218
|
+
```js
|
|
219
|
+
export type Interceptor =
|
|
220
|
+
| {
|
|
221
|
+
type: 'request';
|
|
222
|
+
name: string;
|
|
223
|
+
onRequest?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
|
|
224
|
+
onError?: (error: unknown) => unknown;
|
|
225
|
+
}
|
|
226
|
+
| {
|
|
227
|
+
type: 'response';
|
|
228
|
+
name: string;
|
|
229
|
+
onSuccess?: (response: AxiosResponse<unknown>) => AxiosResponse<unknown>;
|
|
230
|
+
onError?: (error: unknown) => unknown;
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
The following table describes each parameter's function, type, and default value.
|
|
235
|
+
|
|
236
|
+
| Parameter | Type | Description | Default Value |
|
|
237
|
+
|-----------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|
|
|
238
|
+
| `type` (required) | `'request' \| 'response'` | Specifies whether the interceptor should handle requests or responses. | |
|
|
239
|
+
| `name` (required) | string | A unique name for the interceptor. This helps in identifying the interceptor, especially if you have multiple interceptors. | |
|
|
240
|
+
| `onRequest` | `(config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig \| Promise<InternalAxiosRequestConfig>` | A function that processes the request configuration before the request is sent. This can be used to modify the request (e.g., adding headers) or to log the request details. | |
|
|
241
|
+
| `onError` | `(error: unknown) => unknown` | A function that handles errors during the request or response process. This is useful for logging errors or retrying requests under certain conditions. | |
|
|
242
|
+
| `onSuccess` | `(response: AxiosResponse<unknown>) => AxiosResponse<unknown>` | A function that processes the response before it’s passed back to the calling code. This can be used to transform the response data or log successful requests. | |
|
|
243
|
+
|
|
244
|
+
- `request` (`AxiosRequestConfig`, optional): A configuration object that allows you to set default properties for Axios request, such as headers, timeout settings, or credentials handling. For more information, refer to the [Axios documentation](https://axios-http.com/docs/req_config).
|
|
245
|
+
|
|
246
|
+
### API Class
|
|
247
|
+
|
|
248
|
+
The API classes in the AGS TypeScript SDK, such as `*Api` for public endpoints and `*AdminApi` for admin endpoints, are constructed to interact with AGS. These classes offer methods to perform API operations, handling query parameters, request bodies, and returning the corresponding responses.
|
|
249
|
+
|
|
250
|
+
The API classes are instantiated using the following constructor:
|
|
251
|
+
|
|
252
|
+
```js
|
|
253
|
+
constructor(sdk: AccelByteSDK, args?: SdkSetConfigParam)
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
The following table describes each parameter's function, type, and default value.
|
|
257
|
+
|
|
258
|
+
| Parameter | Type | Description | Default Value |
|
|
259
|
+
|-----------|--------|---------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|
|
|
260
|
+
| `sdk` (required) | `AccelByteSDK` | An instance of the AGS SDK | |
|
|
261
|
+
| `args` | `SdkSetConfigParam` | An optional parameter that allows you to override or extend the configuration of the SDK. This includes the core configuration and Axios configuration. | |
|
|
262
|
+
|
|
263
|
+
## AGS APIs
|
|
264
|
+
|
|
265
|
+
TypeScript SDK supports the following AGS APIs:
|
|
266
|
+
|
|
267
|
+
- **Access**
|
|
268
|
+
- [IAM](https://docs.accelbyte.io/api-explorer/#IAM): `@accelbyte/sdk-iam`
|
|
269
|
+
- [GDPR](https://docs.accelbyte.io/api-explorer/#GDPR): `@accelbyte/sdk-event`
|
|
270
|
+
- [Event Log](https://docs.accelbyte.io/api-explorer/#Event%20Log): `@accelbyte/sdk-event`
|
|
271
|
+
- [Legal](https://docs.accelbyte.io/api-explorer/#Legal): `@accelbyte/sdk-legal`
|
|
272
|
+
- **Analytics**
|
|
273
|
+
- [Game Telemetry](https://docs.accelbyte.io/api-explorer/#Telemetry): `@accelbyte/sdk-gametelemetry`
|
|
274
|
+
- **[Basic](https://docs.accelbyte.io/api-explorer/#Basic)**: `@accelbyte/sdk-basic`
|
|
275
|
+
- **Engagement**
|
|
276
|
+
- [Achievement](https://docs.accelbyte.io/api-explorer/#Achievement): `@accelbyte/sdk-achievement`
|
|
277
|
+
- [Challenge](https://docs.accelbyte.io/api-explorer/#Challenge): `@accelbyte/sdk-challenge`
|
|
278
|
+
- [Leaderboard](https://docs.accelbyte.io/api-explorer/#Leaderboard): `@accelbyte/sdk-leaderboard`
|
|
279
|
+
- [User-generated Content (UGC)](https://docs.accelbyte.io/api-explorer/#User%20Generated%20Content): `@accelbyte/sdk-ugc`
|
|
280
|
+
- [SeasonPass](https://docs.accelbyte.io/api-explorer/#SeasonPass): `@accelbyte/sdk-seasonpass`
|
|
281
|
+
- [Reporting](https://docs.accelbyte.io/api-explorer/#Reporting): `@accelbyte/sdk-reporting`
|
|
282
|
+
- **Monetization**
|
|
283
|
+
- [Platform Store](hhttps://docs.accelbyte.io/api-explorer/?api=Platform%20Store#Platform%20Store): `@accelbyte/sdk-platform`
|
|
284
|
+
- **Play**
|
|
285
|
+
- [Session](https://docs.accelbyte.io/api-explorer/#Session): `@accelbyte/sdk-session`
|
|
286
|
+
- [Matchmaking](https://docs.accelbyte.io/api-explorer/#Matchmaking):`@accelbyte/sdk-matchmaking`
|
|
287
|
+
- [Session Browser Play](https://docs.accelbyte.io/api-explorer/#Session%20Browser%20Play): `@accelbyte/sdk-sessionbrowser`
|
|
288
|
+
- [Session History](https://docs.accelbyte.io/api-explorer/#Session%20History): `@accelbyte/sdk-history`
|
|
289
|
+
- **Social**
|
|
290
|
+
- [Chat](https://docs.accelbyte.io/api-explorer/#Chat): `@accelbyte/sdk-chat`
|
|
291
|
+
- [Lobby](https://docs.accelbyte.io/api-explorer/#Lobby%20-%20Friends,%20Presence%20and%20Notifications): `@accelbyte/sdk-lobby`
|
|
292
|
+
- [Groups](https://docs.accelbyte.io/api-explorer/#Groups): `@accelbyte/sdk-groups`
|
|
293
|
+
- **Storage**
|
|
294
|
+
- [CloudSave](https://docs.accelbyte.io/api-explorer/#CloudSave): `@accelbyte/sdk-cloudsave`
|
|
295
|
+
- [Inventory](https://docs.accelbyte.io/api-explorer/#Inventory): `@accelbyte/sdk-inventory`
|
|
296
|
+
- [Statistics](https://docs.accelbyte.io/api-explorer/#Statistics): `@accelbyte/sdk-social`
|
|
297
|
+
- `Add-ons`
|
|
298
|
+
- [AMS](https://docs.accelbyte.io/api-explorer/#AMS): `@accelbyte/sdk-ams`
|
|
299
|
+
|
|
300
|
+
Check out the [AGS Documentation Portal](https://docs.accelbyte.io/gaming-services/services/) to learn more about the functions of these AGS services and how to integrate them into your game.
|
|
301
|
+
|
|
302
|
+
## Migrating from V3 guidelines
|
|
303
|
+
|
|
304
|
+
### SDK initialization
|
|
305
|
+
|
|
306
|
+
SDK now needs to be initialized with two fields: `coreConfig` and `axiosConfig`. This distinction clarifies which configurations apply to each component.
|
|
307
|
+
|
|
308
|
+
```js
|
|
309
|
+
// Before
|
|
310
|
+
const sdk = Accelbyte.SDK({
|
|
311
|
+
options: {
|
|
312
|
+
baseURL: 'http://localhost:3030/api',
|
|
313
|
+
clientId: '77f88506b6174c3ea4d925f5b4096ce8',
|
|
314
|
+
namespace: 'accelbyte',
|
|
315
|
+
redirectURI: 'http://localhost:3030'
|
|
316
|
+
},
|
|
317
|
+
onEvents: {
|
|
318
|
+
onGetUserSession: (accessToken, refreshToken) => {
|
|
319
|
+
console.log('SDK Event: onGetUserSession', { accessToken, refreshToken })
|
|
320
|
+
},
|
|
321
|
+
onSessionExpired: () => {
|
|
322
|
+
console.log('SDK Event: onSessionExpired')
|
|
323
|
+
},
|
|
324
|
+
onError: error => {
|
|
325
|
+
console.error('SDK Event: onError', error.response)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
// After
|
|
331
|
+
const sdk = AccelByte.SDK({
|
|
332
|
+
coreConfig: {
|
|
333
|
+
baseURL: 'http://localhost:3030/api',
|
|
334
|
+
clientId: '77f88506b6174c3ea4d925f5b4096ce8',
|
|
335
|
+
namespace: 'accelbyte',
|
|
336
|
+
redirectURI: 'http://localhost:3030'
|
|
337
|
+
},
|
|
338
|
+
axiosConfig: {
|
|
339
|
+
interceptors: [],
|
|
340
|
+
requests: {}
|
|
341
|
+
}
|
|
342
|
+
})
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### Return response
|
|
346
|
+
|
|
347
|
+
API function calls are now returning `{ data, headers, status }` instead of just the data.
|
|
348
|
+
|
|
349
|
+
```js
|
|
350
|
+
- const data = await UsersAdminApi(sdk).getUsersMe_v3();
|
|
351
|
+
+ const { data, headers, status } = await UsersAdminApi(sdk).getUsersMe_v3();
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### Events and Interceptors
|
|
355
|
+
|
|
356
|
+
The AGS SDK no longer has the ability to provide event listeners, e.g. `onSessionRenewed`. This capability is replaced by interceptors. Axios interceptors are now attached to the `axiosInstance` in the SDK instead of the global axios object. For example:
|
|
357
|
+
|
|
358
|
+
```js
|
|
359
|
+
// Before
|
|
360
|
+
const sdk = Accelbyte.SDK({
|
|
361
|
+
options,
|
|
362
|
+
onEvents: {
|
|
363
|
+
onGetUserSession: (accessToken, refreshToken) => {
|
|
364
|
+
console.log('SDK Event: onGetUserSession', { accessToken, refreshToken })
|
|
365
|
+
},
|
|
366
|
+
onSessionExpired: () => {
|
|
367
|
+
console.log('SDK Event: onSessionExpired')
|
|
368
|
+
},
|
|
369
|
+
onError: error => {
|
|
370
|
+
console.error('SDK Event: onError', error.response)
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
// After
|
|
376
|
+
const sdk = AccelByte.SDK({
|
|
377
|
+
coreConfig: {
|
|
378
|
+
baseURL: 'http://localhost:3030/api',
|
|
379
|
+
clientId: '77f88506b6174c3ea4d925f5b4096ce8',
|
|
380
|
+
namespace: 'accelbyte',
|
|
381
|
+
redirectURI: 'http://localhost:3030'
|
|
382
|
+
},
|
|
383
|
+
axiosConfig: {
|
|
384
|
+
interceptors: [
|
|
385
|
+
createAuthInterceptor({
|
|
386
|
+
clientId,
|
|
387
|
+
onSessionExpired,
|
|
388
|
+
onGetUserSession,
|
|
389
|
+
getRefreshToken
|
|
390
|
+
}),
|
|
391
|
+
createCustomPathInterceptor(),
|
|
392
|
+
{
|
|
393
|
+
type: 'response',
|
|
394
|
+
name: 'cancelled',
|
|
395
|
+
onError: e => {
|
|
396
|
+
const error = e as AxiosError
|
|
397
|
+
if (axios.isCancel(error)) {
|
|
398
|
+
// Expected case, exit
|
|
399
|
+
return Promise.reject(error)
|
|
400
|
+
}
|
|
401
|
+
return Promise.reject(error)
|
|
402
|
+
}
|
|
403
|
+
},
|
|
404
|
+
// Add more interceptors
|
|
405
|
+
],
|
|
406
|
+
requests: {}
|
|
407
|
+
}
|
|
408
|
+
})
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
#### Interceptors
|
|
412
|
+
|
|
413
|
+
We now offer custom interceptors to replace certain events or options. Here's a list of interceptors available for event and option replacement:
|
|
414
|
+
|
|
415
|
+
- **onSessionExpired** and **onGetUserSession** (**createAuthInterceptor**)
|
|
416
|
+
|
|
417
|
+
```js
|
|
418
|
+
createAuthInterceptor({
|
|
419
|
+
/**
|
|
420
|
+
* The client ID used by the SDK, obtained from the Admin Portal under OAuth Clients.
|
|
421
|
+
*/
|
|
422
|
+
clientId: string
|
|
423
|
+
/**
|
|
424
|
+
* An optional array of URLs that should be ignored when handling session expiration.
|
|
425
|
+
* Default to `['/iam/v3/oauth/token', '/iam/v4/oauth/token', '/iam/v3/oauth/revoke']`
|
|
426
|
+
*/
|
|
427
|
+
expectedErrorUrls?: string[]
|
|
428
|
+
/**
|
|
429
|
+
* A callback function that retrieves the current refresh token.
|
|
430
|
+
*/
|
|
431
|
+
getRefreshToken: () => string | undefined
|
|
432
|
+
/**
|
|
433
|
+
* The URL endpoint for obtaining a new token. Defaults to `'/iam/v3/oauth/token'`.
|
|
434
|
+
*/
|
|
435
|
+
tokenUrl?: GrantTokenUrlString
|
|
436
|
+
/**
|
|
437
|
+
* A callback function triggered when the session has expired.
|
|
438
|
+
*/
|
|
439
|
+
onSessionExpired: () => void
|
|
440
|
+
/**
|
|
441
|
+
* A callback function triggered when successfully get new session.
|
|
442
|
+
*/
|
|
443
|
+
onGetUserSession?: (accessToken: string, refreshToken: string) => void
|
|
444
|
+
})
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
- **onUserEligibilityChange**
|
|
448
|
+
|
|
449
|
+
>**Note**: Since there's no dependency on the Internal SDK and it's specific to the application level, we have not created a custom wrapper for it.
|
|
450
|
+
|
|
451
|
+
```js
|
|
452
|
+
{
|
|
453
|
+
type: 'response',
|
|
454
|
+
name: 'user-eligibilitiy-change',
|
|
455
|
+
onError: e => {
|
|
456
|
+
const error = e as AxiosError<any>
|
|
457
|
+
if (error.response) {
|
|
458
|
+
const { response } = error
|
|
459
|
+
if (response?.status === 403 && (response?.config as any).url.includes(process.env.BASE_URL) && response?.config.withCredentials) {
|
|
460
|
+
if (response.data.errorCode === ERROR_ELIGIBILITY_CODE) {
|
|
461
|
+
// Do something when there's an error with ERROR_ELIGIBILITY_CODE
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
return Promise.reject(error)
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
- **onTooManyRequest**
|
|
471
|
+
|
|
472
|
+
>**Note**: Since there's no dependency on the Internal SDK and it's specific to the application level, we have not created a custom wrapper for it.
|
|
473
|
+
|
|
474
|
+
```js
|
|
475
|
+
{
|
|
476
|
+
type: 'response',
|
|
477
|
+
name: 'too-many-request',
|
|
478
|
+
onError: e => {
|
|
479
|
+
const error = e as AxiosError<any>
|
|
480
|
+
if (error.response) {
|
|
481
|
+
const { response } = error
|
|
482
|
+
if (response?.status === 429 /* TooManyRequests */) {
|
|
483
|
+
// onTooManyRequest(error)
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
return Promise.reject(error)
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
- **useInternalNetwork** (**createCustomPathInterceptor**)
|
|
492
|
+
|
|
493
|
+
```js
|
|
494
|
+
createCustomPathInterceptor({
|
|
495
|
+
/**
|
|
496
|
+
* A list of objects specifying which service base paths should be replaced.
|
|
497
|
+
* For example, providing `{'/iam': '/iam-test'}` will redirect all `'/iam'` requests to `'/iam-test'`.
|
|
498
|
+
*/
|
|
499
|
+
basePath: Partial<Record<BasePath, string>>
|
|
500
|
+
/**
|
|
501
|
+
* Indicates whether to use the internal AccelByte network. This should only be used in a server environment.
|
|
502
|
+
* When set to true, requests will be made to `http://{service-name}/{path}` instead of the `baseURL`.
|
|
503
|
+
*/
|
|
504
|
+
isInternalNetwork?: boolean
|
|
505
|
+
})
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
### API Class Arguments
|
|
509
|
+
|
|
510
|
+
The `ApiArgs` (the second `*Api` argument) has been replaced by partial `coreConfig` and `axiosConfig` to make it consistent with the options passed in the initializer.
|
|
511
|
+
|
|
512
|
+
```js
|
|
513
|
+
// Before
|
|
514
|
+
const data = await UsersAdminApi(sdk, { namespace, config: {} }).getUsersMe_v3();
|
|
515
|
+
// After
|
|
516
|
+
const { data } = await UsersAdminApi(sdk, { coreConfig: { namespace }, axiosConfig }).getUsersMe_v3();
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
### Admin Class Endpoints
|
|
520
|
+
|
|
521
|
+
Admin endpoints that have the "Admin" tag in Swagger will only generate one "Admin" keyword. The `AdminPlayerRecordAdmin` has been replaced by `PlayerRecordAdmin`.
|
|
522
|
+
|
|
523
|
+
```js
|
|
524
|
+
- AdminPlayerRecordAdminApi(sdk).getRecords_ByUserId(userId)
|
|
525
|
+
+ PlayerRecordAdminApi(sdk).getRecords_ByUserId(userId)
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### (Extend Functionality) Token Repository
|
|
529
|
+
|
|
530
|
+
Set the `accessToken` and `refreshToken` and update the Axios request headers to use Bearer authentication.
|
|
531
|
+
|
|
532
|
+
```js
|
|
533
|
+
setToken(token: TokenConfig) {
|
|
534
|
+
this.token = {
|
|
535
|
+
...this.token,
|
|
536
|
+
...token
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
removeToken() {
|
|
540
|
+
this.token = {}
|
|
541
|
+
}
|
|
542
|
+
getToken() {
|
|
543
|
+
return this.token
|
|
544
|
+
}
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
### refreshTokens method
|
|
548
|
+
|
|
549
|
+
The `refreshTokens` method is now replaced by `setToken({ accessToken, refreshToken })` with the same behavior.
|
|
550
|
+
|
|
551
|
+
```js
|
|
552
|
+
- sdk.refreshTokens(accessToken, refreshToken)
|
|
553
|
+
+ sdk.refreshTokens({ accessToken, refreshToken })
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
## TypeScript SDK Playground
|
|
557
|
+
|
|
558
|
+
You can live test the TypeScript SDK on the [AccelByte TypeScript SDK Playground](https://accelbyte.github.io/accelbyte-typescript-sdk).
|
|
559
|
+
|
|
560
|
+
## Frequently Asked Questions (FAQ)
|
|
561
|
+
|
|
562
|
+
### Can I install all the AGS SDK packages at once?
|
|
563
|
+
|
|
564
|
+
Yes, you can install all the AGS SDK packages in one command using the following `npm` command:
|
|
565
|
+
|
|
566
|
+
```bash
|
|
567
|
+
npm install @accelbyte/sdk @accelbyte/sdk-ams @accelbyte/sdk-achievement @accelbyte/sdk-basic @accelbyte/sdk-audit @accelbyte/sdk-buildinfo @accelbyte/sdk-challenge @accelbyte/sdk-chat @accelbyte/sdk-cloudsave @accelbyte/sdk-differ @accelbyte/sdk-dsmcontroller @accelbyte/sdk-event @accelbyte/sdk-gametelemetry @accelbyte/sdk-gdpr @accelbyte/sdk-groups @accelbyte/sdk-iam @accelbyte/sdk-inventory @accelbyte/sdk-leaderboard @accelbyte/sdk-legal @accelbyte/sdk-lobby @accelbyte/sdk-matchmaking-v1 @accelbyte/sdk-matchmaking @accelbyte/sdk-qosmanager @accelbyte/sdk-reporting @accelbyte/sdk-seasonpass @accelbyte/sdk-session @accelbyte/sdk-sessionbrowser @accelbyte/sdk-sessionhistory @accelbyte/sdk-social @accelbyte/sdk-ugc @accelbyte/sdk-platform
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
## Contributions
|
|
571
|
+
|
|
572
|
+
Thank you for your interest in contributing to the AGS TypeScript SDK. We value your input and appreciate your input to enhancing the AGS TypeScript SDK project. However, please note that direct contributions via GitHub are not currently accepted.
|
|
573
|
+
|
|
574
|
+
Instead, we encourage you to submit your ideas and suggestions as feature requests through the [AccelByte Customer Support Portal](https://accelbyte.atlassian.net/servicedesk/customer/portal/6) or via your Account Manager. This process helps us manage contributions effectively and ensures that each idea is carefully reviewed and considered by our team.
|