Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@shopware/api-gen) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
shopware/frontends - api-gen
Welcome to @shopware/api-gen CLI.
Generate TypeScript schemas from Shopware OpenAPI specification.
After generating schemas, you can use them in fully typed API Client.
Usage
# ✨ Auto-detect
npx nypm install -D @shopware/api-gen
# npm
npm install -D @shopware/api-gen
# yarn
yarn add -D @shopware/api-gen
# pnpm
pnpm install -D @shopware/api-gen
# bun
bun install -D @shopware/api-genFeatures
Generator will create a new directory api-types with TypeScript schemas inside. Depending on the apiType parameter it will create storeApiTypes.ts or adminApiTypes.ts file.
Overriding
If your instance contains inacurate or outdated OpenAPI specification, you can override it by creating a new file inside api-types directory::
storeApiTypes.overrides.tsfor store APIadminApiTypes.overrides.tsfor admin API
Example of overrides file:
import { components as mainComponents } from "./storeApiTypes";
export type components = mainComponents & {
schemas: Schemas;
};
export type Schemas = {
CustomerAddress: {
qwe: string;
};
};
export type operations = {
"myNewEndpointWithDifferentBodys post /aaaaa/bbbbb":
| {
contentType?: "application/json";
accept?: "application/json";
body: components["schemas"]["CustomerAddress"];
response: components["schemas"]["Country"];
responseCode: 201;
}
| {
contentType: "application/xml";
accept?: "application/json";
body: {
someting: boolean;
};
response: {
thisIs200Response: string;
};
responseCode: 200;
};
"updateCustomerAddress patch /account/address/{addressId}": {
contentType?: "application/json";
accept?: "application/json";
/**
* We're testing overrides, assuming update address can only update the city
*/
body: {
city: string;
};
response: components["schemas"]["CustomerAddress"];
responseCode: 200;
};
};[!IMPORTANT]
Overriding components or operations in the TS files requires you to have a full object definitions!
Partial overrides
There is a possiblity to add patches (partial overrides) to the schema. Partial overrides are applied directly to the JSON schema, so the syntax needs to be correct. It can then be used by the backend CI tool to validate and apply these patches directly to the schema to fix inconsistencies.
By default CLI is fetching the patches from the api-client repository, but you can provide your own patches file by adding a path to the api-gen.config.json file.
Example:
{
"patches": "./api-types/storeApiTypes.overrides.json"
}and then inside the storeApiTypes.overrides.json file you can add your patches:
{
"components": {
"Cart": [
{
"required": ["price"]
},
{
"required": ["errors"]
}
]
}
}you apply this as 2 independent patches, or combine it as a single patch without array:
{
"components": {
"Cart": {
"required": ["price", "errors"]
}
}
}Creating multiple patches is useful when you want to apply different changes to the same object, which can also be corrected on the backend side independently. This way specific patches are becoming outdated and you get the notification that you can remove them safely.
[!NOTE]
Check our current default patches to see more examples: source.
Commands
add shortcut to your package.json scripts
{
"scripts": {
"generate-types": "shopware-api-gen generate --apiType=store"
}
}then running pnpm generate-types will generate types in api-types directory.
generate
Transform OpenAPI specification from JSON file to Typescript schemas. Use loadSchema command first.
options:
pnpx @shopware/api-gen generate --help
# generate schemas from store API
pnpx @shopware/api-gen generate --apiType=store
# generate schemas from admin API
pnpx @shopware/api-gen generate --apiType=adminloadSchema
Load OpenAPI specification from Shopware instance and save it to JSON file.
options:
pnpx @shopware/api-gen loadSchema --help
# load schema from store API
pnpx @shopware/api-gen loadSchema --apiType=store
# load schema from admin API
pnpx @shopware/api-gen loadSchema --apiType=adminRemember to add .env file in order to authenticate with Shopware instance.
OPENAPI_JSON_URL="https://your-shop-instance.shopware.store"
## This one needed to fetch store API schema
OPENAPI_ACCESS_KEY="YOUR_STORE_API_ACCESS_KEY"
## These two needed to fetch admin API schema
SHOPWARE_ADMIN_USERNAME="my@username.com"
SHOPWARE_ADMIN_PASSWORD="my-password"validateJson
This command allow to validate the output JSON file of your instance. You can configure which rules should be applied, we provide you with the schema configuration file, so you can easily modify it.
options:
pnpx @shopware/api-gen validateJson --help
# validate JSON file
pnpx @shopware/api-gen validateJson --apiType=storethis searches for api-types/storeApiTypes.json file and validates it. Use loadSchema command first to fetch your JSON file.
Prepare your config file named api-gen.config.json:
{
"$schema": "https://raw.githubusercontent.com/shopware/frontends/main/packages/api-gen/api-gen.schema.json",
"rules": [
"COMPONENTS_API_ALIAS" // you have description on autocompletion what specific rule does, this one for example ensures correctness of the apiAlias field
],
//"patches": "./api-types/storeApiTypes.overrides.json" // -> path to your overrides file, default is fetched from api-client repository
}Changelog
Full changelog for stable version is available here
Latest changes: 0.0.0-canary-20240625115057
Major Changes
- #871
1566f7aThanks @patzick! - Read more about new major release: https://github.com/shopware/frontends/discussions/965
Minor Changes
#534
6170dcaThanks @patzick! -loadSchemacommand added by splittinggeneratecommand#534
6170dcaThanks @patzick! - Sortingpathsin the same order by api patchs#1017
12c8153Thanks @patzick! - New commandvalidateJsonto check correctness of your OpenAPI json file.#534
6170dcaThanks @patzick! - Commandgeneratehas been splitted and doing only transformation from json to d.ts file#564
93a6048Thanks @patzick! - AddedapiTypeoption inloadSchemacommand. WithSHOPWARE_ADMIN_USERNAMEandSHOPWARE_ADMIN_PASSWORDenv variables we can now authorize Admin API schema.example:
# load schema from store API pnpx @shopware/api-gen loadSchema --apiType=store --filename=storeApiSchema.json # load schema from admin API pnpx @shopware/api-gen loadSchema --apiType=admin --filename=adminApiSchema.json
#1032
0b6133eThanks @patzick! - Possibility to add partial patches to the JSON schema.#903
18d8528Thanks @mdanilowicz! - Add Blob type support#534
6170dcaThanks @patzick! - Schemaoperationsis now a generic type to help with overriding types#1032
0b6133eThanks @patzick! - Added support for JSON5 config files. Now you can use comments in your config files and JSON schema.
Patch Changes
#534
6170dcaThanks @patzick! - GeneratedGenericRecordis more open to avoid type problems#1032
0b6133eThanks @patzick! - Fixed optional body parameters, now it's processed correctly. The body is required by default (if defined).#928
bada1cdThanks @mdanilowicz! - Skip GenericRecord for the object with additionalPropertiesUpdated dependencies [
2343012,1566f7a,782ef4d,9643e56,1583a7a,97d2859,d60d062,c729e70,89a97a4,864616f]:- @shopware/api-client@0.0.0-canary-20240625115057