mirror of
https://github.com/Unleash/unleash.git
synced 2025-10-27 11:02:16 +01:00
This backwards compatible change allows us to specify a schema `id`
(full path) which to me feels a bit better than specifying the schema
name as a string, since a literal string is prone to typos.
### Before
```ts
requestBody: createRequestSchema(
'createResourceSchema',
),
responses: {
...getStandardResponses(400, 401, 403, 415),
201: resourceCreatedResponseSchema(
'resourceSchema',
),
},
```
### After
```ts
requestBody: createRequestSchema(
createResourceSchema.$id,
),
responses: {
...getStandardResponses(400, 401, 403, 415),
201: resourceCreatedResponseSchema(
resourceSchema.$id,
),
},
```
75 lines
1.8 KiB
TypeScript
75 lines
1.8 KiB
TypeScript
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: schemaName.startsWith('#')
|
|
? schemaName
|
|
: `#/components/schemas/${schemaName}`,
|
|
},
|
|
};
|
|
};
|
|
|
|
export const schemaTyped = (
|
|
type: OpenAPIV3.NonArraySchemaObjectType,
|
|
): OpenAPIV3.MediaTypeObject => {
|
|
return {
|
|
schema: {
|
|
type,
|
|
},
|
|
};
|
|
};
|
|
|
|
export const createResponseSchema = (
|
|
schemaName: string,
|
|
): OpenAPIV3.ResponseObject => {
|
|
return createResponseSchemas(schemaName, {
|
|
'application/json': schemaNamed(schemaName),
|
|
});
|
|
};
|
|
|
|
export const createCsvResponseSchema = (
|
|
schemaName: string,
|
|
example: string,
|
|
): OpenAPIV3.ResponseObject => {
|
|
return createResponseSchemas(schemaName, {
|
|
'text/csv': { example, ...schemaTyped('string') },
|
|
});
|
|
};
|
|
|
|
export const resourceCreatedResponseSchema = (
|
|
schemaName: string,
|
|
): OpenAPIV3.ResponseObject => {
|
|
return {
|
|
headers: {
|
|
location: {
|
|
description: 'The location of the newly created resource.',
|
|
schema: {
|
|
type: 'string',
|
|
format: 'uri',
|
|
},
|
|
},
|
|
},
|
|
description: 'The resource was successfully created.',
|
|
content: {
|
|
'application/json': {
|
|
schema: {
|
|
$ref: schemaName.startsWith('#')
|
|
? schemaName
|
|
: `#/components/schemas/${schemaName}`,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
};
|