Skip to content

Commit

Permalink
feat: Modify pies (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode authored Jun 26, 2024
1 parent 6dd8f01 commit bd9e43f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ This [Trading 212](https://www.trading212.com/) API can be used with TypeScript

## Implemented Endpoints

- [x] [Instruments Metadata](https://t212public-api-docs.redoc.ly/#tag/Instruments-Metadata)
- [x] [Pies](https://t212public-api-docs.redoc.ly/#tag/Pies)
- [x] [Equity Orders](https://t212public-api-docs.redoc.ly/#tag/Equity-Orders)
- [x] [Account Data](https://t212public-api-docs.redoc.ly/#tag/Account-Data)
- [x] [Personal Portfolio](https://t212public-api-docs.redoc.ly/#tag/Personal-Portfolio)
- [x] [Equity Orders](https://t212public-api-docs.redoc.ly/#tag/Equity-Orders)
- [x] [Historical items](https://t212public-api-docs.redoc.ly/#tag/Historical-items)
- [x] [Instruments Metadata](https://t212public-api-docs.redoc.ly/#tag/Instruments-Metadata)
- [x] [Personal Portfolio](https://t212public-api-docs.redoc.ly/#tag/Personal-Portfolio)
- [x] [Pies](https://t212public-api-docs.redoc.ly/#tag/Pies)

## Features

Expand Down Expand Up @@ -65,6 +65,12 @@ npm run demo:account

This library utilizes [axios](https://github.com/axios/axios) for HTTP calls. You can configure the axios instance using [interceptors](https://axios-http.com/docs/interceptors) if needed. Retries are handled by [axios-retry](https://github.com/softonic/axios-retry), and payloads are validated with [Zod](https://github.com/colinhacks/zod). Unit tests are implemented with [nock](https://github.com/nock/nock).

## Contributions

Here are some best practices PRs that show how to add endpoints:

- [Add Pie API](https://github.com/bennycode/trading212-api/pull/6/files)

## Resources

### Documentation
Expand Down
4 changes: 3 additions & 1 deletion src/api/history/HistoryAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ const HistoryOrderDataSchema = z.object({
type: ORDER_TYPE,
});

export type RequestExport = z.infer<typeof RequestExportSchema>;

/**
* @see https://t212public-api-docs.redoc.ly/#tag/Historical-items
*/
Expand Down Expand Up @@ -146,7 +148,7 @@ export class HistoryAPI {
/**
* @see https://t212public-api-docs.redoc.ly/#operation/requestReport
*/
async requestExport(request: z.infer<typeof RequestExportSchema>) {
async requestExport(request: RequestExport) {
const resource = HistoryAPI.URL.EXPORTS;
const validated = RequestExportSchema.parse(request);
const response = await this.apiClient.post(resource, validated);
Expand Down
16 changes: 12 additions & 4 deletions src/api/order/OrderAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ const StopLimitOrderSchema = OrderSchema.extend({

export type Order = z.infer<typeof OrderSchema>;

export type PlaceLimitOrder = z.infer<typeof PlaceLimitOrderSchema>;

export type PlaceMarketOrder = z.infer<typeof PlaceMarketOrderSchema>;

export type PlaceStopOrder = z.infer<typeof PlaceStopOrderSchema>;

export type PlaceStopLimitOrder = z.infer<typeof PlaceStopLimitOrderSchema>;

/**
* @see https://t212public-api-docs.redoc.ly/#tag/Equity-Orders
*/
Expand Down Expand Up @@ -93,28 +101,28 @@ export class OrderAPI {
return this.apiClient.delete(resource);
}

async placeLimitOrder(order: z.infer<typeof PlaceLimitOrderSchema>) {
async placeLimitOrder(order: PlaceLimitOrder) {
const resource = OrderAPI.URL.ORDERS_LIMIT;
const validated = PlaceLimitOrderSchema.parse(order);
const response = await this.apiClient.post(resource, validated);
return LimitOrderSchema.parse(response.data);
}

async placeMarketOrder(order: z.infer<typeof PlaceMarketOrderSchema>) {
async placeMarketOrder(order: PlaceMarketOrder) {
const resource = OrderAPI.URL.ORDERS_MARKET;
const validated = PlaceMarketOrderSchema.parse(order);
const response = await this.apiClient.post(resource, validated);
return MarketOrderSchema.parse(response.data);
}

async placeStopOrder(order: z.infer<typeof PlaceStopOrderSchema>) {
async placeStopOrder(order: PlaceStopOrder) {
const resource = OrderAPI.URL.ORDERS_STOP;
const validated = PlaceStopOrderSchema.parse(order);
const response = await this.apiClient.post(resource, validated);
return StopOrderSchema.parse(response.data);
}

async placeStopLimitOrder(order: z.infer<typeof PlaceStopLimitOrderSchema>) {
async placeStopLimitOrder(order: PlaceStopLimitOrder) {
const resource = OrderAPI.URL.ORDERS_STOP_LIMIT;
const validated = PlaceStopLimitOrderSchema.parse(order);
const response = await this.apiClient.post(resource, validated);
Expand Down
39 changes: 39 additions & 0 deletions src/api/pie/PieAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import type {AxiosInstance} from 'axios';
import {z} from 'zod';
import {DIVIDEND_CASH_ACTION, PIE_ICON, PIE_STATUS_GOAL} from '../union.js';

const CreatePieSchema = z.object({
dividendCashAction: DIVIDEND_CASH_ACTION,
endDate: z.string().datetime({offset: true}),
goal: z.number(),
icon: z.string(),
instrumentShares: z.record(z.number()),
name: z.string(),
});

const PieSchema = z.object({
cash: z.number(),
dividendDetails: z.object({
Expand Down Expand Up @@ -54,6 +63,8 @@ export type Pie = z.infer<typeof PieSchema>;

export type DetailedPie = z.infer<typeof DetailedPieSchema>;

export type CreatePie = z.infer<typeof CreatePieSchema>;

/**
* @see https://t212public-api-docs.redoc.ly/#tag/Pies
*/
Expand All @@ -75,4 +86,32 @@ export class PieAPI {
const response = await this.apiClient.get(resource);
return id ? DetailedPieSchema.parse(response.data) : z.array(PieSchema).parse(response.data);
}

/**
* @see https://t212public-api-docs.redoc.ly/#operation/create
*/
async createPie(request: CreatePie) {
const resource = PieAPI.URL.PIES;
const validated = CreatePieSchema.parse(request);
const response = await this.apiClient.post(resource, validated);
return DetailedPieSchema.parse(response.data);
}

/**
* @see https://t212public-api-docs.redoc.ly/#operation/update
*/
async updatePie(id: number, request: CreatePie) {
const resource = `${PieAPI.URL.PIES}/${id}`;
const validated = CreatePieSchema.parse(request);
const response = await this.apiClient.post(resource, validated);
return DetailedPieSchema.parse(response.data);
}

/**
* @see https://t212public-api-docs.redoc.ly/#operation/delete
*/
async deletePie(id: number): Promise<void> {
const resource = `${PieAPI.URL.PIES}/${id}`;
return this.apiClient.delete(resource);
}
}

0 comments on commit bd9e43f

Please # to comment.