simple-strapi 1.0.0-alpha.2 → 1.0.0-alpha.21
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 +42 -5
- package/dist/client.js +130 -13
- package/dist/fields/boolean.d.ts +8 -0
- package/dist/fields/boolean.js +10 -0
- package/dist/fields/component.d.ts +3 -3
- package/dist/fields/json.d.ts +8 -0
- package/dist/fields/json.js +10 -0
- package/dist/fields/number.d.ts +2 -3
- package/dist/fields/number.js +2 -4
- package/dist/fields/relation.d.ts +15 -2
- package/dist/fields/relation.js +4 -1
- package/dist/fields/richText.d.ts +15 -0
- package/dist/fields/richText.js +15 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/utils/schema.d.ts +14 -0
- package/dist/utils/schema.js +22 -2
- package/package.json +2 -2
package/dist/client.d.ts
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
|
+
import { InferBoolean, BooleanField, BooleanOptions } from "./fields/boolean";
|
|
1
2
|
import { InferNumber, NumberField, NumberOptions } from "./fields/number";
|
|
2
3
|
import { InferText, TextField, TextOptions } from "./fields/text";
|
|
3
|
-
import { InferRelationHasMany, RelationHasManyField, RelationHasManyOptions } from "./fields/relation";
|
|
4
|
+
import { InferRelationHasMany, InferRelationHasOne, RelationHasManyField, RelationHasManyOptions, RelationHasOneField, RelationHasOneOptions } from "./fields/relation";
|
|
5
|
+
import z from "zod";
|
|
4
6
|
import { DynamicField, DynamicOptions, InferDynamic } from "./fields/dynamic";
|
|
7
|
+
import { defaultStrapiFieldsSchema } from "./utils/schema";
|
|
5
8
|
import { ComponentRepeatableField, ComponentRepeatableOptions, ComponentSingleField, ComponentSingleOptions, InferComponentRepeatable, InferComponentSingle } from "./fields/component";
|
|
6
9
|
import { InferMediaSingle, MediaSingleField, MediaSingleOptions } from "./fields/media";
|
|
7
10
|
import { EnumerationField, EnumerationOptions, InferEnumeration } from "./fields/enumeration";
|
|
8
11
|
import { InferRichTextBlocks, RichTextBlocksField, RichTextBlocksOptions } from "./fields/richText";
|
|
12
|
+
import { InferJSON, JSONField, JSONOptions } from "./fields/json";
|
|
9
13
|
type RequestParams = Record<string, any>;
|
|
10
14
|
type EntityRequest<P = {}> = {
|
|
15
|
+
where?: Record<string, string>;
|
|
11
16
|
params?: RequestParams;
|
|
12
17
|
headers?: Record<string, string>;
|
|
13
18
|
} & P;
|
|
14
|
-
export type SchemaField = TextField | NumberField | RelationHasManyField | DynamicField | ComponentSingleField | ComponentRepeatableField | MediaSingleField | EnumerationField | RichTextBlocksField;
|
|
19
|
+
export type SchemaField = TextField | NumberField | BooleanField | RelationHasManyField | RelationHasOneField | DynamicField | ComponentSingleField | ComponentRepeatableField | MediaSingleField | EnumerationField | RichTextBlocksField | JSONField;
|
|
15
20
|
export type Schema = Record<string, SchemaField>;
|
|
16
21
|
export type InferSchema<S extends Schema> = {
|
|
17
|
-
[K in keyof S]: S[K] extends ["text", infer O extends TextOptions] ? InferText<O> : S[K] extends ["number", infer O extends NumberOptions] ? InferNumber<O> : S[K] extends [
|
|
22
|
+
[K in keyof S]: S[K] extends ["text", infer O extends TextOptions] ? InferText<O> : S[K] extends ["number", infer O extends NumberOptions] ? InferNumber<O> : S[K] extends ["boolean", infer O extends BooleanOptions] ? InferBoolean<O> : S[K] extends ["json", infer O extends JSONOptions] ? InferJSON<O> : S[K] extends [
|
|
18
23
|
"relation.hasMany",
|
|
19
24
|
infer R extends Schema,
|
|
20
25
|
infer O extends RelationHasManyOptions
|
|
21
26
|
] ? InferRelationHasMany<R, O> : S[K] extends [
|
|
27
|
+
"relation.hasOne",
|
|
28
|
+
infer R extends Schema,
|
|
29
|
+
infer O extends RelationHasOneOptions
|
|
30
|
+
] ? InferRelationHasOne<R, O> : S[K] extends [
|
|
22
31
|
"component.single",
|
|
23
32
|
infer R extends Schema,
|
|
24
33
|
infer O extends ComponentSingleOptions
|
|
@@ -36,6 +45,7 @@ export type InferSchema<S extends Schema> = {
|
|
|
36
45
|
infer O extends EnumerationOptions
|
|
37
46
|
] ? InferEnumeration<V, O> : S[K] extends ["richText.blocks", infer O extends RichTextBlocksOptions] ? InferRichTextBlocks<O> : never;
|
|
38
47
|
};
|
|
48
|
+
export type InferSchemaWithDefaults<S extends Schema> = InferSchema<S> & z.output<typeof defaultStrapiFieldsSchema>;
|
|
39
49
|
declare class Client {
|
|
40
50
|
private options;
|
|
41
51
|
private static headers;
|
|
@@ -69,7 +79,7 @@ declare class Client {
|
|
|
69
79
|
schema: S;
|
|
70
80
|
populate?: any;
|
|
71
81
|
}>): Promise<{
|
|
72
|
-
data:
|
|
82
|
+
data: InferSchemaWithDefaults<S>;
|
|
73
83
|
meta: any;
|
|
74
84
|
}>;
|
|
75
85
|
getSingle(pluralID: string, options: EntityRequest<{
|
|
@@ -85,8 +95,9 @@ declare class Client {
|
|
|
85
95
|
pageSize?: number;
|
|
86
96
|
};
|
|
87
97
|
populate?: any;
|
|
98
|
+
filters?: Record<string, any>;
|
|
88
99
|
}>): Promise<{
|
|
89
|
-
data:
|
|
100
|
+
data: InferSchemaWithDefaults<S>[];
|
|
90
101
|
meta: any;
|
|
91
102
|
}>;
|
|
92
103
|
getCollection(pluralID: string, options: EntityRequest<{
|
|
@@ -95,9 +106,35 @@ declare class Client {
|
|
|
95
106
|
pageSize?: number;
|
|
96
107
|
};
|
|
97
108
|
populate?: any;
|
|
109
|
+
filters?: Record<string, any>;
|
|
98
110
|
}>): Promise<{
|
|
99
111
|
data: any[];
|
|
100
112
|
meta: any;
|
|
101
113
|
}>;
|
|
114
|
+
/**
|
|
115
|
+
*
|
|
116
|
+
* WRITE ACTIONS
|
|
117
|
+
*
|
|
118
|
+
*/
|
|
119
|
+
update<S extends Schema>(pluralID: string, documentId: string, payload: any, options?: EntityRequest<{
|
|
120
|
+
schema?: S;
|
|
121
|
+
}>): Promise<{
|
|
122
|
+
data: InferSchemaWithDefaults<S>;
|
|
123
|
+
meta: any;
|
|
124
|
+
}>;
|
|
125
|
+
create<S extends Schema>(pluralID: string, payload: any, options?: EntityRequest<{
|
|
126
|
+
schema?: S;
|
|
127
|
+
}>): Promise<{
|
|
128
|
+
data: InferSchemaWithDefaults<S>;
|
|
129
|
+
meta: any;
|
|
130
|
+
}>;
|
|
131
|
+
private writeRequest;
|
|
132
|
+
/**
|
|
133
|
+
* Elimina un'entità specifica tramite il suo documentId.
|
|
134
|
+
*/
|
|
135
|
+
delete(pluralID: string, documentId: string, options?: EntityRequest): Promise<{
|
|
136
|
+
data: any;
|
|
137
|
+
meta: any;
|
|
138
|
+
}>;
|
|
102
139
|
}
|
|
103
140
|
export default Client;
|
package/dist/client.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { createSimpleException,
|
|
1
|
+
import { createSimpleException, ensureSimpleException } from "simple-exception";
|
|
2
2
|
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);
|
|
@@ -50,7 +50,7 @@ class Client {
|
|
|
50
50
|
return token;
|
|
51
51
|
}
|
|
52
52
|
catch (exception) {
|
|
53
|
-
throw
|
|
53
|
+
throw ensureSimpleException(exception);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
constructor(options) {
|
|
@@ -68,6 +68,9 @@ class Client {
|
|
|
68
68
|
const populateHasManyRelation = ([, shape]) => {
|
|
69
69
|
return this.populateFromSchema(shape);
|
|
70
70
|
};
|
|
71
|
+
const populateHasOneRelation = ([, shape]) => {
|
|
72
|
+
return this.populateFromSchema(shape);
|
|
73
|
+
};
|
|
71
74
|
const populateComponentSingle = ([, shape]) => {
|
|
72
75
|
return this.populateFromSchema(shape);
|
|
73
76
|
};
|
|
@@ -91,6 +94,11 @@ class Client {
|
|
|
91
94
|
if (!Object.keys(populate[key].populate)["length"])
|
|
92
95
|
populate[key] = true;
|
|
93
96
|
break;
|
|
97
|
+
case "relation.hasOne":
|
|
98
|
+
populate[key] = { populate: populateHasOneRelation(field) };
|
|
99
|
+
if (!Object.keys(populate[key].populate)["length"])
|
|
100
|
+
populate[key] = true;
|
|
101
|
+
break;
|
|
94
102
|
case "component.single":
|
|
95
103
|
populate[key] = { populate: populateComponentSingle(field) };
|
|
96
104
|
if (!Object.keys(populate[key].populate)["length"])
|
|
@@ -161,13 +169,15 @@ class Client {
|
|
|
161
169
|
source: "strapi-utils/client.ts",
|
|
162
170
|
});
|
|
163
171
|
}
|
|
164
|
-
const { data
|
|
172
|
+
const { data, meta } = z
|
|
173
|
+
.object({ data: z.any(), meta: z.any() })
|
|
174
|
+
.parse(await response.json());
|
|
165
175
|
if (!data)
|
|
166
176
|
throw createSimpleException({ code: 404, type: "error", message: "Not found" });
|
|
167
177
|
if ("schema" in options) {
|
|
168
178
|
const { schema: shape } = options;
|
|
169
179
|
if (shape) {
|
|
170
|
-
const schema = z.object(schemaToParser(shape)).loose();
|
|
180
|
+
const schema = z.object(schemaToParser(shape)).extend(defaultStrapiFields).loose();
|
|
171
181
|
const result = schema.safeParse(data);
|
|
172
182
|
if (!result.success) {
|
|
173
183
|
console.warn("⚠️ Single entity parsing error");
|
|
@@ -180,7 +190,7 @@ class Client {
|
|
|
180
190
|
return { data, meta };
|
|
181
191
|
}
|
|
182
192
|
catch (exception) {
|
|
183
|
-
throw
|
|
193
|
+
throw ensureSimpleException(exception);
|
|
184
194
|
}
|
|
185
195
|
}
|
|
186
196
|
async getCollection(pluralID, { params = {}, headers = {}, pagination = { page: 1 }, ...options } = {}) {
|
|
@@ -200,11 +210,12 @@ class Client {
|
|
|
200
210
|
}
|
|
201
211
|
const fetchPage = async (page = 1, acc = []) => {
|
|
202
212
|
params.pagination = { page, pageSize: 100 };
|
|
213
|
+
params.filters = options.filters;
|
|
203
214
|
if (pagination)
|
|
204
215
|
params.pagination = { ...params.pagination, ...pagination };
|
|
205
216
|
const requestURL = Client.getRequestURL({
|
|
206
217
|
origin: this.origin,
|
|
207
|
-
pathname: join(this.pathname, pluralID),
|
|
218
|
+
pathname: join(...[this.pathname, pluralID, options.where?.documentId].flatMap((entry) => !!entry ? [entry] : [])),
|
|
208
219
|
params,
|
|
209
220
|
});
|
|
210
221
|
const response = await fetch(requestURL, {
|
|
@@ -222,10 +233,12 @@ class Client {
|
|
|
222
233
|
source: "strapi-utils/client.ts",
|
|
223
234
|
});
|
|
224
235
|
}
|
|
236
|
+
const responseData = await response.json();
|
|
225
237
|
const { data, meta } = z
|
|
226
|
-
.object({ data: z.array(z.any()).catch([]), meta: z.any() })
|
|
227
|
-
.
|
|
228
|
-
|
|
238
|
+
// .object({ data: z.array(z.any()).catch([]), meta: z.any() })
|
|
239
|
+
.object({ data: z.any(), meta: z.any() })
|
|
240
|
+
.parse(responseData);
|
|
241
|
+
const accData = [...acc, ...(Array.isArray(data) ? data : [data])];
|
|
229
242
|
if (!pagination) {
|
|
230
243
|
if (meta.pagination?.page < meta.pagination?.pageCount) {
|
|
231
244
|
return await fetchPage(meta.pagination.page + 1, accData);
|
|
@@ -237,7 +250,7 @@ class Client {
|
|
|
237
250
|
if ("schema" in options) {
|
|
238
251
|
const { schema: shape } = options;
|
|
239
252
|
if (shape) {
|
|
240
|
-
const schema = z.object(schemaToParser(shape)).loose();
|
|
253
|
+
const schema = z.object(schemaToParser(shape)).extend(defaultStrapiFields).loose();
|
|
241
254
|
const parsedData = [];
|
|
242
255
|
for (const entry of data) {
|
|
243
256
|
const result = schema.safeParse(entry);
|
|
@@ -245,7 +258,7 @@ class Client {
|
|
|
245
258
|
parsedData.push(result.data);
|
|
246
259
|
}
|
|
247
260
|
else {
|
|
248
|
-
console.warn("⚠️ Collection parsing error on entry");
|
|
261
|
+
console.warn("⚠️ Collection parsing error on entry", entry);
|
|
249
262
|
console.error("🚨 Error", result.error);
|
|
250
263
|
}
|
|
251
264
|
}
|
|
@@ -255,7 +268,111 @@ class Client {
|
|
|
255
268
|
return { data, meta };
|
|
256
269
|
}
|
|
257
270
|
catch (exception) {
|
|
258
|
-
throw
|
|
271
|
+
throw ensureSimpleException(exception);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
*
|
|
276
|
+
* WRITE ACTIONS
|
|
277
|
+
*
|
|
278
|
+
*/
|
|
279
|
+
async update(pluralID, documentId, payload, options = {}) {
|
|
280
|
+
const path = join(pluralID, documentId);
|
|
281
|
+
return this.writeRequest("PUT", path, payload, options);
|
|
282
|
+
}
|
|
283
|
+
async create(pluralID, payload, options = {}) {
|
|
284
|
+
return this.writeRequest("POST", pluralID, payload, options);
|
|
285
|
+
}
|
|
286
|
+
async writeRequest(method, path, payload, { params = {}, headers = {}, ...options } = {}) {
|
|
287
|
+
try {
|
|
288
|
+
if ("schema" in options && options.schema) {
|
|
289
|
+
params.populate = this.populateFromSchema(options.schema);
|
|
290
|
+
}
|
|
291
|
+
const requestURL = Client.getRequestURL({
|
|
292
|
+
origin: this.origin,
|
|
293
|
+
pathname: join(this.pathname, path),
|
|
294
|
+
params,
|
|
295
|
+
});
|
|
296
|
+
const response = await fetch(requestURL, {
|
|
297
|
+
method,
|
|
298
|
+
headers: {
|
|
299
|
+
...this.getAuthorizedHeaders(),
|
|
300
|
+
...headers,
|
|
301
|
+
},
|
|
302
|
+
body: JSON.stringify({ data: payload }),
|
|
303
|
+
});
|
|
304
|
+
if (!response.ok) {
|
|
305
|
+
const errorBody = await response.json().catch(() => ({}));
|
|
306
|
+
const getErrorMessage = (err) => {
|
|
307
|
+
if (err && typeof err === "object" && "error" in err) {
|
|
308
|
+
return err.error?.message || response.statusText;
|
|
309
|
+
}
|
|
310
|
+
return response.statusText;
|
|
311
|
+
};
|
|
312
|
+
throw createSimpleException({
|
|
313
|
+
code: response.status,
|
|
314
|
+
message: getErrorMessage(errorBody),
|
|
315
|
+
type: "error",
|
|
316
|
+
source: "strapi-utils/client.ts",
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
const { data, meta } = z
|
|
320
|
+
.object({ data: z.any(), meta: z.any() })
|
|
321
|
+
.parse(await response.json());
|
|
322
|
+
if ("schema" in options && options.schema) {
|
|
323
|
+
const shape = options.schema;
|
|
324
|
+
const schema = z.object(schemaToParser(shape)).extend(defaultStrapiFields).loose();
|
|
325
|
+
const result = schema.safeParse(data);
|
|
326
|
+
if (!result.success) {
|
|
327
|
+
console.warn(`⚠️ ${method} response parsing error`);
|
|
328
|
+
console.error("🚨 Error details:", result.error);
|
|
329
|
+
return { data, meta };
|
|
330
|
+
}
|
|
331
|
+
return { data: result.data, meta };
|
|
332
|
+
}
|
|
333
|
+
return { data, meta };
|
|
334
|
+
}
|
|
335
|
+
catch (exception) {
|
|
336
|
+
throw ensureSimpleException(exception);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Elimina un'entità specifica tramite il suo documentId.
|
|
341
|
+
*/
|
|
342
|
+
async delete(pluralID, documentId, options = {}) {
|
|
343
|
+
try {
|
|
344
|
+
const { params = {}, headers = {} } = options;
|
|
345
|
+
const requestURL = Client.getRequestURL({
|
|
346
|
+
origin: this.origin,
|
|
347
|
+
pathname: join(this.pathname, pluralID, documentId),
|
|
348
|
+
params,
|
|
349
|
+
});
|
|
350
|
+
const response = await fetch(requestURL, {
|
|
351
|
+
method: "DELETE",
|
|
352
|
+
headers: {
|
|
353
|
+
...this.getAuthorizedHeaders(),
|
|
354
|
+
...headers,
|
|
355
|
+
},
|
|
356
|
+
});
|
|
357
|
+
if (!response.ok) {
|
|
358
|
+
const errorBody = await response.json().catch(() => ({}));
|
|
359
|
+
throw createSimpleException({
|
|
360
|
+
code: response.status,
|
|
361
|
+
message: errorBody.error?.message || response.statusText,
|
|
362
|
+
type: "error",
|
|
363
|
+
source: "strapi-utils/client.ts",
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
if (response.status === 204) {
|
|
367
|
+
return { data: { documentId }, meta: {} };
|
|
368
|
+
}
|
|
369
|
+
const { data, meta } = z
|
|
370
|
+
.object({ data: z.any(), meta: z.any() })
|
|
371
|
+
.parse(await response.json());
|
|
372
|
+
return { data, meta };
|
|
373
|
+
}
|
|
374
|
+
catch (exception) {
|
|
375
|
+
throw ensureSimpleException(exception);
|
|
259
376
|
}
|
|
260
377
|
}
|
|
261
378
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ZodType } from "zod";
|
|
2
|
+
export type BooleanOptions = {
|
|
3
|
+
required?: boolean;
|
|
4
|
+
};
|
|
5
|
+
export type InferBoolean<O extends BooleanOptions> = O["required"] extends true ? boolean : boolean | null | undefined;
|
|
6
|
+
export declare const boolean: <O extends BooleanOptions = {}>(options?: O) => ["boolean", O];
|
|
7
|
+
export declare const booleanSchema: (opts: BooleanOptions) => ZodType;
|
|
8
|
+
export type BooleanField = ReturnType<typeof boolean>;
|
|
@@ -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",
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ZodType } from "zod";
|
|
2
|
+
export type JSONOptions = {
|
|
3
|
+
required?: boolean;
|
|
4
|
+
};
|
|
5
|
+
export type InferJSON<O extends JSONOptions> = O["required"] extends true ? any : any | null | undefined;
|
|
6
|
+
export declare const json: <O extends JSONOptions = {}>(options?: O) => ["json", O];
|
|
7
|
+
export declare const jsonSchema: (opts: JSONOptions) => ZodType;
|
|
8
|
+
export type JSONField = ReturnType<typeof json>;
|
package/dist/fields/number.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { ZodType } from "zod";
|
|
2
2
|
export type NumberOptions = {
|
|
3
|
-
|
|
4
|
-
optional?: boolean;
|
|
3
|
+
required?: boolean;
|
|
5
4
|
};
|
|
6
|
-
export type InferNumber<O extends NumberOptions> = O["
|
|
5
|
+
export type InferNumber<O extends NumberOptions> = O["required"] extends true ? number : number | null | undefined;
|
|
7
6
|
export declare const number: <O extends NumberOptions = {}>(options?: O) => ["number", O];
|
|
8
7
|
export declare const numberSchema: (opts: NumberOptions) => ZodType;
|
|
9
8
|
export type NumberField = ReturnType<typeof number>;
|
package/dist/fields/number.js
CHANGED
|
@@ -4,9 +4,7 @@ export const number = (options = {}) => {
|
|
|
4
4
|
};
|
|
5
5
|
export const numberSchema = (opts) => {
|
|
6
6
|
let schema = z.number();
|
|
7
|
-
if (opts.
|
|
8
|
-
schema = schema.nullable();
|
|
9
|
-
if (opts.optional)
|
|
10
|
-
schema = schema.optional();
|
|
7
|
+
if (!opts.required)
|
|
8
|
+
schema = schema.nullable().optional();
|
|
11
9
|
return schema;
|
|
12
10
|
};
|
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InferSchemaWithDefaults, Schema } from "../client";
|
|
2
|
+
/**
|
|
3
|
+
* HAS MANY
|
|
4
|
+
*/
|
|
2
5
|
export type RelationHasManyOptions = {
|
|
3
6
|
nullable?: boolean;
|
|
4
7
|
optional?: boolean;
|
|
5
8
|
};
|
|
6
|
-
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>[];
|
|
7
10
|
export type RelationHasManyField = readonly ["relation.hasMany", Schema, RelationHasManyOptions];
|
|
11
|
+
/**
|
|
12
|
+
* HAS ONE
|
|
13
|
+
*/
|
|
14
|
+
export type RelationHasOneOptions = {
|
|
15
|
+
nullable?: boolean;
|
|
16
|
+
optional?: boolean;
|
|
17
|
+
};
|
|
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
|
+
export type RelationHasOneField = readonly ["relation.hasOne", Schema, RelationHasOneOptions];
|
|
8
20
|
export declare const relation: {
|
|
9
21
|
hasMany: <S = any, O extends RelationHasManyOptions = {}>(shape: S, options?: O) => ["relation.hasMany", S, O];
|
|
22
|
+
hasOne: <S = any, O extends RelationHasOneOptions = {}>(shape: S, options?: O) => ["relation.hasOne", S, O];
|
|
10
23
|
};
|
package/dist/fields/relation.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
const hasMany = (shape, options = {}) => {
|
|
2
2
|
return ["relation.hasMany", shape, options];
|
|
3
3
|
};
|
|
4
|
-
|
|
4
|
+
const hasOne = (shape, options = {}) => {
|
|
5
|
+
return ["relation.hasOne", shape, options];
|
|
6
|
+
};
|
|
7
|
+
export const relation = { hasMany, hasOne };
|
|
@@ -18,8 +18,23 @@ export declare const paragraphChild: z.ZodType<ParagraphChild>;
|
|
|
18
18
|
export declare const zodRichTextBlocksSchema: z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
19
19
|
type: z.ZodLiteral<"paragraph">;
|
|
20
20
|
children: z.ZodArray<z.ZodType<ParagraphChild, unknown, z.core.$ZodTypeInternals<ParagraphChild, unknown>>>;
|
|
21
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
22
|
+
type: z.ZodLiteral<"heading">;
|
|
23
|
+
level: z.ZodNumber;
|
|
24
|
+
children: z.ZodArray<z.ZodType<ParagraphChild, unknown, z.core.$ZodTypeInternals<ParagraphChild, unknown>>>;
|
|
25
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
26
|
+
type: z.ZodLiteral<"list">;
|
|
27
|
+
format: z.ZodEnum<{
|
|
28
|
+
ordered: "ordered";
|
|
29
|
+
unordered: "unordered";
|
|
30
|
+
}>;
|
|
31
|
+
children: z.ZodArray<z.ZodObject<{
|
|
32
|
+
type: z.ZodLiteral<"list-item">;
|
|
33
|
+
children: z.ZodArray<z.ZodType<ParagraphChild, unknown, z.core.$ZodTypeInternals<ParagraphChild, unknown>>>;
|
|
34
|
+
}, z.core.$strip>>;
|
|
21
35
|
}, z.core.$strip>]>>;
|
|
22
36
|
type ZodRichTextBlocksType = z.output<typeof zodRichTextBlocksSchema>;
|
|
37
|
+
export type RichTextBlocks = ZodRichTextBlocksType;
|
|
23
38
|
export type RichTextBlocksOptions = {
|
|
24
39
|
required?: boolean;
|
|
25
40
|
};
|
package/dist/fields/richText.js
CHANGED
|
@@ -19,7 +19,21 @@ const paragraphBlock = z.object({
|
|
|
19
19
|
type: z.literal("paragraph"),
|
|
20
20
|
children: z.array(paragraphChild),
|
|
21
21
|
});
|
|
22
|
-
|
|
22
|
+
const headingBlock = z.object({
|
|
23
|
+
type: z.literal("heading"),
|
|
24
|
+
level: z.number(),
|
|
25
|
+
children: z.array(paragraphChild),
|
|
26
|
+
});
|
|
27
|
+
const listItemBlock = z.object({
|
|
28
|
+
type: z.literal("list-item"),
|
|
29
|
+
children: z.array(paragraphChild),
|
|
30
|
+
});
|
|
31
|
+
const listBlock = z.object({
|
|
32
|
+
type: z.literal("list"),
|
|
33
|
+
format: z.enum(["ordered", "unordered"]),
|
|
34
|
+
children: z.array(listItemBlock),
|
|
35
|
+
});
|
|
36
|
+
export const zodRichTextBlocksSchema = z.array(z.union([paragraphBlock, headingBlock, listBlock]));
|
|
23
37
|
const blocks = (options = {}) => {
|
|
24
38
|
return ["richText.blocks", options];
|
|
25
39
|
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
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
|
@@ -1,11 +1,21 @@
|
|
|
1
|
+
import { booleanSchema } from "../fields/boolean";
|
|
1
2
|
import { dynamicSchema } from "../fields/dynamic";
|
|
3
|
+
import { enumerationSchema } from "../fields/enumeration";
|
|
2
4
|
import { mediaSingleSchema } from "../fields/media";
|
|
3
5
|
import { numberSchema } from "../fields/number";
|
|
4
6
|
import { repeatableSchema, singleSchema } from "../fields/component";
|
|
7
|
+
import { richTextBlocksSchema } from "../fields/richText";
|
|
5
8
|
import { textSchema } from "../fields/text";
|
|
6
9
|
import z from "zod";
|
|
7
|
-
import {
|
|
8
|
-
|
|
10
|
+
import { jsonSchema } from "../fields/json";
|
|
11
|
+
export const defaultStrapiFields = {
|
|
12
|
+
id: z.number(),
|
|
13
|
+
documentId: z.string().optional(),
|
|
14
|
+
createdAt: z.iso.datetime().optional(),
|
|
15
|
+
updatedAt: z.iso.datetime().optional(),
|
|
16
|
+
publishedAt: z.iso.datetime().nullable().optional(),
|
|
17
|
+
};
|
|
18
|
+
export const defaultStrapiFieldsSchema = z.object(defaultStrapiFields);
|
|
9
19
|
export const schemaToParser = (schema) => {
|
|
10
20
|
const shape = {};
|
|
11
21
|
for (const [key, field] of Object.entries(schema)) {
|
|
@@ -20,6 +30,16 @@ export const schemaToParser = (schema) => {
|
|
|
20
30
|
shape[key] = numberSchema(args);
|
|
21
31
|
break;
|
|
22
32
|
}
|
|
33
|
+
case "json": {
|
|
34
|
+
const [, args] = field;
|
|
35
|
+
shape[key] = jsonSchema(args);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case "boolean": {
|
|
39
|
+
const [, args] = field;
|
|
40
|
+
shape[key] = booleanSchema(args);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
23
43
|
case "dynamic": {
|
|
24
44
|
const [, ...args] = field;
|
|
25
45
|
shape[key] = dynamicSchema(...args);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simple-strapi",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.21",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"node-fetch": "^3.3.2",
|
|
45
45
|
"qs": "^6.14.0",
|
|
46
|
-
"simple-exception": "
|
|
46
|
+
"simple-exception": "github:hund-studio/simple-exception",
|
|
47
47
|
"zod": "^4.0.5"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|