Skip to content

Commit

Permalink
[sitecore-jss] Rest Layout Service - Allow override of "named" JSS co…
Browse files Browse the repository at this point in the history
…nfiguration (#716)

* [RestLayoutService] Allow override of "named" JSS configuration

* Add UT

Co-authored-by: illiakovalenko <zlatoborodyi@gmail.com>
  • Loading branch information
anastasiya29 and illiakovalenko authored Jun 9, 2021
1 parent 8309746 commit d944105
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
35 changes: 35 additions & 0 deletions packages/sitecore-jss/src/layout/rest-layout-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,41 @@ describe('RestLayoutService', () => {
});
});

it('should fetch layout data using custom configuration name', () => {
mock.onGet().reply((config) => {
return [
200,
{
...config,
data: { sitecore: { context: {}, route: { name: 'xxx' } } },
},
{
'set-cookie': 'test-set-cookie-value',
},
];
});

const service = new RestLayoutService({
apiHost: 'http://sctest',
apiKey: '0FBFF61E-267A-43E3-9252-B77E71CEE4BA',
siteName: 'supersite',
configurationName: 'listen',
tracking: false,
});

return service.fetchLayoutData('/home', 'da-DK').then((layoutServiceData: any) => {
expect(layoutServiceData.url).to.equal(
'http://sctest/sitecore/api/layout/render/listen?item=%2Fhome&sc_apikey=0FBFF61E-267A-43E3-9252-B77E71CEE4BA&sc_site=supersite&sc_lang=da-DK&tracking=false'
);
expect(layoutServiceData.data).to.deep.equal({
sitecore: {
context: {},
route: { name: 'xxx' },
},
});
});
});

it('should fetch layout data using custom fetcher resolver', () => {
const fetcherSpy = spy((url: string) => {
return new AxiosDataFetcher().fetch<any>(url);
Expand Down
16 changes: 11 additions & 5 deletions packages/sitecore-jss/src/layout/rest-layout-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ export type RestLayoutServiceConfig = {
* Function that handles fetching API data
*/
dataFetcherResolver?: DataFetcherResolver;

/**
* Layout Service "named" configuration
*/
configurationName?: string;
};

/**
Expand Down Expand Up @@ -193,11 +198,11 @@ export class RestLayoutService extends LayoutServiceBase {
// {
// sitecore: {
// context: {
// pageEditing: false,
// language
// },
// route: null
// },
// pageEditing: false,
// language
// },
// route: null
// },
// }
//
return error.response.data;
Expand Down Expand Up @@ -257,6 +262,7 @@ export class RestLayoutService extends LayoutServiceBase {
return {
layoutServiceConfig: {
host: this.serviceConfig.apiHost,
configurationName: this.serviceConfig.configurationName,
},
querystringParams: { ...params },
};
Expand Down
1 change: 1 addition & 0 deletions samples/nextjs/src/lib/layout-service-factory.rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class LayoutServiceFactory {
apiHost: config.sitecoreApiHost,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
configurationName: 'default',
});
}
}
Expand Down

0 comments on commit d944105

Please sign in to comment.