@malevich-studio/strapi-sdk-typescript 1.0.7 → 1.0.9

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
@@ -0,0 +1,125 @@
1
+ # Strapi SDK for TypeScript
2
+
3
+ A TypeScript SDK for interacting with Strapi APIs.
4
+
5
+ ## 🚀 Installation
6
+
7
+ To install the SDK, run:
8
+
9
+ ```sh
10
+ npm install @malevich-studio/strapi-sdk-typescript
11
+ ```
12
+
13
+ ## 🛠 Configuration
14
+
15
+ Create a `.env` file with your Strapi base URL and API token:
16
+
17
+ ```sh
18
+ STRAPI_URL=http://localhost:1337
19
+ STRAPI_TOKEN=<your_strapi_token>
20
+ ```
21
+
22
+ ### Generating API Token
23
+
24
+ To interact with the Strapi API, you need to create an API token with at least `Content-Type Builder` permissions.
25
+ Navigate to:
26
+
27
+ ```
28
+ <your_strapi_base_url>/admin/settings/api-tokens/create
29
+ ```
30
+
31
+ ### Generating API Class
32
+
33
+ Run the following command to generate TypeScript types based on your Strapi schema:
34
+
35
+ ```sh
36
+ npx generate-strapi-types
37
+ ```
38
+
39
+ ## 📌 Usage
40
+
41
+ ### Basic Example
42
+
43
+ Create `strapi.ts` to initialize the API class:
44
+
45
+ ```ts
46
+ import Strapi from "./strapi"; // strapi.ts file
47
+
48
+ const api = new Strapi(process.env.STRAPI_URL || '', process.env.STRAPI_TOKEN || '');
49
+
50
+ const articles = api.articles({
51
+ fields: ["documentId", "title", "text"],
52
+ populate: {
53
+ seo: {
54
+ fields: ["slug", "metaTitle", "metaDescription"],
55
+ populate: {
56
+ openGraph: {
57
+ fields: ["title", "description", "url", "type"],
58
+ populate: {
59
+ image: {
60
+ fields: ["url", "width", "height"]
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }
66
+ }
67
+ });
68
+ ```
69
+
70
+ ### Using in Next.js with Caching
71
+
72
+ If using Next.js, you can integrate caching for better performance:
73
+
74
+ ```ts
75
+ import Strapi from "@/strapi"; // strapi.ts file
76
+
77
+ const api = new Strapi(process.env.STRAPI_URL || '', process.env.STRAPI_TOKEN || '');
78
+
79
+ const articles = api.articles(
80
+ {
81
+ fields: ["documentId", "title", "text"],
82
+ populate: {
83
+ seo: {
84
+ fields: ["slug", "metaTitle", "metaDescription"],
85
+ populate: {
86
+ openGraph: {
87
+ fields: ["title", "description", "url", "type"],
88
+ populate: {
89
+ image: {
90
+ fields: ["url", "width", "height"]
91
+ }
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
97
+ },
98
+ // Cache Options
99
+ {
100
+ cache: "force-cache",
101
+ next: {
102
+ revalidate: 24 * 3600, // Revalidate every 24 hours
103
+ tags: ["contact", "regions"]
104
+ }
105
+ }
106
+ );
107
+ ```
108
+
109
+ ## 📌 TODO List
110
+
111
+ - [ ] Add authentication features:
112
+ - [ ] Log In functionality
113
+ - [ ] User Registration
114
+ - [ ] User privileges check
115
+ - [ ] Refactor `src/generator/index.ts` for better maintainability
116
+ - [ ] Enable passing Strapi credentials via CLI parameters
117
+ - [ ] Allow customization of API class path
118
+ - [ ] Resolve naming conflicts between Components and Content Types
119
+ - [ ] Support custom attributes in `src/generator/attributes/index.ts:15`:
120
+ - [ ] Define attributes by project code
121
+ - [ ] Auto-load attributes from other npm packages by scanning `node_modules`
122
+
123
+ ---
124
+
125
+ 📌 **Contributions are welcome!** If you encounter issues or have feature requests, feel free to open a pull request or an issue. 🚀
package/dist/cli.cjs CHANGED
@@ -21307,7 +21307,7 @@ class Strapi {
21307
21307
  return (await this.baseRequest('upload', {
21308
21308
  method: 'POST',
21309
21309
  body: form,
21310
- })).data;
21310
+ }));
21311
21311
  }
21312
21312
  async baseRequest(endpoint, params = {}) {
21313
21313
  const response = await fetch(`${this.url}/api/${endpoint}`, _.merge({
@@ -21317,13 +21317,10 @@ class Strapi {
21317
21317
  }, params));
21318
21318
  if (!response.ok) {
21319
21319
  console.log(`${this.url}/api/${endpoint}`);
21320
- console.log(_.merge({
21321
- headers: {
21322
- Authorization: `Bearer ${this.token}`,
21323
- },
21324
- }, params));
21325
21320
  console.log(response);
21326
- console.log(await response.json());
21321
+ const data = await response.json();
21322
+ console.log(data);
21323
+ console.log(data?.error?.details?.errors);
21327
21324
  throw new Error(`Помилка запиту до Strapi: ${response.status} ${response.statusText}`);
21328
21325
  }
21329
21326
  return (await response.json());
@@ -21632,6 +21629,17 @@ let Boolean$1 = class Boolean extends Base {
21632
21629
  }
21633
21630
  };
21634
21631
 
21632
+ let Date$1 = class Date extends Base {
21633
+ constructor(name, attribute) {
21634
+ super(name, attribute);
21635
+ this.name = name;
21636
+ this.attribute = attribute;
21637
+ }
21638
+ getType() {
21639
+ return 'string';
21640
+ }
21641
+ };
21642
+
21635
21643
  const types = {
21636
21644
  'string': String$1,
21637
21645
  'text': String$1,
@@ -21645,6 +21653,7 @@ const types = {
21645
21653
  'media': Media,
21646
21654
  'relation': Relation,
21647
21655
  'enumeration': Enumeration,
21656
+ 'date': Date$1,
21648
21657
  'datetime': DateTime,
21649
21658
  'component': Component,
21650
21659
  'blocks': Blocks,