8000 feat: ✨ add openapi documentation by ueberBrot · Pull Request #101 · Ovi/DummyJSON · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: ✨ add openapi documentation #101

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Explore the detailed documentation at [DummyJSON/Docs](https://dummyjson.com/doc

**New**: Now you can generate your own [custom responses](https://dummyjson.com/custom-response) from DummyJSON, [try it now!](https://dummyjson.com/custom-response)

**New**: We now have an OpenAPI 3.1.0 specification available for DummyJSON!
You can find the `openapi.json` [here](https://dummyjson.com/api-docs/openapi.json).
Additionally, we’ve launched a fully interactive API UI powered by [Scalar](https://github.com/scalar/scalar) — making it even easier to explore and integrate DummyJSON into your projects. Check it out [here](https://dummyjson.com/api-docs)!


## Why DummyJSON?

Ever felt bogged down by the complexities of setting up a backend just to fetch dummy data for your front-end project? Or perhaps you dreamt of a learning app where obtaining realistic data didn't involve navigating through convoluted public APIs and cumbersome registration processes. Well, say hello to DummyJSON!
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"license": "MIT",
"dependencies": {
"@aws-sdk/client-s3": "^3.485.0",
"@scalar/express-api-reference": "^0.7.1",
"axios": "^1.7.7",
"color-convert": "^2.0.1",
"compression": "^1.7.4",
Expand All @@ -34,6 +35,7 @@
"on-finished": "^2.3.0",
"on-headers": "^1.0.2",
"sharp": "0.31.1",
"swagger-jsdoc": "^6.2.8",
"uuid": "^10.0.0",
"winston": "^3.14.2"
},
Expand Down
4 changes: 4 additions & 0 deletions public/img/icons/code.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions public/img/icons/doc.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions src/middleware/openapi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const openapiSpec = require('../utils/openapi');
const { log, logError } = require('../helpers/logger');
const { version } = require('../../package.json');

// We'll set up a middleware that will dynamically load the ESM module
let apiReferenceMiddleware = null;

const initializeApiReference = async () => {
try {
const scalarModule = await import('@scalar/express-api-reference');
const { apiReference } = scalarModule;

apiReferenceMiddleware = apiReference({
// URL to the OpenAPI specification
url: '/api-docs/openapi.json',
// URL to the API reference
cdn: 'https://cdn.jsdelivr.net/npm/@scalar/api-reference',
// Set theme, using the default theme
theme: 'default',
// Title for the API reference
title: 'DummyJSON API Reference',
// Version of the API
version,
});

log('Scalar API Reference initialized successfully');
} catch (error) {
logError('Failed to initialize Scalar API Reference:', { error });
// Provide a fallback middleware
apiReferenceMiddleware = (req, res) => {
res.status(500).json({ message: 'API documentation is currently unavailable' });
};
}
};

initializeApiReference();

const serveOpenAPISpec = (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.send(openapiSpec);
};

// Middleware that checks if the API reference is loaded and uses it
const serveApiReference = (req, res, next) => {
if (!apiReferenceMiddleware) {
return res.status(503).json({ message: 'API documentation is loading, please try again shortly' });
}

return apiReferenceMiddleware(req, res, next);
};

module.exports = {
serveOpenAPISpec,
serveApiReference,
};
119 changes: 119 additions & 0 deletions src/models/openapi-schemas/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* @openapi
* components:
* securitySchemes:
* bearerAuth:
* type: http
* scheme: bearer
* bearerFormat: JWT
* description: JWT token for authentication
*
* schemas:
* LoginRequest:
* type: object
* properties:
* username:
* type: string
* description: Username for authentication
* example: "emilys"
* password:
* type: string
* description: Password for authentication
* example: "emilyspass"
* expiresInMins:
* type: integer
* description: Token expiration time in minutes
* default: 60
* example: 60
* required:
* - username
* - password
*
* RefreshTokenRequest:
* type: object
* properties:
* refreshToken:
* type: string
* description: Refresh token obtained from login or previous token refresh
* example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
* expiresInMins:
* type: integer
* description: Token expiration time in minutes
* default: 60
* example: 60
* required:
* - refreshToken
*
* UserAuth:
* type: object
* properties:
* id:
* type: integer
* description: User ID
* example: 1
* username:
* type: string
* description: Username of authenticated user
* example: "emilys"
* email:
* type: string
* description: Email of authenticated user
* example: "emily.johnson@x.dummyjson.com"
* firstName:
* type: string
* description: First name of user
* example: "Emily"
* lastName:
* type: string
* description: Last name of user
* example: "Johnson"
* gender:
* type: string
* description: Gender of user
* example: "female"
* image:
* type: string
* description: Profile image URL
* example: "https://dummyjson.com/icon/emilys/128"
* required:
* - id
* - username
* - email
* - firstName
* - lastName
*
* AuthResponse:
* allOf:
* - $ref: '#/components/schemas/UserAuth'
* - type: object
* properties:
* accessToken:
* type: string
* description: JWT Access Token
* example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
* refreshToken:
* type: string
* description: JWT Refresh Token for obtaining a new access token
* example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
* required:
* - accessToken
* - refreshToken
*
* TokenResponse:
* type: object
* properties:
* accessToken:
* type: string
* description: New JWT access token
* example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
* refreshToken:
* type: string
* description: New JWT refresh token
* example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
* required:
* - accessToken
* - refreshToken
*/

// Export the model schema (empty object since we're just using the JSDoc for OpenAPI)
module.exports = {};
Loading
0