Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 7ee8228
Author: Nicholas Margaritondo <nick.margaritondo@outlook.com>
Date:   Tue May 23 10:18:59 2023 -0500

    LanguageParameter and processDefaultCodename now check for valid codename. Added automated tests for default language param overwrite
  • Loading branch information
Enngage committed Jul 31, 2023
1 parent 288cbb7 commit 951bf26
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
14 changes: 12 additions & 2 deletions lib/models/common/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export namespace Parameters {
if (!element) {
throw Error(`Element specified in 'OrderParameter' is null or empty`);
}

}

getParam(): string {
Expand Down Expand Up @@ -148,19 +149,28 @@ export namespace Parameters {
}

export class LanguageParameter implements IQueryParameter {

/**
* Specifies language version to fetch
* @constructor
* @param {string} languageCodename - Codename of the language
*/
codenameValidationRegExp = new RegExp(`language=[a-zA-Z_][a-zA-Z0-9_]{0,59}$`)
languageQueryString = `language=${this.languageCodename}`
constructor(public languageCodename: string) {
if (!languageCodename) {
throw Error(`'LanguageParameter' must specify codename of the language`);
}
}

getParam(): string {
return `language=${this.languageCodename}`;



getParam(): string{
if(!this.languageQueryString.match(this.codenameValidationRegExp)){
throw Error(`Invalid language codename`)
}
else return this.languageQueryString
}
}
}
6 changes: 3 additions & 3 deletions lib/query/common/base-query.class.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { IHeader, IQueryParameter } from '@kontent-ai/core-sdk';

import { IDeliveryClientConfig } from '../../config';
import { IDeliveryNetworkResponse, IKontentResponse, IQueryConfig, Parameters } from '../../models';
import { IDeliveryNetworkResponse, IKontentResponse, IQueryConfig, Parameters} from '../../models';
import { QueryService } from '../../services';


export abstract class BaseQuery<TResponse extends IKontentResponse, TQueryConfig extends IQueryConfig, TContract> {
protected parameters: IQueryParameter[] = [];
protected customUrl?: string;
protected abstract _queryConfig: TQueryConfig;

constructor(protected config: IDeliveryClientConfig, protected queryService: QueryService) {}

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ export abstract class BaseQuery<TResponse extends IKontentResponse, TQueryConfig
protected processDefaultLanguageParameter(): void {
// add default language if none is specified && default language is specified globally
if (this.config.defaultLanguage) {
const languageParameter = this.getParameters().find((m) => m.getParam() === 'language');
const languageParameter = this.getParameters().find((m) => m.getParam().match(`language=[a-zA-Z_][a-zA-Z0-9_]{0,59}$`));
if (!languageParameter) {
// language parameter was not specified in query, use globally defined language
this.parameters.push(new Parameters.LanguageParameter(this.config.defaultLanguage));
Expand Down
25 changes: 24 additions & 1 deletion test/browser/isolated-tests/url/item-url-parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Context, setup } from '../../setup';

describe('Item url parameters', () => {

const context = new Context();
const context = new Context({defaultLanguage: 'default'});
setup(context);

it(`includeTotalCount parameter should be set to true when used`, () => {
Expand Down Expand Up @@ -174,5 +174,28 @@ describe('Item url parameters', () => {

expect(param).toEqual('elem1[asc]');
});

it('defaultLanguage should set language parameter', ()=> {
const url = new URL(
context.deliveryClient.items()
.getUrl()
);
const param = url.searchParams.get('language')
expect(param).toEqual('default')
});

it(`languageParameter should overwrite default language`, () => {
const url = new URL(
context.deliveryClient.items()
.languageParameter('es_ES')
.getUrl()
);

const param = url.searchParams.get('language')
expect(param).toEqual('es_ES')

});


});

0 comments on commit 951bf26

Please sign in to comment.