diff --git a/README.md b/README.md index 0184f53..a150eb8 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ restful-react is **very well tested, production ready** and powers all of our pr - [Code Generation from OpenAPI / Swagger specs](#code-generation-from-openapi--swagger-specs) - [Usage](#usage) - [Validation of the OpenAPI specification](#validation-of-the-openapi-specification) + - [API Versioning](#api-versioning) - [Import from URL](#import-from-url) - [Import from GitHub](#import-from-github) - [Transforming an Original Spec](#transforming-an-original-spec) @@ -631,6 +632,10 @@ To enforce the best quality as possible of specification, we have integrated the To activate this, add a `--validation` flag to your `restful-react` call. +#### API Versioning + +The generated file will include an exported constant `SPEC_VERSION` that will contain to the OpenAPI `info.version` property's value. + #### Import from URL Adding the `--url` flag to `restful-react import` instead of using the `--file` flag will attempt to fetch the spec from that endpoint. diff --git a/src/scripts/import-open-api.ts b/src/scripts/import-open-api.ts index 1077c20..4c4d015 100644 --- a/src/scripts/import-open-api.ts +++ b/src/scripts/import-open-api.ts @@ -605,6 +605,13 @@ export const resolveDiscriminator = (specs: OpenAPIObject) => { } }; +/** + * Add the version of the spec + * + * @param version + */ +export const addVersionMetadata = (version: string) => `export const SPEC_VERSION = "${version}"; \n`; + /** * Extract all types from #/components/schemas * @@ -816,6 +823,7 @@ const importOpenApi = async ({ let output = ""; + output += addVersionMetadata(specs.info.version); output += generateSchemasDefinition(specs.components && specs.components.schemas); output += generateRequestBodiesDefinition(specs.components && specs.components.requestBodies); output += generateResponsesDefinition(specs.components && specs.components.responses); diff --git a/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap b/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap index a452868..faaa042 100644 --- a/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap +++ b/src/scripts/tests/__snapshots__/import-open-api.test.ts.snap @@ -26,7 +26,8 @@ import { Get, GetProps, useGet, UseGetProps, Mutate, MutateProps, useMutate, Use const encode = encodingTagFactory(encodingFn); - /** + export const SPEC_VERSION = \\"1.0.0\\"; +/** * A pet. */ export type Pet = NewPet & { @@ -225,6 +226,7 @@ export const useUpdatePet = ({id, ...props}: UseUpdatePetProps) => useMutate