Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added bun functuality #4

Merged
merged 18 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

`npm i r6statapi`

## Suported Runtimes

- Node.js (20.13.0+)
- Bun (1.1.26)

## Getting Started

To get started you will need a Ubisoft login, it is best to create a new account and not the account you normally use. Create a new account [here](account.ubisoft.com/login)
Expand Down
975 changes: 164 additions & 811 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"dependencies": {
"eslint": "^9.1.1",
"node-fetch": "^2.6.2"
},
"axios": "^1.7.5",
"eslint": "^9.1.1"
},
"scripts": {
"start": "node ./src/index.js",
"start": "node ./dist/cjs/index.js",
"build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json"
},
"name": "r6statapi",
Expand All @@ -20,16 +20,15 @@
"Ubisoft",
"Rainbow",
"Six",
"Seige"
"Siege"
],
"author": "Calum Wilson",
"author": "Calum Wilson, Fabian Maier",
"license": "MIT",
"bugs": {
"url": "https://github.com/CalumW1/R6StatAPI/issues"
},
"homepage": "https://github.com/CalumW1/R6StatAPI#readme",
"devDependencies": {
"@types/node": "^20.14.11",
"@types/node-fetch": "^2.6.11",
"typescript": "^5.5.3"
}
Expand Down
25 changes: 16 additions & 9 deletions src/methods/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import fetch from 'node-fetch';
import axios, { Method, RawAxiosRequestHeaders } from 'axios';

export const ApiClient = async (url: string, header: any, method: string): Promise<any> => {
const response = await fetch(url, {
method: method,
headers: header,
});
export const ApiClient = async (
url: string,
header: RawAxiosRequestHeaders,
method: Method
): Promise<any> => {
try {
const response = await axios({
url: url,
method: method,
headers: header,
});

if (!response.ok) throw Error('Error with response');

return response;
return response.data;
} catch (error) {
throw new Error(`Error with response: ${(error as any).message}`);
}
};
11 changes: 5 additions & 6 deletions src/methods/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fetch from 'node-fetch';
import axios from 'axios';
import { UBI_APPID, BASE_UBI_URI, UBI_AUTH_URI } from '../constants';
import { writeFile } from 'fs';
import { ApiClient } from './apiClient';

export interface Authorise {
platformType: string;
Expand Down Expand Up @@ -57,9 +56,9 @@ const RequestToken = async (email: string, password: string): Promise<Authorise>

const URI = BASE_UBI_URI(3) + UBI_AUTH_URI;

const response = await ApiClient(URI, headers, 'POST');
const response = await axios.post(URI, {}, { headers });

const data = (await response.json()) as Authorise;
const data = response.data as Authorise;

Token = data.ticket;
NextRefresh = data.expiration;
Expand All @@ -77,10 +76,10 @@ const RequestToken = async (email: string, password: string): Promise<Authorise>
};

export const CheckToken = async (): Promise<string> => {
var currentTime = new Date().toISOString();
const currentTime = new Date().toISOString();

if (Token !== '' && currentTime < NextRefresh) {
console.log('Retreving token from memory');
console.log('Retrieving token from memory');
return Token;
} else return (await RequestToken(Email, Password)).ticket;
};
Expand Down
14 changes: 4 additions & 10 deletions src/methods/getOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ export const GetOperator = async (
var token = await CheckToken();
var expiration = await GetExperation();

const headers = {
const header = {
'Content-Type': 'application/json',
Authorization: `ubi_v1 t=${token}`,
'Ubi-SessionId': UBI_RANKED_SESSIONID,
'Ubi-AppId': UBI_DATADEV_APPID,
'Content-Type': 'application/json',
expiration: expiration,
};

Expand All @@ -90,16 +90,10 @@ export const GetOperator = async (

// https://prod.datadev.ubisoft.com/v1/users/488cd0dd-b8e0-4718-a9da-2767ea44c399/playerstats?spaceId=05bfb3f7-6c21-4c42-be1f-97a33fb5cf66&view=current&aggregation=operators&gameMode=all,ranked,casual,unranked&platformGroup=CONSOLE&teamRole=attacker,defender&seasons=Y9S2
// https://prod.datadev.ubisoft.com/v1/users/488cd0dd-b8e0-4718-a9da-2767ea44c399/playerstats?spaceId=05bfb3f7-6c21-4c42-be1f-97a33fb5cf66&view=current&aggregation=operators&gameMode=ranked,casual&platformGroup=CONSOLE&teamRole=defender,attacker&seasons=Y9S2
console.log(URI);

const data = await ApiClient(URI, headers, 'GET');
const data = await ApiClient(URI, header, 'GET');

const operators: GameModes = await ExtractOperators(
await data.json(),
userId,
gameMode,
platformChange
);
const operators: GameModes = await ExtractOperators(await data, userId, gameMode, platformChange);

return operators;
};
Expand Down
2 changes: 1 addition & 1 deletion src/methods/getServerStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const GetServerStatus = async (platform: string): Promise<ServerStatus> =

const response = await ApiClient(URI, headers, 'GET');

const data = await response.json();
const data = await response;

const serverStatus: ServerStatus = {
MDM: data[0].MDM,
Expand Down
2 changes: 1 addition & 1 deletion src/methods/getUserByUserId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const GetUserByUserId = async (userId: string): Promise<User[] | []> => {

const response = await ApiClient(URI, headers, 'GET');

const data = (await response.json()) as Profiles;
const data = (await response) as Profiles;

data.profiles = data.profiles.map(profile => ({
...profile,
Expand Down
2 changes: 1 addition & 1 deletion src/methods/getUserByUsername.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const GetUserByUsername = async (username: string, platform: string): Pro

const response = await ApiClient(URI, headers, 'GET');

const data = (await response.json()) as Profiles;
const data = (await response) as Profiles;

data.profiles = data.profiles.map(profile => ({
...profile,
Expand Down
2 changes: 1 addition & 1 deletion src/methods/getUserProgression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export const GetUserProgression = async (userId: string): Promise<Progression> =

const response = await ApiClient(URI, headers, 'GET');

return (await response.json()) as Progression;
return (await response) as Progression;
};
2 changes: 1 addition & 1 deletion src/methods/getUserRank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const GetUserRank = async (userId: string, platform: string): Promise<Use

const response = await ApiClient(URI, headers, 'GET');

const result: UserRank = await extractValues(await response.json());
const result: UserRank = await extractValues(await response);

return result;
};
Expand Down
2 changes: 1 addition & 1 deletion src/methods/getUserStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const GetUserStats = async (
const response = await ApiClient(URI, headers, 'GET');

const usersStats: UserStats = await BuildUserStats(
await response.json(),
await response,
user,
platformTransformation,
platform
Expand Down
Loading