Skip to content

Commit

Permalink
update: trade service completed
Browse files Browse the repository at this point in the history
  • Loading branch information
NadirHaciyev committed Feb 2, 2024
1 parent c80ae79 commit c236c6b
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-dolls-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"steamwebapi-wrapper": minor
---

trade service completed
41 changes: 10 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p align="center">
<a href="https://github.com/NadirHaciyev/steamwebapi-wrapper">
<picture>
<img alt="Steamwebapi Wrapper" src="https://raw.githubusercontent.com/NadirHaciyev/steamwebapi-wrapper/HEAD/.github/steamwebapi-wrapper-logo.svg" width="500" style="max-width: 100%; aspect-ratio: 5 / 1.5;">
<img alt="Steamwebapi Wrapper" src="https://raw.githubusercontent.com/NadirHaciyev/steamwebapi-wrapper/HEAD/.github/steamwebapi-wrapper-logo.svg" width="300" style="max-width: 100%; aspect-ratio: 5 / 1.5;">
</picture>
</a>
</p>
Expand Down Expand Up @@ -68,9 +68,9 @@ import SteamWebApi from "steamwebapi-wrapper";
// Create instance by providing API Key
const swa = new SteamWebApi({ apiKey: "your_api_key" });

// Get all trade offers
// Creata a new trade offer
swa.trade
.getAllTradeOffers()
.createOffer()
.then((res) => console.log(res))
.catch((error) => console.log(error));
```
Expand All @@ -79,37 +79,16 @@ swa.trade

The API wrapper supports the following endpoints:

- **Currency**

- Get a list of currencies
- Get exchange rates for currencies

- **Profiles**

- Get random steam profile
- Get a steam profile with high valued inventory

- **Items**

- Get Steam items
- Get information about Steam items
- Find Steam items
- Get details of a specific Steam item
- Get item history

- **Inventory**

- Get Steam inventory
- Get Steam inventory tracked by day

- **Profile**
- Get Steam IDs
- Get Steam profiles
- Get Steam friend lists
- **Trade**
- Initiate a New Trade Offer
- Retrieve Status of Trade Offers
- Cancel Trade Offer
- Decline Trade Offer
- Accept Trade Offer

## Examples

For more examples check the [examples directory](https://github.com/oguzhantasimaz/steam-web-api-go/tree/main/examples).
For more examples check the [examples directory](https://github.com/NadirHaciyev/steamwebapi-wrapper/tree/main/examples).

## Contributing

Expand Down
11 changes: 11 additions & 0 deletions examples/credentials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import SteamWebApi from "../dist/index.cjs";

function credentials() {
const swa = new SteamWebApi({ apiKey: "your_api_key" });

// Set new Api Key
swa.setApiKey("new_api_key");

// Get Api Key
swa.getApiKey();
}
10 changes: 10 additions & 0 deletions examples/trade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SteamWebApi from "../dist/index.cjs";

function trade() {
const swa = new SteamWebApi({ apiKey: "your_api_key" });

// Create a new trade offer
swa.trade.createOffer({
/* create params https://www.steamwebapi.com/api/list */
});
}
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

6 changes: 2 additions & 4 deletions src/services/steamwebapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ interface ISteamWebApi {

export default class SteamWebApi implements ISteamWebApi {
private apiKey: string;
trade: Trade;

constructor({ apiKey }: ConstructorParams) {
this.apiKey = apiKey;
}
get trade() {
return new Trade(this.apiKey);
this.trade = new Trade(apiKey);
}

setApiKey(apiKey: string) {
Expand All @@ -28,4 +27,3 @@ export default class SteamWebApi implements ISteamWebApi {
return this.apiKey;
}
}

69 changes: 49 additions & 20 deletions src/services/trade.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,78 @@
import { client } from "../utils";

interface OfferDataParams {}
import type {
AcceptParamsType,
CancelParamsType,
CreateParamsType,
DeclineParamsType,
StatusParamsType,
} from "../types/trade";

export interface ITrade {
getTradeOffers: () => void;
sendTradeOffer: (offerData: OfferDataParams) => void;
createOffer: (createParams: CreateParamsType) => void;
getOffersStatus: (statusParams: StatusParamsType) => void;
cancelOffer: (cancelParams: CancelParamsType) => void;
declineOffer: (declineParams: DeclineParamsType) => void;
acceptOffer: (acceptParams: AcceptParamsType) => void;
}

export default class Trade implements ITrade {
private apiKey: string;

constructor(apiKey: string) {
this.apiKey = apiKey;
client.defaults.params.apiKey = apiKey;
client.defaults.params.baseUrl =
"https//www.steamwebapi.com/steam/api/trade";
}

async getTradeOffers() {
async createOffer(createParams: CreateParamsType) {
try {
const offers = await client.get("/tradeoffer", {
params: { key: this.apiKey },
});
const createRes = await client.post("/create", createParams);

if (createRes.status !== 200) throw new Error("Trade Offer not created");

if (!offers) throw new Error("Trade offers not founded");
return createRes;
} catch (error) {
return error;
}
}

async getOffersStatus(statusParams: StatusParamsType) {
try {
const offersStatusRes = await client.post("/status", statusParams);
if (offersStatusRes.status !== 200) throw new Error("An error occurred");

return offers;
return offersStatusRes;
} catch (error) {
return error;
}
}

async getTradeOffer(id: string) {
async cancelOffer(cancelParams: CancelParamsType) {
try {
const offer = await client.get("/tradeoffer", {});
const cancelRes = await client.put("/cancel", cancelParams);
if (cancelRes.status !== 200) throw new Error("An error occurred");

return { success: true };
} catch (error) {
return error;
}
}

async declineOffer(declineParams: DeclineParamsType) {
try {
const declineRes = await client.put("/decline", declineParams);
if (declineRes.status !== 200) throw new Error("An error occurred");

return { success: true };
} catch (error) {
return error;
}
}

async sendTradeOffer(offerData: OfferDataParams) {
async acceptOffer(acceptParams: AcceptParamsType) {
try {
await client.post("/tradeoffer", offerData, {
params: { key: this.apiKey },
});
const acceptRes = await client.put("/accept", acceptParams);
if (acceptRes.status !== 200) throw new Error("An error occurred");

return true;
return { success: true };
} catch (error) {
return error;
}
Expand Down
31 changes: 31 additions & 0 deletions src/types/trade.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export type CreateParamsType = {
steamloginsecure: boolean;
partneritemassetids: string;
myitemsassetids: string;
tradelink: string;
partnersteamid: string;
message: string;
game: string;
};

export type StatusParamsType = {
steamcommunityapikey: string;
partnersteamid?: string;
assetid?: string;
};

export type CancelParamsType = {
steamloginsecure: string;
tradeofferid: string;
};

export type DeclineParamsType = {
steamloginsecure: string;
tradeofferid: string;
};

export type AcceptParamsType = {
steamloginsecure: string;
tradeofferid: string;
partnersteamid: string;
};
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosInstance } from "axios";

export const client: AxiosInstance = axios.create({
baseURL: "https://www.steamwebapi.com/steam/api",
baseURL: "https://www.steamwebapi.com",
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"skipLibCheck": true,
"noUncheckedIndexedAccess": true,
"noEmit": true

// "allowSyntheticDefaultImports": true
}
}

0 comments on commit c236c6b

Please sign in to comment.