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

Add NuxtJS support #4

Merged
merged 6 commits into from
Oct 30, 2024
Merged

Conversation

unibeck
Copy link
Contributor

@unibeck unibeck commented Oct 28, 2024

Added NuxtJS support. The biggest difference from the Next impl is the use of Nuxt's server api routes to handle Polar SDK. In Nuxt, this is how we keep PATs server-side and hidden from the user.

This impl makes some assumptions, based on what I deem a typical Nuxt project:

  • Directory structure (users can manually move the files to their repo's correct locations, if needed)
  • Magic imports (users can manually import dependencies, if needed)
  • useRuntimeConfig for secret handling

I used GitHub Copilot Workspace for the initial migration, then hand crafted the rest.

* **README.md**
  - Update usage instructions to include Nuxt v3 support

* **package.json**
  - Add Nuxt v3 dependencies

* **src/cli.tsx**
  - Add check for Nuxt v3 projects
  - Update prompts to handle Nuxt v3 projects

* **src/utils.ts**
  - Add function to check for Nuxt v3 projects

* **templates/nuxt3**
  - Add Nuxt v3 webhook route template
  - Add Nuxt v3 checkout confirmation page template
  - Add Nuxt v3 checkout route template
  - Add Nuxt v3 Polar client template
  - Add Nuxt v3 products page template
* **Import**
  - Import `isNuxtDirectory` from `utils.js`
* **Precheck**
  - Add check for Nuxt v3 projects in `precheck` function
  - Update error message to include Nuxt v3 projects
* **Precheck Disclaimer**
  - Update spinner label to include Nuxt v3 projects
* **src/utils.ts**
  - Add `src/pages` to `nuxtIndicators` in `isNuxtDirectory` function

* **src/template.ts**
  - Add `pages` and `src/pages` to `resolveAppDirectory` function
  - Update error message to include `pages` and `src/pages` directories
* **src/cli.tsx**
  - Add checks for Nuxt v3 projects
  - Update prompts to handle Nuxt v3 projects
  - Copy Nuxt templates if the project is a Nuxt project

* **src/template.ts**
  - Update `copyProductsTemplate`, `copyCheckoutTemplate`, and `copyWebhooksTemplate` to handle Nuxt v3 projects
  - Add framework parameter to template copy functions
@unibeck
Copy link
Contributor Author

unibeck commented Oct 28, 2024

Ran npx polar-init on this Nuxt Starter template as a test and it worked perfectly. Here are the changes to expect.
image

@emilwidlund would you be able to test this branch against a NextJS project to make sure there are no regressions?

@emilwidlund
Copy link
Member

@unibeck Amazing! Very much appreciated! Will take a look at this today.

@emilwidlund
Copy link
Member

@unibeck Looks really good! I'm however encountering an issue when running the nuxt-starter.

image

Looks like something is a little off with that usePolar import?

@emilwidlund
Copy link
Member

Also, it would be nice if we somehow could pre-populate the polarOrgId in the runtimeConfig automatically.

@unibeck
Copy link
Contributor Author

unibeck commented Oct 29, 2024

Ah this is how ~/ is interpreted. On my project, ~/ is the repo's root dir, whereas the default of ~/ is ./app/. Sorry about that, let me move the ./utils dir into the ./app dir.

Let me look into the pre-populating polarOrgId as well, nice suggestion.

@unibeck
Copy link
Contributor Author

unibeck commented Oct 29, 2024

I fixed the polar util issue.

As for pre-populating the values in runtimeConfig, I did add the NUXT_ prefix which helps Nuxt default import those env vars. Though there are two more steps the user has to manually take to get those env vars.

  1. run Nuxt with --dotenv .env.local
  2. manually add the polarServer, polarAccessToken, polarOrganizationId, polarWebhookSecret keys to runtimeConfig in the nuxt.config.(ts/mjs/js) file. See Nuxt's docs

Your desired variables must be defined in your nuxt.config. This ensures that arbitrary environment variables are not exposed to your application code.

I tried automating this, though it gets convoluted very quickly. We would need to support each nuxt.config file types, then parse it to find defineNuxtConfig and the runtimeConfig objects. Instead, is there a place in the docs/cli where we can tell users of these two extra manually steps?

@emilwidlund
Copy link
Member

@unibeck Nice work! I'm going to merge this. I'll add a few tweaks in regards to notifying about manually adding the runtimeConfig, etc.

I'll issue a new release to NPM once I have that in place.

Thanks again! Really nice to see this evolve & to support multiple types of frameworks going forward :)

@emilwidlund emilwidlund merged commit 9c56422 into polarsource:main Oct 30, 2024
1 check failed
@unibeck
Copy link
Contributor Author

unibeck commented Oct 30, 2024

Thanks Emil! I'll keep an eye out for the release to share with the Nuxt community and on X.

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