Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Swagger api docs #35

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading