generated from Himenon/template-js
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support build github-rest-api-description (#16)
BREAKING_CHANGE * Remove MapLike -> use Record * Corresponds to the case where a character string such as _ or - is included * Closes: #12 #13 #14 #15
- Loading branch information
Showing
40 changed files
with
904 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import * as Name from "./Name"; | ||
/** | ||
* ユーザーが利用できる各種変換オプション | ||
*/ | ||
// export interface Options { | ||
|
||
// } | ||
|
||
export interface Types { | ||
/** | ||
* operationIdに対するescape | ||
*/ | ||
escapeOperationIdText: (operationId: string) => string; | ||
/** | ||
* interface/namespace/typeAliasのnameをescapeする | ||
* import/exportなどの予約語も裁く | ||
*/ | ||
escapeDeclarationText: (text: string) => string; | ||
/** | ||
* 非破壊: PropertySignatureのname用のescape | ||
*/ | ||
escapePropertySignatureName: (text: string) => string; | ||
/** | ||
* 破壊: TypeReferenceのname用のescape | ||
*/ | ||
escapeTypeReferenceNodeName: (text: string) => string; | ||
generateResponseName: (operationId: string, statusCode: string) => string; | ||
generateArgumentParamsTypeDeclaration: (operationId: string) => string; | ||
generateRequestContentTypeName: (operationId: string) => string; | ||
generateResponseContentTypeName: (operationId: string) => string; | ||
generateParameterName: (operationId: string) => string; | ||
generateRequestBodyName: (operationId: string) => string; | ||
generateFunctionName: (operationId: string) => string; | ||
} | ||
|
||
/** | ||
* ユーザーが利用できる各種変換オプション | ||
*/ | ||
export const create = (): Types => { | ||
const convertReservedWord = (word: string): string => { | ||
if (["import", "export"].includes(word)) { | ||
return word + "_"; | ||
} | ||
return word; | ||
}; | ||
const convertString = (text: string): string => { | ||
if (Name.isAvailableVariableName(text)) { | ||
return text; | ||
} | ||
return text.replace(/-/g, "$").replace(/\//g, "$"); | ||
}; | ||
return { | ||
escapeOperationIdText: (operationId: string): string => { | ||
return convertString(operationId); | ||
}, | ||
escapeDeclarationText: (text: string) => { | ||
return convertReservedWord(convertString(text)); | ||
}, | ||
escapePropertySignatureName: (text: string) => { | ||
return Name.escapeText(text); | ||
}, | ||
escapeTypeReferenceNodeName: (text: string) => { | ||
return convertString(text); | ||
}, | ||
generateResponseName: (operationId: string, statusCode: string): string => { | ||
return Name.responseName(convertString(operationId), statusCode); | ||
}, | ||
generateArgumentParamsTypeDeclaration: (operationId: string) => { | ||
return Name.argumentParamsTypeDeclaration(convertString(operationId)); | ||
}, | ||
generateRequestContentTypeName: (operationId: string) => { | ||
return Name.requestContentType(convertString(operationId)); | ||
}, | ||
generateResponseContentTypeName: (operationId: string) => { | ||
return Name.responseContentType(convertString(operationId)); | ||
}, | ||
generateParameterName: (operationId: string) => { | ||
return Name.parameterName(convertString(operationId)); | ||
}, | ||
generateRequestBodyName: (operationId: string) => { | ||
return Name.requestBodyName(convertString(operationId)); | ||
}, | ||
generateFunctionName: (operationId: string) => { | ||
return convertString(operationId); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as Types from "./types"; | ||
|
||
export const getInferredType = (schema: Types.OpenApi.Schema): Types.OpenApi.Schema | undefined => { | ||
if (schema.type || schema.oneOf || schema.allOf || schema.anyOf) { | ||
return schema; | ||
} | ||
// type: arrayを指定せずに、itemsのみを指定している場合に type array変換する | ||
if (schema.items) { | ||
return { ...schema, type: "array" }; | ||
} | ||
// type: string/numberを指定せずに、enumのみを指定している場合に type array変換する | ||
if (schema.enum) { | ||
return { ...schema, type: "string" }; | ||
} | ||
// type: objectを指定せずに、propertiesのみを指定している場合に type object変換する | ||
if (schema.properties) { | ||
return { ...schema, type: "object" }; | ||
} | ||
// type: object, propertiesを指定せずに、requiredのみを指定している場合に type object変換する | ||
if (schema.required) { | ||
const properties = schema.required.reduce((s, name) => { | ||
return { ...s, [name]: { type: "any" } }; | ||
}, {}); | ||
return { ...schema, type: "object", properties }; | ||
} | ||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.