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

Commit 9407357

Browse files
micha-ffabien0102
authored andcommitted
Include spec version in generated file.
1 parent 3d28a49 commit 9407357

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ restful-react is **very well tested, production ready** and powers all of our pr
3434
- [Code Generation from OpenAPI / Swagger specs](#code-generation-from-openapi--swagger-specs)
3535
- [Usage](#usage)
3636
- [Validation of the OpenAPI specification](#validation-of-the-openapi-specification)
37+
- [API Versioning](#api-versioning)
3738
- [Import from URL](#import-from-url)
3839
- [Import from GitHub](#import-from-github)
3940
- [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
631632
632633
To activate this, add a `--validation` flag to your `restful-react` call.
633634
635+
#### API Versioning
636+
637+
The generated file will include an exported constant `SPEC_VERSION` that will contain to the OpenAPI `info.version` property's value.
638+
634639
#### Import from URL
635640
636641
Adding the `--url` flag to `restful-react import` instead of using the `--file` flag will attempt to fetch the spec from that endpoint.

src/scripts/import-open-api.ts

+8
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,13 @@ export const resolveDiscriminator = (specs: OpenAPIObject) => {
605605
}
606606
};
607607

608+
/**
609+
* Add the version of the spec
610+
*
611+
* @param version
612+
*/
613+
export const addVersionMetadata = (version: string) => `export const SPEC_VERSION = "${version}"; \n`;
614+
608615
/**
609616
* Extract all types from #/components/schemas
610617
*
@@ -816,6 +823,7 @@ const importOpenApi = async ({
816823

817824
let output = "";
818825

826+
output += addVersionMetadata(specs.info.version);
819827
output += generateSchemasDefinition(specs.components && specs.components.schemas);
820828
output += generateRequestBodiesDefinition(specs.components && specs.components.requestBodies);
821829
output += generateResponsesDefinition(specs.components && specs.components.responses);

src/scripts/tests/__snapshots__/import-open-api.test.ts.snap

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import { Get, GetProps, useGet, UseGetProps, Mutate, MutateProps, useMutate, Use
2626
2727
const encode = encodingTagFactory(encodingFn);
2828
29-
/**
29+
export const SPEC_VERSION = \\"1.0.0\\";
30+
/**
3031
* A pet.
3132
*/
3233
export type Pet = NewPet & {
@@ -225,6 +226,7 @@ export const useUpdatePet = ({id, ...props}: UseUpdatePetProps) => useMutate<Pet
225226
exports[`scripts/import-open-api should parse correctly petstore-expanded.yaml (without react component) 1`] = `
226227
"/* Generated by restful-react */
227228
229+
export const SPEC_VERSION = \\"1.0.0\\";
228230
/**
229231
* A pet.
230232
*/
@@ -315,6 +317,7 @@ exports[`scripts/import-open-api should parse correctly petstore-expanded.yaml 1
315317
316318
import React from \\"react\\";
317319
import { Get, GetProps, useGet, UseGetProps, Mutate, MutateProps, useMutate, UseMutateProps } from \\"restful-react\\";
320+
export const SPEC_VERSION = \\"1.0.0\\";
318321
/**
319322
* A pet.
320323
*/

0 commit comments

Comments
 (0)