Skip to content

Commit

Permalink
Add Swagger api docs (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelyn authored Apr 19, 2024
1 parent e6ee79c commit 859058e
Show file tree
Hide file tree
Showing 8 changed files with 2,255 additions and 91 deletions.
12 changes: 12 additions & 0 deletions app/api-docs/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getApiDocs } from "@/lib/swagger";
import ReactSwagger from "./react-swagger";
import "@/styles/swagger.css";

export default async function IndexPage() {
const spec = await getApiDocs();
return (
<section className="container">
<ReactSwagger spec={spec} />
</section>
);
}
14 changes: 14 additions & 0 deletions app/api-docs/react-swagger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use client";

import SwaggerUI from "swagger-ui-react";
import "swagger-ui-react/swagger-ui.css";

type Props = {
spec: Record<string, any>;
};

function ReactSwagger({ spec }: Props) {
return <SwaggerUI spec={spec} />;
}

export default ReactSwagger;
49 changes: 49 additions & 0 deletions app/api/v1/attributes/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,55 @@ import { apiAuthenticatedRoute } from "@/lib/api-helpers";
import { AttributeService } from "@/lib/services/attribute";
import { NextRequest, NextResponse } from "next/server";

/**
* @swagger
* /api/v1/attributes:
* get:
* tags: [/api/v1/]
* summary: Retrieve all attributes for the logged-in user
* description: Returns a list of attributes associated with the logged-in user.
* parameters:
* - in: header
* name: Authorization
* required: true
* schema:
* type: string
* description: Bearer token for authentication.
* - in: header
* name: x-listener-auth
* required: true
* schema:
* type: string
* - in: header
* name: x-space-id
* required: true
* schema:
* type: string
* responses:
* 200:
* description: Successfully retrieved the attributes.
* content:
* application/json:
* schema:
* type: object
* properties:
* attributes:
* type: array
* items:
* type: object
* properties:
* name:
* type: string
* value:
* type: string
* unit:
* type: string
* externalId:
* type: string
* 401:
* description: Unauthorized, if the user is not logged in or token is invalid.
*/

export const GET = (
req: NextRequest,
context: { params: { userId: string } }
Expand Down
53 changes: 53 additions & 0 deletions app/api/v1/products/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,59 @@ import { apiAuthenticatedRoute } from "@/lib/api-helpers";
import { ProductService } from "@/lib/services/product";
import { NextRequest, NextResponse } from "next/server";

/**
* @swagger
* /api/v1/products:
* get:
* tags: [/api/v1/]
* summary: Retrieve all products for the logged-in user
* description: Returns a list of products associated with the logged-in user.
* parameters:
* - in: header
* name: Authorization
* required: true
* schema:
* type: string
* description: Bearer token for authentication.
* - in: header
* name: x-listener-auth
* required: true
* schema:
* type: string
* - in: header
* name: x-space-id
* required: true
* schema:
* type: string
* responses:
* 200:
* description: Successfully retrieved the products.
* content:
* application/json:
* schema:
* type: object
* properties:
* products:
* type: array
* items:
* type: object
* properties:
* name:
* type: string
* description:
* type: string
* price:
* type: number
* quantity:
* type: number
* imageUrl:
* type: string
* externalProductId:
* type: string
* 401:
* description: Unauthorized, if the user is not logged in or token is invalid.
*/

export const GET = (
req: NextRequest,
context: { params: { userId: string } }
Expand Down
26 changes: 26 additions & 0 deletions lib/swagger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { List } from "lucide-react";
import { createSwaggerSpec } from "next-swagger-doc";

export const getApiDocs = async () => {
const spec = createSwaggerSpec({
apiFolder: "app/api",
definition: {
openapi: "3.0.0",
info: {
title: "Next Swagger API Example",
version: "1.0",
},
components: {
// securitySchemes: {
// BearerAuth: {
// type: "http",
// scheme: "bearer",
// bearerFormat: "JWT",
// },
// },
},
security: [],
},
});
return spec;
};
Loading

0 comments on commit 859058e

Please sign in to comment.