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

Commit

Permalink
Generate docs for object properties and fix components docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Dec 16, 2019
1 parent b6614dc commit b79b234
Show file tree
Hide file tree
Showing 3 changed files with 522 additions and 327 deletions.
41 changes: 24 additions & 17 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,30 @@ export const getObject = (item: SchemaObject): string => {
}

// Consolidation of item.properties & item.additionalProperties
let output = "{";
let output = "{\n";
if (item.properties) {
output += Object.entries(item.properties)
.map(([key, prop]: [string, ReferenceObject | SchemaObject]) => {
const isRequired = (item.required || []).includes(key);
const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
return `${processedKey}${isRequired ? "" : "?"}: ${resolveValue(prop)}`;
})
.join("; ");
output +=
Object.entries(item.properties)
.map(([key, prop]: [string, ReferenceObject | SchemaObject]) => {
const doc = isReference(prop) ? "" : formatDescription(prop.description, 2);
const isRequired = (item.required || []).includes(key);
const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
return ` ${doc}${processedKey}${isRequired ? "" : "?"}: ${resolveValue(prop)}`;
})
.join(";\n") + ";";
}

if (item.additionalProperties) {
if (item.properties) {
output += "; ";
output += "\n";
}
output += `[key: string]: ${item.additionalProperties === true ? "any" : resolveValue(item.additionalProperties)}`;
output += ` [key: string]: ${
item.additionalProperties === true ? "any" : resolveValue(item.additionalProperties)
};`;
}

if (output !== "{") {
return output + "}";
if (output !== "{\n") {
return output + "\n}";
}

return item.type === "object" ? "{[key: string]: any}" : "any";
Expand Down Expand Up @@ -376,8 +380,11 @@ export type ${componentName}Props = Omit<${Component}Props<${genericsTypes}>, "p
verb === "get" ? "" : ` | "verb"`
}>${paramsInPath.length ? ` & {${paramsTypes}}` : ""};
${operation.summary ? "// " + operation.summary : ""}
export const ${componentName} = (${
${formatDescription(
operation.summary && operation.description
? `${operation.summary}\n\n${operation.description}`
: `${operation.summary || ""}${operation.description || ""}`,
)}export const ${componentName} = (${
paramsInPath.length ? `{${paramsInPath.join(", ")}, ...props}` : "props"
}: ${componentName}Props) => (
<${Component}<${genericsTypes}>${
Expand Down Expand Up @@ -538,12 +545,12 @@ export interface ${pascal(name)}Response ${type}`;
*
* @param description
*/
export const formatDescription = (description?: string) =>
export const formatDescription = (description?: string, tabSize = 0) =>
description
? `/**\n${description
.split("\n")
.map(i => ` * ${i}`)
.join("\n")}\n */\n`
.map(i => `${" ".repeat(tabSize)} * ${i}`)
.join("\n")}\n${" ".repeat(tabSize)} */\n${" ".repeat(tabSize)}`
: "";

/**
Expand Down
44 changes: 35 additions & 9 deletions src/scripts/tests/__snapshots__/import-open-api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,43 @@ import { Get, GetProps, useGet, UseGetProps, Mutate, MutateProps, useMutate, Use
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type Pet = NewPet & {id: number};
export type Pet = NewPet & {
id: number;
};
export interface NewPet {name: string; tag?: string}
export interface NewPet {
name: string;
tag?: string;
}
export type CatOrDog = Cat | Dog;
export interface Cat {type: \\"cat\\"; breed: \\"labrador\\" | \\"carlin\\" | \\"beagle\\"}
export interface Cat {
type: \\"cat\\";
breed: \\"labrador\\" | \\"carlin\\" | \\"beagle\\";
}
export interface Dog {type: \\"dog\\"; breed: \\"saimois\\" | \\"bengal\\" | \\"british shorthair\\"}
export interface Dog {
type: \\"dog\\";
breed: \\"saimois\\" | \\"bengal\\" | \\"british shorthair\\";
}
export interface Error {code: number; message: string}
export interface Error {
code: number;
message: string;
}
export interface FindPetsQueryParams {tags?: string[]; limit?: number}
export type FindPetsProps = Omit<GetProps<Pet[], Error, FindPetsQueryParams>, \\"path\\">;
/**
* Returns all pets from the system that the user has access to
* Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.
*
* Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.
*
*/
export const FindPets = (props: FindPetsProps) => (
<Get<Pet[], Error, FindPetsQueryParams>
path={\`/pets\`}
Expand All @@ -40,7 +60,9 @@ export const useFindPets = (props: UseFindPetsProps) => useGet<Pet[], Error, Fin
export type AddPetProps = Omit<MutateProps<Pet, Error, void, NewPet>, \\"path\\" | \\"verb\\">;
/**
* Creates a new pet in the store. Duplicates are allowed
*/
export const AddPet = (props: AddPetProps) => (
<Mutate<Pet, Error, void, NewPet>
verb=\\"POST\\"
Expand All @@ -57,7 +79,9 @@ export const useAddPet = (props: UseAddPetProps) => useMutate<Pet, Error, void,
export type FindPetByIdProps = Omit<GetProps<Pet, Error, void>, \\"path\\"> & {id: number};
/**
* Returns a user based on a single ID, if the user does not have access to the pet
*/
export const FindPetById = ({id, ...props}: FindPetByIdProps) => (
<Get<Pet, Error, void>
path={\`/pets/\${id}\`}
Expand All @@ -73,7 +97,9 @@ export const useFindPetById = ({id, ...props}: UseFindPetByIdProps) => useGet<Pe
export type DeletePetProps = Omit<MutateProps<void, Error, void, string>, \\"path\\" | \\"verb\\">;
/**
* deletes a single pet based on the ID supplied
*/
export const DeletePet = (props: DeletePetProps) => (
<Mutate<void, Error, void, string>
verb=\\"DELETE\\"
Expand Down
Loading

0 comments on commit b79b234

Please sign in to comment.