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

fix(trpc): adding host and port env variables to nitro dev process & … #695

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions apps/trpc-app-e2e-playwright/tests/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,20 @@ describe('tRPC Demo App', () => {
});

test<TRPCTestContext>(`
If user enters the first note the note should be storedsuccessfully and listed in the notes array.
Still unauthorized the user should not be able to delete the note and the error should be displayed.
After the users clicks the Login button and gets authorized, deleting the note again should work successfully,
If user enters the first note the note should be stored successfully and listed in the notes array.
After reloading the page, the first note should show immediately, as the page is server side rendered.
Still unauthorized, the user should not be able to delete the note and the error should be displayed.
After the users clicks the "Login" button and gets authorized, deleting the note again should work successfully,
and the error should disappear.
`, async (ctx) => {
await ctx.notesPage.typeNote(notes.first.note);

await ctx.notesPage.addNote();
expect(await ctx.notesPage.notes().elementHandles()).toHaveLength(1);

await ctx.notesPage.page.reload();
expect(await ctx.notesPage.notes().elementHandles()).toHaveLength(1);

await ctx.notesPage.removeNote(0);
expect(await ctx.notesPage.notes().elementHandles()).toHaveLength(1);
expect(await ctx.notesPage.getDeleteErrorCount()).toBe(1);
Expand Down
13 changes: 7 additions & 6 deletions apps/trpc-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ export default defineConfig(({ mode }) => {
include: ['@angular/common', '@angular/forms', 'isomorphic-fetch'],
},
ssr: {
noExternal: '@analogjs/trpc',
noExternal: ['@analogjs/trpc', '@trpc/server'],
},
build: {
target: ['es2020'],
},
plugins: [
analog({
vite: {
inlineStylesExtension: 'css',
},
prerender: {
routes: ['/'],
nitro: {
routeRules: {
'/': {
prerender: false,
},
},
},
}),
nxViteTsPaths(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ export default defineConfig(({ mode }) => {
return {
publicDir: 'src/public',
<% if (addTRPC) { %>
server: {
host: '127.0.0.1'
},
ssr: {
noExternal: '@analogjs/trpc/**',
noExternal: ['@analogjs/trpc','@trpc/server'],
},
<% } %>
build: {
target: ['es2020'],
},
plugins: [
<% if (addTRPC) { %>
analog({
nitro: {
routeRules: {
'/': {
prerender: false,
}
}
}
}),
<% } else { %>
analog(),
<% } %>
tsConfigPaths({
root: '<%= offsetFromRoot %>',
}),
Expand Down
4 changes: 1 addition & 3 deletions packages/trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
"isomorphic-fetch": "^3.0.0",
"superjson": "^1.12.3"
},
"dependencies": {
"tslib": "^2.3.0"
},
"dependencies": {},
"ng-update": {
"packageGroup": [
"@analogjs/astro-angular",
Expand Down
15 changes: 15 additions & 0 deletions packages/trpc/src/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ function customFetch(
json: () => Promise.resolve(response._data),
}));
}

// dev server trpc for analog & nitro
if (typeof window === 'undefined') {
const host =
process.env['NITRO_HOST'] ?? process.env['ANALOG_HOST'] ?? 'localhost';
const port =
process.env['NITRO_PORT'] ?? process.env['ANALOG_PORT'] ?? 4205;
const base = `http://${host}:${port}`;
if (input instanceof Request) {
input = new Request(base, input);
} else {
input = new URL(input, base);
}
}

return fetch(input, init);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/trpc/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": []
"types": ["node"]
},
"exclude": ["src/**/*.spec.ts", "jest.config.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
Expand Down
7 changes: 7 additions & 0 deletions packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ export function nitro(options?: Options, nitroOptions?: NitroConfig): Plugin[] {
toNodeListener(server.app as unknown as App)
);

viteServer.httpServer?.once('listening', () => {
process.env['ANALOG_HOST'] = !viteServer.config.server.host
? 'localhost'
: (viteServer.config.server.host as string);
process.env['ANALOG_PORT'] = `${viteServer.config.server.port}`;
});

console.log(
`\n\nThe server endpoints are accessible under the "${apiPrefix}" path.`
);
Expand Down