simple-strapi 1.0.0-alpha.3 → 1.0.0-alpha.4
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/client.d.ts +5 -2
- package/dist/client.js +3 -3
- package/dist/fields/component.d.ts +3 -3
- package/dist/fields/relation.d.ts +3 -3
- package/dist/utils/schema.d.ts +14 -0
- package/dist/utils/schema.js +8 -0
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { InferNumber, NumberField, NumberOptions } from "./fields/number";
|
|
2
2
|
import { InferText, TextField, TextOptions } from "./fields/text";
|
|
3
3
|
import { InferRelationHasMany, InferRelationHasOne, RelationHasManyField, RelationHasManyOptions, RelationHasOneField, RelationHasOneOptions } from "./fields/relation";
|
|
4
|
+
import z from "zod";
|
|
4
5
|
import { DynamicField, DynamicOptions, InferDynamic } from "./fields/dynamic";
|
|
6
|
+
import { defaultStrapiFieldsSchema } from "./utils/schema";
|
|
5
7
|
import { ComponentRepeatableField, ComponentRepeatableOptions, ComponentSingleField, ComponentSingleOptions, InferComponentRepeatable, InferComponentSingle } from "./fields/component";
|
|
6
8
|
import { InferMediaSingle, MediaSingleField, MediaSingleOptions } from "./fields/media";
|
|
7
9
|
import { EnumerationField, EnumerationOptions, InferEnumeration } from "./fields/enumeration";
|
|
@@ -40,6 +42,7 @@ export type InferSchema<S extends Schema> = {
|
|
|
40
42
|
infer O extends EnumerationOptions
|
|
41
43
|
] ? InferEnumeration<V, O> : S[K] extends ["richText.blocks", infer O extends RichTextBlocksOptions] ? InferRichTextBlocks<O> : never;
|
|
42
44
|
};
|
|
45
|
+
export type InferSchemaWithDefaults<S extends Schema> = InferSchema<S> & z.output<typeof defaultStrapiFieldsSchema>;
|
|
43
46
|
declare class Client {
|
|
44
47
|
private options;
|
|
45
48
|
private static headers;
|
|
@@ -73,7 +76,7 @@ declare class Client {
|
|
|
73
76
|
schema: S;
|
|
74
77
|
populate?: any;
|
|
75
78
|
}>): Promise<{
|
|
76
|
-
data:
|
|
79
|
+
data: InferSchemaWithDefaults<S>;
|
|
77
80
|
meta: any;
|
|
78
81
|
}>;
|
|
79
82
|
getSingle(pluralID: string, options: EntityRequest<{
|
|
@@ -90,7 +93,7 @@ declare class Client {
|
|
|
90
93
|
};
|
|
91
94
|
populate?: any;
|
|
92
95
|
}>): Promise<{
|
|
93
|
-
data:
|
|
96
|
+
data: InferSchemaWithDefaults<S>[];
|
|
94
97
|
meta: any;
|
|
95
98
|
}>;
|
|
96
99
|
getCollection(pluralID: string, options: EntityRequest<{
|
package/dist/client.js
CHANGED
|
@@ -3,7 +3,7 @@ import { join } from "path";
|
|
|
3
3
|
import fetch from "node-fetch";
|
|
4
4
|
import qs from "qs";
|
|
5
5
|
import z from "zod";
|
|
6
|
-
import { schemaToParser } from "./utils/schema";
|
|
6
|
+
import { defaultStrapiFields, schemaToParser } from "./utils/schema";
|
|
7
7
|
class Client {
|
|
8
8
|
static async create(endpoint, { auth, ...options } = {}) {
|
|
9
9
|
const endpointURL = new URL(endpoint);
|
|
@@ -177,7 +177,7 @@ class Client {
|
|
|
177
177
|
if ("schema" in options) {
|
|
178
178
|
const { schema: shape } = options;
|
|
179
179
|
if (shape) {
|
|
180
|
-
const schema = z.object(schemaToParser(shape)).loose();
|
|
180
|
+
const schema = z.object(schemaToParser(shape)).extend(defaultStrapiFields).loose();
|
|
181
181
|
const result = schema.safeParse(data);
|
|
182
182
|
if (!result.success) {
|
|
183
183
|
console.warn("⚠️ Single entity parsing error");
|
|
@@ -247,7 +247,7 @@ class Client {
|
|
|
247
247
|
if ("schema" in options) {
|
|
248
248
|
const { schema: shape } = options;
|
|
249
249
|
if (shape) {
|
|
250
|
-
const schema = z.object(schemaToParser(shape)).loose();
|
|
250
|
+
const schema = z.object(schemaToParser(shape)).extend(defaultStrapiFields).loose();
|
|
251
251
|
const parsedData = [];
|
|
252
252
|
for (const entry of data) {
|
|
253
253
|
const result = schema.safeParse(entry);
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { ZodType } from "zod";
|
|
2
|
-
import {
|
|
2
|
+
import { InferSchemaWithDefaults, Schema } from "../client";
|
|
3
3
|
/**
|
|
4
4
|
* SINGLE
|
|
5
5
|
*/
|
|
6
6
|
export type ComponentSingleOptions = {
|
|
7
7
|
required?: boolean;
|
|
8
8
|
};
|
|
9
|
-
export type InferComponentSingle<S extends Schema, O extends ComponentSingleOptions> = O["required"] extends true ?
|
|
9
|
+
export type InferComponentSingle<S extends Schema, O extends ComponentSingleOptions> = O["required"] extends true ? InferSchemaWithDefaults<S> : InferSchemaWithDefaults<S> | null | undefined;
|
|
10
10
|
export declare const singleSchema: (shape: Schema, opts: ComponentSingleOptions) => ZodType;
|
|
11
11
|
export type ComponentSingleField = readonly ["component.single", Schema, ComponentSingleOptions];
|
|
12
12
|
/**
|
|
@@ -15,7 +15,7 @@ export type ComponentSingleField = readonly ["component.single", Schema, Compone
|
|
|
15
15
|
export type ComponentRepeatableOptions = {
|
|
16
16
|
required?: boolean;
|
|
17
17
|
};
|
|
18
|
-
export type InferComponentRepeatable<S extends Schema, O extends ComponentRepeatableOptions> = O["required"] extends true ?
|
|
18
|
+
export type InferComponentRepeatable<S extends Schema, O extends ComponentRepeatableOptions> = O["required"] extends true ? InferSchemaWithDefaults<S>[] : InferSchemaWithDefaults<S>[] | null | undefined;
|
|
19
19
|
export declare const repeatableSchema: (shape: Schema, opts: ComponentRepeatableOptions) => ZodType;
|
|
20
20
|
export type ComponentRepeatableField = readonly [
|
|
21
21
|
"component.repeatable",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InferSchemaWithDefaults, Schema } from "../client";
|
|
2
2
|
/**
|
|
3
3
|
* HAS MANY
|
|
4
4
|
*/
|
|
@@ -6,7 +6,7 @@ export type RelationHasManyOptions = {
|
|
|
6
6
|
nullable?: boolean;
|
|
7
7
|
optional?: boolean;
|
|
8
8
|
};
|
|
9
|
-
export type InferRelationHasMany<S extends Schema, O extends RelationHasManyOptions> = O["nullable"] extends true ? O["optional"] extends true ?
|
|
9
|
+
export type InferRelationHasMany<S extends Schema, O extends RelationHasManyOptions> = O["nullable"] extends true ? O["optional"] extends true ? InferSchemaWithDefaults<S>[] | null | undefined : InferSchemaWithDefaults<S>[] | null : O["optional"] extends true ? InferSchemaWithDefaults<S>[] | undefined : InferSchemaWithDefaults<S>[];
|
|
10
10
|
export type RelationHasManyField = readonly ["relation.hasMany", Schema, RelationHasManyOptions];
|
|
11
11
|
/**
|
|
12
12
|
* HAS ONE
|
|
@@ -15,7 +15,7 @@ export type RelationHasOneOptions = {
|
|
|
15
15
|
nullable?: boolean;
|
|
16
16
|
optional?: boolean;
|
|
17
17
|
};
|
|
18
|
-
export type InferRelationHasOne<S extends Schema, O extends RelationHasOneOptions> = O["nullable"] extends true ? O["optional"] extends true ?
|
|
18
|
+
export type InferRelationHasOne<S extends Schema, O extends RelationHasOneOptions> = O["nullable"] extends true ? O["optional"] extends true ? InferSchemaWithDefaults<S> | null | undefined : InferSchemaWithDefaults<S> | null : O["optional"] extends true ? InferSchemaWithDefaults<S> | undefined : InferSchemaWithDefaults<S>;
|
|
19
19
|
export type RelationHasOneField = readonly ["relation.hasOne", Schema, RelationHasOneOptions];
|
|
20
20
|
export declare const relation: {
|
|
21
21
|
hasMany: <S = any, O extends RelationHasManyOptions = {}>(shape: S, options?: O) => ["relation.hasMany", S, O];
|
package/dist/utils/schema.d.ts
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
1
|
import { Schema } from "../client";
|
|
2
2
|
import z from "zod";
|
|
3
|
+
export declare const defaultStrapiFields: {
|
|
4
|
+
id: z.ZodNumber;
|
|
5
|
+
documentId: z.ZodOptional<z.ZodString>;
|
|
6
|
+
createdAt: z.ZodOptional<z.ZodISODateTime>;
|
|
7
|
+
updatedAt: z.ZodOptional<z.ZodISODateTime>;
|
|
8
|
+
publishedAt: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
|
|
9
|
+
};
|
|
10
|
+
export declare const defaultStrapiFieldsSchema: z.ZodObject<{
|
|
11
|
+
id: z.ZodNumber;
|
|
12
|
+
documentId: z.ZodOptional<z.ZodString>;
|
|
13
|
+
createdAt: z.ZodOptional<z.ZodISODateTime>;
|
|
14
|
+
updatedAt: z.ZodOptional<z.ZodISODateTime>;
|
|
15
|
+
publishedAt: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
|
|
16
|
+
}, z.core.$strip>;
|
|
3
17
|
export declare const schemaToParser: (schema: Schema) => Record<string, z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>>;
|
package/dist/utils/schema.js
CHANGED
|
@@ -6,6 +6,14 @@ import { textSchema } from "../fields/text";
|
|
|
6
6
|
import z from "zod";
|
|
7
7
|
import { enumerationSchema } from "../fields/enumeration";
|
|
8
8
|
import { richTextBlocksSchema } from "../fields/richText";
|
|
9
|
+
export const defaultStrapiFields = {
|
|
10
|
+
id: z.number(),
|
|
11
|
+
documentId: z.string().optional(),
|
|
12
|
+
createdAt: z.iso.datetime().optional(),
|
|
13
|
+
updatedAt: z.iso.datetime().optional(),
|
|
14
|
+
publishedAt: z.iso.datetime().nullable().optional(),
|
|
15
|
+
};
|
|
16
|
+
export const defaultStrapiFieldsSchema = z.object(defaultStrapiFields);
|
|
9
17
|
export const schemaToParser = (schema) => {
|
|
10
18
|
const shape = {};
|
|
11
19
|
for (const [key, field] of Object.entries(schema)) {
|