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

refactor: @formbricks/api package #782

Merged
merged 11 commits into from
Oct 1, 2023

Conversation

ShubhamPalriwala
Copy link
Contributor

@ShubhamPalriwala ShubhamPalriwala commented Sep 6, 2023

What does this PR do?

This is currently a Draft PR for feedback on the rewritten package! Once the changes are gtg, it will be renamed from the current formbricks-api-wrapper to our formbricks/api and the old package will be deleted! This is not done till now so that its easier to review and compare the packages!

Methods supported:

- api.client.response.create()
- api.client.response.update()
- api.client.display.markSurveyAsRespondedForPerson()
- api.client.display.markSurveyAsDisplayedForPerson()

ToDo before Merging

  • Remove old package
  • Rename this as per the old package
  • Update README when migrating to the directory
  • Find usages of the current API package in the monorepo and update them accordingly

Type of change

  • Chore (refactoring code, technical debt, workflow improvements)

How should this be tested?

  • Create a new file and call the package methods after initialising it!
  • Now access the available methods

Checklist

  • Added a screen recording or screenshots to this PR
  • Filled out the "How to test" section in this PR
  • Read the contributing guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand bits
  • Ran pnpm build
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues
  • Updated the Formbricks Docs if changes were necessary

@vercel
Copy link

vercel bot commented Sep 6, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
formbricks-cloud ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 1, 2023 9:50am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
formbricks-com ⬜️ Ignored (Inspect) Visit Preview Oct 1, 2023 9:50am

@github-actions
Copy link
Contributor

github-actions bot commented Sep 6, 2023

Thank you for following the naming conventions for pull request titles! 🙏

@github-actions
Copy link
Contributor

github-actions bot commented Sep 6, 2023

📦 Next.js Bundle Analysis for @formbricks/web

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@github-actions
Copy link
Contributor

github-actions bot commented Sep 6, 2023

📦 Next.js Bundle Analysis for @formbricks/web

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

Copy link
Member

@mattinannt mattinannt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShubhamPalriwala looks great! thank you for this first draft :-)

@@ -0,0 +1,37 @@
import fetch from "node-fetch";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our repository the package is mainly used client side (formbricks-js) but should also work server side in. What do we need to do to make it work both client and server side?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the node-fetch for that purpose itself! For non-browser interactions, to have the fetch, we can use the node-fetch packale

import { Result, err, ok, wrapThrows } from "@formbricks/errors";
import { NetworkError, ApiResponse } from "../types/index";

export async function makeRequest<T = any, E = any>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at the errors package and try to simplify this generic type as this is hard to understand. (If it makes sense?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! please take a look now

import { ApiConfig } from "../../types";

export class Client {
response: ResponseAPI;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer plural responses as it is also used that way in our api route. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, having singular is better since it's a method name and can differ from the API route! Singular keeps it simple and does not set any expectations

@github-actions
Copy link
Contributor

github-actions bot commented Sep 6, 2023

📦 Next.js Bundle Analysis for @formbricks/web

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2023

📦 Next.js Bundle Analysis for @formbricks/web

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@ShubhamPalriwala ShubhamPalriwala marked this pull request as ready for review September 7, 2023 12:12
@mattinannt
Copy link
Member

@ShubhamPalriwala we are also using the api package in the onboarding in apps/web/app/(app)/onboarding/page.tsx (passed through the js package). Can you please adjust this also to the new syntax? 🤗

@mattinannt
Copy link
Member

@ShubhamPalriwala Can you please solve the merge conflicts and also update the Formbricks Onboarding as mentioned in the comment above? 😊🙏

this.apiHost = baseUrl;
}

async markSurveyAsDisplayedForPerson({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should just name it markResponded ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, new survey methods are:
markDisplayedForPerson, markResponded

@@ -0,0 +1,15 @@
export type NetworkError = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we maybe move this error to @formbricks/types/errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks for pointing this out

…shubham/for-1151-rewrite-formbricks-api-package
@ShubhamPalriwala
Copy link
Contributor Author

hey, i've updated the PR as per your comments and resolved merge conflicts as well! however, i'm running into the following build issues:

@formbricks/demo:build: - info Linting and checking validity of types...
@formbricks/formbricks-com:build: - info Creating an optimized production build...
@formbricks/demo:build:
@formbricks/demo:build: ./pages/_app.tsx
@formbricks/demo:build: 34:6  Warning: React Hook useEffect has a missing dependency: 'router.events'. Either include it or remove the dependency array.  react-hooks/exhaustive-deps
@formbricks/demo:build:
@formbricks/demo:build: info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
@formbricks/demo:build: - info Creating an optimized production build...
@formbricks/demo:build: Failed to compile.
@formbricks/demo:build:
@formbricks/demo:build: ../../packages/js/dist/chunk-HIQASOGE.mjs
@formbricks/demo:build: Module not found: Can't resolve 'fs'
@formbricks/demo:build:
@formbricks/demo:build: https://nextjs.org/docs/messages/module-not-found
@formbricks/demo:build:
@formbricks/demo:build: Import trace for requested module:
@formbricks/demo:build: ../../packages/js/dist/index.mjs
@formbricks/demo:build: ./pages/_app.tsx
@formbricks/demo:build:
@formbricks/demo:build: ../../packages/js/dist/index.mjs
@formbricks/demo:build: Module not found: Can't resolve 'net'
@formbricks/demo:build:
@formbricks/demo:build: https://nextjs.org/docs/messages/module-not-found
@formbricks/demo:build:
@formbricks/demo:build: Import trace for requested module:
@formbricks/demo:build: ./pages/_app.tsx
@formbricks/demo:build:
@formbricks/demo:build:
@formbricks/demo:build: > Build failed because of webpack errors
@formbricks/demo:build:  ELIFECYCLE  Command failed with exit code 1.
@formbricks/demo:build: ERROR: command finished with error: command (/home/shubham/Desktop/formbricks/apps/demo) pnpm run build exited (1)
command (/home/shubham/Desktop/formbricks/apps/demo) pnpm run build exited (1)

 Tasks:    5 successful, 8 total
Cached:    4 cached, 8 total
  Time:    33.39s
Failed:    @formbricks/demo#build

 ERROR  run failed: command  exited (1)
 ELIFECYCLE  Command failed with exit code 1.

I tried adding the "fs" module as well but then after couldn't figure out why the build was having the "net" package.
Can you help me with this? :/

@mattinannt mattinannt self-assigned this Sep 18, 2023
@mattinannt
Copy link
Member

hey, i've updated the PR as per your comments and resolved merge conflicts as well! however, i'm running into the following build issues:

@formbricks/demo:build: - info Linting and checking validity of types...
@formbricks/formbricks-com:build: - info Creating an optimized production build...
@formbricks/demo:build:
@formbricks/demo:build: ./pages/_app.tsx
@formbricks/demo:build: 34:6  Warning: React Hook useEffect has a missing dependency: 'router.events'. Either include it or remove the dependency array.  react-hooks/exhaustive-deps
@formbricks/demo:build:
@formbricks/demo:build: info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
@formbricks/demo:build: - info Creating an optimized production build...
@formbricks/demo:build: Failed to compile.
@formbricks/demo:build:
@formbricks/demo:build: ../../packages/js/dist/chunk-HIQASOGE.mjs
@formbricks/demo:build: Module not found: Can't resolve 'fs'
@formbricks/demo:build:
@formbricks/demo:build: https://nextjs.org/docs/messages/module-not-found
@formbricks/demo:build:
@formbricks/demo:build: Import trace for requested module:
@formbricks/demo:build: ../../packages/js/dist/index.mjs
@formbricks/demo:build: ./pages/_app.tsx
@formbricks/demo:build:
@formbricks/demo:build: ../../packages/js/dist/index.mjs
@formbricks/demo:build: Module not found: Can't resolve 'net'
@formbricks/demo:build:
@formbricks/demo:build: https://nextjs.org/docs/messages/module-not-found
@formbricks/demo:build:
@formbricks/demo:build: Import trace for requested module:
@formbricks/demo:build: ./pages/_app.tsx
@formbricks/demo:build:
@formbricks/demo:build:
@formbricks/demo:build: > Build failed because of webpack errors
@formbricks/demo:build:  ELIFECYCLE  Command failed with exit code 1.
@formbricks/demo:build: ERROR: command finished with error: command (/home/shubham/Desktop/formbricks/apps/demo) pnpm run build exited (1)
command (/home/shubham/Desktop/formbricks/apps/demo) pnpm run build exited (1)

 Tasks:    5 successful, 8 total
Cached:    4 cached, 8 total
  Time:    33.39s
Failed:    @formbricks/demo#build

 ERROR  run failed: command  exited (1)
 ELIFECYCLE  Command failed with exit code 1.

I tried adding the "fs" module as well but then after couldn't figure out why the build was having the "net" package. Can you help me with this? :/

It seems to we an issue with node-fetch but I don't know why. But I think as we currently only use the api-package client side (inside js package and inside formbricks client components using the js package), we can just leave it out. Maybe if we need server-side support again later we can find a solution then.
I pushed a fix removing the node-fetch package.
Except for this, is this ready from your side?

@ShubhamPalriwala
Copy link
Contributor Author

yessir, it's good to go 🚀

Copy link
Member

@mattinannt mattinannt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ShubhamPalriwala Looks great, thank you :-)

@mattinannt mattinannt merged commit 769ceb1 into main Oct 1, 2023
11 of 12 checks passed
@mattinannt mattinannt deleted the shubham/for-1151-rewrite-formbricks-api-package branch October 1, 2023 09:50
waseemrabani pushed a commit to LuminosoInsight/formbricks that referenced this pull request Feb 2, 2024
* init: rewritten formbricks api package

* restrucure: client prepended formbricks api package

* feat: cleanup error templating and node-fetch for non browser access

* feat: replace package (build error for js packge due to api)

* fix: rename methods & move error type

* fix: imports of api via js

* remove node-fetch

---------

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
waseemrabani pushed a commit to LuminosoInsight/formbricks that referenced this pull request Feb 2, 2024
* init: rewritten formbricks api package

* restrucure: client prepended formbricks api package

* feat: cleanup error templating and node-fetch for non browser access

* feat: replace package (build error for js packge due to api)

* fix: rename methods & move error type

* fix: imports of api via js

* remove node-fetch

---------

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
waseemrabani pushed a commit to LuminosoInsight/formbricks that referenced this pull request Feb 2, 2024
* init: rewritten formbricks api package

* restrucure: client prepended formbricks api package

* feat: cleanup error templating and node-fetch for non browser access

* feat: replace package (build error for js packge due to api)

* fix: rename methods & move error type

* fix: imports of api via js

* remove node-fetch

---------

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants