Skip to content

Commit

Permalink
sync 2 (#3)
Browse files Browse the repository at this point in the history
* Create README.md

* Fix package.json and README.md

* Sync with main branch

* Create npm-publish.yml

* Add google oauth register, Define custom error obj

* Version Up 0.0.3
  • Loading branch information
Phantola authored Jan 31, 2023
1 parent 79869ff commit a65e168
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 36 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -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}}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
88 changes: 53 additions & 35 deletions src/AptoPlay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import axios from 'axios';
import { parseObjectPascalToCamel } from './utils';
import {
generateErrorObject,
getGoogleProfileByAccessToken,
parseObjectPascalToCamel
} from './utils';

export class AptoPlay {
private titleId: string;
Expand All @@ -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 {
Expand All @@ -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);
}
}
}
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface CustomError {
errorTitle: string;
code?: number;
response?: any;
message?: string;
errorObject: any;
}
41 changes: 41 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
} {
Expand All @@ -12,3 +34,22 @@ export function parseObjectPascalToCamel(object: { [k: string]: any }): {

return newObject;
}

export async function getGoogleProfileByAccessToken(
accessToken: string
): Promise<string> {
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);
}
}

0 comments on commit a65e168

Please sign in to comment.