@strapi2front/generators 0.3.2 → 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)
@@ -3290,7 +3292,7 @@ function flattenV4ListResponse(items) {
3290
3292
  return items.map(item => flattenV4Response(item));
3291
3293
  }
3292
3294
 
3293
- module.exports = { flattenV4Response, flattenV4ListResponse };` : "";
3295
+ ${useESM ? "export { flattenV4Response, flattenV4ListResponse };" : "module.exports = { flattenV4Response, flattenV4ListResponse };"}` : "";
3294
3296
  return `// @ts-check
3295
3297
  /**
3296
3298
  * Strapi utility types
@@ -3340,12 +3342,13 @@ ${baseEntity}
3340
3342
  ${v4RawResponseTypes}
3341
3343
  ${blocksContentType}
3342
3344
 
3343
- module.exports = {};
3345
+ ${useESM ? "export {};" : "module.exports = {};"}
3344
3346
  `;
3345
3347
  }
3346
- function generateClientJSDoc(strapiVersion, apiPrefix = "/api") {
3348
+ function generateClientJSDoc(strapiVersion, apiPrefix = "/api", useESM = false) {
3347
3349
  const isV4 = strapiVersion === "v4";
3348
3350
  const normalizedPrefix = apiPrefix.startsWith("/") ? apiPrefix : "/" + apiPrefix;
3351
+ const importStatement = useESM ? `import Strapi from 'strapi-sdk-js';` : `const Strapi = require('strapi-sdk-js').default;`;
3349
3352
  if (isV4) {
3350
3353
  return `// @ts-check
3351
3354
  /**
@@ -3353,7 +3356,7 @@ function generateClientJSDoc(strapiVersion, apiPrefix = "/api") {
3353
3356
  * Generated by strapi2front
3354
3357
  */
3355
3358
 
3356
- const Strapi = require('strapi-sdk-js').default;
3359
+ ${importStatement}
3357
3360
 
3358
3361
  // Initialize the Strapi client
3359
3362
  const strapiUrl = process.env.STRAPI_URL || 'http://localhost:1337';
@@ -3553,7 +3556,7 @@ function single(singularName) {
3553
3556
  };
3554
3557
  }
3555
3558
 
3556
- module.exports = { strapi, collection, single };
3559
+ ${useESM ? "export { strapi, collection, single };" : "module.exports = { strapi, collection, single };"}
3557
3560
  `;
3558
3561
  }
3559
3562
  return `// @ts-check
@@ -3562,7 +3565,7 @@ module.exports = { strapi, collection, single };
3562
3565
  * Generated by strapi2front
3563
3566
  */
3564
3567
 
3565
- const Strapi = require('strapi-sdk-js').default;
3568
+ ${importStatement}
3566
3569
 
3567
3570
  // Initialize the Strapi client
3568
3571
  const strapiUrl = process.env.STRAPI_URL || 'http://localhost:1337';
@@ -3705,10 +3708,10 @@ function single(singularName) {
3705
3708
  };
3706
3709
  }
3707
3710
 
3708
- module.exports = { strapi, collection, single };
3711
+ ${useESM ? "export { strapi, collection, single };" : "module.exports = { strapi, collection, single };"}
3709
3712
  `;
3710
3713
  }
3711
- function generateLocalesFileJSDoc(locales) {
3714
+ function generateLocalesFileJSDoc(locales, useESM = false) {
3712
3715
  if (locales.length === 0) {
3713
3716
  return `// @ts-check
3714
3717
  /**
@@ -3744,7 +3747,7 @@ function getLocaleName(code) {
3744
3747
  return code;
3745
3748
  }
3746
3749
 
3747
- module.exports = { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };
3750
+ ${useESM ? "export { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };" : "module.exports = { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };"}
3748
3751
  `;
3749
3752
  }
3750
3753
  const localeCodes = locales.map((l) => l.code);
@@ -3786,10 +3789,10 @@ function getLocaleName(code) {
3786
3789
  return localeNames[code] || code;
3787
3790
  }
3788
3791
 
3789
- module.exports = { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };
3792
+ ${useESM ? "export { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };" : "module.exports = { locales, defaultLocale, localeNames, isValidLocale, getLocaleName };"}
3790
3793
  `;
3791
3794
  }
3792
- function generateCollectionTypesJSDoc(collection, schema) {
3795
+ function generateCollectionTypesJSDoc(collection, schema, useESM = false) {
3793
3796
  const typeName = toPascalCase(collection.singularName);
3794
3797
  const attributes = generateJSDocAttributes(collection.attributes, schema, "collection");
3795
3798
  return `// @ts-check
@@ -3820,10 +3823,10 @@ ${attributes}
3820
3823
  * @property {${typeName}Filters} [$not]
3821
3824
  */
3822
3825
 
3823
- module.exports = {};
3826
+ ${useESM ? "export {};" : "module.exports = {};"}
3824
3827
  `;
3825
3828
  }
3826
- function generateSingleTypesJSDoc(single, schema) {
3829
+ function generateSingleTypesJSDoc(single, schema, useESM = false) {
3827
3830
  const typeName = toPascalCase(single.singularName);
3828
3831
  const attributes = generateJSDocAttributes(single.attributes, schema, "single");
3829
3832
  return `// @ts-check
@@ -3842,10 +3845,10 @@ function generateSingleTypesJSDoc(single, schema) {
3842
3845
  ${attributes}
3843
3846
  */
3844
3847
 
3845
- module.exports = {};
3848
+ ${useESM ? "export {};" : "module.exports = {};"}
3846
3849
  `;
3847
3850
  }
3848
- function generateComponentTypesJSDoc(component, schema) {
3851
+ function generateComponentTypesJSDoc(component, schema, useESM = false) {
3849
3852
  const typeName = toPascalCase(component.name);
3850
3853
  const attributes = generateJSDocAttributes(component.attributes, schema, "component");
3851
3854
  return `// @ts-check
@@ -3862,7 +3865,7 @@ function generateComponentTypesJSDoc(component, schema) {
3862
3865
  ${attributes}
3863
3866
  */
3864
3867
 
3865
- module.exports = {};
3868
+ ${useESM ? "export {};" : "module.exports = {};"}
3866
3869
  `;
3867
3870
  }
3868
3871
  function generateJSDocAttributes(attributes, schema, context) {
@@ -3960,7 +3963,7 @@ function attributeToJSDocType(attr, schema, relativePrefix) {
3960
3963
  return "Object";
3961
3964
  }
3962
3965
  }
3963
- function generateCollectionServiceJSDoc(collection, strapiVersion) {
3966
+ function generateCollectionServiceJSDoc(collection, strapiVersion, useESM = false) {
3964
3967
  const typeName = toPascalCase(collection.singularName);
3965
3968
  const serviceName = toCamelCase(collection.singularName) + "Service";
3966
3969
  const endpoint = collection.pluralName;
@@ -3969,6 +3972,7 @@ function generateCollectionServiceJSDoc(collection, strapiVersion) {
3969
3972
  const isV4 = strapiVersion === "v4";
3970
3973
  const idParam = isV4 ? "id" : "documentId";
3971
3974
  const idType = isV4 ? "number" : "string";
3975
+ const importStatement = useESM ? `import { collection } from '../../shared/client.js';` : `const { collection } = require('../../shared/client');`;
3972
3976
  return `// @ts-check
3973
3977
  /**
3974
3978
  * ${collection.displayName} Service
@@ -3977,7 +3981,7 @@ function generateCollectionServiceJSDoc(collection, strapiVersion) {
3977
3981
  * Strapi version: ${strapiVersion}
3978
3982
  */
3979
3983
 
3980
- const { collection } = require('../../shared/client');
3984
+ ${importStatement}
3981
3985
 
3982
3986
  /**
3983
3987
  * @typedef {Object} FindManyOptions
@@ -4118,14 +4122,15 @@ ${hasSlug ? `
4118
4122
  },
4119
4123
  };
4120
4124
 
4121
- module.exports = { ${serviceName} };
4125
+ ${useESM ? `export { ${serviceName} };` : `module.exports = { ${serviceName} };`}
4122
4126
  `;
4123
4127
  }
4124
- function generateSingleServiceJSDoc(single, strapiVersion) {
4128
+ function generateSingleServiceJSDoc(single, strapiVersion, useESM = false) {
4125
4129
  const typeName = toPascalCase(single.singularName);
4126
4130
  const serviceName = toCamelCase(single.singularName) + "Service";
4127
4131
  const endpoint = single.singularName;
4128
4132
  const { localized, draftAndPublish } = single;
4133
+ const importStatement = useESM ? `import { single } from '../../shared/client.js';` : `const { single } = require('../../shared/client');`;
4129
4134
  return `// @ts-check
4130
4135
  /**
4131
4136
  * ${single.displayName} Service (Single Type)
@@ -4134,7 +4139,7 @@ function generateSingleServiceJSDoc(single, strapiVersion) {
4134
4139
  * Strapi version: ${strapiVersion}
4135
4140
  */
4136
4141
 
4137
- const { single } = require('../../shared/client');
4142
+ ${importStatement}
4138
4143
 
4139
4144
  /**
4140
4145
  * @typedef {Object} FindOptions
@@ -4181,7 +4186,7 @@ const ${serviceName} = {
4181
4186
  },
4182
4187
  };
4183
4188
 
4184
- module.exports = { ${serviceName} };
4189
+ ${useESM ? `export { ${serviceName} };` : `module.exports = { ${serviceName} };`}
4185
4190
  `;
4186
4191
  }
4187
4192