Skip to content

Commit

Permalink
feat(gatsby-source-url): basic TS imgix params with io-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
frederickfogerty committed Sep 10, 2020
1 parent dd7089a commit 319b149
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions packages/gatsby-source-url/src/publicTypes.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
import imgixUrlParameters from 'imgix-url-params/dist/parameters.json';
import * as t from 'io-ts';
import R from 'ramda';

type IImgixParamsKey =
| keyof ImgixUrlParametersSpec['parameters']
| keyof ImgixUrlParametersSpec['aliases'];

const ImgixParamValueIOTS = t.union([
t.string,
t.number,
t.boolean,
t.undefined,
t.null,
t.array(t.string),
t.array(t.number),
t.array(t.boolean),
]);

const mapToImgixParamValue = <TKey extends string>(
obj: Record<TKey, unknown>,
): Record<TKey, typeof ImgixParamValueIOTS> =>
R.mapObjIndexed(() => ImgixParamValueIOTS, obj);

const ImgixParamsIOTS = t.partial({
...mapToImgixParamValue(imgixUrlParameters.aliases),
...mapToImgixParamValue(imgixUrlParameters.parameters),
});
type IImgixParams = t.TypeOf<typeof ImgixParamsIOTS>;

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

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

export type IImgixFixedImageData = {};

export type IGatsbySourceUrlRootArgs = {
url: string;
};

export type ImgixUrlParametersSpec = typeof imgixUrlParameters;
type ImgixUrlParametersSpec = typeof imgixUrlParameters;

// Can be improved
export type ImgixUrlParams = Partial<
Record<
| keyof ImgixUrlParametersSpec['parameters']
| keyof ImgixUrlParametersSpec['aliases'],
string | number | boolean | undefined
>
Record<IImgixParamsKey, string | number | boolean | undefined>
> & {
ar?: string;
};
Expand Down

0 comments on commit 319b149

Please sign in to comment.