Skip to content

Commit

Permalink
Feat: Ability to specify base/preview urls (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Nov 24, 2017
1 parent 1f1dfb8 commit 82af7ff
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 27 deletions.
25 changes: 20 additions & 5 deletions lib/config/delivery-client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ export class DeliveryClientConfig {
/**
* Preview API key used to get unpublished content items
*/
public previewApiKey: string;
public previewApiKey?: string;

/**
* Indicates if preview mode is enabled globally
*/
public enablePreviewMode: boolean;
public enablePreviewMode: boolean = false;

/**
* Default content language, can be overidden with languageParameter method
*/
public defaultLanguage: string;
public defaultLanguage?: string;

/**
* Base Url. Can be overriden if e.g. a proxy is required
*/
public baseUrl?: string;

/**
* Base preview Url. Can be overriden if e.g. a proxy is required
*/
public basePreviewUrl?: string;

/**
* Configuration of Kentico Cloud Delivery client
Expand All @@ -31,15 +41,20 @@ export class DeliveryClientConfig {
* @param {string} previewApiKey - Preview API key used to get unpublished content items
* @param {boolean} enablePreviewMode - Indicates if preview mode is enabled globally
* @param {string} defaultLanguage - Sets default language that will be used for all queries unless overriden with query parameters
* @param {string} baseUrl - Can be used to configure custom base url
* @param {string} basePreviewUrl - Can bse used to configure custom preview url
*/
constructor(
public projectId: string,
public typeResolvers: TypeResolver[],
public options?: {
private options?: {
enableAdvancedLogging?: boolean,
previewApiKey?: string,
enablePreviewMode?: boolean,
defaultLanguage?: string
defaultLanguage?: string,
baseUrl?: string,
basePreviewUrl?: string

}) {

if (!projectId) {
Expand Down
23 changes: 17 additions & 6 deletions lib/services/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ import { IHttpService } from './http/ihttp.service';
export class QueryService {

/**
* Base Url to Kentico Delivery API
* Default base Url to Kentico Delivery API
*/
private readonly baseDeliveryApiUrl: string = 'https://deliver.kenticocloud.com';
private readonly defaultBaseDeliveryApiUrl: string = 'https://deliver.kenticocloud.com';

/**
* Preview url to Kentico Delivery API
* Default preview url to Kentico Delivery API
*/
private readonly previewDeliveryApiUrl: string = 'https://preview-deliver.kenticocloud.com';
private readonly defaultPreviewDeliveryApiUrl: string = 'https://preview-deliver.kenticocloud.com';

/**
* Name of the header used when 'wait for loading new content' feature is used
Expand Down Expand Up @@ -112,9 +112,20 @@ export class QueryService {
throw Error(`You have to configure 'previewApiKey' to use 'preview' mode`);
}

return this.previewDeliveryApiUrl;
// use custom base / preview url if its configured
if (this.config.basePreviewUrl) {
return this.config.basePreviewUrl;
}

// use default preview url
return this.defaultPreviewDeliveryApiUrl;
}

// use custom base / preview url if its configured
if (this.config.baseUrl) {
return this.config.baseUrl;
}
return this.baseDeliveryApiUrl;
return this.defaultBaseDeliveryApiUrl;
}

/**
Expand Down
20 changes: 17 additions & 3 deletions test/isolated-tests/core/base-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { setup, Context } from '../../setup';
// tests
describe('Base URL', () => {

var context = new Context();
const context = new Context();
setup(context);

var itemsUrl: string;
var parsedUrl: URL;
let itemsUrl: string;
let parsedUrl: URL;

beforeAll(() => {
itemsUrl = context.deliveryClient.items().toString();
Expand All @@ -29,5 +29,19 @@ describe('Base URL', () => {

it(`pathname should contain project id'`, () => expect(itemsUrl).toContain(context.projectId));

it(`custom base URL should be used'`, () => {
const baseUrl = 'http://custombase.com';
const contextCustom = new Context({baseUrl: baseUrl});
setup(contextCustom);
expect(contextCustom.deliveryClient.items().toString()).toContain(baseUrl);
});

it(`custom preview URL should be used'`, () => {
const previewUrl = 'http://custompreview.com';
const contextCustom = new Context({basePreviewUrl: previewUrl, usePreviewMode: true});
setup(contextCustom);
expect(contextCustom.deliveryClient.items().toString()).toContain(previewUrl);
});

});

10 changes: 8 additions & 2 deletions test/setup/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export class Context {
public deliveryClient: DeliveryClient;
public usePreviewMode: boolean;
public defaultLanguage: string;
public baseUrl: string;
public basePreviewUrl: string;

constructor(
options?: {
Expand All @@ -17,7 +19,9 @@ export class Context {
previewApiKey?: string,
deliveryClient?: DeliveryClient,
usePreviewMode?: boolean,
defaultLanguage?: string
defaultLanguage?: string,
baseUrl?: string,
basePreviewUrl?: string,
}
) {
if (options) {
Expand All @@ -33,7 +37,9 @@ export class Context {
enableAdvancedLogging: true,
enablePreviewMode: this.usePreviewMode,
previewApiKey: this.previewApiKey,
defaultLanguage: this.defaultLanguage
defaultLanguage: this.defaultLanguage,
baseUrl: this.baseUrl,
basePreviewUrl: this.basePreviewUrl
}
);
}
Expand Down
26 changes: 15 additions & 11 deletions test/setup/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import { DeliveryClient, TypeResolver, DeliveryClientConfig} from '../../lib';
import { Context } from './context';

// ---------- default setup ------------- //
var defaultTypeResolvers: TypeResolver[] = [
new TypeResolver("movie", () => new Movie()),
new TypeResolver("actor", () => new Actor())
const defaultTypeResolvers: TypeResolver[] = [
new TypeResolver('movie', () => new Movie()),
new TypeResolver('actor', () => new Actor())
];

var defaultProjectId = 'da5abe9f-fdad-4168-97cd-b3464be2ccb9';
var defaultPreviewApiKey = 'ew0KICAiYWxnIjogIkhTMjU2IiwNCiAgInR5cCI6ICJKV1QiDQp9.ew0KICAidWlkIjogInVzcl8wdlFZQkNxQXZybm81cmlmSG5pWUVHIiwNCiAgImVtYWlsIjogInJpY2hhcmRzQGtlbnRpY28uY29tIiwNCiAgInByb2plY3RfaWQiOiAiZGE1YWJlOWYtZmRhZC00MTY4LTk3Y2QtYjM0NjRiZTJjY2I5IiwNCiAgImp0aSI6ICJpLXNFVWJlNmZPeUtBQmJOIiwNCiAgInZlciI6ICIxLjAuMCIsDQogICJnaXZlbl9uYW1lIjogIlJpY2hhcmQiLA0KICAiZmFtaWx5X25hbWUiOiAiU3VzdGVrIiwNCiAgImF1ZCI6ICJwcmV2aWV3LmRlbGl2ZXIua2VudGljb2Nsb3VkLmNvbSINCn0.jSq0owesXGAGf8l7e0Ue7wPkP28MT_--ZK5T02sO7yw';
const defaultProjectId = 'da5abe9f-fdad-4168-97cd-b3464be2ccb9';
const defaultPreviewApiKey = 'ew0KICAiYWxnIjogIkhTMjU2IiwNCiAgInR5cCI6ICJKV1QiDQp9.ew0KICAidWlkIjogInVzcl8wdlFZQkNxQXZybm81cmlmSG5pWUVHIiwNCiAgImVtYWlsIjogInJpY2hhcmRzQGtlbnRpY28uY29tIiwNCiAgInByb2plY3RfaWQiOiAiZGE1YWJlOWYtZmRhZC00MTY4LTk3Y2QtYjM0NjRiZTJjY2I5IiwNCiAgImp0aSI6ICJpLXNFVWJlNmZPeUtBQmJOIiwNCiAgInZlciI6ICIxLjAuMCIsDQogICJnaXZlbl9uYW1lIjogIlJpY2hhcmQiLA0KICAiZmFtaWx5X25hbWUiOiAiU3VzdGVrIiwNCiAgImF1ZCI6ICJwcmV2aWV3LmRlbGl2ZXIua2VudGljb2Nsb3VkLmNvbSINCn0.jSq0owesXGAGf8l7e0Ue7wPkP28MT_--ZK5T02sO7yw';

// ----------- setup function o------------ //
export function setup(context: Context) {

// get delivery client with given context
var projectId: string = defaultProjectId;
var previewApiKey: string = defaultPreviewApiKey;
var typeResolvers: TypeResolver[] = defaultTypeResolvers;
let projectId: string = defaultProjectId;
let previewApiKey: string = defaultPreviewApiKey;
let typeResolvers: TypeResolver[] = defaultTypeResolvers;

// context settings override default setup
if (context.projectId) {
Expand All @@ -37,18 +37,22 @@ export function setup(context: Context) {
typeResolvers = context.typeResolvers;
}

var deliveryClientConfig: DeliveryClientConfig = new DeliveryClientConfig(projectId, typeResolvers, {
const deliveryClientConfig: DeliveryClientConfig = new DeliveryClientConfig(projectId, typeResolvers, {
previewApiKey: previewApiKey,
enablePreviewMode: context.usePreviewMode,
defaultLanguage: context.defaultLanguage
defaultLanguage: context.defaultLanguage,
baseUrl: context.baseUrl,
basePreviewUrl: context.basePreviewUrl
})

// set context
context.projectId = projectId;
context.previewApiKey = previewApiKey;
context.typeResolvers = typeResolvers;
context.defaultLanguage = context.defaultLanguage;
context.baseUrl = context.baseUrl;
context.basePreviewUrl = context.basePreviewUrl;

// set delivery client
context.deliveryClient = new DeliveryClient(deliveryClientConfig);
}
}

0 comments on commit 82af7ff

Please sign in to comment.