@strapi2front/generators 0.3.1 → 0.4.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/index.d.ts CHANGED
@@ -136,6 +136,11 @@ interface ByFeatureGeneratorOptions {
136
136
  * @default 'typescript'
137
137
  */
138
138
  outputFormat?: "typescript" | "jsdoc";
139
+ /**
140
+ * Module type for JSDoc output: 'esm' for ES Modules, 'commonjs' for CommonJS
141
+ * @default 'commonjs'
142
+ */
143
+ moduleType?: "esm" | "commonjs";
139
144
  }
140
145
  /**
141
146
  * Generate all files using 'by-feature' structure
package/dist/index.js CHANGED
@@ -2135,9 +2135,11 @@ async function generateByFeature(schema, locales, options) {
2135
2135
  blocksRendererInstalled = false,
2136
2136
  strapiVersion = "v5",
2137
2137
  apiPrefix = "/api",
2138
- outputFormat = "typescript"
2138
+ outputFormat = "typescript",
2139
+ moduleType = "commonjs"
2139
2140
  } = options;
2140
2141
  const generatedFiles = [];
2142
+ const useESM = outputFormat === "jsdoc" && moduleType === "esm";
2141
2143
  const ext = outputFormat === "jsdoc" ? "js" : "ts";
2142
2144
  await ensureDir(path9.join(outputDir, "collections"));
2143
2145
  await ensureDir(path9.join(outputDir, "singles"));
@@ -2145,15 +2147,15 @@ async function generateByFeature(schema, locales, options) {
2145
2147
  await ensureDir(path9.join(outputDir, "shared"));
2146
2148
  const sharedDir = path9.join(outputDir, "shared");
2147
2149
  const utilsPath = path9.join(sharedDir, `utils.${ext}`);
2148
- const utilsContent = outputFormat === "jsdoc" ? generateUtilityTypesJSDoc(blocksRendererInstalled, strapiVersion) : generateUtilityTypes3(blocksRendererInstalled, strapiVersion);
2150
+ const utilsContent = outputFormat === "jsdoc" ? generateUtilityTypesJSDoc(blocksRendererInstalled, strapiVersion, useESM) : generateUtilityTypes3(blocksRendererInstalled, strapiVersion);
2149
2151
  await writeFile(utilsPath, await formatCode(utilsContent));
2150
2152
  generatedFiles.push(utilsPath);
2151
2153
  const clientPath = path9.join(sharedDir, `client.${ext}`);
2152
- const clientContent = outputFormat === "jsdoc" ? generateClientJSDoc(strapiVersion, apiPrefix) : generateClient2(strapiVersion, apiPrefix);
2154
+ const clientContent = outputFormat === "jsdoc" ? generateClientJSDoc(strapiVersion, apiPrefix, useESM) : generateClient2(strapiVersion, apiPrefix);
2153
2155
  await writeFile(clientPath, await formatCode(clientContent));
2154
2156
  generatedFiles.push(clientPath);
2155
2157
  const localesPath = path9.join(sharedDir, `locales.${ext}`);
2156
- const localesContent = outputFormat === "jsdoc" ? generateLocalesFileJSDoc(locales) : generateLocalesFile2(locales);
2158
+ const localesContent = outputFormat === "jsdoc" ? generateLocalesFileJSDoc(locales, useESM) : generateLocalesFile2(locales);
2157
2159
  await writeFile(localesPath, await formatCode(localesContent));
2158
2160
  generatedFiles.push(localesPath);
2159
2161
  for (const collection of schema.collections) {
@@ -2161,13 +2163,13 @@ async function generateByFeature(schema, locales, options) {
2161
2163
  await ensureDir(featureDir);
2162
2164
  if (features.types) {
2163
2165
  const typesPath = path9.join(featureDir, `types.${ext}`);
2164
- const content = outputFormat === "jsdoc" ? generateCollectionTypesJSDoc(collection, schema) : generateCollectionTypes(collection, schema);
2166
+ const content = outputFormat === "jsdoc" ? generateCollectionTypesJSDoc(collection, schema, useESM) : generateCollectionTypes(collection, schema);
2165
2167
  await writeFile(typesPath, await formatCode(content));
2166
2168
  generatedFiles.push(typesPath);
2167
2169
  }
2168
2170
  if (features.services) {
2169
2171
  const servicePath = path9.join(featureDir, `service.${ext}`);
2170
- const content = outputFormat === "jsdoc" ? generateCollectionServiceJSDoc(collection, strapiVersion) : generateCollectionService3(collection, strapiVersion);
2172
+ const content = outputFormat === "jsdoc" ? generateCollectionServiceJSDoc(collection, strapiVersion, useESM) : generateCollectionService3(collection, strapiVersion);
2171
2173
  await writeFile(servicePath, await formatCode(content));
2172
2174
  generatedFiles.push(servicePath);
2173
2175
  }
@@ -2182,20 +2184,20 @@ async function generateByFeature(schema, locales, options) {
2182
2184
  await ensureDir(featureDir);
2183
2185
  if (features.types) {
2184
2186
  const typesPath = path9.join(featureDir, `types.${ext}`);
2185
- const content = outputFormat === "jsdoc" ? generateSingleTypesJSDoc(single, schema) : generateSingleTypes(single, schema);
2187
+ const content = outputFormat === "jsdoc" ? generateSingleTypesJSDoc(single, schema, useESM) : generateSingleTypes(single, schema);
2186
2188
  await writeFile(typesPath, await formatCode(content));
2187
2189
  generatedFiles.push(typesPath);
2188
2190
  }
2189
2191
  if (features.services) {
2190
2192
  const servicePath = path9.join(featureDir, `service.${ext}`);
2191
- const content = outputFormat === "jsdoc" ? generateSingleServiceJSDoc(single, strapiVersion) : generateSingleService3(single, strapiVersion);
2193
+ const content = outputFormat === "jsdoc" ? generateSingleServiceJSDoc(single, strapiVersion, useESM) : generateSingleService3(single, strapiVersion);
2192
2194
  await writeFile(servicePath, await formatCode(content));
2193
2195
  generatedFiles.push(servicePath);
2194
2196
  }
2195
2197
  }
2196
2198
  for (const component of schema.components) {
2197
2199
  const componentPath = path9.join(outputDir, "components", `${toKebabCase(component.name)}.${ext}`);
2198
- const content = outputFormat === "jsdoc" ? generateComponentTypesJSDoc(component, schema) : generateComponentTypes(component, schema);
2200
+ const content = outputFormat === "jsdoc" ? generateComponentTypesJSDoc(component, schema, useESM) : generateComponentTypes(component, schema);
2199
2201
  await writeFile(componentPath, await formatCode(content));
2200
2202
  generatedFiles.push(componentPath);
2201
2203
  }
@@ -3166,7 +3168,7 @@ export const ${actionPrefix}Actions = {
3166
3168
  };
3167
3169
  `;
3168
3170
  }
3169
- function generateUtilityTypesJSDoc(blocksRendererInstalled, strapiVersion) {
3171
+ function generateUtilityTypesJSDoc(blocksRendererInstalled, strapiVersion, useESM = false) {
3170
3172
  const isV4 = strapiVersion === "v4";
3171
3173
  const blocksContentType = isV4 ? `/**
3172
3174
  * Rich text content (Strapi v4)
@@ -3275,7 +3277,9 @@ function generateUtilityTypesJSDoc(blocksRendererInstalled, strapiVersion) {
3275
3277
  * @returns {T}
3276
3278
  */
3277
3279
  function flattenV4Response(item) {
3278
- return { id: item.id, ...item.attributes };
3280
+ /** @type {any} */
3281
+ const merged = { id: item.id, ...item.attributes };
3282
+ return merged;
3279
3283
  }
3280
3284
 
3281
3285
  /**
@@ -3288,7 +3292,7 @@ function flattenV4ListResponse(items) {
3288
3292
  return items.map(item => flattenV4Response(item));
3289
3293
  }
3290
3294
 
3291
- module.exports = { flattenV4Response, flattenV4ListResponse };` : "";
3295
+ ${useESM ? "export { flattenV4Response, flattenV4ListResponse };" : "module.exports = { flattenV4Response, flattenV4ListResponse };"}` : "";
3292
3296
  return `// @ts-check
3293
3297
  /**
3294
3298
  * Strapi utility types
@@ -3338,12 +3342,13 @@ ${baseEntity}
3338
3342
  ${v4RawResponseTypes}
3339
3343
  ${blocksContentType}
3340
3344
 
3341
- module.exports = {};
3345
+ ${useESM ? "export {};" : "module.exports = {};"}
3342
3346
  `;
3343
3347
  }
3344
- function generateClientJSDoc(strapiVersion, apiPrefix = "/api") {
3348
+ function generateClientJSDoc(strapiVersion, apiPrefix = "/api", useESM = false) {
3345
3349
  const isV4 = strapiVersion === "v4";
3346
3350
  const normalizedPrefix = apiPrefix.startsWith("/") ? apiPrefix : "/" + apiPrefix;
3351
+ const importStatement = useESM ? `import Strapi from 'strapi-sdk-js';` : `const Strapi = require('strapi-sdk-js').default;`;
3347
3352
  if (isV4) {
3348
3353
  return `// @ts-check
3349
3354
  /**
@@ -3351,7 +3356,7 @@ function generateClientJSDoc(strapiVersion, apiPrefix = "/api") {
3351
3356
  * Generated by strapi2front
3352
3357
  */
3353
3358
 
3354
- const Strapi = require('strapi-sdk-js').default;
3359
+ ${importStatement}
3355
3360
 
3356
3361
  // Initialize the Strapi client
3357
3362
  const strapiUrl = process.env.STRAPI_URL || 'http://localhost:1337';
@@ -3551,7 +3556,7 @@ function single(singularName) {
3551
3556
  };
3552
3557
  }
3553
3558
 
3554
- module.exports = { strapi, collection, single };
3559
+ ${useESM ? "export { strapi, collection, single };" : "module.exports = { strapi, collection, single };"}
3555
3560
  `;
3556
3561
  }
3557
3562
  return `// @ts-check
@@ -3560,7 +3565,7 @@ module.exports = { strapi, collection, single };
3560
3565
  * Generated by strapi2front
3561
3566
  */
3562
3567
 
3563
- const Strapi = require('strapi-sdk-js').default;
3568
+ ${importStatement}
3564
3569
 
3565
3570
  // Initialize the Strapi client
3566
3571
  const strapiUrl = process.env.STRAPI_URL || 'http://localhost:1337';
@@ -3703,10 +3708,10 @@ function single(singularName) {
3703
3708
  };
3704
3709
  }
3705
3710
 
3706
- module.exports = { strapi, collection, single };
3711
+ ${useESM ? "export { strapi, collection, single };" : "module.exports = { strapi, collection, single };"}
3707
3712
  `;
3708
3713
  }
3709
- function generateLocalesFileJSDoc(locales) {
3714
+ function generateLocalesFileJSDoc(locales, useESM = false) {
3710
3715
  if (locales.length === 0) {
3711
3716
  return `// @ts-check
3712
3717
  /**
@@ -3742,7 +3747,7 @@ function getLocaleName(code) {
3742
3747
  return code;
3743
3748
  }
3744
3749
 
3745
- module.exports = { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };
3750
+ ${useESM ? "export { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };" : "module.exports = { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };"}
3746
3751
  `;
3747
3752
  }
3748
3753
  const localeCodes = locales.map((l) => l.code);
@@ -3784,10 +3789,10 @@ function getLocaleName(code) {
3784
3789
  return localeNames[code] || code;
3785
3790
  }
3786
3791
 
3787
- module.exports = { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };
3792
+ ${useESM ? "export { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };" : "module.exports = { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };"}
3788
3793
  `;
3789
3794
  }
3790
- function generateCollectionTypesJSDoc(collection, schema) {
3795
+ function generateCollectionTypesJSDoc(collection, schema, useESM = false) {
3791
3796
  const typeName = toPascalCase(collection.singularName);
3792
3797
  const attributes = generateJSDocAttributes(collection.attributes, schema, "collection");
3793
3798
  return `// @ts-check
@@ -3818,10 +3823,10 @@ ${attributes}
3818
3823
  * @property {${typeName}Filters} [$not]
3819
3824
  */
3820
3825
 
3821
- module.exports = {};
3826
+ ${useESM ? "export {};" : "module.exports = {};"}
3822
3827
  `;
3823
3828
  }
3824
- function generateSingleTypesJSDoc(single, schema) {
3829
+ function generateSingleTypesJSDoc(single, schema, useESM = false) {
3825
3830
  const typeName = toPascalCase(single.singularName);
3826
3831
  const attributes = generateJSDocAttributes(single.attributes, schema, "single");
3827
3832
  return `// @ts-check
@@ -3840,10 +3845,10 @@ function generateSingleTypesJSDoc(single, schema) {
3840
3845
  ${attributes}
3841
3846
  */
3842
3847
 
3843
- module.exports = {};
3848
+ ${useESM ? "export {};" : "module.exports = {};"}
3844
3849
  `;
3845
3850
  }
3846
- function generateComponentTypesJSDoc(component, schema) {
3851
+ function generateComponentTypesJSDoc(component, schema, useESM = false) {
3847
3852
  const typeName = toPascalCase(component.name);
3848
3853
  const attributes = generateJSDocAttributes(component.attributes, schema, "component");
3849
3854
  return `// @ts-check
@@ -3860,7 +3865,7 @@ function generateComponentTypesJSDoc(component, schema) {
3860
3865
  ${attributes}
3861
3866
  */
3862
3867
 
3863
- module.exports = {};
3868
+ ${useESM ? "export {};" : "module.exports = {};"}
3864
3869
  `;
3865
3870
  }
3866
3871
  function generateJSDocAttributes(attributes, schema, context) {
@@ -3958,7 +3963,7 @@ function attributeToJSDocType(attr, schema, relativePrefix) {
3958
3963
  return "Object";
3959
3964
  }
3960
3965
  }
3961
- function generateCollectionServiceJSDoc(collection, strapiVersion) {
3966
+ function generateCollectionServiceJSDoc(collection, strapiVersion, useESM = false) {
3962
3967
  const typeName = toPascalCase(collection.singularName);
3963
3968
  const serviceName = toCamelCase(collection.singularName) + "Service";
3964
3969
  const endpoint = collection.pluralName;
@@ -3967,6 +3972,7 @@ function generateCollectionServiceJSDoc(collection, strapiVersion) {
3967
3972
  const isV4 = strapiVersion === "v4";
3968
3973
  const idParam = isV4 ? "id" : "documentId";
3969
3974
  const idType = isV4 ? "number" : "string";
3975
+ const importStatement = useESM ? `import { collection } from '../../shared/client.js';` : `const { collection } = require('../../shared/client');`;
3970
3976
  return `// @ts-check
3971
3977
  /**
3972
3978
  * ${collection.displayName} Service
@@ -3975,7 +3981,7 @@ function generateCollectionServiceJSDoc(collection, strapiVersion) {
3975
3981
  * Strapi version: ${strapiVersion}
3976
3982
  */
3977
3983
 
3978
- const { collection } = require('../../shared/client');
3984
+ ${importStatement}
3979
3985
 
3980
3986
  /**
3981
3987
  * @typedef {Object} FindManyOptions
@@ -4116,14 +4122,15 @@ ${hasSlug ? `
4116
4122
  },
4117
4123
  };
4118
4124
 
4119
- module.exports = { ${serviceName} };
4125
+ ${useESM ? `export { ${serviceName} };` : `module.exports = { ${serviceName} };`}
4120
4126
  `;
4121
4127
  }
4122
- function generateSingleServiceJSDoc(single, strapiVersion) {
4128
+ function generateSingleServiceJSDoc(single, strapiVersion, useESM = false) {
4123
4129
  const typeName = toPascalCase(single.singularName);
4124
4130
  const serviceName = toCamelCase(single.singularName) + "Service";
4125
4131
  const endpoint = single.singularName;
4126
4132
  const { localized, draftAndPublish } = single;
4133
+ const importStatement = useESM ? `import { single } from '../../shared/client.js';` : `const { single } = require('../../shared/client');`;
4127
4134
  return `// @ts-check
4128
4135
  /**
4129
4136
  * ${single.displayName} Service (Single Type)
@@ -4132,7 +4139,7 @@ function generateSingleServiceJSDoc(single, strapiVersion) {
4132
4139
  * Strapi version: ${strapiVersion}
4133
4140
  */
4134
4141
 
4135
- const { single } = require('../../shared/client');
4142
+ ${importStatement}
4136
4143
 
4137
4144
  /**
4138
4145
  * @typedef {Object} FindOptions
@@ -4179,7 +4186,7 @@ const ${serviceName} = {
4179
4186
  },
4180
4187
  };
4181
4188
 
4182
- module.exports = { ${serviceName} };
4189
+ ${useESM ? `export { ${serviceName} };` : `module.exports = { ${serviceName} };`}
4183
4190
  `;
4184
4191
  }
4185
4192