@diphyx/harlemify 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": "^3.14.0 || ^4.0.0"
6
6
  },
7
- "version": "6.0.0",
7
+ "version": "6.1.0",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "0.8.4",
10
10
  "unbuild": "unknown"
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import type { ShapeFactory, ShapeRawDefinition } from "../types/shape.js";
2
+ import type { ShapeCall, ShapeFactory, ShapeRawDefinition } from "../types/shape.js";
3
3
  export declare const primitiveField: {
4
4
  string: typeof z.string;
5
5
  number: typeof z.number;
@@ -42,4 +42,17 @@ export declare const specialField: {
42
42
  nullable: typeof z.nullable;
43
43
  optional: typeof z.optional;
44
44
  };
45
- export declare function shape<T extends ShapeRawDefinition>(definition: T | ((factory: ShapeFactory) => T)): import("../types/shape").ShapeCall<T>;
45
+ declare function shapeFn<T extends ShapeRawDefinition>(definition: T | z.ZodObject<T> | ((factory: ShapeFactory) => T)): ShapeCall<T>;
46
+ declare function shapeExtend<B extends ShapeRawDefinition, E extends ShapeRawDefinition>(base: ShapeCall<B>, extension: E): ShapeCall<B & E>;
47
+ declare function shapePick<B extends ShapeRawDefinition, M extends {
48
+ [K in keyof B]?: true;
49
+ }>(base: ShapeCall<B>, mask: M): ShapeCall<Pick<B, Extract<keyof B, keyof M>>>;
50
+ declare function shapeOmit<B extends ShapeRawDefinition, M extends {
51
+ [K in keyof B]?: true;
52
+ }>(base: ShapeCall<B>, mask: M): ShapeCall<Omit<B, Extract<keyof B, keyof M>>>;
53
+ export declare const shape: typeof shapeFn & {
54
+ extend: typeof shapeExtend;
55
+ pick: typeof shapePick;
56
+ omit: typeof shapeOmit;
57
+ };
58
+ export {};
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { createShape } from "../utils/shape.js";
2
+ import { createShape, decorateShape } from "../utils/shape.js";
3
3
  export const primitiveField = {
4
4
  string: z.string,
5
5
  number: z.number,
@@ -42,16 +42,32 @@ export const specialField = {
42
42
  nullable: z.nullable,
43
43
  optional: z.optional
44
44
  };
45
- export function shape(definition) {
45
+ const factory = {
46
+ ...primitiveField,
47
+ ...structureField,
48
+ ...formatField,
49
+ ...specialField
50
+ };
51
+ function shapeFn(definition) {
52
+ if (definition instanceof z.ZodObject) {
53
+ return decorateShape(definition);
54
+ }
46
55
  if (typeof definition === "function") {
47
- return createShape(
48
- definition({
49
- ...primitiveField,
50
- ...structureField,
51
- ...formatField,
52
- ...specialField
53
- })
54
- );
56
+ return createShape(definition(factory));
55
57
  }
56
58
  return createShape(definition);
57
59
  }
60
+ function shapeExtend(base, extension) {
61
+ return decorateShape(base.extend(extension));
62
+ }
63
+ function shapePick(base, mask) {
64
+ return decorateShape(base.pick(mask));
65
+ }
66
+ function shapeOmit(base, mask) {
67
+ return decorateShape(base.omit(mask));
68
+ }
69
+ export const shape = Object.assign(shapeFn, {
70
+ extend: shapeExtend,
71
+ pick: shapePick,
72
+ omit: shapeOmit
73
+ });
@@ -1,7 +1,9 @@
1
+ import { z } from "zod";
1
2
  import type { ShapeCall, ShapeDefinition, ShapeInfer, ShapeRawDefinition, ShapeType } from "../types/shape.js";
2
3
  export declare function resolveAliasInbound<T = unknown>(data: T, aliases?: Record<string, string>): T;
3
4
  export declare function resolveAliasOutbound<T = unknown>(data: T, aliases?: Record<string, string>): T;
4
5
  export declare function resolveShapeIdentifier(shape: ShapeType<unknown>, ...overrides: (string | undefined)[]): string;
5
6
  export declare function resolveShapeAliases(shape: ShapeType<unknown>): Record<string, string>;
6
7
  export declare function resolveZeroValues<T extends ShapeDefinition>(shape: T): ShapeInfer<T>;
8
+ export declare function decorateShape<T extends ShapeRawDefinition>(object: z.ZodObject<T>): ShapeCall<T>;
7
9
  export declare function createShape<T extends ShapeRawDefinition>(definition: T): ShapeCall<T>;
@@ -178,8 +178,7 @@ export function resolveZeroValues(shape) {
178
178
  }
179
179
  return output;
180
180
  }
181
- export function createShape(definition) {
182
- const object = z.object(definition);
181
+ export function decorateShape(object) {
183
182
  const shape = Object.assign(object, {
184
183
  defaults(overrides) {
185
184
  const zero = resolveZeroValues(object);
@@ -191,3 +190,7 @@ export function createShape(definition) {
191
190
  });
192
191
  return shape;
193
192
  }
193
+ export function createShape(definition) {
194
+ const object = z.object(definition);
195
+ return decorateShape(object);
196
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diphyx/harlemify",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "API state management for Nuxt powered by Harlem",
5
5
  "keywords": [
6
6
  "nuxt",