Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle loot-core types into the API #2053

Merged
merged 8 commits into from
Jan 20, 2024

Conversation

twk3
Copy link
Contributor

@twk3 twk3 commented Dec 9, 2023

So we can have loot-core be the source of truth for some types that get passed through between the API and bundle

  • Improves downstream development with API by including types (currently just the known return types are wired up)
  • Include type output with custom tsconfig for loot-core for the API
    • build loot-core (server focused) types at the same time as the webpack bundle
    • currently copying over existing loot-core handwritten .d.ts files (we may want to rename these to .ts files, so we only pull in the ones the loot-core main.ts actually use)
  • Use path aliases for dist vs dev tsconfigs
  • Convert api index to typescript as example
  • convert the send in mutators.ts to typescript to show mutators return types working
  • Permit ts-ignore for issues with our version of typescript

Testing

  • confirmed vscode was fine with package/api/index.ts (top level tsconfig alias working properly)
  • typecheck and lint pass on clean checkout, but also after api is build and dist and @types is populated
  • Ran a yarn build and yarn pack on the api, then installed into a test project (actual_mint_importer).
    • vscode giving the right typehints
    • didn't do any tsc linting from the test project

Future thoughts

tsconfig composite

I took a crack at turning on references and composite for the api, (and moving the tsconfig.dist.json in the api to be just tsconfig.dist.json), but this requires a lot more tuning of the tsconfig files (dropping the extends, fixing the rootDir, and fun with how includes and excludes combine). Wasn't worth it for this

@trafico-bot trafico-bot bot added the 🚧 WIP label Dec 9, 2023
Copy link

netlify bot commented Dec 9, 2023

Deploy Preview for actualbudget ready!

Name Link
🔨 Latest commit d4e1688
🔍 Latest deploy log https://app.netlify.com/sites/actualbudget/deploys/65ab2619caf6750008d0ae0c
😎 Deploy Preview https://deploy-preview-2053.demo.actualbudget.org/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

github-actions bot commented Dec 9, 2023

Bundle Stats — desktop-client

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
10 5.08 MB 0%

Changeset

No files were changed

View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
static/js/indexeddb-main-thread-worker-e59fee74.js 13.5 kB 0%
static/js/resize-observer.js 18.37 kB 0%
static/js/BackgroundImage.js 122.29 kB 0%
static/js/ButtonLink.js 379 B 0%
static/js/narrow.js 82.17 kB 0%
static/js/BalanceTooltip.js 6.07 kB 0%
static/js/FiltersMenu.js 28.92 kB 0%
static/js/wide.js 239.01 kB 0%
static/js/ReportRouter.js 1.95 MB 0%
static/js/index.js 2.63 MB 0%

Copy link
Contributor

github-actions bot commented Dec 9, 2023

Bundle Stats — loot-core

Hey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle.

As this PR is updated, I'll keep you updated on how the bundle size is impacted.

Total

Files count Total bundle size % Changed
1 1.23 MB 0%
Changeset
File Δ Size
packages/api/methods.ts 🆕 +12.28 kB 0 B → 12.28 kB
packages/loot-core/src/shared/schedules.ts 📈 +182 B (+2.01%) 8.86 kB → 9.04 kB
packages/api/methods.js 🔥 -12.25 kB (-100%) 12.25 kB → 0 B
View detailed bundle breakdown

Added

No assets were added

Removed

No assets were removed

Bigger

No assets were bigger

Smaller

No assets were smaller

Unchanged

Asset File Size % Changed
kcab.worker.js 1.23 MB 0%

@twk3 twk3 force-pushed the api-bundle-loot-core-types branch from 206d4ee to e63c8c4 Compare December 9, 2023 20:15
@twk3 twk3 changed the title [WIP] Bundle loot-core types into the API Bundle loot-core types into the API Dec 10, 2023
@MatissJanis
Copy link
Member

👋 Overall I really like where this is going. Adding proper types to the api package would be amazing.

But I think we're not there yet with this PR. I looked through the output type definitions and they aren't yet pulling through the full type objects. Everything is typed as any.

For example: methods.getBudgetMonth. (red: current; green: what my expectation would have been)

- export function getBudgetMonth(month: any): any;
+ export function getBudgetMonth(month: string): {
+    month: unknown;
+    incomeAvailable: number;
+    lastMonthOverspent: number;
+    forNextMonth: number;
+    totalBudgeted: number;
+    toBudget: number;
+    fromLastMonth: number;
+    totalIncome: number;
+    totalSpent: number;
+    totalBalance: number;
+    categoryGroups: Record<string, unknown>[];
+  };

Is there anything we could do to pull in these full, rich types?

@twk3 twk3 force-pushed the api-bundle-loot-core-types branch from e63c8c4 to 3d45bbe Compare December 20, 2023 21:38
@twk3 twk3 changed the title Bundle loot-core types into the API [WIP]Bundle loot-core types into the API Dec 20, 2023
@twk3
Copy link
Contributor Author

twk3 commented Dec 20, 2023

👋 Overall I really like where this is going. Adding proper types to the api package would be amazing.

But I think we're not there yet with this PR. I looked through the output type definitions and they aren't yet pulling through the full type objects. Everything is typed as any.

For example: methods.getBudgetMonth. (red: current; green: what my expectation would have been)

- export function getBudgetMonth(month: any): any;
+ export function getBudgetMonth(month: string): {
+    month: unknown;
+    incomeAvailable: number;
+    lastMonthOverspent: number;
+    forNextMonth: number;
+    totalBudgeted: number;
+    toBudget: number;
+    fromLastMonth: number;
+    totalIncome: number;
+    totalSpent: number;
+    totalBalance: number;
+    categoryGroups: Record<string, unknown>[];
+  };

Is there anything we could do to pull in these full, rich types?

@MatissJanis yep, they are in there now. Needed to bring the handlers types into the mutators and typescripting the send method. I had just initially done the index because it was smaller.

I've moved this back into WIP, as doing the mutators has shown that there is already some fixes needed in loot-core to get runHandler also passing around types. And that the api-handlers and server handlers types have diverged. (it's all working here in this MR, but I want to split those fixes into their own).

I also want to finish more of the mutator api tests before fully moving mutator.js to typescript. As it's hard to catch typescript errors for methods that are never called.

And I need to take a closer loot at the security finding from codeQL.

@MatissJanis
Copy link
Member

Amazing! Thank you for all this work here. It's definitely a massive improvement!

Let me know if there's any way I could assist.

@twk3
Copy link
Contributor Author

twk3 commented Dec 30, 2023

Split the loot-core fixes to #2136 which should be ready for review, and is useful in it's own right separate from the api.

So we can have loot-core be the source of truth
for some types that get passed through

- Improves downstream development with API by including types
- Use path aliases for dist vs dev tsconfigs
- Convert api index to typescript as example
- Permit ts-ignore for issues with our version of typescript
@twk3 twk3 force-pushed the api-bundle-loot-core-types branch from 3d45bbe to ab03b4d Compare January 17, 2024 00:20
@twk3 twk3 changed the title [WIP]Bundle loot-core types into the API Bundle loot-core types into the API Jan 17, 2024
@twk3 twk3 requested a review from MatissJanis January 17, 2024 23:43
@twk3
Copy link
Contributor Author

twk3 commented Jan 17, 2024

@MatissJanis this is ready for another review. Just the already existing return types are wired up, so I've disabled strict for the methods.ts to keep this PR smaller. Will need a few followups to get into useful shape for folks using the api.

@MatissJanis
Copy link
Member

Merged master into your branch to see if it still passes CI with strict mode enabled. Hope you don't mind.

@MatissJanis
Copy link
Member

We seem to be missing something in the output.

Screenshot 2024-01-18 at 18 32 41

MatissJanis
MatissJanis previously approved these changes Jan 19, 2024
joel-jeremy
joel-jeremy previously approved these changes Jan 19, 2024
MatissJanis
MatissJanis previously approved these changes Jan 19, 2024
@twk3 twk3 merged commit 0045d92 into actualbudget:master Jan 20, 2024
19 checks passed
@twk3 twk3 deleted the api-bundle-loot-core-types branch January 20, 2024 18:30
@trafico-bot trafico-bot bot added ✨ Merged Pull Request has been merged successfully and removed ✅ Approved labels Jan 20, 2024
FlorianLang06 pushed a commit to FlorianLang06/actual that referenced this pull request Mar 7, 2024
* Bundle loot-core types into the API

So we can have loot-core be the source of truth
for some types that get passed through

- Improves downstream development with API by including types
- Use path aliases for dist vs dev tsconfigs
- Convert api index to typescript as example
- Permit ts-ignore for issues with our version of typescript

---------

Co-authored-by: Matiss Janis Aboltins <matiss@mja.lv>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ Merged Pull Request has been merged successfully
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants