Skip to content

Commit

Permalink
feat: global default parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
frederickfogerty committed May 1, 2020
1 parent 56b1e9b commit 367974c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Vue.config.productionTip = false;

Vue.use(VueImgix, {
domain: 'assets.imgix.net',
defaultIxParams: {
auto: 'format',
},
});

new Vue({
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vue-imgix/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type IImgixParams = {};
export interface IImgixClientOptions {
domain: string;
includeLibraryParam?: boolean;
defaultIxParams?: IImgixParams;
}

export interface IBuildUrlObjectResult {
Expand Down
27 changes: 16 additions & 11 deletions src/plugins/vue-imgix/vue-imgix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
};
}

Expand Down

0 comments on commit 367974c

Please sign in to comment.