Skip to content

Commit

Permalink
Update core helpers to support accurate typing for transformed models (
Browse files Browse the repository at this point in the history
…#691)

* feat(): update code helpers

* feat(): handle rest list

* chore(): add changeset
  • Loading branch information
nima-ct authored Oct 9, 2024
1 parent a6a39db commit 1e2bba1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-spiders-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-test-data/core': patch
---

Update core helpers to support accurate typing for transformed models
38 changes: 22 additions & 16 deletions core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ const toGraphqlPaginatedQueryResult = <Model>(
};
};

const buildField = <Model>(
const buildField = <Model, TransformedModel = Model>(
builder: Model | TBuilder<Model>,
transformName: TTransformType = 'default',
meta?: TBuildFieldMeta<Model>
): Model => {
): TransformedModel => {
const buildName = convertTransformNameToBuildName(transformName);
// @ts-ignore: TS does not know about the `Model` being an object.
const builderField = builder?.[buildName];
// We need to cast this to `() => Model` as otherwise the value is unknown.
// We know it's a function because of the proxy builder.
const builderFn = builderField as (() => Model) | undefined;
const builderFn = builderField as (() => TransformedModel) | undefined;
if (!builderFn) {
throw new Error(
`Builder with name '${buildName}' does not exist on field '${String(
Expand All @@ -140,19 +140,22 @@ const buildField = <Model>(
return builderFn();
};

const buildFields = <Model>(
const buildFields = <Model, TransformedModel = Model>(
builders: (Model | TBuilder<Model>)[],
transformName: TTransformType = 'default',
meta?: TBuildFieldMeta<Model>
): Model[] =>
builders.map((builder) => buildField(builder, transformName, meta));
): TransformedModel[] => {
return builders.map((builder) =>
buildField<Model, TransformedModel>(builder, transformName, meta)
);
};

const buildGraphqlList = <Model>(
const buildGraphqlList = <Model, GraphqlModel = Model>(
builders: TBuilder<Model>[],
{ name, total, offset, __typename }: TGraphqlPaginatedQueryResultOptions
): TGraphqlPaginatedQueryResult<Model> => {
return toGraphqlPaginatedQueryResult<Model>(
buildFields<Model>(builders, 'graphql'),
): TGraphqlPaginatedQueryResult<GraphqlModel> => {
return toGraphqlPaginatedQueryResult<GraphqlModel>(
buildFields<Model, GraphqlModel>(builders, 'graphql'),
{
name,
__typename,
Expand All @@ -162,14 +165,17 @@ const buildGraphqlList = <Model>(
);
};

const buildRestList = <Model>(
const buildRestList = <Model, RestModel = Model>(
builders: TBuilder<Model>[],
{ total, offset }: TPaginatedQueryResultOptions
): TPaginatedQueryResult<Model> => {
return toRestPaginatedQueryResult(buildFields(builders, 'rest'), {
total,
offset,
});
): TPaginatedQueryResult<RestModel> => {
return toRestPaginatedQueryResult<RestModel>(
buildFields<Model, RestModel>(builders, 'rest'),
{
total,
offset,
}
);
};

type TCreateSpecializedTransformersParams<TModel> = {
Expand Down

0 comments on commit 1e2bba1

Please sign in to comment.