simple-strapi 1.0.0-alpha.27 → 1.0.0-alpha.28

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/README.md CHANGED
@@ -279,7 +279,7 @@ const media = await client.upload(file, options?)
279
279
  ```ts
280
280
  // Upload a File from a browser input
281
281
  const [file] = inputEl.files!;
282
- const [uploaded] = await client.upload(file, { path: "products/2024" });
282
+ const [uploaded] = await client.upload(file);
283
283
  console.log(uploaded.url);
284
284
 
285
285
  // Upload a base64 data URI and attach it to an entity
@@ -454,6 +454,27 @@ TypeScript types: `MediaSingleField`, `MediaSingleOptions`, `InferMediaSingle<O>
454
454
 
455
455
  ---
456
456
 
457
+ ### `media.multiple(options?)`
458
+
459
+ A multiple Strapi media upload field (array). Automatically adds the correct `populate` entry.
460
+
461
+ ```ts
462
+ import { media } from "simple-strapi";
463
+
464
+ media.multiple(); // MediaType[] | null | undefined
465
+ media.multiple({ required: true }); // MediaType[]
466
+ ```
467
+
468
+ The resolved `MediaType` shape is the same as [`media.single()`](#mediasingleoptions).
469
+
470
+ | Option | Type | Default | Description |
471
+ | ---------- | --------- | ------- | -------------------------------------------------------------------------------- |
472
+ | `required` | `boolean` | `false` | If true, type is `MediaType[]` instead of `MediaType[] \| null \| undefined` |
473
+
474
+ TypeScript types: `MediaMultipleField`, `MediaMultipleOptions`, `InferMediaMultiple<O>`
475
+
476
+ ---
477
+
457
478
  ### `richText.blocks(options?)`
458
479
 
459
480
  A Strapi rich text blocks field (Strapi v5 block editor format).
@@ -474,6 +495,11 @@ type RichTextBlocks = Array<
474
495
  | { type: "list"; format: "ordered" | "unordered"; children: ListItemBlock[] }
475
496
  >;
476
497
 
498
+ type ListItemBlock = {
499
+ type: "list-item";
500
+ children: ParagraphChild[];
501
+ };
502
+
477
503
  type ParagraphChild =
478
504
  | {
479
505
  type: "text";
package/dist/client.js CHANGED
@@ -1,7 +1,20 @@
1
1
  import { createSimpleException, ensureSimpleException } from "simple-exception";
2
2
  import { join } from "path";
3
3
  import fetch from "node-fetch";
4
+ import http from "http";
5
+ import https from "https";
4
6
  import qs from "qs";
7
+ const httpAgent = new http.Agent({ keepAlive: true });
8
+ const httpsAgent = new https.Agent({ keepAlive: true });
9
+ function agentFor(url) {
10
+ return url.protocol === "https:" ? httpsAgent : httpAgent;
11
+ }
12
+ async function safeResponseJson(response) {
13
+ const text = await response.text();
14
+ if (!text)
15
+ return null;
16
+ return JSON.parse(text);
17
+ }
5
18
  import z from "zod";
6
19
  import { defaultStrapiFields, schemaToParser } from "./utils/schema";
7
20
  import { zodMediaSchema, } from "./fields/media";
@@ -37,6 +50,7 @@ class Client {
37
50
  method: "POST",
38
51
  headers: this.headers,
39
52
  body: JSON.stringify({ identifier: auth.email, password: auth.password }),
53
+ agent: agentFor(requestURL),
40
54
  });
41
55
  if (!response.ok) {
42
56
  throw createSimpleException({
@@ -46,7 +60,7 @@ class Client {
46
60
  source: "strapi-utils/client.ts",
47
61
  });
48
62
  }
49
- const data = await response.json();
63
+ const data = await safeResponseJson(response);
50
64
  const { token } = z.object({ token: z.string() }).parse(data);
51
65
  return token;
52
66
  }
@@ -167,6 +181,7 @@ class Client {
167
181
  ...this.getAuthorizedHeaders(),
168
182
  ...headers,
169
183
  },
184
+ agent: agentFor(requestURL),
170
185
  });
171
186
  if (!response.ok) {
172
187
  throw createSimpleException({
@@ -178,7 +193,7 @@ class Client {
178
193
  }
179
194
  const { data, meta } = z
180
195
  .object({ data: z.any(), meta: z.any() })
181
- .parse(await response.json());
196
+ .parse(await safeResponseJson(response));
182
197
  if (!data)
183
198
  throw createSimpleException({ code: 404, type: "error", message: "Not found" });
184
199
  if ("schema" in options) {
@@ -234,6 +249,7 @@ class Client {
234
249
  ...this.getAuthorizedHeaders(),
235
250
  ...headers,
236
251
  },
252
+ agent: agentFor(requestURL),
237
253
  });
238
254
  if (!response.ok) {
239
255
  throw createSimpleException({
@@ -243,7 +259,7 @@ class Client {
243
259
  source: "strapi-utils/client.ts",
244
260
  });
245
261
  }
246
- const responseData = await response.json();
262
+ const responseData = await safeResponseJson(response);
247
263
  const { data, meta } = z
248
264
  // .object({ data: z.array(z.any()).catch([]), meta: z.any() })
249
265
  .object({ data: z.any(), meta: z.any() })
@@ -310,6 +326,7 @@ class Client {
310
326
  ...headers,
311
327
  },
312
328
  body: JSON.stringify({ data: payload }),
329
+ agent: agentFor(requestURL),
313
330
  });
314
331
  if (!response.ok) {
315
332
  const errorBody = await response.json().catch(() => ({}));
@@ -328,7 +345,7 @@ class Client {
328
345
  }
329
346
  const { data, meta } = z
330
347
  .object({ data: z.any(), meta: z.any() })
331
- .parse(await response.json());
348
+ .parse(await safeResponseJson(response));
332
349
  if ("schema" in options && options.schema) {
333
350
  const shape = options.schema;
334
351
  const schema = z.object(schemaToParser(shape)).extend(defaultStrapiFields).loose();
@@ -363,6 +380,7 @@ class Client {
363
380
  ...this.getAuthorizedHeaders(),
364
381
  ...headers,
365
382
  },
383
+ agent: agentFor(requestURL),
366
384
  });
367
385
  if (!response.ok) {
368
386
  const errorBody = await response.json().catch(() => ({}));
@@ -378,7 +396,7 @@ class Client {
378
396
  }
379
397
  const { data, meta } = z
380
398
  .object({ data: z.any(), meta: z.any() })
381
- .parse(await response.json());
399
+ .parse(await safeResponseJson(response));
382
400
  return { data, meta };
383
401
  }
384
402
  catch (exception) {
@@ -457,6 +475,7 @@ class Client {
457
475
  ...headers,
458
476
  },
459
477
  body: formData,
478
+ agent: agentFor(requestURL),
460
479
  });
461
480
  if (!response.ok) {
462
481
  const errorBody = await response.json().catch(() => ({}));
@@ -467,7 +486,7 @@ class Client {
467
486
  source: "strapi-utils/client.ts",
468
487
  });
469
488
  }
470
- const data = await response.json();
489
+ const data = await safeResponseJson(response);
471
490
  return z.array(zodMediaSchema).parse(data);
472
491
  }
473
492
  catch (exception) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simple-strapi",
3
- "version": "1.0.0-alpha.27",
3
+ "version": "1.0.0-alpha.28",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",