route.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { NextResponse } from 'next/server';
  2. const openApiSpec = {
  3. openapi: '3.0.3',
  4. info: {
  5. title: 'Vtorio API',
  6. version: '1.0.0',
  7. description: 'File upload and management API for Vtorio application',
  8. },
  9. servers: [
  10. {
  11. url: 'http://localhost:3000',
  12. description: 'Development server',
  13. },
  14. ],
  15. components: {
  16. schemas: {
  17. File: {
  18. type: 'object',
  19. properties: {
  20. id: { type: 'string' },
  21. filename: { type: 'string' },
  22. mimetype: { type: 'string' },
  23. size: { type: 'number' },
  24. createdAt: { type: 'string', format: 'date-time' },
  25. updatedAt: { type: 'string', format: 'date-time' },
  26. },
  27. required: ['id', 'filename', 'mimetype', 'size', 'createdAt'],
  28. },
  29. Error: {
  30. type: 'object',
  31. properties: {
  32. success: { type: 'boolean', example: false },
  33. error: { type: 'string' },
  34. },
  35. required: ['success', 'error'],
  36. },
  37. },
  38. },
  39. paths: {
  40. '/api/upload': {
  41. post: {
  42. summary: 'Upload a file',
  43. description: 'Upload a file to the server and store it in the database',
  44. tags: ['Files'],
  45. requestBody: {
  46. required: true,
  47. content: {
  48. 'multipart/form-data': {
  49. schema: {
  50. type: 'object',
  51. properties: {
  52. file: {
  53. type: 'string',
  54. format: 'binary',
  55. description: 'The file to upload',
  56. },
  57. },
  58. required: ['file'],
  59. },
  60. },
  61. },
  62. },
  63. responses: {
  64. '200': {
  65. description: 'File uploaded successfully',
  66. content: {
  67. 'application/json': {
  68. schema: {
  69. type: 'object',
  70. properties: {
  71. success: { type: 'boolean', example: true },
  72. file: { $ref: '#/components/schemas/File' },
  73. },
  74. },
  75. },
  76. },
  77. },
  78. '400': {
  79. description: 'Bad request',
  80. content: {
  81. 'application/json': {
  82. schema: { $ref: '#/components/schemas/Error' },
  83. },
  84. },
  85. },
  86. '500': {
  87. description: 'Internal server error',
  88. content: {
  89. 'application/json': {
  90. schema: { $ref: '#/components/schemas/Error' },
  91. },
  92. },
  93. },
  94. },
  95. },
  96. },
  97. '/api/files': {
  98. get: {
  99. summary: 'List all files',
  100. description: 'Get a list of all uploaded files with metadata',
  101. tags: ['Files'],
  102. responses: {
  103. '200': {
  104. description: 'List of files retrieved successfully',
  105. content: {
  106. 'application/json': {
  107. schema: {
  108. type: 'object',
  109. properties: {
  110. success: { type: 'boolean', example: true },
  111. files: {
  112. type: 'array',
  113. items: { $ref: '#/components/schemas/File' },
  114. },
  115. },
  116. },
  117. },
  118. },
  119. },
  120. },
  121. },
  122. },
  123. '/api/files/{id}': {
  124. get: {
  125. summary: 'Download a file',
  126. description: 'Download a specific file by its ID',
  127. tags: ['Files'],
  128. parameters: [
  129. {
  130. name: 'id',
  131. in: 'path',
  132. required: true,
  133. description: 'The ID of the file to download',
  134. schema: {
  135. type: 'string',
  136. example: 'clh123abc456',
  137. },
  138. },
  139. ],
  140. responses: {
  141. '200': {
  142. description: 'File downloaded successfully',
  143. content: {
  144. 'application/octet-stream': {
  145. schema: {
  146. type: 'string',
  147. format: 'binary',
  148. },
  149. },
  150. },
  151. },
  152. '404': {
  153. description: 'File not found',
  154. content: {
  155. 'application/json': {
  156. schema: { $ref: '#/components/schemas/Error' },
  157. },
  158. },
  159. },
  160. '500': {
  161. description: 'Internal server error',
  162. content: {
  163. 'application/json': {
  164. schema: { $ref: '#/components/schemas/Error' },
  165. },
  166. },
  167. },
  168. },
  169. },
  170. },
  171. },
  172. };
  173. export const GET = async () => {
  174. return NextResponse.json(openApiSpec, {
  175. headers: {
  176. 'Content-Type': 'application/json',
  177. },
  178. });
  179. };