Skip to content

Commit

Permalink
fix(vite-plugin-nitro): use proxy to relay requests without api prefix (
Browse files Browse the repository at this point in the history
  • Loading branch information
goetzrobin authored May 19, 2023
1 parent 6da21fd commit 0d74281
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 67 deletions.
2 changes: 1 addition & 1 deletion apps/analog-app-e2e-playwright/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"executor": "nx:run-commands",
"options": {
"cwd": "",
"command": "start-server-and-test 'nx serve analog-app' 3000 'nx run analog-app-e2e-playwright:vitest'"
"command": "start-server-and-test 'nx serve-nitro analog-app' http://localhost:3000 'nx run analog-app-e2e-playwright:vitest'"
}
},
"lint": {
Expand Down
64 changes: 6 additions & 58 deletions apps/analog-app/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,54 +28,6 @@
}
}
},
"build-ng": {
"executor": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/apps/analog-app-ng",
"index": "apps/analog-app/src/index.html",
"main": "apps/analog-app/src/main.ts",
"polyfills": "apps/analog-app/src/polyfills.ts",
"tsConfig": "apps/analog-app/tsconfig.app.json",
"assets": [
"apps/analog-app/src/favicon.ico",
"apps/analog-app/src/assets"
],
"styles": ["apps/analog-app/src/styles.css"],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"fileReplacements": [
{
"replace": "apps/analog-app/src/environments/environment.ts",
"with": "apps/analog-app/src/environments/environment.prod.ts"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"executor": "@nx/vite:dev-server",
"defaultConfiguration": "development",
Expand All @@ -93,17 +45,13 @@
}
}
},
"serve-ng": {
"executor": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "analog-app:build-ng:production"
},
"development": {
"browserTarget": "analog-app:build-ng:development"
}
"serve-nitro": {
"executor": "nx:run-commands",
"options": {
"cwd": "",
"command": "PORT=3000 node dist/apps/analog-app/analog/server/index.mjs"
},
"defaultConfiguration": "development"
"dependsOn": ["build"]
},
"extract-i18n": {
"executor": "@angular-devkit/build-angular:extract-i18n",
Expand Down
2 changes: 1 addition & 1 deletion apps/astro-app-e2e-playwright/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"executor": "nx:run-commands",
"options": {
"cwd": "",
"command": "start-server-and-test 'nx serve astro-app' 3000 'nx run astro-app-e2e-playwright:vitest'"
"command": "start-server-and-test 'nx serve astro-app' http://localhost:3000 'nx run astro-app-e2e-playwright:vitest'"
}
},
"lint": {
Expand Down
2 changes: 1 addition & 1 deletion apps/trpc-app-e2e-playwright/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"executor": "nx:run-commands",
"options": {
"cwd": "",
"command": "start-server-and-test 'yarn nx serve trpc-app' http://localhost:4205/api/health 'yarn nx run trpc-app-e2e-playwright:vitest'"
"command": "start-server-and-test 'yarn nx serve-nitro trpc-app' http://localhost:4205/api/health 'yarn nx run trpc-app-e2e-playwright:vitest'"
}
},
"lint": {
Expand Down
8 changes: 8 additions & 0 deletions apps/trpc-app/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
}
}
},
"serve-nitro": {
"executor": "nx:run-commands",
"options": {
"cwd": "",
"command": "PORT=4205 node dist/apps/trpc-app/analog/server/index.mjs"
},
"dependsOn": ["build"]
},
"extract-i18n": {
"executor": "@angular-devkit/build-angular:extract-i18n",
"options": {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@
"@ngtools/webpack": "^16.0.0",
"@nx-plus/docusaurus": "15.0.0-rc.0",
"@nx/cypress": "16.1.0",
"@nx/web": "16.1.0",
"@nx/eslint-plugin": "16.1.0",
"@nx/vite": "16.1.0",
"@nx/plugin": "16.1.0",
"@nx/jest": "16.1.0",
"@nx/plugin": "16.1.0",
"@nx/vite": "16.1.0",
"@nx/web": "16.1.0",
"@schematics/angular": "^16.0.0",
"@swc-node/register": "^1.4.2",
"@swc/cli": "0.1.62",
Expand Down
12 changes: 9 additions & 3 deletions packages/vite-plugin-nitro/src/lib/runtime/api-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
* The package is shipped as commonjs
* which won't be parsed by Nitro correctly.
*/
import { eventHandler } from 'h3';
import { eventHandler, proxyRequest } from 'h3';

export default eventHandler(async (event) => {
const apiPrefix = `/${import.meta.env.RUNTIME_CONFIG?.apiPrefix ?? 'api'}`;
if (event.req.url?.startsWith(apiPrefix)) {
return $fetch(`${event.req.url?.replace(apiPrefix, '')}`);
if (event.node.req.url?.startsWith(apiPrefix)) {
return proxyRequest(
event,
`${event.node.req.url?.replace(apiPrefix, '')}`,
{
fetch: $fetch.native,
}
);
}
});

0 comments on commit 0d74281

Please sign in to comment.