Skip to content

Commit

Permalink
feat(schema): enable shape upsert operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Dec 11, 2024
1 parent 5d38597 commit ca359a3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 59 deletions.
2 changes: 1 addition & 1 deletion components/schema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crystallize/schema",
"version": "3.0.0",
"version": "3.0.1",
"license": "MIT",
"exports": {
"./pim": {
Expand Down
19 changes: 4 additions & 15 deletions components/schema/src/mass-operation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import {
UpdateItemOperationSchema,
UpsertItemOperationSchema,
} from './item.js';
import {
CreateProductShapeOperationSchema,
CreateFolderShapeOperationSchema,
CreateDocumentShapeOperationSchema,
UpdateShapeOperationSchema,
} from './shape.js';
import { CreateShapeOperationSchema, UpdateShapeOperationSchema, UpsertShapeOperationSchema } from './shape.js';

export const OperationSchema = z.discriminatedUnion('intent', [
CreateItemOperationSchema,
Expand All @@ -23,10 +18,9 @@ export const OperationSchema = z.discriminatedUnion('intent', [
UnPublishItemOperationSchema,
UpdateItemComponentOperationSchema,
UpdateSkuComponentOperationSchema,
CreateProductShapeOperationSchema,
CreateFolderShapeOperationSchema,
CreateDocumentShapeOperationSchema,
CreateShapeOperationSchema,
UpdateShapeOperationSchema,
UpsertShapeOperationSchema,
]);

export const OperationsSchema = z.object({
Expand All @@ -48,9 +42,4 @@ export type {
UpsertItemOperation,
} from './item.js';

export type {
CreateProductShapeOperation,
CreateDocumentShapeOperation,
CreateFolderShapeOperation,
UpdateShapeOperation,
} from './shape.js';
export type { CreateShapeOperation, UpdateShapeOperation, UpsertShapeOperation } from './shape.js';
29 changes: 9 additions & 20 deletions components/schema/src/mass-operation/shape.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import { z } from 'zod';
import {
CreateDocumentShapeInputSchema,
CreateFolderShapeInputSchema,
CreateProductShapeInputSchema,
UpdateShapeInputSchema,
} from '../pim/shape/index.js';
import { CreateShapeInputSchema, UpdateShapeInputSchema } from '../pim/shape/index.js';

export const CreateDocumentShapeOperationSchema = CreateDocumentShapeInputSchema.extend({
intent: z.literal('shape/create/document'),
export const CreateShapeOperationSchema = CreateShapeInputSchema.extend({
intent: z.literal('shape/create'),
});
export type CreateDocumentShapeOperation = z.infer<typeof CreateDocumentShapeOperationSchema>;

export const CreateFolderShapeOperationSchema = CreateFolderShapeInputSchema.extend({
intent: z.literal('shape/create/folder'),
});
export type CreateFolderShapeOperation = z.infer<typeof CreateDocumentShapeOperationSchema>;

export const CreateProductShapeOperationSchema = CreateProductShapeInputSchema.extend({
intent: z.literal('shape/create/product'),
});
export type CreateProductShapeOperation = z.infer<typeof CreateProductShapeOperationSchema>;
export type CreateShapeOperation = z.infer<typeof CreateShapeOperationSchema>;

export const UpdateShapeOperationSchema = UpdateShapeInputSchema.extend({
intent: z.literal('shape/update'),
identifier: z.string().min(1),
});
export type UpdateShapeOperation = z.infer<typeof UpdateShapeOperationSchema>;

export const UpsertShapeOperationSchema = CreateShapeOperationSchema.extend({
intent: z.literal('shape/upsert'),
});
export type UpsertShapeOperation = z.infer<typeof UpsertShapeOperationSchema>;
8 changes: 4 additions & 4 deletions components/schema/src/pim/components/component-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ export const ComponentDefinitionSchema = z.object({
id: z.string().min(1),
type: ComponentTypeEnum,
name: z.string().min(1),
description: z.string(),
config: ComponentConfigSchema,
description: z.string().optional(),
config: ComponentConfigSchema.optional(),
});
export type ComponentDefinition = z.infer<typeof ComponentDefinitionSchema>;

export const ComponentDefinitionInputSchema = z.object({
id: z.string().min(1),
type: ComponentTypeEnum,
name: z.string().min(1),
description: z.string(),
config: ComponentConfigInputSchema,
description: z.string().optional(),
config: ComponentConfigInputSchema.optional(),
});

export type ComponentDefinitionInput = z.infer<typeof ComponentDefinitionInputSchema>;
23 changes: 4 additions & 19 deletions components/schema/src/pim/shape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,16 @@ import { z } from 'zod';
import { ItemTypeEnum, KeyValuePairInputSchema } from '../../shared/index.js';
import { ComponentDefinitionInputSchema, ComponentDefinitionSchema } from '../components/component-definition.js';

const BaseShapeInputSchema = z.object({
export const UpdateShapeInputSchema = z.object({
identifier: z.string().min(2).max(64),
name: z.string().min(1),
meta: KeyValuePairInputSchema.optional().nullable(),
components: z.array(ComponentDefinitionInputSchema).optional().nullable(),
});

export const CreateDocumentShapeInputSchema = BaseShapeInputSchema.extend({
type: z.literal(ItemTypeEnum.Values.document),
});

export const CreateFolderShapeInputSchema = BaseShapeInputSchema.extend({
type: z.literal(ItemTypeEnum.Values.folder),
});

export const CreateProductShapeInputSchema = BaseShapeInputSchema.extend({
type: z.literal(ItemTypeEnum.Values.product),
variantComponents: z.array(ComponentDefinitionInputSchema).optional().nullable(),
});

export const UpdateShapeInputSchema = BaseShapeInputSchema.extend({
components: z.array(ComponentDefinitionInputSchema).optional().nullable(),
variantComponents: z.array(ComponentDefinitionInputSchema).optional().nullable(),
export const CreateShapeInputSchema = UpdateShapeInputSchema.extend({
type: ItemTypeEnum,
});

export const BasicShapeSchema = z.object({
Expand All @@ -38,8 +25,6 @@ export const ShapeSchema = BasicShapeSchema.extend({
variantComponents: z.array(ComponentDefinitionSchema).optional(),
});

export type CreateDocumentShapeInput = z.infer<typeof CreateDocumentShapeInputSchema>;
export type CreateFolderShapeInput = z.infer<typeof CreateFolderShapeInputSchema>;
export type CreateProductShapeInput = z.infer<typeof CreateProductShapeInputSchema>;
export type CreateShapeInput = z.infer<typeof UpdateShapeInputSchema>;
export type UpdateShapeInput = z.infer<typeof UpdateShapeInputSchema>;
export type Shape = z.infer<typeof ShapeSchema>;

0 comments on commit ca359a3

Please sign in to comment.