Skip to content

Commit

Permalink
fix (types): changed document type to a discriminating union (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bram-dc authored and mcollina committed Jul 23, 2024
1 parent f8591cd commit 409380c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
20 changes: 11 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,24 @@ declare module 'fastify' {
}
}

type SwaggerDocumentObject = {
swaggerObject: Partial<OpenAPIV2.Document>;
} | {
openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>;
}

type SwaggerTransform = <S extends FastifySchema = FastifySchema>({
schema,
url,
route,
swaggerObject,
openapiObject,
...documentObject
}: {
schema: S;
url: string;
route: RouteOptions;
swaggerObject: Partial<OpenAPIV2.Document>;
openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>;
}) => { schema: FastifySchema; url: string }
} & SwaggerDocumentObject) => { schema: FastifySchema; url: string }

type SwaggerTransformObject = (documentObject: SwaggerDocumentObject) => Partial<OpenAPIV2.Document> | Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>;

type FastifySwagger = FastifyPluginCallback<fastifySwagger.SwaggerOptions>

Expand Down Expand Up @@ -147,10 +152,7 @@ declare namespace fastifySwagger {
/**
* custom function to transform the openapi or swagger object before it is rendered
*/
transformObject?: ({ swaggerObject, openapiObject }: {
swaggerObject: Partial<OpenAPIV2.Document>;
openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>;
}) => Partial<OpenAPIV2.Document> | Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>;
transformObject?: SwaggerTransformObject;

refResolver?: {
/** Clone the input schema without changing it. Default to `false`. */
Expand Down
8 changes: 2 additions & 6 deletions test/types/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,12 @@ app.get(
schema,
url,
route,
swaggerObject,
openapiObject,
...documentObject
}) => {
schema satisfies FastifySchema;
url satisfies string;
route satisfies RouteOptions;
swaggerObject satisfies Partial<OpenAPIV2.Document>;
openapiObject satisfies Partial<
OpenAPIV3.Document | OpenAPIV3_1.Document
>;
documentObject satisfies { swaggerObject: Partial<OpenAPIV2.Document> } | { openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document> };
return { schema, url };
},
},
Expand Down

0 comments on commit 409380c

Please sign in to comment.