@shopware/api-gen 1.2.1 → 1.3.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/README.md +8 -8
- package/dist/cli.mjs +111 -88
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -107,7 +107,8 @@ Example:
|
|
|
107
107
|
|
|
108
108
|
```json
|
|
109
109
|
{
|
|
110
|
-
"
|
|
110
|
+
"$schema": "https://raw.githubusercontent.com/shopware/frontends/main/packages/api-gen/api-gen.schema.json",
|
|
111
|
+
"patches": ["storeApiTypes.overrides.json"]
|
|
111
112
|
}
|
|
112
113
|
```
|
|
113
114
|
|
|
@@ -115,6 +116,7 @@ or you could use multiple patches and add your own overrides on top:
|
|
|
115
116
|
|
|
116
117
|
```json
|
|
117
118
|
{
|
|
119
|
+
"$schema": "https://raw.githubusercontent.com/shopware/frontends/main/packages/api-gen/api-gen.schema.json",
|
|
118
120
|
"patches": [
|
|
119
121
|
"https://raw.githubusercontent.com/shopware/frontends/refs/heads/main/packages/api-client/api-types/storeApiSchema.overrides.json",
|
|
120
122
|
"./api-types/myOwnPatches.overrides.json"
|
|
@@ -246,7 +248,7 @@ Prepare your config file named **api-gen.config.json**:
|
|
|
246
248
|
"rules": [
|
|
247
249
|
"COMPONENTS_API_ALIAS" // you have description on autocompletion what specific rule does, this one for example ensures correctness of the apiAlias field
|
|
248
250
|
],
|
|
249
|
-
//"patches": "
|
|
251
|
+
//"patches": "storeApiTypes.overrides.json" // -> path to your overrides file in api-types folder, default is fetched from api-client repository
|
|
250
252
|
}
|
|
251
253
|
```
|
|
252
254
|
|
|
@@ -264,11 +266,9 @@ Prepare your config file named **api-gen.config.json**:
|
|
|
264
266
|
|
|
265
267
|
Full changelog for stable version is available [here](https://github.com/shopware/frontends/blob/main/packages/api-gen/CHANGELOG.md)
|
|
266
268
|
|
|
267
|
-
### Latest changes: 1.
|
|
268
|
-
|
|
269
|
-
### Patch Changes
|
|
269
|
+
### Latest changes: 1.3.0
|
|
270
270
|
|
|
271
|
-
|
|
271
|
+
### Minor Changes
|
|
272
272
|
|
|
273
|
-
-
|
|
274
|
-
|
|
273
|
+
- [#1706](https://github.com/shopware/frontends/pull/1706) [`d7bf6d7`](https://github.com/shopware/frontends/commit/d7bf6d715689f5d0cbef9f0bbbc78f19f54215d5) Thanks [@patzick](https://github.com/patzick)! - Simplify `api-gen.config.json` configuration. Now you can use names instead of paths in patches like `storeApiSchema.overrides.json` or `storeApiSchema.b2b.overrides.json`.
|
|
274
|
+
Thanks to this you can simple and more granularly apply patches for your project.
|
package/dist/cli.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import yargs from 'yargs';
|
|
2
2
|
import { hideBin } from 'yargs/helpers';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
|
|
4
|
+
import { resolve, join, dirname } from 'node:path';
|
|
5
5
|
import openapiTS, { transformSchemaObjectWithComposition, astToString } from 'openapi-typescript';
|
|
6
6
|
import require$$1$1 from 'fs';
|
|
7
7
|
import require$$1 from 'path';
|
|
@@ -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.
|
|
27
|
+
const version$2 = "1.3.0";
|
|
28
28
|
const description$1 = "Shopware CLI for API client generation.";
|
|
29
29
|
const author = "Shopware";
|
|
30
30
|
const type$1 = "module";
|
|
@@ -207241,8 +207241,7 @@ ${c.cyan(
|
|
|
207241
207241
|
openApiSchema?.paths || {}
|
|
207242
207242
|
)) {
|
|
207243
207243
|
for (const httpMethod of Object.keys(pathObject)) {
|
|
207244
|
-
if (!(httpMethod in pathObject))
|
|
207245
|
-
continue;
|
|
207244
|
+
if (!(httpMethod in pathObject)) continue;
|
|
207246
207245
|
schemaPaths.push({
|
|
207247
207246
|
path: pathName,
|
|
207248
207247
|
method: httpMethod.toUpperCase()
|
|
@@ -207271,6 +207270,77 @@ ${c.cyan(
|
|
|
207271
207270
|
};
|
|
207272
207271
|
}
|
|
207273
207272
|
|
|
207273
|
+
function getTypePropertyNames(type) {
|
|
207274
|
+
return type.getProperties().map((prop) => prop.name);
|
|
207275
|
+
}
|
|
207276
|
+
function getDeepProperty({
|
|
207277
|
+
type,
|
|
207278
|
+
names,
|
|
207279
|
+
node,
|
|
207280
|
+
typeChecker
|
|
207281
|
+
}) {
|
|
207282
|
+
if (names.length === 0) {
|
|
207283
|
+
return type;
|
|
207284
|
+
}
|
|
207285
|
+
const [currentName, ...restNames] = names;
|
|
207286
|
+
const property = type.getProperty(currentName);
|
|
207287
|
+
if (!property) {
|
|
207288
|
+
return void 0;
|
|
207289
|
+
}
|
|
207290
|
+
const propertyType = typeChecker.getTypeOfSymbolAtLocation(property, node);
|
|
207291
|
+
return getDeepProperty({
|
|
207292
|
+
type: propertyType,
|
|
207293
|
+
names: restNames,
|
|
207294
|
+
node,
|
|
207295
|
+
typeChecker
|
|
207296
|
+
});
|
|
207297
|
+
}
|
|
207298
|
+
function getDeepPropertyCode({
|
|
207299
|
+
type,
|
|
207300
|
+
names,
|
|
207301
|
+
node,
|
|
207302
|
+
typeChecker
|
|
207303
|
+
}) {
|
|
207304
|
+
const namesWithoutLast = names.slice(0, -1);
|
|
207305
|
+
const currentName = names[names.length - 1];
|
|
207306
|
+
const deepProperty = getDeepProperty({
|
|
207307
|
+
type,
|
|
207308
|
+
names: namesWithoutLast,
|
|
207309
|
+
node,
|
|
207310
|
+
typeChecker
|
|
207311
|
+
});
|
|
207312
|
+
if (deepProperty) {
|
|
207313
|
+
const property = deepProperty.getProperty(currentName);
|
|
207314
|
+
if (!property) {
|
|
207315
|
+
return void 0;
|
|
207316
|
+
}
|
|
207317
|
+
return property.valueDeclaration?.getText().replace(/^[^:]+:/, "");
|
|
207318
|
+
}
|
|
207319
|
+
}
|
|
207320
|
+
function isOptional(symbol) {
|
|
207321
|
+
const declarations = symbol.getDeclarations();
|
|
207322
|
+
if (!declarations) return false;
|
|
207323
|
+
const declaration = declarations[0];
|
|
207324
|
+
if (ts.isPropertySignature(declaration) && declaration.questionToken !== void 0) {
|
|
207325
|
+
return true;
|
|
207326
|
+
}
|
|
207327
|
+
if (ts.isParameter(declaration) && declaration.questionToken !== void 0) {
|
|
207328
|
+
return true;
|
|
207329
|
+
}
|
|
207330
|
+
return false;
|
|
207331
|
+
}
|
|
207332
|
+
function isNeverType(symbol) {
|
|
207333
|
+
if (!symbol) return false;
|
|
207334
|
+
return symbol.flags === ts.TypeFlags.Never;
|
|
207335
|
+
}
|
|
207336
|
+
async function loadLocalJSONFile(path) {
|
|
207337
|
+
const localPath = resolve(path);
|
|
207338
|
+
if (existsSync(localPath)) {
|
|
207339
|
+
return lib.parse(readFileSync(localPath, "utf-8"));
|
|
207340
|
+
}
|
|
207341
|
+
return void 0;
|
|
207342
|
+
}
|
|
207343
|
+
|
|
207274
207344
|
const API_GEN_CONFIG_FILENAME = "api-gen.config.json";
|
|
207275
207345
|
async function loadApiGenConfig(params = {}) {
|
|
207276
207346
|
try {
|
|
@@ -207303,21 +207373,44 @@ async function resolveSinglePath(pathToResolve) {
|
|
|
207303
207373
|
responseType: "json",
|
|
207304
207374
|
parseResponse: lib.parse
|
|
207305
207375
|
});
|
|
207376
|
+
console.log("Resolved file from", pathToResolve);
|
|
207306
207377
|
return response;
|
|
207307
207378
|
}
|
|
207308
|
-
const
|
|
207309
|
-
|
|
207310
|
-
|
|
207311
|
-
|
|
207379
|
+
const localFile = await loadLocalJSONFile(pathToResolve);
|
|
207380
|
+
if (localFile) {
|
|
207381
|
+
console.log("Resolved file from", pathToResolve);
|
|
207382
|
+
return localFile;
|
|
207383
|
+
}
|
|
207384
|
+
const apiTypesFolder = join(process.cwd(), "api-types");
|
|
207385
|
+
const apiTypesFile = join(apiTypesFolder, pathToResolve);
|
|
207386
|
+
const localApiTypesFile = await loadLocalJSONFile(apiTypesFile);
|
|
207387
|
+
if (localApiTypesFile) {
|
|
207388
|
+
console.log("Resolved file from", apiTypesFile);
|
|
207389
|
+
return localApiTypesFile;
|
|
207390
|
+
}
|
|
207391
|
+
const nodeModulesFolder = resolve(
|
|
207392
|
+
`node_modules/@shopware/api-client/api-types/${pathToResolve}`
|
|
207393
|
+
);
|
|
207394
|
+
const localNodeModulesFile = await loadLocalJSONFile(nodeModulesFolder);
|
|
207395
|
+
if (localNodeModulesFile) {
|
|
207396
|
+
console.log("Resolved file from", nodeModulesFolder);
|
|
207397
|
+
return localNodeModulesFile;
|
|
207398
|
+
}
|
|
207399
|
+
const githubFile = `https://raw.githubusercontent.com/shopware/frontends/main/packages/api-client/api-types/${pathToResolve}`;
|
|
207400
|
+
const localGithubFile = await loadLocalJSONFile(githubFile);
|
|
207401
|
+
if (localGithubFile) {
|
|
207402
|
+
console.log("Resolved file from", githubFile);
|
|
207403
|
+
return localGithubFile;
|
|
207404
|
+
}
|
|
207312
207405
|
} catch (error) {
|
|
207313
207406
|
console.warn(
|
|
207314
207407
|
c.yellow(
|
|
207315
|
-
`Problem with resolving
|
|
207408
|
+
`Problem with resolving file at address ${pathToResolve}. Check whether you configured it properly in your ${API_GEN_CONFIG_FILENAME}
|
|
207316
207409
|
`
|
|
207317
207410
|
),
|
|
207318
207411
|
error
|
|
207319
207412
|
);
|
|
207320
|
-
return
|
|
207413
|
+
return void 0;
|
|
207321
207414
|
}
|
|
207322
207415
|
}
|
|
207323
207416
|
async function loadJsonOverrides({
|
|
@@ -207337,7 +207430,7 @@ async function loadJsonOverrides({
|
|
|
207337
207430
|
);
|
|
207338
207431
|
return extendedDefu(
|
|
207339
207432
|
{},
|
|
207340
|
-
...results.filter((result) => result.status === "fulfilled").map((result) => result.value)
|
|
207433
|
+
...results.filter((result) => result.status === "fulfilled").map((result) => result.value || {})
|
|
207341
207434
|
);
|
|
207342
207435
|
} catch (error) {
|
|
207343
207436
|
console.warn(
|
|
@@ -207554,72 +207647,6 @@ async function prepareFileContent({
|
|
|
207554
207647
|
return project;
|
|
207555
207648
|
}
|
|
207556
207649
|
|
|
207557
|
-
function getTypePropertyNames(type) {
|
|
207558
|
-
return type.getProperties().map((prop) => prop.name);
|
|
207559
|
-
}
|
|
207560
|
-
function getDeepProperty({
|
|
207561
|
-
type,
|
|
207562
|
-
names,
|
|
207563
|
-
node,
|
|
207564
|
-
typeChecker
|
|
207565
|
-
}) {
|
|
207566
|
-
if (names.length === 0) {
|
|
207567
|
-
return type;
|
|
207568
|
-
}
|
|
207569
|
-
const [currentName, ...restNames] = names;
|
|
207570
|
-
const property = type.getProperty(currentName);
|
|
207571
|
-
if (!property) {
|
|
207572
|
-
return void 0;
|
|
207573
|
-
}
|
|
207574
|
-
const propertyType = typeChecker.getTypeOfSymbolAtLocation(property, node);
|
|
207575
|
-
return getDeepProperty({
|
|
207576
|
-
type: propertyType,
|
|
207577
|
-
names: restNames,
|
|
207578
|
-
node,
|
|
207579
|
-
typeChecker
|
|
207580
|
-
});
|
|
207581
|
-
}
|
|
207582
|
-
function getDeepPropertyCode({
|
|
207583
|
-
type,
|
|
207584
|
-
names,
|
|
207585
|
-
node,
|
|
207586
|
-
typeChecker
|
|
207587
|
-
}) {
|
|
207588
|
-
const namesWithoutLast = names.slice(0, -1);
|
|
207589
|
-
const currentName = names[names.length - 1];
|
|
207590
|
-
const deepProperty = getDeepProperty({
|
|
207591
|
-
type,
|
|
207592
|
-
names: namesWithoutLast,
|
|
207593
|
-
node,
|
|
207594
|
-
typeChecker
|
|
207595
|
-
});
|
|
207596
|
-
if (deepProperty) {
|
|
207597
|
-
const property = deepProperty.getProperty(currentName);
|
|
207598
|
-
if (!property) {
|
|
207599
|
-
return void 0;
|
|
207600
|
-
}
|
|
207601
|
-
return property.valueDeclaration?.getText().replace(/^[^:]+:/, "");
|
|
207602
|
-
}
|
|
207603
|
-
}
|
|
207604
|
-
function isOptional(symbol) {
|
|
207605
|
-
const declarations = symbol.getDeclarations();
|
|
207606
|
-
if (!declarations)
|
|
207607
|
-
return false;
|
|
207608
|
-
const declaration = declarations[0];
|
|
207609
|
-
if (ts.isPropertySignature(declaration) && declaration.questionToken !== void 0) {
|
|
207610
|
-
return true;
|
|
207611
|
-
}
|
|
207612
|
-
if (ts.isParameter(declaration) && declaration.questionToken !== void 0) {
|
|
207613
|
-
return true;
|
|
207614
|
-
}
|
|
207615
|
-
return false;
|
|
207616
|
-
}
|
|
207617
|
-
function isNeverType(symbol) {
|
|
207618
|
-
if (!symbol)
|
|
207619
|
-
return false;
|
|
207620
|
-
return symbol.flags === ts.TypeFlags.Never;
|
|
207621
|
-
}
|
|
207622
|
-
|
|
207623
207650
|
function createVirtualFiles(files) {
|
|
207624
207651
|
const filesMap = /* @__PURE__ */ new Map();
|
|
207625
207652
|
for (const fileDefinition of files) {
|
|
@@ -208012,8 +208039,8 @@ async function generate(args) {
|
|
|
208012
208039
|
const outputFilename = inputFilename.replace(".json", ".d.ts");
|
|
208013
208040
|
const fullInputFilePath = join(args.cwd, "api-types", inputFilename);
|
|
208014
208041
|
const fullOutputFilePath = join(args.cwd, "api-types", outputFilename);
|
|
208015
|
-
const
|
|
208016
|
-
if (!
|
|
208042
|
+
const resolvedSchema = await resolveSinglePath(inputFilename);
|
|
208043
|
+
if (!resolvedSchema && !args.apiType) {
|
|
208017
208044
|
console.log(
|
|
208018
208045
|
c.yellow(
|
|
208019
208046
|
`Schema file ${c.bold(
|
|
@@ -208028,11 +208055,8 @@ async function generate(args) {
|
|
|
208028
208055
|
let schema = "";
|
|
208029
208056
|
let processedSchemaAst;
|
|
208030
208057
|
let apiVersion = "unknown";
|
|
208031
|
-
if (
|
|
208032
|
-
const
|
|
208033
|
-
encoding: "utf-8"
|
|
208034
|
-
});
|
|
208035
|
-
const schemaForPatching = lib.parse(schemaFile);
|
|
208058
|
+
if (resolvedSchema) {
|
|
208059
|
+
const schemaForPatching = structuredClone(resolvedSchema);
|
|
208036
208060
|
apiVersion = schemaForPatching?.info?.version;
|
|
208037
208061
|
const configJSON = await loadApiGenConfig({
|
|
208038
208062
|
silent: true
|
|
@@ -208065,7 +208089,7 @@ async function generate(args) {
|
|
|
208065
208089
|
openApiSchema: schemaForPatching,
|
|
208066
208090
|
jsonOverrides
|
|
208067
208091
|
});
|
|
208068
|
-
const originalSchema =
|
|
208092
|
+
const originalSchema = structuredClone(resolvedSchema);
|
|
208069
208093
|
console.log("schema", originalSchema.info);
|
|
208070
208094
|
displayPatchingSummary({
|
|
208071
208095
|
todosToFix,
|
|
@@ -208119,8 +208143,7 @@ async function generate(args) {
|
|
|
208119
208143
|
];
|
|
208120
208144
|
const stringFields = Object.keys(schemaObject.properties).filter(
|
|
208121
208145
|
(key) => {
|
|
208122
|
-
if (notAllowedKeys.includes(key))
|
|
208123
|
-
return false;
|
|
208146
|
+
if (notAllowedKeys.includes(key)) return false;
|
|
208124
208147
|
const property = schemaObject.properties?.[key];
|
|
208125
208148
|
return !!property && "type" in property && property.type === "string";
|
|
208126
208149
|
}
|