Skip to content

Commit

Permalink
feat(gatsby-source-url): disableIxlibParam global setting
Browse files Browse the repository at this point in the history
  • Loading branch information
frederickfogerty committed Sep 16, 2020
1 parent 8109e95 commit 2ff3f33
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
44 changes: 44 additions & 0 deletions packages/gatsby-source-url/src/common/ioTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { pipe } from 'fp-ts/lib/function';
import * as t from 'io-ts';
import { Optional } from './tsUtils';

const typeOptional = <P extends t.Props>(obj: P) =>
pipe(obj, (v) => t.type<P>(v), fixOptionals);

/**
* Type lambda returning a union of key names from input type P having type A
*/
type FieldsWithType<T, Type> = {
[K in keyof T]-?: Type extends T[K] ? K : never;
}[keyof T];

/**
* Dual for FieldsWith - returns the rest of the fields
*/
type FieldsWithoutType<T, Type> = Exclude<keyof T, FieldsWithType<T, Type>>;

// Original implementation
// type MakeOptional<T, Type = undefined> = Pick<T, FieldsWithoutType<T, Type>> & Partial<Pick<T, FieldsWithType<T, Type>>>;

/**
* Type lambda returning new type with all fields within P having type U marked as optional
*/
type MakeOptional<T, Type = undefined> = Optional<T, FieldsWithType<T, Type>>;

/**
* Fix signature by marking all fields with undefined as optional
*/
const fixOptionals = <C extends t.Mixed>(
c: C,
): t.Type<MakeOptional<t.TypeOf<C>>, t.OutputOf<C>, t.InputOf<C>> => c;

/**
* Just an alias for T | undefined coded
*/
const optional = <C extends t.Mixed>(
c: C,
): t.Type<t.TypeOf<C> | undefined, t.OutputOf<C>, t.InputOf<C>> =>
t.union([t.undefined, c]);

export * from 'io-ts';
export { typeOptional, optional };
1 change: 1 addition & 0 deletions packages/gatsby-source-url/src/common/tsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>;
10 changes: 7 additions & 3 deletions packages/gatsby-source-url/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,21 @@ export const createResolvers: GatsbyNode['createResolvers'] = async (
),
),
)
.bindL('imgixClient', ({ options }) => createImgixClient(options))
.bindL('imgixClient', ({ options: { domain } }) =>
createImgixClient({ domain }),
)
.bind(
'packageVersion',
pipe(
readPkgUp.sync({ cwd: __dirname })?.packageJson.version,
E.fromNullable(new Error('Could not read package version.')),
),
)
.doL(({ imgixClient, packageVersion }) => {
.doL(({ options, imgixClient, packageVersion }) => {
imgixClient.includeLibraryParam = false;
(imgixClient as any).settings.libraryParam = `gatsby-source-url-${packageVersion}`;
if (options.disableIxlibParam !== true) {
(imgixClient as any).settings.libraryParam = `gatsby-source-url-${packageVersion}`;
}
return E.right(imgixClient);
})
.letL('rootQueryTypeMap', ({ imgixClient, options }) => ({
Expand Down
13 changes: 4 additions & 9 deletions packages/gatsby-source-url/src/publicTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import imgixUrlParameters from 'imgix-url-params/dist/parameters.json';
import * as t from 'io-ts';
import R from 'ramda';
import * as t from './common/ioTs';

type IImgixParamsKey =
| keyof ImgixUrlParametersSpec['parameters']
Expand Down Expand Up @@ -28,18 +28,13 @@ const ImgixParamsIOTS = t.partial({
});
export type IImgixParams = t.TypeOf<typeof ImgixParamsIOTS>;

export const GatsbySourceUrlOptions = t.type({
export const GatsbySourceUrlOptions = t.typeOptional({
domain: t.string,
defaultImgixParams: t.union([ImgixParamsIOTS, t.undefined]),
defaultImgixParams: t.optional(ImgixParamsIOTS),
disableIxlibParam: t.optional(t.boolean),
});
export type IGatsbySourceUrlOptions = t.TypeOf<typeof GatsbySourceUrlOptions>;

const imgixParamsTest2: IImgixParams = {
auto: ['format'],
};

export type IImgixFixedImageData = {};

export type IGatsbySourceUrlRootArgs = {
url: string;
};
Expand Down

0 comments on commit 2ff3f33

Please sign in to comment.