Skip to content

Commit

Permalink
feat(caller): add the current visited url of the user
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBunlon committed Jan 26, 2024
1 parent 5aaa5f7 commit 921d3c9
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/agent/src/utils/query-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ export default class QueryStringParser {
QueryStringParser.VALID_TIMEZONES.add(timezone);
}

return { ...context.state.user, timezone, requestId: uuidv4() };
return {
...context.state.user,
timezone,
requestId: uuidv4(),
webAppURL: new URL(context.originalUrl),
};
}

static parsePagination(context: Context): Page {
Expand Down
15 changes: 15 additions & 0 deletions packages/agent/test/utils/query-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,21 @@ describe('QueryStringParser', () => {
});
});

test('should return the current visited url from user eg forestContextUrl', () => {
const context = createMockContext({
state: { user: { email: '[email protected]' } },
customProperties: { query: { timezone: 'America/Los_Angeles' } },
url: 'https://app.forestadmin.com/aProject/anEnvironment/aRendering',
});

expect(QueryStringParser.parseCaller(context)).toEqual({
email: '[email protected]',
requestId: expect.any(String),
timezone: 'America/Los_Angeles',
webAppURL: new URL('https://app.forestadmin.com/aProject/anEnvironment/aRendering'),
});
});

test('should throw a ValidationError when the timezone is missing', () => {
const context = createMockContext({
customProperties: { query: {} },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class ActionCollectionDecorator extends CollectionDecorator {
if (!action) return this.childCollection.execute(caller, name, data, filter);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const context = this.getContext(caller, action, data, filter) as any;
const context = this.getContext(caller, action, data, filter, null, null) as any;
const resultBuilder = new ResultBuilder();
const result = await action.execute(context, resultBuilder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class CacheDataSourceInterface {
team: 'system',
timezone: 'UTC',
requestId: '',
webAppURL: new URL('https://app.forestadmin.com/aProject/anEnvironment/aRendering'),
};

constructor(dataSource: DataSource) {
Expand Down
1 change: 1 addition & 0 deletions packages/datasource-toolkit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export * from './interfaces/query/sort';
export * from './interfaces/query/page';
export * from './interfaces/record';
export * from './interfaces/schema';

export {
GenericTree,
GenericTreeBranch,
Expand Down
1 change: 1 addition & 0 deletions packages/datasource-toolkit/src/interfaces/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type Caller = {
role: string;
tags: { [key: string]: string };
timezone: string;
webAppURL: URL;
};
1 change: 1 addition & 0 deletions packages/datasource-toolkit/test/__factories__/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export default Factory.define<Caller>(() => ({
tags: {},
team: 'team',
timezone: 'Europe/Paris',
webAppURL: new URL('https://app.forestadmin.com/aProject/anEnvironment/aRendering'),
}));

0 comments on commit 921d3c9

Please sign in to comment.