Skip to content

Commit d0ea3ac

Browse files
[Dictionary] [Editing Service] Throw an error when "siteName" or "language" is not passed
1 parent 5813a2d commit d0ea3ac

4 files changed

+77
-0
lines changed

packages/sitecore-jss/src/editing/graphql-editing-service.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,38 @@ describe('GraphQLEditingService', () => {
319319
expect(error.response.error).to.equal('Internal server error');
320320
}
321321
});
322+
323+
it('should throw an error when siteName is not provided', async () => {
324+
const service = new GraphQLEditingService({
325+
clientFactory,
326+
});
327+
328+
try {
329+
await service.fetchEditingData({
330+
language,
331+
version,
332+
itemId,
333+
siteName: '',
334+
});
335+
} catch (error) {
336+
expect(error.message).to.equal('The site name must be a non-empty string');
337+
}
338+
});
339+
340+
it('should throw an error when language is not provided', async () => {
341+
const service = new GraphQLEditingService({
342+
clientFactory,
343+
});
344+
345+
try {
346+
await service.fetchEditingData({
347+
language: '',
348+
version,
349+
itemId,
350+
siteName,
351+
});
352+
} catch (error) {
353+
expect(error.message).to.equal('The language must be a non-empty string');
354+
}
355+
});
322356
});

packages/sitecore-jss/src/editing/graphql-editing-service.ts

+8
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ export class GraphQLEditingService {
122122
}) {
123123
debug.editing('fetching editing data for %s %s %s %s', siteName, itemId, language, version);
124124

125+
if (!siteName) {
126+
throw new RangeError('The site name must be a non-empty string');
127+
}
128+
129+
if (!language) {
130+
throw new RangeError('The language must be a non-empty string');
131+
}
132+
125133
const dictionary: DictionaryPhrases = {};
126134
let dictionaryResults: { key: string; value: string }[] = [];
127135
let hasNext = true;

packages/sitecore-jss/src/i18n/graphql-dictionary-service.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,31 @@ describe('GraphQLDictionaryService', () => {
375375
const result = await service.fetchDictionaryData('en');
376376
expect(result).to.deep.equal({});
377377
});
378+
379+
it('should throw error if siteName is not provided', async () => {
380+
const service = new GraphQLDictionaryService({
381+
clientFactory,
382+
siteName: '',
383+
cacheEnabled: false,
384+
useSiteQuery: true,
385+
});
386+
387+
await service.fetchDictionaryData('en').catch((error) => {
388+
expect(error.message).to.equal('The site name must be a non-empty string');
389+
});
390+
});
391+
392+
it('should throw error if language is not provided', async () => {
393+
const service = new GraphQLDictionaryService({
394+
clientFactory,
395+
siteName,
396+
cacheEnabled: false,
397+
useSiteQuery: true,
398+
});
399+
400+
await service.fetchDictionaryData('').catch((error) => {
401+
expect(error.message).to.equal('The language must be a non-empty string');
402+
});
403+
});
378404
});
379405
});

packages/sitecore-jss/src/i18n/graphql-dictionary-service.ts

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SitecoreTemplateId } from '../constants';
77
import { DictionaryPhrases, DictionaryServiceBase } from './dictionary-service';
88
import { CacheOptions } from '../cache-client';
99
import { getAppRootId, SearchQueryService, PageInfo, SearchQueryVariables } from '../graphql';
10+
import { siteNameError, languageError } from '../graphql/app-root-query';
1011
import debug from '../debug';
1112

1213
/** @private */
@@ -225,6 +226,14 @@ export class GraphQLDictionaryService extends DictionaryServiceBase {
225226
let hasNext = true;
226227
let after = '';
227228

229+
if (!this.options.siteName) {
230+
throw new RangeError(siteNameError);
231+
}
232+
233+
if (!language) {
234+
throw new RangeError(languageError);
235+
}
236+
228237
while (hasNext) {
229238
const fetchResponse = await this.graphQLClient.request<DictionarySiteQueryResponse>(
230239
siteQuery,

0 commit comments

Comments
 (0)