Skip to content

Commit

Permalink
fix: use proper PB URL in prod/dev; resolve issue with server.js modu…
Browse files Browse the repository at this point in the history
…le imports

Signed-off-by: Robert Goniszewski <robertgoniszewski@outlook.com>
  • Loading branch information
goniszewski committed Mar 30, 2024
1 parent a11e272 commit 5d107a8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const getProcessEnvValue = (key: string) =>
typeof process === 'object' ? process.env[key] : undefined;

const config = {
IS_DEV: import.meta.env.DEV,
ORIGIN: env.PUBLIC_ORIGIN || getProcessEnvValue('PUBLIC_ORIGIN') || 'http://localhost:5173',
POCKETBASE_URL:
env.PUBLIC_POCKETBASE_URL || getProcessEnvValue('PUBLIC_POCKETBASE_URL') || 'http://pocketbase',
Expand Down
17 changes: 14 additions & 3 deletions src/lib/pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import { urls } from './enums/urls';
import type { RecordModel } from 'pocketbase';
import type { User } from './types/User.type';
import type { UserSettings } from './types/UserSettings.type';
export const pb = new PocketBase(config.ORIGIN + urls.INTERNAL_PB);

const pbUrl = config.IS_DEV
? config.POCKETBASE_URL === 'http://pocketbase'
? 'http://localhost:5173'
: config.POCKETBASE_URL
: config.ORIGIN + urls.INTERNAL_PB;

export const pb = new PocketBase(pbUrl);

export const user = writable(
pb.authStore as BaseAuthStore & {
Expand Down Expand Up @@ -198,7 +205,9 @@ export async function authenticateUserApiRequest(
return response;
}

export const removePocketbaseFields = <T extends Partial<RecordModel> | Partial<RecordModel>[]>(record: T): T => {
export const removePocketbaseFields = <T extends Partial<RecordModel> | Partial<RecordModel>[]>(
record: T
): T => {
const keys = ['collectionId', 'collectionName'];
const removeFields = <T>(obj: T): T => {
if (Array.isArray(obj)) {
Expand All @@ -222,4 +231,6 @@ export const removePocketbaseFields = <T extends Partial<RecordModel> | Partial<
};

export const checkPocketbaseConnection = async (): Promise<boolean> =>
fetch(config.ORIGIN + `${urls.INTERNAL_PB}/api/health`).then((res) => res.ok);
fetch(
config.IS_DEV ? 'http://localhost:5173' : config.ORIGIN + `${urls.INTERNAL_PB}/api/health`
).then((res) => res.ok);
8 changes: 4 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import express from 'express';
import proxy from 'express-http-proxy';

import { handler } from '../build/handler.js';
import { urls } from '$lib/enums/urls.js';

const app = express();

const config = {
POCKETBASE_URL: process.env.PUBLIC_POCKETBASE_URL || 'http://pocketbase',
PORT: process.env.PORT || 5137
PORT: process.env.PORT || 5137,
INTERNAL_PATH: '/internal/pb'

This comment has been minimized.

Copy link
@guizaodev

guizaodev Apr 15, 2024

is this right? I'm trying to use with the Unraid model, and the requests are going to pocketbase:PORT/internal/pb/api/ what is causing an 404 error. The right address should be pocketbase:PORT/api/

};

app.use(
urls.INTERNAL_PB,
config.INTERNAL_PATH,
proxy(config.POCKETBASE_URL, {
proxyReqPathResolver: function (req) {
return req.url.replace(urls.INTERNAL_PB, '');
return req.url.replace(config.INTERNAL_PATH, '');
}
})
);
Expand Down

0 comments on commit 5d107a8

Please sign in to comment.