1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

chore: support for additional media (#3247)

## About the changes
Add new methods to simplify the creation of schemas for endpoints with
additional media types (other than `application/json`)

This is a follow-up on exporting an endpoint as `text/csv`
This commit is contained in:
Gastón Fournier 2023-03-06 12:44:12 +01:00 committed by GitHub
parent 4c6361d82e
commit e325704a0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 11 deletions

View File

@ -1,4 +1,9 @@
import { createResponseSchema } from './create-response-schema';
import {
createResponseSchema,
createResponseSchemas,
schemaNamed,
schemaTyped,
} from './create-response-schema';
test('createResponseSchema', () => {
expect(createResponseSchema('schemaName')).toMatchInlineSnapshot(`
@ -14,3 +19,28 @@ test('createResponseSchema', () => {
}
`);
});
test('createResponseSchemaWithDifferentMedia', () => {
expect(
createResponseSchemas('my-schema', {
'application/json': schemaNamed('schemaName'),
'text/css': schemaTyped('string'),
}),
).toMatchInlineSnapshot(`
{
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/schemaName",
},
},
"text/css": {
"schema": {
"type": "string",
},
},
},
"description": "my-schema",
}
`);
});

View File

@ -1,18 +1,39 @@
import { OpenAPIV3 } from 'openapi-types';
export const createResponseSchemas = (
description: string,
content: { [media: string]: OpenAPIV3.MediaTypeObject },
): OpenAPIV3.ResponseObject => {
return {
description,
content: content,
};
};
export const schemaNamed = (schemaName: string): OpenAPIV3.MediaTypeObject => {
return {
schema: {
$ref: `#/components/schemas/${schemaName}`,
},
};
};
export const schemaTyped = (
type: OpenAPIV3.NonArraySchemaObjectType,
): OpenAPIV3.MediaTypeObject => {
return {
schema: {
type,
},
};
};
export const createResponseSchema = (
schemaName: string,
): OpenAPIV3.ResponseObject => {
return {
description: schemaName,
content: {
'application/json': {
schema: {
$ref: `#/components/schemas/${schemaName}`,
},
},
},
};
return createResponseSchemas(schemaName, {
'application/json': schemaNamed(schemaName),
});
};
export const resourceCreatedResponseSchema = (