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

Commit

Permalink
Include spec version in generated file.
Browse files Browse the repository at this point in the history
  • Loading branch information
micha-f authored and fabien0102 committed Feb 1, 2021
1 parent 3d28a49 commit 9407357
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/scripts/tests/__snapshots__/import-open-api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 & {
Expand Down Expand Up @@ -225,6 +226,7 @@ export const useUpdatePet = ({id, ...props}: UseUpdatePetProps) => useMutate<Pet
exports[`scripts/import-open-api should parse correctly petstore-expanded.yaml (without react component) 1`] = `
"/* Generated by restful-react */
export const SPEC_VERSION = \\"1.0.0\\";
/**
* A pet.
*/
Expand Down Expand Up @@ -315,6 +317,7 @@ exports[`scripts/import-open-api should parse correctly petstore-expanded.yaml 1
import React from \\"react\\";
import { Get, GetProps, useGet, UseGetProps, Mutate, MutateProps, useMutate, UseMutateProps } from \\"restful-react\\";
export const SPEC_VERSION = \\"1.0.0\\";
/**
* A pet.
*/
Expand Down

0 comments on commit 9407357

Please sign in to comment.