Skip to content

prefix options for interfaces like postfix option #2642

@kuldeep-capsitech

Description

@kuldeep-capsitech

if its possible i would like to see a prefix to add to types , interfaces or other declartation depending on user need, as i needd to add prefix to all the interafaces for which i had to try many methods just to get it working as using Vite ESM with TS, it was hard to add that in output
at the end of day this got it working but it is still a prototype

input: {
      target: "https://localhost:7142/swagger/v1/swagger.json",
      override: {
        transformer: (openapi) => {
          const prefix = "I";
          const schemas = openapi.components?.schemas;

          if (!schemas) return openapi;

          const renamedMap: Record<string, string> = {};

          const prefixedSchemas = Object.keys(schemas).reduce(
            (acc: any, key) => {
              const schema = schemas[key] as any; // Cast to 'any' to avoid the TS(2339) error

              // Check if it is an enum (OpenAPI enums have an 'enum' array)
              const isEnum =
                !!schema.enum || (schema.type === "string" && schema.enum);

              // If it's a standard object (interface), apply the prefix
              if (!isEnum && !schema.$ref) {
                const newKey = `${prefix}${key}`;
                acc[newKey] = schema;
                renamedMap[key] = newKey;
              } else {
                // Keep enums and original references as they are
                acc[key] = schema;
              }
              return acc;
            },
            {}
          );

          openapi.components!.schemas = prefixedSchemas;

          // Final step: update all internal references to match the new prefixed interface names
          let stringified = JSON.stringify(openapi);
          Object.entries(renamedMap).forEach(([oldName, newName]) => {
            const refRegex = new RegExp(
              `"\\$ref"\\s*:\\s*"#\\/components\\/schemas\\/${oldName}"`,
              "g"
            );
            stringified = stringified.replace(
              refRegex,
              `"$ref":"#/components/schemas/${newName}"`
            );
          });

          return JSON.parse(stringified);
        },
      },
    },

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions