166 lines
3.9 KiB
TypeScript
166 lines
3.9 KiB
TypeScript
//import path from "node:path";
|
|
//import { fileURLToPath } from "node:url";
|
|
import type { Express } from "express";
|
|
|
|
//const __filename = fileURLToPath(import.meta.url);
|
|
// const __dirname = path.dirname(__filename);
|
|
|
|
import { apiReference } from "@scalar/express-api-reference";
|
|
// const port = 3000;
|
|
import type { OpenAPIV3_1 } from "openapi-types";
|
|
import { datamartAddSpec } from "../scaler/datamartAdd.spec.js";
|
|
import { datamartUpdateSpec } from "../scaler/datamartUpdate.spec.js";
|
|
import { getDatamartSpec } from "../scaler/getDatamart.spec.js";
|
|
import { prodLoginSpec } from "../scaler/login.spec.js";
|
|
import { prodRestartSpec } from "../scaler/prodSqlRestart.spec.js";
|
|
import { prodStartSpec } from "../scaler/prodSqlStart.spec.js";
|
|
import { prodStopSpec } from "../scaler/prodSqlStop.spec.js";
|
|
import { prodRegisterSpec } from "../scaler/register.spec.js";
|
|
// all the specs
|
|
import { statusSpec } from "../scaler/stats.spec.js";
|
|
|
|
export const openApiBase: OpenAPIV3_1.Document = {
|
|
openapi: "3.1.0",
|
|
info: {
|
|
title: "LST API",
|
|
version: "3.0.0",
|
|
description: "Label System Tracking API",
|
|
},
|
|
servers: [
|
|
{
|
|
url: `http://localhost:3000${process.env.NODE_ENV?.trim() !== "production" ? "/lst" : "/"}`,
|
|
description: "Development server",
|
|
},
|
|
],
|
|
components: {
|
|
securitySchemes: {
|
|
bearerAuth: {
|
|
type: "http",
|
|
scheme: "bearer",
|
|
bearerFormat: "JWT",
|
|
},
|
|
ApiKeyAuth: {
|
|
type: "apiKey",
|
|
description: "API key required for authentication",
|
|
name: "api_key",
|
|
in: "header",
|
|
},
|
|
basicAuth: {
|
|
type: "http",
|
|
scheme: "basic",
|
|
description: "Basic authentication using username and password",
|
|
},
|
|
},
|
|
// schemas: {
|
|
// Error: {
|
|
// type: "object",
|
|
// properties: {
|
|
// error: { type: "string" },
|
|
// message: { type: "string" },
|
|
// },
|
|
// },
|
|
// },.
|
|
},
|
|
|
|
tags: [
|
|
{
|
|
name: "Auth",
|
|
description:
|
|
"Authentication section where you get and create users and api keys",
|
|
},
|
|
{
|
|
name: "System",
|
|
description: "All system endpoints that will be available to run",
|
|
},
|
|
{
|
|
name: "Datamart",
|
|
description:
|
|
"All Special queries to run based on there names.\n Refer to the docs to see all possible queries that can be ran here, you can also run the getQueries to see available.",
|
|
},
|
|
// { name: "TMS", description: "TMS integration" },
|
|
],
|
|
paths: {}, // Will be populated
|
|
};
|
|
|
|
export const setupApiDocsRoutes = (baseUrl: string, app: Express) => {
|
|
const mergedDatamart = {
|
|
"/api/datamart": {
|
|
...(getDatamartSpec["/api/datamart"] ?? {}),
|
|
...(datamartAddSpec["/api/datamart"] ?? {}),
|
|
...(datamartUpdateSpec["/api/datamart"] ?? {}),
|
|
},
|
|
"/api/datamart/{name}": getDatamartSpec["/api/datamart/{name}"],
|
|
};
|
|
|
|
const fullSpec = {
|
|
...openApiBase,
|
|
paths: {
|
|
...statusSpec,
|
|
...prodStartSpec,
|
|
...prodStopSpec,
|
|
...prodRestartSpec,
|
|
...prodLoginSpec,
|
|
...prodRegisterSpec,
|
|
...mergedDatamart,
|
|
|
|
// Add more specs here as you build features
|
|
},
|
|
};
|
|
app.get(`${baseUrl}/api/docs.json`, (_, res) => {
|
|
res.json(fullSpec);
|
|
});
|
|
|
|
app.use(
|
|
`${baseUrl}/api/docs`,
|
|
apiReference({
|
|
url: `${baseUrl}/api/docs.json`,
|
|
theme: "purple",
|
|
darkMode: true,
|
|
authentication: {
|
|
securitySchemes: {
|
|
httpBasic: {
|
|
username: "username",
|
|
password: "password",
|
|
},
|
|
},
|
|
},
|
|
defaultHttpClient: {
|
|
targetKey: "node",
|
|
clientKey: "axios",
|
|
},
|
|
|
|
documentDownloadType: "json",
|
|
hideClientButton: true,
|
|
hiddenClients: {
|
|
// C
|
|
c: ["libcurl"],
|
|
// Clojure
|
|
clojure: ["clj_http"],
|
|
// C#
|
|
csharp: ["httpclient", "restsharp"],
|
|
// Dart
|
|
dart: ["http"],
|
|
// F#
|
|
fsharp: ["httpclient"],
|
|
// Java
|
|
java: ["asynchttp", "nethttp", "okhttp", "unirest"],
|
|
// Objective-C
|
|
objc: ["nsurlsession"],
|
|
// OCaml
|
|
ocaml: ["cohttp"],
|
|
// PHP
|
|
php: ["curl", "guzzle"],
|
|
|
|
// R
|
|
r: ["httr"],
|
|
// Ruby
|
|
ruby: ["native"],
|
|
// Rust
|
|
rust: ["reqwest"],
|
|
// Swift
|
|
swift: ["nsurlsession"],
|
|
},
|
|
}),
|
|
);
|
|
};
|