Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Task/improve type safety in API helpers (#8352)

Kenrick Tandriana8193354d810
diff --git a/apps/api/src/helper/bearer-token.helper.ts b/apps/api/src/helper/bearer-token.helper.ts
index 2c7f9cd65e..d9bc82c43e 100644
--- a/apps/api/src/helper/bearer-token.helper.ts
+++ b/apps/api/src/helper/bearer-token.helper.ts
@@ -40,8 +40,9 @@ export function getActiveImpersonationOfBearerToken(
* hence the transport lists only the tools which the access covers.
*/
export function getMcpUserOfBearerToken(request: unknown) {
- const impersonationOfBearerToken =
- getActiveImpersonationOfBearerToken(request);
+ const impersonationOfBearerToken = getActiveImpersonationOfBearerToken(
+ request as Pick<RequestWithUser, 'impersonationOfBearerToken'>
+ );
return impersonationOfBearerToken
? { scopes: impersonationOfBearerToken.scopes }
@@ -54,8 +55,9 @@ export function getMcpUserOfBearerToken(request: unknown) {
* they apply only to an access which reads the monetary values.
*/
export function hasLimitsOfBasicSubscriptionOfBearerToken(request: unknown) {
- const impersonationOfBearerToken =
- getActiveImpersonationOfBearerToken(request);
+ const impersonationOfBearerToken = getActiveImpersonationOfBearerToken(
+ request as Pick<RequestWithUser, 'impersonationOfBearerToken'>
+ );
return (
impersonationOfBearerToken?.userSubscription?.type ===
diff --git a/apps/api/src/helper/country.helper.ts b/apps/api/src/helper/country.helper.ts
index 119f5e44c1..9523dec339 100644
--- a/apps/api/src/helper/country.helper.ts
+++ b/apps/api/src/helper/country.helper.ts
@@ -9,7 +9,7 @@ export function getCountryCodeByName({
aliases?: Record<string, string>;
dataSource?: string;
name: string;
-}): string {
+}): string | undefined {
if (aliases[name]) {
return aliases[name];
}
diff --git a/apps/api/src/helper/data-source.helper.spec.ts b/apps/api/src/helper/data-source.helper.spec.ts
index 3bfef82add..68b578c92e 100644
--- a/apps/api/src/helper/data-source.helper.spec.ts
+++ b/apps/api/src/helper/data-source.helper.spec.ts
@@ -29,9 +29,9 @@ function createConfigurationService({
describe('DataSourceHelper', () => {
describe('isValidEncodedDataSource', () => {
it('Accepts an encoded data source', () => {
- expect(isValidEncodedDataSource(encodeDataSource(DataSource.YAHOO))).toBe(
- true
- );
+ expect(
+ isValidEncodedDataSource(encodeDataSource(DataSource.YAHOO) ?? '')
+ ).toBe(true);
});
it('Refuses a data source which is not encoded', () => {
diff --git a/apps/api/src/helper/data-source.helper.ts b/apps/api/src/helper/data-source.helper.ts
index fb0ded3f1f..831705e5ca 100644
--- a/apps/api/src/helper/data-source.helper.ts
+++ b/apps/api/src/helper/data-source.helper.ts
@@ -64,7 +64,7 @@ function hashDataSource(dataSource: DataSource) {
return createHash('sha256').update(dataSource).digest('hex').slice(0, 8);
}
-export function decodeDataSource(encodedDataSource: string) {
+export function decodeDataSource(encodedDataSource?: string) {
if (!encodedDataSource) {
return undefined;
}
@@ -128,7 +128,7 @@ export function transformDataSourceInRequest({
configurationService: ConfigurationService;
dataSource?: string;
}) {
- if (Object.hasOwn(DataSource, dataSource)) {
+ if (dataSource && Object.hasOwn(DataSource, dataSource)) {
return getUnmaskedGhostfolioDataSource({
dataSource: dataSource as DataSource,
ghostfolioDataSources: getGhostfolioDataSources({ configurationService })
diff --git a/apps/api/src/helper/markdown-table.helper.ts b/apps/api/src/helper/markdown-table.helper.ts
index 3bc8a7a8c7..26979cde3d 100644
--- a/apps/api/src/helper/markdown-table.helper.ts
+++ b/apps/api/src/helper/markdown-table.helper.ts
@@ -17,7 +17,7 @@ export function getTableInput<T, C = void>({
rows: rows.map((row) => {
return columnDefinitions.reduce(
(tableRow, { getValue, name }) => {
- tableRow[name] = getValue(row, context);
+ tableRow[name] = getValue(row, context as C);
return tableRow;
},
diff --git a/apps/api/src/helper/portfolio.helper.ts b/apps/api/src/helper/portfolio.helper.ts
index b8152414bb..f91ab0f281 100644
--- a/apps/api/src/helper/portfolio.helper.ts
+++ b/apps/api/src/helper/portfolio.helper.ts
@@ -21,7 +21,7 @@ export function convertValuesToPercentages({
const totalValue = holdings
.map(({ valueInBaseCurrency }) => {
- return valueInBaseCurrency;
+ return valueInBaseCurrency ?? 0;
})
.reduce((a, b) => {
return a + b;
@@ -32,7 +32,7 @@ export function convertValuesToPercentages({
? holding.investment / totalInvestment
: 0;
holding.valueInPercentage = totalValue
- ? holding.valueInBaseCurrency / totalValue
+ ? (holding.valueInBaseCurrency ?? 0) / totalValue
: 0;
}
diff --git a/apps/api/src/helper/string.helper.ts b/apps/api/src/helper/string.helper.ts
index 75f9d00fd0..4acb4cb235 100644
--- a/apps/api/src/helper/string.helper.ts
+++ b/apps/api/src/helper/string.helper.ts
@@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto';
export function getRandomString(length: number) {
const bytes = randomBytes(length);
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- const result = [];
+ const result: string[] = [];
for (let i = 0; i < length; i++) {
const randomByte = bytes[i];