diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..44698c5 --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,23 @@ +# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created +# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages + +name: Node.js Package + +on: + push: + branches: [ main ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16 + registry-url: https://registry.npmjs.org/ + - run: npm ci + - run: npm run build + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.npm_token}} diff --git a/package.json b/package.json index 6871f04..a5289a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aptoplay-core", - "version": "0.0.2", + "version": "0.0.3", "description": "Aptos Gaming TS library", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/AptoPlay.ts b/src/AptoPlay.ts index 294b812..c3fa234 100644 --- a/src/AptoPlay.ts +++ b/src/AptoPlay.ts @@ -1,5 +1,9 @@ import axios from 'axios'; -import { parseObjectPascalToCamel } from './utils'; +import { + generateErrorObject, + getGoogleProfileByAccessToken, + parseObjectPascalToCamel +} from './utils'; export class AptoPlay { private titleId: string; @@ -9,7 +13,7 @@ export class AptoPlay { constructor(titleId: string, xSecretKey: string) { this.titleId = titleId; this.xSecretKey = xSecretKey; - this.baseUrl = `https://${this.titleId}.playfabapi.com`; + this.baseUrl = `https://${titleId}.playfabapi.com`; } public getTitleId(): string { @@ -24,47 +28,61 @@ export class AptoPlay { return this.baseUrl; } - public async registerUser( - email: string, - password: string, - callback?: (data: any) => any - ) { - const res = await axios.post(this.baseUrl + '/Client/RegisterPlayFabUser', { - TitleId: this.titleId, - Email: email, - Password: password, - Username: new Date().getTime().toString() // Username: PlayFab username for the account (3-20 characters) - }); + public async registerUser(email: string, password: string) { + try { + const res = await axios.post( + this.baseUrl + '/Client/RegisterPlayFabUser', + { + TitleId: this.titleId, + Email: email, + Password: password, + Username: new Date().getTime().toString() // Username: PlayFab username for the account (3-20 characters) + } + ); - const data = parseObjectPascalToCamel(res.data); + const data = parseObjectPascalToCamel(res.data); - if (callback) { - callback(data); + return parseObjectPascalToCamel(data); + } catch (err: any) { + throw generateErrorObject('PLAYFAB_REGISTER_WITH_EMAIL_ERROR', err); } - - return parseObjectPascalToCamel(data); } - public async login( - email: string, - password: string, - callback?: (data: any) => any - ) { - const res = await axios.post( - this.baseUrl + '/Client/LoginWithEmailAddress', - { - TitleId: this.titleId, - Email: email, - Password: password - } - ); + public async login(email: string, password: string) { + try { + const res = await axios.post( + this.baseUrl + '/Client/LoginWithEmailAddress', + { + TitleId: this.titleId, + Email: email, + Password: password + } + ); - const data = parseObjectPascalToCamel(res.data); + const data = parseObjectPascalToCamel(res.data); - if (callback) { - callback(data); + return parseObjectPascalToCamel(data); + } catch (err: any) { + throw generateErrorObject('PLAYFAB_LOGIN_WITH_EMAIL_ERROR', err); } + } + + public async registerWithGoogleAccount(accessToken: string) { + const email = await getGoogleProfileByAccessToken(accessToken); - return parseObjectPascalToCamel(data); + try { + const playFabRes = await axios.post( + `${this.baseUrl}/Client/LoginWithGoogleAccount`, + { + TitleId: this.titleId, + CreateAccount: true, + AccessToken: accessToken + } + ); + + return parseObjectPascalToCamel({ ...playFabRes, email }); + } catch (err: any) { + throw generateErrorObject('PLAYFAB_GOOGLE_SOCIAL_REGISER_ERROR', err); + } } } diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..f0d6ad9 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,7 @@ +interface CustomError { + errorTitle: string; + code?: number; + response?: any; + message?: string; + errorObject: any; +} diff --git a/src/utils.ts b/src/utils.ts index f78ba7b..18a1eba 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,25 @@ +import axios from 'axios'; + +export function generateErrorObject( + errorTitle: string, + errorObject: any +): CustomError { + const error: CustomError = { + errorTitle, + errorObject: {} + }; + + if (axios.isAxiosError(errorObject)) { + error.errorObject['code'] = errorObject.code; + error.errorObject['response'] = errorObject.response; + error.errorObject['message'] = errorObject.message; + } else { + error.errorObject = errorObject; + } + + return error; +} + export function parseObjectPascalToCamel(object: { [k: string]: any }): { [k: string]: any; } { @@ -12,3 +34,22 @@ export function parseObjectPascalToCamel(object: { [k: string]: any }): { return newObject; } + +export async function getGoogleProfileByAccessToken( + accessToken: string +): Promise { + try { + const userInfoRes = await axios.get( + 'https://www.googleapis.com/oauth2/v3/userinfo', + { + params: { + access_token: accessToken + } + } + ); + + return userInfoRes.data.email; + } catch (err: any) { + throw generateErrorObject('GOOGLE_GET_PROFILE_ERROR', err); + } +}