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:
parent
4c6361d82e
commit
e325704a0b
@ -1,4 +1,9 @@
|
|||||||
import { createResponseSchema } from './create-response-schema';
|
import {
|
||||||
|
createResponseSchema,
|
||||||
|
createResponseSchemas,
|
||||||
|
schemaNamed,
|
||||||
|
schemaTyped,
|
||||||
|
} from './create-response-schema';
|
||||||
|
|
||||||
test('createResponseSchema', () => {
|
test('createResponseSchema', () => {
|
||||||
expect(createResponseSchema('schemaName')).toMatchInlineSnapshot(`
|
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",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
@ -1,18 +1,39 @@
|
|||||||
import { OpenAPIV3 } from 'openapi-types';
|
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 = (
|
export const createResponseSchema = (
|
||||||
schemaName: string,
|
schemaName: string,
|
||||||
): OpenAPIV3.ResponseObject => {
|
): OpenAPIV3.ResponseObject => {
|
||||||
return {
|
return createResponseSchemas(schemaName, {
|
||||||
description: schemaName,
|
'application/json': schemaNamed(schemaName),
|
||||||
content: {
|
});
|
||||||
'application/json': {
|
|
||||||
schema: {
|
|
||||||
$ref: `#/components/schemas/${schemaName}`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const resourceCreatedResponseSchema = (
|
export const resourceCreatedResponseSchema = (
|
||||||
|
Loading…
Reference in New Issue
Block a user