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 4 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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');
});

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}`]();
}
})
);

Config.put(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