Skip to content

feat: Allow option publicServerURL to be set dynamically as async function #9803

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

Open
wants to merge 2 commits into
base: alpha
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,17 @@ describe('server', () => {
expect(config.masterKeyCache.expiresAt.getTime()).toBeGreaterThan(Date.now());
});

it('should load publicServerURL', async () => {
await reconfigureServer({
publicServerURL: () => 'https://myserver.com/1',
});

await new Parse.Object('TestObject').save();

const config = Config.get(Parse.applicationId);
expect(config.publicServerURL).toEqual('https://myserver.com/1');
});
Comment on lines +618 to +627
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Expand test coverage for complete functionality.

The test only covers the function case but the type definition also allows Promise<string>. Consider adding test coverage for:

  1. Promise-based publicServerURL
  2. Error handling scenarios (function throws, invalid URL returned)

Add comprehensive test coverage:

it('should load publicServerURL from Promise', async () => {
  await reconfigureServer({
    publicServerURL: Promise.resolve('https://myserver.com/2'),
  });

  await new Parse.Object('TestObject').save();

  const config = Config.get(Parse.applicationId);
  expect(config.publicServerURL).toEqual('https://myserver.com/2');
});

it('should handle publicServerURL function errors', async () => {
  await expectAsync(reconfigureServer({
    publicServerURL: () => { throw new Error('Function failed'); },
  })).toBeRejected();
});
🤖 Prompt for AI Agents
In spec/index.spec.js around lines 618 to 627, the test only covers the case
where publicServerURL is a function returning a string, but it also supports a
Promise<string> and error scenarios. Add tests to cover when publicServerURL is
a Promise resolving to a URL string and when the function throws an error,
verifying that reconfigureServer rejects appropriately. This will ensure full
coverage of the publicServerURL option's behavior.


it('should not reload if ttl is not set', async () => {
const masterKeySpy = jasmine.createSpy().and.returnValue(Promise.resolve('initialMasterKey'));

Expand Down
32 changes: 30 additions & 2 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function removeTrailingSlash(str) {
return str;
}

const asyncKeys = ['publicServerURL'];
export class Config {
static get(applicationId: string, mount: string) {
const cacheInfo = AppCache.get(applicationId);
Expand All @@ -56,9 +57,33 @@ export class Config {
return config;
}

async loadKeys() {
const asyncKeys = ['publicServerURL'];

await Promise.all(
asyncKeys.map(async key => {
if (typeof this[`_${key}`] === 'function') {
this[key] = await this[`_${key}`]();
}
})
);

AppCache.put(this.appId, this);
}

static transformConfiguration(serverConfiguration) {
for (const key of Object.keys(serverConfiguration)) {
if (asyncKeys.includes(key) && typeof serverConfiguration[key] === 'function') {
serverConfiguration[`_${key}`] = serverConfiguration[key];
delete serverConfiguration[key];
}
}
}

static put(serverConfiguration) {
Config.validateOptions(serverConfiguration);
Config.validateControllers(serverConfiguration);
Config.transformConfiguration(serverConfiguration);
AppCache.put(serverConfiguration.appId, serverConfiguration);
Config.setupPasswordValidator(serverConfiguration.passwordPolicy);
return serverConfiguration;
Expand Down Expand Up @@ -116,7 +141,11 @@ export class Config {
}

if (publicServerURL) {
if (!publicServerURL.startsWith('http://') && !publicServerURL.startsWith('https://')) {
if (
typeof publicServerURL !== 'function' &&
!publicServerURL.startsWith('http://') &&
!publicServerURL.startsWith('https://')
) {
throw 'publicServerURL should be a valid HTTPS URL starting with https://';
}
}
Expand Down Expand Up @@ -757,7 +786,6 @@ export class Config {
return this.masterKey;
}


// TODO: Remove this function once PagesRouter replaces the PublicAPIRouter;
// the (default) endpoint has to be defined in PagesRouter only.
get pagesEndpoint() {
Expand Down
1 change: 1 addition & 0 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export async function handleParseHeaders(req, res, next) {
});
return;
}
await config.loadKeys();

info.app = AppCache.get(info.appId);
req.config = config;
Expand Down
2 changes: 1 addition & 1 deletion types/Options/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface ParseServerOptions {
cacheAdapter?: Adapter<CacheAdapter>;
emailAdapter?: Adapter<MailAdapter>;
encodeParseObjectInCloudFunction?: boolean;
publicServerURL?: string;
publicServerURL?: string | (() => string) | Promise<string>;
pages?: PagesOptions;
customPages?: CustomPagesOptions;
liveQuery?: LiveQueryOptions;
Expand Down
Loading