@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.
- package/dist/cjs/node/index.node.js +842 -0
- package/dist/cjs/node/index.node.js.map +1 -0
- package/dist/es/browser/index.browser.js +516 -18584
- package/dist/es/browser/index.browser.js.map +1 -1
- package/dist/es/node/index.node.js +516 -18585
- package/dist/es/node/index.node.js.map +1 -1
- package/dist/index.d.ts +261 -72654
- package/examples/next/README.md +40 -7
- package/examples/next/package.json +3 -5
- package/examples/next/pages/index.tsx +41 -26
- package/examples/next/src/sdk.ts +32 -0
- package/examples/next/yarn.lock +271 -211
- package/examples/node/README.md +29 -0
- package/examples/node/package.json +2 -4
- package/examples/node/yarn.lock +156 -551
- package/examples/vite/README.md +40 -7
- package/examples/vite/package.json +2 -4
- package/examples/vite/src/App.tsx +13 -10
- package/examples/vite/src/Sdk.ts +30 -15
- package/examples/vite/yarn.lock +4 -4
- package/package.json +3 -3
- package/dist/cjs/node/index.cjs +0 -19808
- package/dist/cjs/node/index.cjs.map +0 -1
- package/examples/next/pages/api/hello.ts +0 -10
package/examples/next/README.md
CHANGED
|
@@ -1,13 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
# SDK Usage Example: Next.js
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This is a sample usage of SDK using [Next.js](https://nextjs.org).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## How to Run
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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/next/next.config.js#L8). It is required because otherwise during local development, we will be blocked by [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) for some client side requests.
|
|
28
|
+
2. Change the [base URL, client ID, namespace, and redirect URI](https://github.com/AccelByte/accelbyte-web-sdk/blob/main/packages/sdk/examples/next/pages/index.tsx#L17-L20) 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.NEXT_PUBLIC_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 `NEXT_PUBLIC_BASE_URL` environment variable instead of the `localhost:3030` one.
|
|
@@ -3,15 +3,13 @@
|
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"private": true,
|
|
5
5
|
"scripts": {
|
|
6
|
-
"dev": "next dev",
|
|
6
|
+
"dev": "next dev -p 3030",
|
|
7
7
|
"build": "next build",
|
|
8
8
|
"start": "next start",
|
|
9
|
-
"lint": "next lint"
|
|
10
|
-
"link:start": "yarn link @accelbyte/sdk",
|
|
11
|
-
"link:stop": "yarn unlink @accelbyte/sdk && yarn install --force"
|
|
9
|
+
"lint": "next lint"
|
|
12
10
|
},
|
|
13
11
|
"dependencies": {
|
|
14
|
-
"@accelbyte/sdk": "
|
|
12
|
+
"@accelbyte/sdk": "1.1.3",
|
|
15
13
|
"@next/font": "13.0.7",
|
|
16
14
|
"@types/node": "18.11.17",
|
|
17
15
|
"@types/react": "18.0.26",
|
|
@@ -3,16 +3,17 @@
|
|
|
3
3
|
* This is licensed software from AccelByte Inc, for limitations
|
|
4
4
|
* and restrictions contact your company contract manager.
|
|
5
5
|
*/
|
|
6
|
-
import React from 'react'
|
|
6
|
+
import React, { useEffect } from 'react'
|
|
7
7
|
import { GetServerSideProps, InferGetServerSidePropsType } from 'next'
|
|
8
8
|
import Head from 'next/head'
|
|
9
9
|
import Image from 'next/image'
|
|
10
|
-
import
|
|
10
|
+
import { Accelbyte, CurrencyInfo, DiscoveryConfigData, ItemPagingSlicedResult, UserResponseV3 } from '@accelbyte/sdk'
|
|
11
|
+
import { exchangeAuthorizationCode, login } from '~/sdk'
|
|
11
12
|
|
|
12
|
-
import
|
|
13
|
+
import styles from '../styles/Home.module.css'
|
|
13
14
|
|
|
14
15
|
const SDK_CONFIG = {
|
|
15
|
-
baseURL: '
|
|
16
|
+
baseURL: 'http://localhost:3030/api',
|
|
16
17
|
clientId: '77f88506b6174c3ea4d925f5b4096ce8',
|
|
17
18
|
namespace: 'accelbyte',
|
|
18
19
|
redirectURI: 'http://localhost:3030'
|
|
@@ -23,39 +24,35 @@ const sdk = Accelbyte.SDK({
|
|
|
23
24
|
})
|
|
24
25
|
|
|
25
26
|
interface TestSdkReturnType {
|
|
26
|
-
|
|
27
|
-
listDiscoveryConfigs: DiscoveryConfigData | undefined
|
|
28
|
-
listOfCurrencies: CurrencyInfo[] | undefined
|
|
29
|
-
listOfItems: ItemPagingSlicedResult | undefined
|
|
30
|
-
listOfNamespaces: NamespaceInfo[] | undefined
|
|
27
|
+
currentUser: UserResponseV3 | null | undefined
|
|
28
|
+
listDiscoveryConfigs: DiscoveryConfigData | null | undefined
|
|
29
|
+
listOfCurrencies: CurrencyInfo[] | null | undefined
|
|
30
|
+
listOfItems: ItemPagingSlicedResult | null | undefined
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
export const getServerSideProps: GetServerSideProps<{ data: TestSdkReturnType }> = async () => {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
sdk.Platform.Currency().getCurrencies(),
|
|
38
|
-
sdk.Platform.Item().fetchItemsByCriteria({}),
|
|
39
|
-
|
|
40
|
-
// These require authentication and we can't use it right away.
|
|
41
|
-
// Ensure that you have logged in (have cookies) or pass the access token to the `Authorization` header.
|
|
42
|
-
sdk.Basic.Namespace({
|
|
33
|
+
export const getServerSideProps: GetServerSideProps<{ data: TestSdkReturnType }> = async ({ req }) => {
|
|
34
|
+
const accessToken = req.cookies.access_token
|
|
35
|
+
const [currentUser, listDiscoveryConfigs, listOfCurrencies, listOfItems] = await Promise.all([
|
|
36
|
+
sdk.IAM.User({
|
|
43
37
|
config: {
|
|
44
38
|
headers: {
|
|
45
|
-
|
|
39
|
+
authorization: `Bearer ${accessToken}`
|
|
46
40
|
}
|
|
47
41
|
}
|
|
48
|
-
}).
|
|
42
|
+
}).getCurrentUser(),
|
|
43
|
+
sdk.AccelbyteConfig.PublicTemplate().getDiscoveryTemplateConfigs(),
|
|
44
|
+
|
|
45
|
+
sdk.Platform.Currency().getCurrencies(),
|
|
46
|
+
sdk.Platform.Item().fetchItemsByCriteria({})
|
|
49
47
|
])
|
|
50
48
|
|
|
51
49
|
return {
|
|
52
50
|
props: {
|
|
53
51
|
data: {
|
|
54
|
-
|
|
55
|
-
listDiscoveryConfigs: listDiscoveryConfigs.response?.data,
|
|
56
|
-
listOfCurrencies: listOfCurrencies.response?.data,
|
|
57
|
-
listOfItems: listOfItems.response?.data
|
|
58
|
-
listOfNamespaces: listOfNamespaces.response?.data || null
|
|
52
|
+
currentUser: currentUser.response?.data || null,
|
|
53
|
+
listDiscoveryConfigs: listDiscoveryConfigs.response?.data || null,
|
|
54
|
+
listOfCurrencies: listOfCurrencies.response?.data || null,
|
|
55
|
+
listOfItems: listOfItems.response?.data || null
|
|
59
56
|
}
|
|
60
57
|
}
|
|
61
58
|
}
|
|
@@ -63,6 +60,18 @@ export const getServerSideProps: GetServerSideProps<{ data: TestSdkReturnType }>
|
|
|
63
60
|
|
|
64
61
|
// Page.
|
|
65
62
|
export default function Home({ data }: InferGetServerSidePropsType<typeof getServerSideProps>) {
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
async function exchange() {
|
|
65
|
+
const result = await exchangeAuthorizationCode(sdk, window.location.href)
|
|
66
|
+
if (result) {
|
|
67
|
+
// Reload so we can re-fetch the `currentUser` from the server side.
|
|
68
|
+
window.location.reload()
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
exchange()
|
|
73
|
+
}, [])
|
|
74
|
+
|
|
66
75
|
return (
|
|
67
76
|
<>
|
|
68
77
|
<Head>
|
|
@@ -87,6 +96,12 @@ export default function Home({ data }: InferGetServerSidePropsType<typeof getSer
|
|
|
87
96
|
</div>
|
|
88
97
|
</div>
|
|
89
98
|
|
|
99
|
+
{!data?.currentUser && (
|
|
100
|
+
<div>
|
|
101
|
+
<button onClick={() => login(sdk)}>Log in</button>
|
|
102
|
+
</div>
|
|
103
|
+
)}
|
|
104
|
+
|
|
90
105
|
<div className={styles.responses} id="response">
|
|
91
106
|
{Object.keys(data).map(key => (
|
|
92
107
|
<Collapsible key={key} json={data[key as keyof TestSdkReturnType]} title={key} />
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
|
|
7
|
+
import { AccelbyteSDK } from '@accelbyte/sdk'
|
|
8
|
+
|
|
9
|
+
export function login(sdk: AccelbyteSDK) {
|
|
10
|
+
const url = sdk.IAM.UserAuthorization().createLoginURL()
|
|
11
|
+
window.location.replace(url)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function exchangeAuthorizationCode(sdk: AccelbyteSDK, url: string) {
|
|
15
|
+
const searchParams = new URL(url).searchParams
|
|
16
|
+
const { code, error, state } = Object.fromEntries(searchParams.entries())
|
|
17
|
+
if (!code) return null
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const response = await sdk.IAM.UserAuthorization().exchangeAuthorizationCode({ code, error, state })
|
|
21
|
+
window.history.pushState({}, '', window.location.origin)
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
accessToken: response?.response?.data.access_token,
|
|
25
|
+
refreshToken: response?.response?.data.refresh_token
|
|
26
|
+
}
|
|
27
|
+
} catch (err) {
|
|
28
|
+
console.error(err)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return null
|
|
32
|
+
}
|