From 367974c248fa6c2a15774d9a55af56481ca3c811 Mon Sep 17 00:00:00 2001 From: Frederick Fogerty Date: Fri, 1 May 2020 11:01:01 +1200 Subject: [PATCH] feat: global default parameters --- src/main.ts | 3 +++ src/plugins/vue-imgix/types.ts | 1 + src/plugins/vue-imgix/vue-imgix.ts | 27 ++++++++++++++++----------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main.ts b/src/main.ts index d9596173..2ce822c5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,9 @@ Vue.config.productionTip = false; Vue.use(VueImgix, { domain: 'assets.imgix.net', + defaultIxParams: { + auto: 'format', + }, }); new Vue({ diff --git a/src/plugins/vue-imgix/types.ts b/src/plugins/vue-imgix/types.ts index d1d72512..44e2e5b3 100644 --- a/src/plugins/vue-imgix/types.ts +++ b/src/plugins/vue-imgix/types.ts @@ -3,6 +3,7 @@ export type IImgixParams = {}; export interface IImgixClientOptions { domain: string; includeLibraryParam?: boolean; + defaultIxParams?: IImgixParams; } export interface IBuildUrlObjectResult { diff --git a/src/plugins/vue-imgix/vue-imgix.ts b/src/plugins/vue-imgix/vue-imgix.ts index cf1eed2c..d7873e85 100644 --- a/src/plugins/vue-imgix/vue-imgix.ts +++ b/src/plugins/vue-imgix/vue-imgix.ts @@ -23,35 +23,40 @@ class VueImgixClient implements IVueImgixClient { constructor(options: IImgixClientOptions) { this.options = { ...clientOptionDefaults, ...options }; - const { includeLibraryParam, ...imgixClientOptions } = this.options; - this.client = new ImgixClient({ - ...imgixClientOptions, + domain: this.options.domain, includeLibraryParam: false, // force false so that imgix-core-js doesn't include its own library param }); // This is not a public API, so it is not included in the type definitions for ImgixClient - if (includeLibraryParam) { + if (this.options.includeLibraryParam) { // eslint-disable-next-line @typescript-eslint/no-explicit-any (this.client as any).settings.libraryParam = `vue-${VERSION}`; } } + buildIxParams = (ixParams?: IImgixParams) => { + return { + ...this.options.defaultIxParams, + ...ixParams, + }; + }; + buildUrlObject = ( url: string, - options?: IImgixParams, + ixParams?: IImgixParams, ): IBuildUrlObjectResult => { - const src = this.client.buildURL(url, options); - const srcset = this.client.buildSrcSet(url, options); + const src = this.buildUrl(url, ixParams); + const srcset = this.buildSrcSet(url, ixParams); return { src, srcset }; }; - buildUrl = (url: string, options?: IImgixParams): string => { - return this.client.buildURL(url, options); + buildUrl = (url: string, ixParams?: IImgixParams): string => { + return this.client.buildURL(url, this.buildIxParams(ixParams)); }; - buildSrcSet = (url: string, options?: IImgixParams): string => { - return this.client.buildSrcSet(url, options); + buildSrcSet = (url: string, ixParams?: IImgixParams): string => { + return this.client.buildSrcSet(url, this.buildIxParams(ixParams)); }; }