Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Add --skip-react flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Oct 7, 2020
1 parent cba0e05 commit a9a0e73
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 55 deletions.
31 changes: 16 additions & 15 deletions src/bin/restful-react-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import program from "commander";
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import inquirer from "inquirer";
import difference from "lodash/difference";
import pick from "lodash/pick";
import { join, parse } from "path";
import request from "request";
import { homedir } from "os";
Expand All @@ -20,6 +21,7 @@ export interface Options {
github?: string;
transformer?: string;
validation?: boolean;
skipReact?: boolean;
}

export type AdvancedOptions = Options & {
Expand Down Expand Up @@ -52,6 +54,7 @@ program.option("-u, --url [value]", "url to spec (yaml or json openapi specs)");
program.option("-g, --github [value]", "github path (format: `owner:repo:branch:path`)");
program.option("-t, --transformer [value]", "transformer function path");
program.option("--validation", "add the validation step (provided by ibm-openapi-validator)");
program.option("--skip-react", "skip the generation of react components/hooks");
program.option("--config [value]", "override flags by a config file");
program.parse(process.argv);

Expand All @@ -67,6 +70,16 @@ const successWithoutOutputMessage = chalk.yellow("Success! No output path specif
const importSpecs = async (options: AdvancedOptions) => {
const transformer = options.transformer ? require(join(process.cwd(), options.transformer)) : undefined;

const optionsKeys: Array<keyof Options | keyof AdvancedOptions> = [
"validation",
"customImport",
"customProps",
"customGenerator",
"pathParametersEncodingMode",
"skipReact",
];
const importOptions = pick(options, optionsKeys);

if (!options.file && !options.url && !options.github) {
throw new Error("You need to provide an input specification with `--file`, '--url', or `--github`");
}
Expand All @@ -80,11 +93,7 @@ const importSpecs = async (options: AdvancedOptions) => {
data,
format,
transformer,
validation: options.validation,
customImport: options.customImport,
customProps: options.customProps,
customGenerator: options.customGenerator,
pathParametersEncodingMode: options.pathParametersEncodingMode,
...importOptions,
});
} else if (options.url) {
const { url } = options;
Expand Down Expand Up @@ -116,11 +125,7 @@ const importSpecs = async (options: AdvancedOptions) => {
data: body,
format,
transformer,
validation: options.validation,
customImport: options.customImport,
customProps: options.customProps,
customGenerator: options.customGenerator,
pathParametersEncodingMode: options.pathParametersEncodingMode,
...importOptions,
}),
);
});
Expand Down Expand Up @@ -204,11 +209,7 @@ const importSpecs = async (options: AdvancedOptions) => {
data: body.data.repository.object.text,
format,
transformer,
validation: options.validation,
customImport: options.customImport,
customProps: options.customProps,
customGenerator: options.customGenerator,
pathParametersEncodingMode: options.pathParametersEncodingMode,
...importOptions,
}),
);
});
Expand Down
94 changes: 54 additions & 40 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export const generateRestfulComponent = (
parameters: Array<ReferenceObject | ParameterObject> = [],
schemasComponents?: ComponentsObject,
customProps: AdvancedOptions["customProps"] = {},
skipReact = false,
customGenerator?: AdvancedOptions["customGenerator"],
) => {
if (!operation.operationId) {
Expand Down Expand Up @@ -461,59 +462,64 @@ export interface ${componentName}RequestBody ${requestBodyTypes}
`
: ""
}
export type ${componentName}Props = Omit<${Component}Props<${genericsTypes}>, "path"${
verb === "get" ? "" : ` | "verb"`
}>${paramsInPath.length ? ` & ${componentName}PathParams` : ""};
`;

if (!skipReact) {
// Component version
output += `export type ${componentName}Props = Omit<${Component}Props<${genericsTypes}>, "path"${
verb === "get" ? "" : ` | "verb"`
}>${paramsInPath.length ? ` & ${componentName}PathParams` : ""};
${description}export const ${componentName} = (${
paramsInPath.length ? `{${paramsInPath.join(", ")}, ...props}` : "props"
}: ${componentName}Props) => (
paramsInPath.length ? `{${paramsInPath.join(", ")}, ...props}` : "props"
}: ${componentName}Props) => (
<${Component}<${genericsTypes}>${
verb === "get"
? ""
: `
verb === "get"
? ""
: `
verb="${verb.toUpperCase()}"`
}
}
path={encode\`${route}\`}${
customPropsEntries.length
? "\n " + customPropsEntries.map(([key, value]) => `${key}=${value}`).join("\n ")
: ""
}
customPropsEntries.length
? "\n " + customPropsEntries.map(([key, value]) => `${key}=${value}`).join("\n ")
: ""
}
${verb === "delete" ? "pathInlineBodyEncode={encodingFn}" : ""}
{...props}
/>
);
`;

// Hooks version
output += `export type Use${componentName}Props = Omit<Use${Component}Props<${genericsTypesForHooksProps}>, "path"${
verb === "get" ? "" : ` | "verb"`
}>${paramsInPath.length ? ` & ${componentName}PathParams` : ""};
// Hooks version
output += `export type Use${componentName}Props = Omit<Use${Component}Props<${genericsTypesForHooksProps}>, "path"${
verb === "get" ? "" : ` | "verb"`
}>${paramsInPath.length ? ` & ${componentName}PathParams` : ""};
${description}export const use${componentName} = (${
paramsInPath.length ? `{${paramsInPath.join(", ")}, ...props}` : "props"
}: Use${componentName}Props) => use${Component}<${genericsTypes}>(${
verb === "get" ? "" : `"${verb.toUpperCase()}", `
}${
paramsInPath.length
? `(paramsInPath: ${componentName}PathParams) => encode\`${route.replace(/\$\{/g, "${paramsInPath.")}\``
: `encode\`${route}\``
}, ${
customPropsEntries.length || paramsInPath.length || verb === "delete"
? `{ ${
customPropsEntries.length
? `${customPropsEntries
.map(([key, value]) => `${key}:${reactPropsValueToObjectValue(value || "")}`)
.join(", ")},`
: ""
}${verb === "delete" ? "pathInlineBodyEncode: encodingFn, " : " "}${
paramsInPath.length ? `pathParams: { ${paramsInPath.join(", ")} },` : ""
} ...props }`
: "props"
});
paramsInPath.length ? `{${paramsInPath.join(", ")}, ...props}` : "props"
}: Use${componentName}Props) => use${Component}<${genericsTypes}>(${
verb === "get" ? "" : `"${verb.toUpperCase()}", `
}${
paramsInPath.length
? `(paramsInPath: ${componentName}PathParams) => encode\`${route.replace(/\$\{/g, "${paramsInPath.")}\``
: `encode\`${route}\``
}, ${
customPropsEntries.length || paramsInPath.length || verb === "delete"
? `{ ${
customPropsEntries.length
? `${customPropsEntries
.map(([key, value]) => `${key}:${reactPropsValueToObjectValue(value || "")}`)
.join(", ")},`
: ""
}${verb === "delete" ? "pathInlineBodyEncode: encodingFn, " : " "}${
paramsInPath.length ? `pathParams: { ${paramsInPath.join(", ")} },` : ""
} ...props }`
: "props"
});
`;
}

// Custom version
if (customGenerator) {
Expand All @@ -529,7 +535,7 @@ ${description}export const use${componentName} = (${
});
}

if (headerParams.map(({ name }) => name.toLocaleLowerCase()).includes("prefer")) {
if (!skipReact && headerParams.map(({ name }) => name.toLocaleLowerCase()).includes("prefer")) {
output += `export type Poll${componentName}Props = Omit<PollProps<${genericsTypes}>, "path">${
paramsInPath.length ? ` & {${paramsTypes}}` : ""
};
Expand Down Expand Up @@ -765,12 +771,14 @@ const getEncodingFunction = (mode: "uriComponent" | "rfc3986") => {
* @param options.format format of the spec
* @param options.transformer custom function to transform your spec
* @param options.validation validate the spec with ibm-openapi-validator tool
* @param options.skipReact skip the generation of react components/hooks
*/
const importOpenApi = async ({
data,
format,
transformer,
validation,
skipReact,
customImport,
customProps,
customGenerator,
Expand All @@ -780,6 +788,7 @@ const importOpenApi = async ({
format: "yaml" | "json";
transformer?: (specs: OpenAPIObject) => OpenAPIObject;
validation?: boolean;
skipReact?: boolean;
customImport?: AdvancedOptions["customImport"];
customProps?: AdvancedOptions["customProps"];
customGenerator?: AdvancedOptions["customGenerator"];
Expand Down Expand Up @@ -813,6 +822,7 @@ const importOpenApi = async ({
verbs.parameters,
specs.components,
customProps,
skipReact,
customGenerator,
);
}
Expand All @@ -833,11 +843,15 @@ const importOpenApi = async ({
if (havePoll) {
imports.push("Poll", "PollProps");
}
const restfulReactImports = skipReact
? ""
: `import React from "react";
import { ${imports.join(", ")} } from "restful-react";`;

output =
`/* Generated by restful-react */
import React from "react";
import { ${imports.join(", ")} } from "restful-react";${customImport ? `\n${customImport}\n` : ""}
${restfulReactImports}${customImport ? `\n${customImport}\n` : ""}
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
Expand Down

0 comments on commit a9a0e73

Please sign in to comment.