@shopware/api-gen 1.1.4 → 1.2.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.
Files changed (3) hide show
  1. package/README.md +25 -7
  2. package/dist/cli.mjs +183 -113
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -107,7 +107,18 @@ Example:
107
107
 
108
108
  ```json
109
109
  {
110
- "patches": "./api-types/storeApiTypes.overrides.json"
110
+ "patches": ["./api-types/storeApiTypes.overrides.json"]
111
+ }
112
+ ```
113
+
114
+ or you could use multiple patches and add your own overrides on top:
115
+
116
+ ```json
117
+ {
118
+ "patches": [
119
+ "https://raw.githubusercontent.com/shopware/frontends/refs/heads/main/packages/api-client/api-types/storeApiSchema.overrides.json",
120
+ "./api-types/myOwnPatches.overrides.json"
121
+ ]
111
122
  }
112
123
  ```
113
124
 
@@ -175,6 +186,11 @@ pnpx @shopware/api-gen generate --apiType=store
175
186
  pnpx @shopware/api-gen generate --apiType=admin
176
187
  ```
177
188
 
189
+ flags:
190
+
191
+ - `--debug` - display debug logs and additional information which can be helpful in case of issues
192
+ - `--logPatches` - display patched logs, useful when you want to fix schema in original file
193
+
178
194
  ### `loadSchema`
179
195
 
180
196
  Load OpenAPI specification from Shopware instance and save it to JSON file.
@@ -191,6 +207,11 @@ pnpx @shopware/api-gen loadSchema --apiType=store
191
207
  pnpx @shopware/api-gen loadSchema --apiType=admin
192
208
  ```
193
209
 
210
+ flags:
211
+
212
+ - `--debug` - display debug logs and additional information which can be helpful in case of issues
213
+ - `--logPatches` - display patched logs, useful when you want to fix schema in original file
214
+
194
215
  Remember to add `.env` file in order to authenticate with Shopware instance.
195
216
 
196
217
  ```bash
@@ -243,11 +264,8 @@ Prepare your config file named **api-gen.config.json**:
243
264
 
244
265
  Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-gen/CHANGELOG.md)
245
266
 
246
- ### Latest changes: 1.1.4
247
-
248
- ### Patch Changes
267
+ ### Latest changes: 1.2.0
249
268
 
250
- - [#1447](https://github.com/shopware/frontends/pull/1447) [`11ea41f`](https://github.com/shopware/frontends/commit/11ea41fc00493d4997ddfcb23e2f5bf89dd2a828) Thanks [@patzick](https://github.com/patzick)! - Improved error messages and merging override patches.
269
+ ### Minor Changes
251
270
 
252
- - Updated dependencies [[`a87bbcf`](https://github.com/shopware/frontends/commit/a87bbcfa3f5aa440265b1e8f0fc72a204863befc)]:
253
- - @shopware/api-client@1.2.0
271
+ - [#1566](https://github.com/shopware/frontends/pull/1566) [`541cd6e`](https://github.com/shopware/frontends/commit/541cd6e5b5acaa20fb8aad699b2674e81b9330ce) Thanks [@patzick](https://github.com/patzick)! - Possibility to add multiple override json patches. Now you can use our default overrides and add your own on top of it.
package/dist/cli.mjs CHANGED
@@ -24,7 +24,7 @@ const __filename = __cjs_url__.fileURLToPath(import.meta.url);
24
24
  const __dirname = __cjs_path__.dirname(__filename);
25
25
  const require = __cjs_mod__.createRequire(import.meta.url);
26
26
  const name$1 = "@shopware/api-gen";
27
- const version$2 = "1.1.4";
27
+ const version$2 = "1.2.0";
28
28
  const description$1 = "Shopware CLI for API client generation.";
29
29
  const author = "Shopware";
30
30
  const type$1 = "module";
@@ -199781,114 +199781,6 @@ picocolors.exports.createColors = createColors;
199781
199781
  var picocolorsExports = picocolors.exports;
199782
199782
  const c = /*@__PURE__*/getDefaultExportFromCjs$1(picocolorsExports);
199783
199783
 
199784
- const API_GEN_CONFIG_FILENAME = "api-gen.config.json";
199785
- async function loadApiGenConfig(params = {}) {
199786
- try {
199787
- const config = await readFileSync(API_GEN_CONFIG_FILENAME, {
199788
- // TODO: use c12 library for that
199789
- // name: "api-gen", // file should be "api-gen.config.json"
199790
- encoding: "utf-8"
199791
- });
199792
- return lib.parse(config);
199793
- } catch (error) {
199794
- if (!params.silent) {
199795
- console.error(
199796
- c.red(
199797
- `Error while parsing config file ${API_GEN_CONFIG_FILENAME}. Check whether the file is correct JSON file.
199798
- `
199799
- ),
199800
- error
199801
- );
199802
- }
199803
- return null;
199804
- }
199805
- }
199806
- function isURL(str) {
199807
- return str.startsWith("http");
199808
- }
199809
- async function loadJsonOverrides({
199810
- path,
199811
- apiType
199812
- }) {
199813
- const localPath = `./api-types/${apiType}ApiSchema.overrides.json`;
199814
- const fallbackPath = existsSync(localPath) ? localPath : `https://raw.githubusercontent.com/shopware/frontends/main/packages/api-client/api-types/${apiType}ApiSchema.overrides.json`;
199815
- const pathToResolve = path || fallbackPath;
199816
- console.log("Loading overrides from:", pathToResolve);
199817
- try {
199818
- if (isURL(pathToResolve)) {
199819
- const response = await ofetch(pathToResolve, {
199820
- responseType: "json",
199821
- parseResponse: lib.parse
199822
- });
199823
- return response;
199824
- }
199825
- const jsonOverridesFile = await readFileSync(pathToResolve, {
199826
- encoding: "utf-8"
199827
- });
199828
- const content = lib.parse(jsonOverridesFile);
199829
- return content;
199830
- } catch (error) {
199831
- console.warn(
199832
- c.yellow(
199833
- `Problem with resolving overrides "patches" at address ${pathToResolve}. Check whether you configuret it properly in your ${API_GEN_CONFIG_FILENAME}
199834
- `
199835
- ),
199836
- error
199837
- );
199838
- }
199839
- }
199840
- function displayPatchingSummary({
199841
- todosToFix,
199842
- outdatedPatches,
199843
- alreadyApliedPatches,
199844
- errors
199845
- }) {
199846
- if (!errors?.length && todosToFix.length) {
199847
- console.log(c.yellow("Warnings to fix in the schema:"));
199848
- for (const todo of todosToFix) {
199849
- console.log(`${c.yellow("WARNING")}: ${todo[0]}`);
199850
- console.log("Diff:\n", todo[1], "\n\n");
199851
- }
199852
- }
199853
- if (errors?.length) {
199854
- console.log(c.red("Errors found:"));
199855
- for (const error of errors) {
199856
- console.log(`
199857
- <==== ${c.red("ERROR")}:
199858
- ${error}
199859
- ====>
199860
-
199861
- `);
199862
- }
199863
- }
199864
- if (outdatedPatches.length) {
199865
- console.log(c.yellow("Info: Outdated patches:"));
199866
- console.log(
199867
- c.gray(
199868
- "No action needed. Patches are already applied. You can remove them from the overrides file to clean it up."
199869
- )
199870
- );
199871
- for (const todo of outdatedPatches) {
199872
- console.log(`${todo[0]}`);
199873
- console.log("Patch to remove:\n", todo[1], "\n\n");
199874
- }
199875
- }
199876
- console.log("\n\n===== Summary =====");
199877
- if (alreadyApliedPatches) {
199878
- console.log(
199879
- c.gray(
199880
- ` \u2714\uFE0F Already fixed patches: ${c.bold(alreadyApliedPatches)} They can be removed now from the overrides file.`
199881
- )
199882
- );
199883
- }
199884
- const formatColor = todosToFix.length ? c.yellow : c.green;
199885
- console.log(
199886
- formatColor(
199887
- `We've found ${c.bold(todosToFix.length)} warning(s) in the schema.${todosToFix.length ? " Apply patches to fix them in the original schema." : ""}`
199888
- )
199889
- );
199890
- }
199891
-
199892
199784
  // src/index.ts
199893
199785
  var f$1 = {
199894
199786
  reset: [0, 0],
@@ -206979,6 +206871,138 @@ ${c.cyan(
206979
206871
  };
206980
206872
  }
206981
206873
 
206874
+ const API_GEN_CONFIG_FILENAME = "api-gen.config.json";
206875
+ async function loadApiGenConfig(params = {}) {
206876
+ try {
206877
+ const config = await readFileSync(API_GEN_CONFIG_FILENAME, {
206878
+ // TODO: use c12 library for that
206879
+ // name: "api-gen", // file should be "api-gen.config.json"
206880
+ encoding: "utf-8"
206881
+ });
206882
+ return lib.parse(config);
206883
+ } catch (error) {
206884
+ if (!params.silent) {
206885
+ console.error(
206886
+ c.red(
206887
+ `Error while parsing config file ${API_GEN_CONFIG_FILENAME}. Check whether the file is correct JSON file.
206888
+ `
206889
+ ),
206890
+ error
206891
+ );
206892
+ }
206893
+ return null;
206894
+ }
206895
+ }
206896
+ function isURL(str) {
206897
+ return str.startsWith("http");
206898
+ }
206899
+ async function resolveSinglePath(pathToResolve) {
206900
+ try {
206901
+ if (isURL(pathToResolve)) {
206902
+ const response = await ofetch(pathToResolve, {
206903
+ responseType: "json",
206904
+ parseResponse: lib.parse
206905
+ });
206906
+ return response;
206907
+ }
206908
+ const jsonOverridesFile = await readFileSync(pathToResolve, {
206909
+ encoding: "utf-8"
206910
+ });
206911
+ return lib.parse(jsonOverridesFile);
206912
+ } catch (error) {
206913
+ console.warn(
206914
+ c.yellow(
206915
+ `Problem with resolving overrides "patches" at address ${pathToResolve}. Check whether you configured it properly in your ${API_GEN_CONFIG_FILENAME}
206916
+ `
206917
+ ),
206918
+ error
206919
+ );
206920
+ return {};
206921
+ }
206922
+ }
206923
+ async function loadJsonOverrides({
206924
+ paths,
206925
+ apiType
206926
+ }) {
206927
+ const localPath = `./api-types/${apiType}ApiSchema.overrides.json`;
206928
+ const fallbackPath = existsSync(localPath) ? localPath : `https://raw.githubusercontent.com/shopware/frontends/main/packages/api-client/api-types/${apiType}ApiSchema.overrides.json`;
206929
+ const patchesToResolve = Array.isArray(paths) ? paths : paths ? [paths] : [];
206930
+ if (!patchesToResolve?.length) {
206931
+ patchesToResolve.push(fallbackPath);
206932
+ }
206933
+ console.log("Loading overrides from:", patchesToResolve);
206934
+ try {
206935
+ const results = await Promise.allSettled(
206936
+ patchesToResolve.map(resolveSinglePath)
206937
+ );
206938
+ return extendedDefu(
206939
+ {},
206940
+ ...results.filter((result) => result.status === "fulfilled").map((result) => result.value)
206941
+ );
206942
+ } catch (error) {
206943
+ console.warn(
206944
+ c.yellow(
206945
+ `Problem with resolving overrides "patches". Check whether you configured it properly in your ${API_GEN_CONFIG_FILENAME}
206946
+ `
206947
+ ),
206948
+ error
206949
+ );
206950
+ return {};
206951
+ }
206952
+ }
206953
+ function displayPatchingSummary({
206954
+ todosToFix,
206955
+ outdatedPatches,
206956
+ alreadyApliedPatches,
206957
+ errors,
206958
+ displayPatchedLogs
206959
+ }) {
206960
+ if (displayPatchedLogs && !errors?.length && todosToFix.length) {
206961
+ console.log(c.yellow("Warnings to fix in the schema:"));
206962
+ for (const todo of todosToFix) {
206963
+ console.log(`${c.yellow("WARNING")}: ${todo[0]}`);
206964
+ console.log("Diff:\n", todo[1], "\n\n");
206965
+ }
206966
+ }
206967
+ if (errors?.length) {
206968
+ console.log(c.red("Errors found:"));
206969
+ for (const error of errors) {
206970
+ console.log(`
206971
+ <==== ${c.red("ERROR")}:
206972
+ ${error}
206973
+ ====>
206974
+
206975
+ `);
206976
+ }
206977
+ }
206978
+ if (outdatedPatches.length) {
206979
+ console.log(c.yellow("Info: Outdated patches:"));
206980
+ console.log(
206981
+ c.gray(
206982
+ "No action needed. Patches are already applied. You can remove them from the overrides file to clean it up."
206983
+ )
206984
+ );
206985
+ for (const todo of outdatedPatches) {
206986
+ console.log(`${todo[0]}`);
206987
+ console.log("Patch to remove:\n", todo[1], "\n\n");
206988
+ }
206989
+ }
206990
+ console.log("\n\n===== Summary =====");
206991
+ if (alreadyApliedPatches) {
206992
+ console.log(
206993
+ c.gray(
206994
+ ` \u2714\uFE0F Already fixed patches: ${c.bold(alreadyApliedPatches)} They can be removed now from the overrides file.`
206995
+ )
206996
+ );
206997
+ }
206998
+ const formatColor = todosToFix.length ? c.yellow : c.green;
206999
+ console.log(
207000
+ formatColor(
207001
+ `We've found ${c.bold(todosToFix.length)} warning(s) in the schema.${todosToFix.length ? " Apply patches to fix them in the original schema." : ""}`
207002
+ )
207003
+ );
207004
+ }
207005
+
206982
207006
  async function generateFile(filepath, operationsMap, existingTypes, schemasMap, options) {
206983
207007
  const project = await prepareFileContent({
206984
207008
  filepath,
@@ -207615,9 +207639,23 @@ async function generate(args) {
207615
207639
  // we allow to not have the config file in this command
207616
207640
  });
207617
207641
  const jsonOverrides = await loadJsonOverrides({
207618
- path: configJSON?.patches,
207642
+ paths: configJSON?.patches,
207619
207643
  apiType: args.apiType
207620
207644
  });
207645
+ if (args.debug) {
207646
+ const overridesFilePath = join(
207647
+ args.cwd,
207648
+ "api-types",
207649
+ `${args.apiType}ApiTypes.overrides-result.json`
207650
+ );
207651
+ writeFileSync(
207652
+ overridesFilePath,
207653
+ lib.stringify(jsonOverrides, null, 2)
207654
+ );
207655
+ console.log(
207656
+ `[DEBUG] Check the overrides result in: ${c.bold(overridesFilePath)} file.`
207657
+ );
207658
+ }
207621
207659
  const {
207622
207660
  patchedSchema,
207623
207661
  todosToFix,
@@ -207632,7 +207670,8 @@ async function generate(args) {
207632
207670
  displayPatchingSummary({
207633
207671
  todosToFix,
207634
207672
  outdatedPatches,
207635
- alreadyApliedPatches
207673
+ alreadyApliedPatches,
207674
+ displayPatchedLogs: args.logPatches
207636
207675
  });
207637
207676
  if (args.debug) {
207638
207677
  const patchedSchemaFilename = inputFilename.replace(
@@ -207647,6 +207686,9 @@ async function generate(args) {
207647
207686
  writeFileSync(patchedSchemaPath, lib.stringify(patchedSchema), {
207648
207687
  encoding: "utf-8"
207649
207688
  });
207689
+ console.log(
207690
+ `[DEBUG] Check the patched schema in: ${c.bold(patchedSchemaPath)} file.`
207691
+ );
207650
207692
  }
207651
207693
  const astSchema = await openapiTS(patchedSchema, {
207652
207694
  version: +(process.env.OPENAPI_VERSION || 3),
@@ -207727,6 +207769,7 @@ async function generate(args) {
207727
207769
  writeFileSync(fullOutputFilePath, schema, {
207728
207770
  encoding: "utf-8"
207729
207771
  });
207772
+ console.log(`[DEBUG]: Debug Schema saved to ${fullOutputFilePath}`);
207730
207773
  schema = await format$1(schema, {
207731
207774
  // semi: false,
207732
207775
  parser: "typescript"
@@ -207761,6 +207804,9 @@ async function generate(args) {
207761
207804
  writeFileSync(fullOutputFilePath, schema, {
207762
207805
  encoding: "utf-8"
207763
207806
  });
207807
+ console.log(
207808
+ `[DEBUG] Check the generated schema in: ${c.bold(fullOutputFilePath)} file.`
207809
+ );
207764
207810
  }
207765
207811
  const overridesFilepath = join(
207766
207812
  args.cwd,
@@ -208093,9 +208139,20 @@ async function validateJson(args) {
208093
208139
  }
208094
208140
  const errors = [];
208095
208141
  const jsonOverrides = await loadJsonOverrides({
208096
- path: configJSON.patches,
208142
+ paths: configJSON.patches,
208097
208143
  apiType: args.apiType
208098
208144
  });
208145
+ if (args.debug) {
208146
+ const overridesFilePath = join(
208147
+ args.cwd,
208148
+ "api-types",
208149
+ `${args.apiType}ApiTypes.overrides-result.json`
208150
+ );
208151
+ writeFileSync(overridesFilePath, lib.stringify(jsonOverrides, null, 2));
208152
+ console.log(
208153
+ `[DEBUG] Check the overrides result in: ${c.bold(overridesFilePath)} file.`
208154
+ );
208155
+ }
208099
208156
  for (const [schemaName, schema] of Object.entries(
208100
208157
  fileContentAsJson.components?.schemas || {}
208101
208158
  )) {
@@ -208175,7 +208232,8 @@ async function validateJson(args) {
208175
208232
  todosToFix,
208176
208233
  errors,
208177
208234
  outdatedPatches,
208178
- alreadyApliedPatches
208235
+ alreadyApliedPatches,
208236
+ displayPatchedLogs: args.logPatches
208179
208237
  });
208180
208238
  console.log(
208181
208239
  endpointsMissingInSchema > 0 ? c.red("\u274C") : c.green("\u2714\uFE0F"),
@@ -208225,6 +208283,10 @@ yargs(hideBin(process.argv)).scriptName("@shopware/api-gen").usage("$0 [args]").
208225
208283
  type: "boolean",
208226
208284
  default: false,
208227
208285
  describe: "show debug information and generate intermediate files"
208286
+ }).option("logPatches", {
208287
+ type: "boolean",
208288
+ default: false,
208289
+ describe: "show patched logs"
208228
208290
  }).help();
208229
208291
  },
208230
208292
  async (args) => generate(args)
@@ -208253,6 +208315,14 @@ yargs(hideBin(process.argv)).scriptName("@shopware/api-gen").usage("$0 [args]").
208253
208315
  }).positional("filename", {
208254
208316
  type: "string",
208255
208317
  describe: "name of the schema json file. The default (based on apiType parameter) is 'storeApiSchema.json' or 'adminApiSchema.json'"
208318
+ }).option("logPatches", {
208319
+ type: "boolean",
208320
+ default: false,
208321
+ describe: "show patched logs"
208322
+ }).positional("debug", {
208323
+ type: "boolean",
208324
+ default: false,
208325
+ describe: "show debug information and generate intermediate files"
208256
208326
  }).help();
208257
208327
  },
208258
208328
  async (args) => validateJson(args)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopware/api-gen",
3
- "version": "1.1.4",
3
+ "version": "1.2.0",
4
4
  "description": "Shopware CLI for API client generation.",
5
5
  "author": "Shopware",
6
6
  "type": "module",