diff --git a/packages/gatsby-source-url/src/common/ioTs.ts b/packages/gatsby-source-url/src/common/ioTs.ts new file mode 100644 index 00000000..1a7048b3 --- /dev/null +++ b/packages/gatsby-source-url/src/common/ioTs.ts @@ -0,0 +1,44 @@ +import { pipe } from 'fp-ts/lib/function'; +import * as t from 'io-ts'; +import { Optional } from './tsUtils'; + +const typeOptional =

(obj: P) => + pipe(obj, (v) => t.type

(v), fixOptionals); + +/** + * Type lambda returning a union of key names from input type P having type A + */ +type FieldsWithType = { + [K in keyof T]-?: Type extends T[K] ? K : never; +}[keyof T]; + +/** + * Dual for FieldsWith - returns the rest of the fields + */ +type FieldsWithoutType = Exclude>; + +// Original implementation +// type MakeOptional = Pick> & Partial>>; + +/** + * Type lambda returning new type with all fields within P having type U marked as optional + */ +type MakeOptional = Optional>; + +/** + * Fix signature by marking all fields with undefined as optional + */ +const fixOptionals = ( + c: C, +): t.Type>, t.OutputOf, t.InputOf> => c; + +/** + * Just an alias for T | undefined coded + */ +const optional = ( + c: C, +): t.Type | undefined, t.OutputOf, t.InputOf> => + t.union([t.undefined, c]); + +export * from 'io-ts'; +export { typeOptional, optional }; diff --git a/packages/gatsby-source-url/src/common/tsUtils.ts b/packages/gatsby-source-url/src/common/tsUtils.ts new file mode 100644 index 00000000..3aafb594 --- /dev/null +++ b/packages/gatsby-source-url/src/common/tsUtils.ts @@ -0,0 +1 @@ +export type Optional = Omit & Partial; diff --git a/packages/gatsby-source-url/src/gatsby-node.ts b/packages/gatsby-source-url/src/gatsby-node.ts index 09fcb0cd..dd0e054a 100644 --- a/packages/gatsby-source-url/src/gatsby-node.ts +++ b/packages/gatsby-source-url/src/gatsby-node.ts @@ -48,7 +48,9 @@ export const createResolvers: GatsbyNode['createResolvers'] = async ( ), ), ) - .bindL('imgixClient', ({ options }) => createImgixClient(options)) + .bindL('imgixClient', ({ options: { domain } }) => + createImgixClient({ domain }), + ) .bind( 'packageVersion', pipe( @@ -56,9 +58,11 @@ export const createResolvers: GatsbyNode['createResolvers'] = async ( 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 }) => ({ diff --git a/packages/gatsby-source-url/src/publicTypes.ts b/packages/gatsby-source-url/src/publicTypes.ts index cea44b63..5c8529c5 100644 --- a/packages/gatsby-source-url/src/publicTypes.ts +++ b/packages/gatsby-source-url/src/publicTypes.ts @@ -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'] @@ -28,18 +28,13 @@ const ImgixParamsIOTS = t.partial({ }); export type IImgixParams = t.TypeOf; -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; -const imgixParamsTest2: IImgixParams = { - auto: ['format'], -}; - -export type IImgixFixedImageData = {}; - export type IGatsbySourceUrlRootArgs = { url: string; };