Skip to content

Commit

Permalink
feat(api-gen): allow multiple json overrides for schema
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick committed Jan 7, 2025
1 parent cb816e2 commit f0328ba
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 27 deletions.
3 changes: 2 additions & 1 deletion packages/api-client/api-gen.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "../api-gen/api-gen.schema.json",
"rules": ["COMPONENTS_API_ALIAS"]
"rules": ["COMPONENTS_API_ALIAS"],
"patches": ["./api-types/storeApiSchema.overrides.json"]
}
28 changes: 23 additions & 5 deletions packages/api-gen/api-gen.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,32 @@
}
},
"patches": {
"type": "string",
"description": "Path to a file containing patches to apply to the schema",
"anyOf": [
"description": "Path to a file or files containing patches to apply to the schema",
"oneOf": [
{
"format": "url"
"type": "string",
"anyOf": [
{
"format": "url"
},
{
"format": "file-path"
}
]
},
{
"format": "file-path"
"type": "array",
"items": {
"type": "string",
"anyOf": [
{
"format": "url"
},
{
"format": "file-path"
}
]
}
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api-gen/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export async function generate(args: {
silent: true, // we allow to not have the config file in this command
});
const jsonOverrides = await loadJsonOverrides({
path: configJSON?.patches,
paths: configJSON?.patches,
apiType: args.apiType,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/api-gen/src/commands/validateJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function validateJson(args: {

const errors: string[] = [];
const jsonOverrides = await loadJsonOverrides({
path: configJSON.patches,
paths: configJSON.patches,
apiType: args.apiType,
});

Expand Down
70 changes: 51 additions & 19 deletions packages/api-gen/src/jsonOverrideUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync, readFileSync } from "node:fs";
import json5 from "json5";
import { ofetch } from "ofetch";
import c from "picocolors";
import type { OverridesSchema } from "./patchJsonSchema";
import { type OverridesSchema, extendedDefu } from "./patchJsonSchema";
import type { validationRules } from "./validation-rules";

export type ApiGenConfig = {
Expand Down Expand Up @@ -44,11 +44,36 @@ function isURL(str: string) {
return str.startsWith("http");
}

async function resolveSinglePath(pathToResolve: string) {
try {
if (isURL(pathToResolve)) {
const response = await ofetch(pathToResolve, {
responseType: "json",
parseResponse: json5.parse,
});
return response;
}

const jsonOverridesFile = await readFileSync(pathToResolve, {
encoding: "utf-8",
});
return json5.parse(jsonOverridesFile);
} catch (error) {
console.warn(
c.yellow(
`Problem with resolving overrides "patches" at address ${pathToResolve}. Check whether you configured it properly in your ${API_GEN_CONFIG_FILENAME}\n`,
),
error,
);
return {};
}
}

export async function loadJsonOverrides({
path,
paths,
apiType,
}: {
path?: string;
paths?: string | string[];
apiType: string;
}): Promise<OverridesSchema | undefined> {
const localPath = `./api-types/${apiType}ApiSchema.overrides.json`;
Expand All @@ -57,30 +82,37 @@ export async function loadJsonOverrides({
? localPath
: `https://raw.githubusercontent.com/shopware/frontends/main/packages/api-client/api-types/${apiType}ApiSchema.overrides.json`;

const pathToResolve = path || fallbackPath;
console.log("Loading overrides from:", pathToResolve);
const patchesToResolve: string[] = Array.isArray(paths)
? paths
: paths
? [paths]
: [];

try {
if (isURL(pathToResolve)) {
const response = await ofetch(pathToResolve, {
responseType: "json",
parseResponse: json5.parse,
});
return response;
}
if (!patchesToResolve?.length) {
patchesToResolve.push(fallbackPath);
}

const jsonOverridesFile = await readFileSync(pathToResolve, {
encoding: "utf-8",
});
const content = json5.parse(jsonOverridesFile);
return content;
console.log("Loading overrides from:", patchesToResolve);

try {
const results = await Promise.allSettled(
patchesToResolve.map(resolveSinglePath),
);
// merge results from correctly settled promises
return extendedDefu(
{},
...results
.filter((result) => result.status === "fulfilled")
.map((result) => result.value),
);
} catch (error) {
console.warn(
c.yellow(
`Problem with resolving overrides "patches" at address ${pathToResolve}. Check whether you configuret it properly in your ${API_GEN_CONFIG_FILENAME}\n`,
`Problem with resolving overrides "patches". Check whether you configured it properly in your ${API_GEN_CONFIG_FILENAME}\n`,
),
error,
);
return {};
}
}

Expand Down

0 comments on commit f0328ba

Please sign in to comment.