@accelbyte/sdk 1.1.2 → 1.2.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.
@@ -1,13 +1,46 @@
1
- This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
1
+ # SDK Usage Example: vite
2
2
 
3
- ## Getting Started
3
+ This is a sample usage of SDK using [vite](https://vitejs.dev/).
4
4
 
5
- Run the command below:
5
+ ## How to Run
6
6
 
7
- ```bash
8
- npm run dev
9
- # or
7
+ This folder should be ready to run without any changes as it is pointing to the [AccelByte's demo environment](https://demo.accelbyte.io) by default.
8
+
9
+ ```sh
10
+ # Install the dependencies.
11
+ yarn install
12
+
13
+ # Run the dev server.
10
14
  yarn dev
11
15
  ```
12
16
 
13
- And open the link http://localhost:3030/
17
+ After that, open http://localhost:3030. There will be few requests fired on the background, which then you could inspect the response by collapsing the `<details>` element.
18
+
19
+ There will be also a `<details>` element with the title `currentUser`, which will contain your user information. By default, if you aren't logged in, then you will get `401 Unauthorized` in the network response.
20
+
21
+ If you want to log in, you can click on the "Log in" button and it will redirect you to the Login website. After you submit with the valid credentials, you will be redirected back to http://localhost:3030 with some query parameters in it. These query parameters will be processed by this function `exchangeAuthorizationCode`, which will exchange the code passed in the query parameters to cookies.
22
+
23
+ ## How to Point to Other Environments
24
+
25
+ If you want to use this example folder for other environments, do these steps:
26
+
27
+ 1. Change the [proxy target](https://github.com/AccelByte/accelbyte-web-sdk/blob/main/packages/sdk/examples/vite/vite.config.ts#L16-L22). It is required because otherwise during local development, we will be blocked by [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS).
28
+ 2. Change the [client ID, namespace, and redirect URI](https://github.com/AccelByte/accelbyte-web-sdk/blob/main/packages/sdk/examples/vite/src/Sdk.ts#L11-L13) of the SDK options. You can get these from Admin Portal's IAM Clients page. As for the redirect URI, ensure that it is a subset of the redirect URIs listed in the IAM Client.
29
+
30
+ ## Production builds
31
+
32
+ For production builds, please ensure that you have added conditionals to the `baseURL`. As mentioned in the section above, the default `baseURL` is the local development's origin with `/api` suffix to bypass CORS. However, we won't need it in production builds, because the default proxy server doesn't exist when we run in production mode.
33
+
34
+ As such, what we can do is to have something like this:
35
+
36
+ ```ts
37
+ const BASE_URL = process.env.VITE_BASE_URL
38
+ const SDK_CONFIG = {
39
+ baseURL: process.env.NODE_ENV === 'production' ? BASE_URL : 'http://localhost:3030/api',
40
+ clientId: '77f88506b6174c3ea4d925f5b4096ce8',
41
+ namespace: 'accelbyte',
42
+ redirectURI: process.env.NODE_ENV === 'production' ? BASE_URL : 'http://localhost:3030'
43
+ }
44
+ ```
45
+
46
+ So, when we are building for production, it will use `VITE_BASE_URL` environment variable instead of the `localhost:3030` one.
@@ -6,12 +6,10 @@
6
6
  "scripts": {
7
7
  "dev": "vite",
8
8
  "build": "tsc && vite build",
9
- "preview": "vite preview",
10
- "link:start": "yarn link @accelbyte/sdk",
11
- "link:stop": "yarn unlink @accelbyte/sdk && yarn install --force"
9
+ "preview": "vite preview"
12
10
  },
13
11
  "dependencies": {
14
- "@accelbyte/sdk": "0.2.0-beta.20",
12
+ "@accelbyte/sdk": "1.1.3",
15
13
  "dotenv": "16.0.3",
16
14
  "react": "18.2.0",
17
15
  "react-dom": "18.2.0"
@@ -5,25 +5,22 @@
5
5
  */
6
6
  import React, { useEffect, useState } from 'react'
7
7
  import './App.css'
8
- import { getSdkTestValues } from './Sdk'
8
+ import { exchangeAuthorizationCode, getSdkTestValues, login } from './Sdk'
9
9
 
10
10
  function App() {
11
11
  const [responses, setResponses] = useState<any>(null)
12
- useEffect(() => {
13
- let ignore = false
14
12
 
13
+ useEffect(() => {
15
14
  async function initialize() {
15
+ // Exchange authorization code first, so that by the time we fetch the stuff, we already have the cookie.
16
+ await exchangeAuthorizationCode()
17
+
18
+ // After that, fetch the stuff.
16
19
  setResponses(null)
17
- if (!ignore) {
18
- setResponses(await getSdkTestValues())
19
- }
20
+ setResponses(await getSdkTestValues())
20
21
  }
21
22
 
22
23
  initialize()
23
-
24
- return () => {
25
- ignore = true
26
- }
27
24
  }, [])
28
25
 
29
26
  return (
@@ -37,6 +34,12 @@ function App() {
37
34
  </div>
38
35
  </div>
39
36
 
37
+ {!responses?.currentUser?.response?.data && (
38
+ <div>
39
+ <button onClick={login}>Log in</button>
40
+ </div>
41
+ )}
42
+
40
43
  {responses === null ? (
41
44
  'No responses yet'
42
45
  ) : (
@@ -1,7 +1,13 @@
1
+ /*
2
+ * Copyright (c) 2023 AccelByte Inc. All Rights Reserved
3
+ * This is licensed software from AccelByte Inc, for limitations
4
+ * and restrictions contact your company contract manager.
5
+ */
6
+
1
7
  import { Accelbyte } from '@accelbyte/sdk'
2
8
 
3
9
  const SDK_CONFIG = {
4
- baseURL: '/api',
10
+ baseURL: 'http://localhost:3030/api',
5
11
  clientId: '77f88506b6174c3ea4d925f5b4096ce8',
6
12
  namespace: 'accelbyte',
7
13
  redirectURI: 'http://localhost:3030'
@@ -12,27 +18,36 @@ const sdk = Accelbyte.SDK({
12
18
  })
13
19
 
14
20
  export async function getSdkTestValues() {
15
- const [listDiscoveryConfigs, listOfCurrencies, listOfItems, listOfNamespaces] = await Promise.all([
21
+ const [currentUser, listDiscoveryConfigs, listOfCurrencies, listOfItems] = await Promise.all([
22
+ sdk.IAM.User().getCurrentUser(),
16
23
  sdk.AccelbyteConfig.PublicTemplate().getDiscoveryTemplateConfigs(),
17
24
 
18
25
  sdk.Platform.Currency().getCurrencies(),
19
- sdk.Platform.Item().fetchItemsByCriteria({}),
20
-
21
- // These require authentication and we can't use it right away.
22
- // Ensure that you have logged in (have cookies) or pass the access token to the `Authorization` header.
23
- sdk.Basic.Namespace({
24
- config: {
25
- headers: {
26
- Authorization: `Bearer <replace-this-with-access-token>`
27
- }
28
- }
29
- }).getNamespaces()
26
+ sdk.Platform.Item().fetchItemsByCriteria({})
30
27
  ])
31
28
 
32
29
  return {
30
+ currentUser,
33
31
  listDiscoveryConfigs,
34
32
  listOfCurrencies,
35
- listOfItems,
36
- listOfNamespaces
33
+ listOfItems
34
+ }
35
+ }
36
+
37
+ export function login() {
38
+ const url = sdk.IAM.UserAuthorization().createLoginURL()
39
+ window.location.replace(url)
40
+ }
41
+
42
+ export async function exchangeAuthorizationCode() {
43
+ const searchParams = new URL(window.location.href).searchParams
44
+ const { code, error, state } = Object.fromEntries(searchParams.entries())
45
+ if (!code) return
46
+
47
+ try {
48
+ await sdk.IAM.UserAuthorization().exchangeAuthorizationCode({ code, error, state })
49
+ window.history.pushState({}, '', window.location.origin)
50
+ } catch (err) {
51
+ console.error(err)
37
52
  }
38
53
  }
@@ -2,10 +2,10 @@
2
2
  # yarn lockfile v1
3
3
 
4
4
 
5
- "@accelbyte/sdk@0.2.0-beta.20":
6
- version "0.2.0-beta.20"
7
- resolved "https://registry.yarnpkg.com/@accelbyte/sdk/-/sdk-0.2.0-beta.20.tgz#7ae3195ee467a920422d307c142c1c2db5bd140d"
8
- integrity sha512-phwB+uGHoOidD6+V/60/5oJilnpmAIk/bF9eLb6drkqzo0mfehrFi3muLJdhBtgDRrV/attzPJgMNcRf8UCWOw==
5
+ "@accelbyte/sdk@1.1.3":
6
+ version "1.1.3"
7
+ resolved "https://registry.yarnpkg.com/@accelbyte/sdk/-/sdk-1.1.3.tgz#93de5299c99b12a79a4e04a918989d4c7ce124dd"
8
+ integrity sha512-DL0JXLeBL7yJs+EWATEuftSghpfOeGZMG2dGUjQD6sly+RsBi3UJVSjxAHRv/fo/nlW7yr4lTt2XQ2XbRDJeeg==
9
9
  dependencies:
10
10
  "@accelbyte/validator" "0.2.0-beta.2"
11
11
  axios "0.27.2"
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@accelbyte/sdk",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "author": "AccelByte Inc",
5
- "license": "AccelByte License",
5
+ "license": "SEE LICENSE IN LICENSE",
6
6
  "main": "./dist/cjs/node/index.cjs",
7
7
  "types": "./dist/index.d.ts",
8
8
  "type": "module",
@@ -22,7 +22,7 @@
22
22
  "./package.json": "./package.json"
23
23
  },
24
24
  "dependencies": {
25
- "@accelbyte/validator": "0.2.0-beta.2",
25
+ "@accelbyte/validator": "0.2.0-beta.4",
26
26
  "axios": "0.27.2",
27
27
  "buffer": "6.0.3",
28
28
  "crypto-js": "4.1.1",