UNPKG

@risemaxi/api-client

Version:

Client Library for Rise

582 lines (581 loc) 18.7 kB
import { Type } from "@sinclair/typebox"; export const PlanType = Type.Union([ Type.Literal("Savings"), Type.Literal("Build Wealth"), Type.Literal("Rent"), Type.Literal("Wedding"), Type.Literal("Kids"), Type.Literal("Business"), Type.Literal("School"), Type.Literal("Fixed Income"), Type.Literal("Stocks"), Type.Literal("Real Estate"), Type.Literal("Home"), Type.Literal("Travel"), ]); export const AssetClass = Type.String(); export const PortfolioConfigData = Type.Partial(Type.Object({ percentage: Type.Number(), asset_class: AssetClass, })); export const PortfolioConfig = Type.Partial(Type.Object({ "a81bc81b-dead-4e5d-abff-90865d1e13b1": PortfolioConfigData, "b21dc81b-bcde-4e5d-bcda-90865d1e13b2": PortfolioConfigData, })); export const CreatePlanDTO = Type.Object({ plan_name: Type.String(), plan_type: PlanType, owner_id: Type.String(), portfolio_config: PortfolioConfig, cycle: Type.Optional(Type.Union([Type.Number(), Type.Undefined()])), duration: Type.Number(), metadata: Type.Optional(Type.Union([ Type.Partial(Type.Object({ goal_amount: Type.Number(), created_by: Type.String(), })), Type.Undefined(), ])), }); export const EditPlanDTO = Type.Partial(Type.Object({ plan_name: Type.String(), picture: Type.Union([Type.String(), Type.Null()]), })); export const PlanStatus = Type.Union([ Type.Literal("active"), Type.Literal("inactive"), Type.Literal("matured"), ]); export const AutoInvestFrequency = Type.Union([ Type.Literal("daily"), Type.Literal("weekly"), Type.Literal("monthly"), ]); export const AutoInvestConfig = Type.Partial(Type.Object({ month_day: Type.Number(), week_day: Type.Number(), hour: Type.Number(), amount: Type.Number(), current_attempt_count: Type.Number(), })); export const Plan = Type.Object({ id: Type.Optional(Type.Union([Type.String(), Type.Undefined()])), plan_name: Type.String(), owner_id: Type.String(), portfolio_config: PortfolioConfig, status: PlanStatus, duration: Type.Number(), cycle: Type.Number(), capital: Type.Number(), investment: Type.Number(), picture: Type.Optional(Type.Union([Type.String(), Type.Null(), Type.Undefined()])), reinvest: Type.Boolean(), auto_invest_enabled: Type.Boolean(), auto_invest_frequency: AutoInvestFrequency, auto_invest_config: AutoInvestConfig, next_auto_invest_date: Type.String(), next_compound_date: Type.String(), created_at: Type.Optional(Type.Union([Type.String(), Type.Undefined()])), total_funded: Type.Number(), total_withdrawn: Type.Number(), total_returns: Type.Number(), available_returns: Type.Number(), maturity_date: Type.String(), plan_type: PlanType, metadata: Type.Optional(Type.Union([ Type.Partial(Type.Object({ created_by: Type.String(), goal_amount: Type.Number(), is_gifted: Type.Boolean(), })), Type.Undefined(), ])), }); export const PaginatedPlanResult = Type.Partial(Type.Object({ items: Type.Array(Plan), item_count: Type.Number(), offset: Type.Number(), })); export const TransactionSource = Type.Union([ Type.Literal("crypto_wallet"), Type.Literal("account"), Type.Literal("virtual_account"), Type.Literal("bank_account"), Type.Literal("mobile_money"), Type.Literal("card"), Type.Literal("virtual_card"), Type.Literal("api_vendor"), Type.Literal("plan"), Type.Literal("wallet"), Type.Literal("portfolio"), Type.Literal("revenue"), Type.Literal("system"), Type.Literal("commission"), Type.Literal("gift"), Type.Literal("returns"), ]); export const TransactionDestination = Type.Union([ Type.Literal("crypto_wallet"), Type.Literal("account"), Type.Literal("virtual_account"), Type.Literal("bank_account"), Type.Literal("mobile_money"), Type.Literal("card"), Type.Literal("virtual_card"), Type.Literal("api_vendor"), Type.Literal("plan"), Type.Literal("wallet"), Type.Literal("portfolio"), Type.Literal("revenue"), Type.Literal("system"), Type.Literal("commission"), Type.Literal("gift"), Type.Literal("returns"), ]); export const TransactionMetadata = Type.Partial(Type.Object({ source: TransactionSource, destination: TransactionDestination, source_id: Type.String(), destination_id: Type.String(), portfolio_config: PortfolioConfig, wallet_fund_transaction_id: Type.Union([Type.String(), Type.Null()]), fee_transaction_id: Type.Union([Type.String(), Type.Null()]), fee: Type.Union([Type.Number(), Type.Null()]), app_version: Type.Union([Type.String(), Type.Null()]), description: Type.Union([Type.String(), Type.Null()]), initiated_by: Type.Union([Type.String(), Type.Null()]), is_payout: Type.Union([Type.Boolean(), Type.Null()]), is_reinvested: Type.Union([Type.Boolean(), Type.Null()]), })); export const TransactionRequest = Type.Object({ id: Type.String(), plan_id: Type.String(), owner_id: Type.String(), request_type: Type.Union([ Type.Literal("funding"), Type.Literal("withdrawal"), ]), status: Type.Union([ Type.Literal("pending"), Type.Literal("successful"), Type.Literal("failed"), Type.Literal("canceled"), Type.Literal("processing"), ]), currency: Type.String(), amount: Type.Number(), metadata: Type.Optional(Type.Union([TransactionMetadata, Type.Undefined()])), }); export const PaginatedTransactionRequestResult = Type.Partial(Type.Object({ data: Type.Array(TransactionRequest), item_count: Type.Number(), offset: Type.Number(), })); export const MaturityLog = Type.Partial(Type.Object({ plan_id: Type.String(), owner_id: Type.String(), matured_at: Type.String(), cycle: Type.Number(), capital: Type.Number(), total_returns: Type.Number(), available_returns: Type.Number(), total_funded: Type.Number(), total_withdrawn: Type.Number(), created_at: Type.String(), })); export const PlanReturn = Type.Partial(Type.Object({ plan_id: Type.String(), description: Type.String(), interest_date: Type.String(), amount: Type.Number(), })); export const PerformanceType = Type.Union([ Type.Literal("daily"), Type.Literal("weekly"), Type.Literal("monthly"), Type.Literal("yearly"), ]); export const PlanPerformance = Type.Partial(Type.Object({ plan_id: Type.String(), type: PerformanceType, description: Type.String(), holding_period_return: Type.Number(), geometric_return: Type.Number(), twr: Type.Number(), date: Type.String(), starting_balance: Type.Number(), closing_balance: Type.Number(), deposits: Type.Number(), withdrawals: Type.Number(), cashflow: Type.Number(), returns: Type.Number(), })); export const PaginatedMaturityLogResult = Type.Partial(Type.Object({ items: Type.Array(MaturityLog), item_count: Type.Number(), offset: Type.Number(), })); export const PaginatedPlanReturnsResult = Type.Partial(Type.Object({ items: Type.Array(PlanReturn), item_count: Type.Number(), offset: Type.Number(), })); export const PaginatedPlanPerformancesResult = Type.Partial(Type.Object({ items: Type.Array(PlanPerformance), item_count: Type.Number(), offset: Type.Number(), })); export const PlanConfigType = Type.Union([ Type.Literal("autofund"), Type.Literal("portfolio"), ]); export const AutoInvestConfigDTO = Type.Partial(Type.Object({ auto_invest_enabled: Type.Boolean(), auto_invest_frequency: AutoInvestFrequency, auto_invest_config: AutoInvestConfig, })); export const ConfigDTO = Type.Object({ type: PlanConfigType, config: Type.Union([AutoInvestConfigDTO, PortfolioConfig]), }); export const SendGiftDTO = Type.Object({ plan_name: Type.String(), portfolio_config: PortfolioConfig, amount: Type.Number(), duration: Type.Number(), description: Type.String(), recipient_identifier: Type.String(), plan_type: PlanType, }); export const Gift = Type.Partial(Type.Object({ id: Type.String(), owner_id: Type.String(), recipient_identifier: Type.String(), plan_name: Type.String(), amount: Type.Number(), duration: Type.Number(), description: Type.String(), status: Type.Union([ Type.Literal("pending"), Type.Literal("accepted"), Type.Literal("declined"), Type.Literal("cancelled"), ]), portfolio_config: PortfolioConfig, plan_type: PlanType, created_at: Type.String(), updated_at: Type.String(), })); export const PlanTransactionDTO = Type.Object({ plan_id: Type.String(), owner_id: Type.String(), amount: Type.Number(), type: Type.Union([Type.Literal("funding"), Type.Literal("withdrawal")]), should_waive_charges: Type.Optional(Type.Union([Type.Boolean(), Type.Undefined()])), penalty_fee_percent: Type.Optional(Type.Union([Type.Number(), Type.Undefined()])), transaction_request_id: Type.Optional(Type.Union([Type.String(), Type.Undefined()])), }); export const HistoricalPerformance = Type.Partial(Type.Object({ year: Type.Number(), returns_percentage: Type.Number(), })); export const Composition = Type.Partial(Type.Object({ asset_class: Type.String(), percentage: Type.Number(), })); export const CustomPlan = Type.Partial(Type.Object({ name: Type.String(), category: Type.String(), plan_type: Type.String(), description: Type.String(), landing_offset_image: Type.String(), plan_image: Type.String(), is_live: Type.Boolean(), has_add_money_screen: Type.String(), risk_level: Type.String(), returns_range: Type.String(), historical_perfromance: Type.Array(HistoricalPerformance), composition: Type.Unknown(), })); export const CustomPlans = Type.Array(CustomPlan); const __ENDPOINTS_START__ = Type.Object({}); export const post_Plans = Type.Object({ method: Type.Literal("POST"), path: Type.Literal("/plans"), parameters: Type.Object({ body: CreatePlanDTO, }), response: Plan, }); export const get_Plans = Type.Object({ method: Type.Literal("GET"), path: Type.Literal("/plans"), parameters: Type.Object({ query: Type.Partial(Type.Object({ status: Type.Union([ Type.Literal("active"), Type.Literal("inactive"), Type.Literal("completed"), ]), offset: Type.Number(), limit: Type.Number(), })), }), response: PaginatedPlanResult, }); export const get_PlansId = Type.Object({ method: Type.Literal("GET"), path: Type.Literal("/plans/{id}"), parameters: Type.Object({ path: Type.Object({ id: Type.String(), }), }), response: Plan, }); export const patch_PlansId = Type.Object({ method: Type.Literal("PATCH"), path: Type.Literal("/plans/{id}"), parameters: Type.Object({ path: Type.Object({ id: Type.String(), }), body: EditPlanDTO, }), response: Plan, }); export const delete_PlansId = Type.Object({ method: Type.Literal("DELETE"), path: Type.Literal("/plans/{id}"), parameters: Type.Object({ path: Type.Object({ id: Type.String(), }), }), response: Type.Unknown(), }); export const get_PlansIdhistory = Type.Object({ method: Type.Literal("GET"), path: Type.Literal("/plans/{id}/history"), parameters: Type.Object({ query: Type.Partial(Type.Object({ offset: Type.Number(), limit: Type.Number(), order: Type.Union([Type.Literal("asc"), Type.Literal("desc")]), })), path: Type.Object({ id: Type.String(), }), }), response: PaginatedMaturityLogResult, }); export const get_PlansIdreturns = Type.Object({ method: Type.Literal("GET"), path: Type.Literal("/plans/{id}/returns"), parameters: Type.Object({ query: Type.Partial(Type.Object({ offset: Type.Number(), limit: Type.Number(), order: Type.Union([Type.Literal("asc"), Type.Literal("desc")]), })), path: Type.Object({ id: Type.String(), }), }), response: PaginatedPlanReturnsResult, }); export const get_PlansIdperformances = Type.Object({ method: Type.Literal("GET"), path: Type.Literal("/plans/{id}/performances"), parameters: Type.Object({ query: Type.Object({ offset: Type.Union([Type.Number(), Type.Undefined()]), limit: Type.Union([Type.Number(), Type.Undefined()]), order: Type.Union([ Type.Literal("asc"), Type.Literal("desc"), Type.Undefined(), ]), type: Type.Union([ Type.Literal("daily"), Type.Literal("weekly"), Type.Literal("monthly"), Type.Literal("yearly"), ]), }), path: Type.Object({ id: Type.String(), }), }), response: PaginatedPlanPerformancesResult, }); export const post_PlansIdconfig = Type.Object({ method: Type.Literal("POST"), path: Type.Literal("/plans/{id}/config"), parameters: Type.Object({ path: Type.Object({ id: Type.String(), }), body: ConfigDTO, }), response: Plan, }); export const post_PlansIdtoggleReinvest = Type.Object({ method: Type.Literal("POST"), path: Type.Literal("/plans/{id}/toggle-reinvest"), parameters: Type.Object({ path: Type.Object({ id: Type.String(), }), }), response: Plan, }); export const get_Config = Type.Object({ method: Type.Literal("GET"), path: Type.Literal("/config"), parameters: Type.Object({ query: Type.Partial(Type.Object({ entity: Type.String(), filter_key: Type.Union([ Type.Literal("category"), Type.Literal("is_live"), Type.Literal("pure"), ]), filter_value: Type.Literal("goal"), })), }), response: CustomPlans, }); export const post_Gifts = Type.Object({ method: Type.Literal("POST"), path: Type.Literal("/gifts"), parameters: Type.Object({ body: SendGiftDTO, }), response: Gift, }); export const post_GiftsGiftIdAction = Type.Object({ method: Type.Literal("POST"), path: Type.Literal("/gifts/{giftId}/{action}"), parameters: Type.Object({ path: Type.Object({ giftId: Type.String(), action: Type.Union([Type.Literal("accept"), Type.Literal("decline")]), }), }), response: Gift, }); export const patch_GiftsIdcancel = Type.Object({ method: Type.Literal("PATCH"), path: Type.Literal("/gifts/{id}/cancel"), parameters: Type.Object({ path: Type.Object({ id: Type.String(), }), }), response: Gift, }); export const post_PlanstransactionRequests = Type.Object({ method: Type.Literal("POST"), path: Type.Literal("/plans/transaction-requests"), parameters: Type.Object({ body: PlanTransactionDTO, }), response: Plan, }); export const get_PlanstransactionRequests = Type.Object({ method: Type.Literal("GET"), path: Type.Literal("/plans/transaction-requests"), parameters: Type.Object({ query: Type.Object({ plan_id: Type.String(), owner_id: Type.Union([Type.String(), Type.Undefined()]), order: Type.Union([ Type.Literal("desc"), Type.Literal("asc"), Type.Undefined(), ]), start_date: Type.Union([Type.String(), Type.Undefined()]), end_date: Type.Union([Type.String(), Type.Undefined()]), page: Type.Union([Type.Number(), Type.Undefined()]), limit: Type.Union([Type.Number(), Type.Undefined()]), }), }), response: PaginatedTransactionRequestResult, }); const __ENDPOINTS_END__ = Type.Object({}); // <EndpointByMethod> export const EndpointByMethod = { post: { "/plans": post_Plans, "/plans/{id}/config": post_PlansIdconfig, "/plans/{id}/toggle-reinvest": post_PlansIdtoggleReinvest, "/gifts": post_Gifts, "/gifts/{giftId}/{action}": post_GiftsGiftIdAction, "/plans/transaction-requests": post_PlanstransactionRequests, }, get: { "/plans": get_Plans, "/plans/{id}": get_PlansId, "/plans/{id}/history": get_PlansIdhistory, "/plans/{id}/returns": get_PlansIdreturns, "/plans/{id}/performances": get_PlansIdperformances, "/config": get_Config, "/plans/transaction-requests": get_PlanstransactionRequests, }, patch: { "/plans/{id}": patch_PlansId, "/gifts/{id}/cancel": patch_GiftsIdcancel, }, delete: { "/plans/{id}": delete_PlansId, }, put: { "/plans/{id}": delete_PlansId, }, }; // </ApiClientTypes> // <ApiClient> export class ApiClient { fetcher; baseUrl = ""; constructor(fetcher) { this.fetcher = fetcher; } setBaseUrl(baseUrl) { this.baseUrl = baseUrl; return this; } // <ApiClient.post> post(path, ...params) { return this.fetcher("post", this.baseUrl + path, params[0]); } // </ApiClient.post> // <ApiClient.get> get(path, ...params) { return this.fetcher("get", this.baseUrl + path, params[0]); } // </ApiClient.get> // <ApiClient.patch> patch(path, ...params) { return this.fetcher("patch", this.baseUrl + path, params[0]); } // </ApiClient.patch> // <ApiClient.delete> delete(path, ...params) { return this.fetcher("delete", this.baseUrl + path, params[0]); } } export function createApiClient(fetcher, baseUrl) { return new ApiClient(fetcher).setBaseUrl(baseUrl ?? ""); } /** Example usage: const api = createApiClient((method, url, params) => fetch(url, { method, body: JSON.stringify(params) }).then((res) => res.json()), ); api.get("/users").then((users) => console.log(users)); api.post("/users", { body: { name: "John" } }).then((user) => console.log(user)); api.put("/users/:id", { path: { id: 1 }, body: { name: "John" } }).then((user) => console.log(user)); */ // </ApiClient