ray1sx
ray1sx
TTCTheo's Typesafe Cult
Created by ray1sx on 8/12/2023 in #questions
TypeScript complaining even the code is correct?
type TradeSummary = Record<string, {
symbol: string;
datetime: string;
realizedPnl: number;
commission: number;
}>;


const closedTrades = Object.values(transactions.reduce((acc: TradeSummary, curr) => {
const { symbol, realizedPnl, commission } = curr.info;
const year = curr.datetime.slice(0, 4);
const month = curr.datetime.slice(5, 7);
const day = curr.datetime.slice(8, 10);
const datetime = `${year}-${month}-${day}`;

const key = `${symbol}_${datetime}`;

if (!acc[key]) {
acc[key] = { symbol, datetime, realizedPnl: 0, commission: 0};
}

acc[key].realizedPnl += parseFloat(realizedPnl);
acc[key].commission += parseFloat(commission);

acc[key].realizedPnl = parseFloat(acc[key].realizedPnl.toFixed(2))
acc[key].commission = parseFloat(acc[key].commission.toFixed(2));

return acc;
}, {}));
type TradeSummary = Record<string, {
symbol: string;
datetime: string;
realizedPnl: number;
commission: number;
}>;


const closedTrades = Object.values(transactions.reduce((acc: TradeSummary, curr) => {
const { symbol, realizedPnl, commission } = curr.info;
const year = curr.datetime.slice(0, 4);
const month = curr.datetime.slice(5, 7);
const day = curr.datetime.slice(8, 10);
const datetime = `${year}-${month}-${day}`;

const key = `${symbol}_${datetime}`;

if (!acc[key]) {
acc[key] = { symbol, datetime, realizedPnl: 0, commission: 0};
}

acc[key].realizedPnl += parseFloat(realizedPnl);
acc[key].commission += parseFloat(commission);

acc[key].realizedPnl = parseFloat(acc[key].realizedPnl.toFixed(2))
acc[key].commission = parseFloat(acc[key].commission.toFixed(2));

return acc;
}, {}));
So even I am initializing the object TypeScript keeps yelling acc[key] might be undefined. What am I missing?
6 replies
TTCTheo's Typesafe Cult
Created by ray1sx on 7/11/2023 in #questions
POST API endpoint only working if request made from POSTMAN
I am developing an app which needs to send information to an endpoint another client has put online to receive POST requests. This is just a simple POST request I send with a body, then the client saves its body to wherever he wants to. This endpoint has no Authorization header as it has been removed recently due to thinking it might be the cause for it not to work. So, now, it only works if you do the request from Postman. I created a simple Expressjs server and deployed on Vercel to mock that client endpoint for testing purposes (just returns the req.body to the client) I deployed my main project on Vercel and made POST requests to my mock Express app. Works. I deployed my main project on AWS Amplify and made POST requests to my mock Express app. Works. But when I try to attack the original endpoint URL the client has put online, it just does not work. It returns a 404 status everytime. But not from Postman (200 status). I just don't get it... maybe I am malforming the requests? I just want to be really sure the issues is not on my end. My code is part of a create-t3-app stack that uses tRPC first to fetch information from my PlanetScale DB and then makes a POST request to that endpoint:
35 replies
TTCTheo's Typesafe Cult
Created by ray1sx on 7/6/2023 in #questions
Experiencing hell while dealing with tRPC procedures and the "input needs to be an object"... help
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { env } from "~/env.mjs";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import axios from "axios";

export const profileRouter = createTRPCRouter({
sendProfileInfo: publicProcedure
.input(
z.object({
ig_account: z.string(),
followers_count: z.number(),
audience_city: z.record(z.number()),
audience_country: z.record(z.number()),
audience_gender_age: z.record(z.number()),
audience_locale: z.record(z.number()),
})
)
.mutation(async ({ input }) => {
const axiosConfig = {
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": `${env.TOKEN}`,
},
};
try {
const response = await axios.post($env.URL, input, axiosConfig);
return {response: response.data, inputType: typeof(input)};
} catch (e) {
throw new TRPCError({
message: "Error",
code: "BAD_REQUEST",
});
}
}),
});
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { env } from "~/env.mjs";
import { createTRPCRouter, publicProcedure } from "~/server/api/trpc";
import axios from "axios";

export const profileRouter = createTRPCRouter({
sendProfileInfo: publicProcedure
.input(
z.object({
ig_account: z.string(),
followers_count: z.number(),
audience_city: z.record(z.number()),
audience_country: z.record(z.number()),
audience_gender_age: z.record(z.number()),
audience_locale: z.record(z.number()),
})
)
.mutation(async ({ input }) => {
const axiosConfig = {
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": `${env.TOKEN}`,
},
};
try {
const response = await axios.post($env.URL, input, axiosConfig);
return {response: response.data, inputType: typeof(input)};
} catch (e) {
throw new TRPCError({
message: "Error",
code: "BAD_REQUEST",
});
}
}),
});
This is just a project that uses FB IG Graph API to retrieve information about the user and sends that to an endpoint. I googled and tried different approaches and I still can't see what is wrong. I have also checked that returning typeof(input) results in "object" so I cannot understand why tRPC is not validating it as an object. Also, I don't know why, but if I change the url to another url I get no trouble at all. This just has been driving me crazy the past few days trying to figure it out... Any help extremely appreciated!
14 replies