Skip to content

Commit

Permalink
simplify types
Browse files Browse the repository at this point in the history
  • Loading branch information
korsvanloon committed Jun 11, 2024
1 parent 6008c97 commit ab7d263
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
33 changes: 20 additions & 13 deletions packages/plugin-json/src/lib/amplience-schema-transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ import type {
StringValueNode,
TypeDefinitionNode,
TypeNode,
UnionTypeDefinitionNode} from "graphql";
import {
isEnumType,
isObjectType,
isUnionType,
UnionTypeDefinitionNode,
} from "graphql";
import { isEnumType, isObjectType, isUnionType } from "graphql";
import type {
AmplienceContentTypeSchemaBody,
AmplienceDeliveryKeyType,
AmpliencePropertyType,
EnumProperties,
FilterableTrait,
Expand Down Expand Up @@ -75,18 +71,26 @@ export const contentTypeSchemaBody = (

propertyOrder: [
// always place delivery keys at the top
type.fields?.some((field) => hasDirective(field, "amplienceDeliveryKey"))
? "_meta"
: undefined,
maybeDeliveryKeyDirective(type) ? "_meta" : undefined,

...(type.fields
?.filter(
(field) =>
isAmplienceProperty(type, field) && !hasDeliveryKeyDirective(field),
isAmplienceProperty(type, field) &&
!hasDirective(field, "amplienceDeliveryKey"),
)
.map((field) => field.name.value) ?? []),
].filter(isValue),

required: type.fields
?.filter((field) => isAmplienceProperty(type, field))
?.filter(
(field) =>
isAmplienceProperty(type, field) &&
!hasDirective(field, "amplienceDeliveryKey"),
)
.filter(
(field) =>
field.type.kind === "NonNullType" ||
Expand Down Expand Up @@ -117,16 +121,19 @@ export const objectProperties = (
type.fields
?.filter(
(field) =>
isAmplienceProperty(type, field) && !hasDeliveryKeyDirective(field),
isAmplienceProperty(type, field) &&
!hasDirective(field, "amplienceDeliveryKey"),
)
.map((prop) => [
prop.name.value,
{
title: capitalCase(prop.name.value),

description: prop.description?.value,
...switchArray<AmpliencePropertyType>(prop.type, {
ifArray: (subType) => ({
type: "array",

minItems: ifValue(
ifValue(
maybeDirective(prop, "amplienceList"),
Expand All @@ -135,6 +142,7 @@ export const objectProperties = (
),
Number,
),

maxItems: ifValue(
ifValue(
maybeDirective(prop, "amplienceList"),
Expand All @@ -143,7 +151,9 @@ export const objectProperties = (
),
Number,
),

items: ampliencePropertyType(prop, subType, schema, schemaHost),

const: arrayConstValues(prop),
}),
other: (type) =>
Expand All @@ -162,12 +172,9 @@ const maybeDeliveryKeyDirective = (
undefined,
);

const hasDeliveryKeyDirective = (field: FieldDefinitionNode) =>
Boolean(maybeDirective(field, "amplienceDeliveryKey"));

const deliveryKeyMetaProperty = (
deliveryKeyDirective: ConstDirectiveNode,
): AmplienceDeliveryKeyType => ({
): AmpliencePropertyType => ({
type: "object",
title: "Delivery Key",
properties: {
Expand Down
26 changes: 2 additions & 24 deletions packages/plugin-json/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ export type EnumProperties = {
};
};

export type InlineObject = {
type: string;
properties: Properties;
propertyOrder: string[] | undefined;
required: string[] | undefined;
};

export type InlineContentReference = {
type: "object";
allOf: RefType["allOf"];
Expand Down Expand Up @@ -72,21 +65,9 @@ export interface AmpliencePropertyType {
minimum?: number;
maximum?: number;
examples?: string[];
pattern?: string;
}

export type AmplienceDeliveryKeyType = {
type: "object";
title: "Delivery Key";
properties: {
deliveryKey: {
type: "string";
title: string;
description: string;
pattern?: string;
};
};
};

export interface AmplienceContentTypeSchemaBody {
$id: string;
$schema: string;
Expand All @@ -98,10 +79,7 @@ export interface AmplienceContentTypeSchemaBody {
"trait:sortable"?: {};
type: "object";
properties?: {
[name: string | "_meta"]:
| AmpliencePropertyType
| AmplienceDeliveryKeyType
| undefined;
[name: string | "_meta"]: AmpliencePropertyType | undefined;
};
definitions?: { [name: string]: AmpliencePropertyType };
propertyOrder?: string[];
Expand Down

0 comments on commit ab7d263

Please sign in to comment.