UNPKG

edge-master

Version:
157 lines (156 loc) 3.78 kB
/** * Basic OpenAPI 3.0 schema generation utilities * This is a simplified implementation - for advanced features, consider using a dedicated OpenAPI library */ /** * Creates a basic OpenAPI specification */ export function createOpenAPISpec(info) { return { openapi: '3.0.0', info, paths: {}, }; } /** * Adds a server to the OpenAPI spec */ export function addServer(spec, server) { if (!spec.servers) { spec.servers = []; } spec.servers.push(server); return spec; } /** * Adds a path to the OpenAPI spec */ export function addPath(spec, path, method, operation) { if (!spec.paths[path]) { spec.paths[path] = {}; } spec.paths[path][method] = operation; return spec; } /** * Adds a schema to the components section */ export function addSchema(spec, name, schema) { if (!spec.components) { spec.components = {}; } if (!spec.components.schemas) { spec.components.schemas = {}; } spec.components.schemas[name] = schema; return spec; } /** * Adds a security scheme to the components section */ export function addSecurityScheme(spec, name, scheme) { if (!spec.components) { spec.components = {}; } if (!spec.components.securitySchemes) { spec.components.securitySchemes = {}; } spec.components.securitySchemes[name] = scheme; return spec; } /** * Adds a tag to the OpenAPI spec */ export function addTag(spec, name, description) { if (!spec.tags) { spec.tags = []; } spec.tags.push({ name, description }); return spec; } /** * Helper to create a JSON response schema */ export function jsonResponse(description, schema) { return { description, content: { 'application/json': { schema, }, }, }; } /** * Helper to create a JSON request body */ export function jsonRequestBody(schema, required = true) { return { required, content: { 'application/json': { schema, }, }, }; } /** * Helper to create common response schemas */ export const commonResponses = { success: (schema) => jsonResponse('Successful response', schema || { type: 'object' }), created: (schema) => jsonResponse('Resource created', schema || { type: 'object' }), noContent: () => ({ description: 'No content', }), badRequest: () => jsonResponse('Bad request', { type: 'object', properties: { error: { type: 'string' }, message: { type: 'string' }, }, }), unauthorized: () => jsonResponse('Unauthorized', { type: 'object', properties: { error: { type: 'string' }, message: { type: 'string' }, }, }), forbidden: () => jsonResponse('Forbidden', { type: 'object', properties: { error: { type: 'string' }, message: { type: 'string' }, }, }), notFound: () => jsonResponse('Not found', { type: 'object', properties: { error: { type: 'string' }, message: { type: 'string' }, }, }), serverError: () => jsonResponse('Internal server error', { type: 'object', properties: { error: { type: 'string' }, message: { type: 'string' }, }, }), }; /** * Helper to create common security schemes */ export const commonSecuritySchemes = { bearerAuth: () => ({ type: 'http', scheme: 'bearer', bearerFormat: 'JWT', }), apiKey: (name = 'X-API-Key', inLocation = 'header') => ({ type: 'apiKey', name, in: inLocation, }), };