2025-02-15 00:25:23 +02:00
import { fileURLToPath } from "url" ;
import { dirname , join } from "path" ;
2025-03-02 20:47:57 +01:00
import swaggerJsdoc from "swagger-jsdoc" ;
2025-02-15 00:25:23 +02:00
import fs from "fs" ;
2025-02-13 17:02:03 +01:00
/*
2025-04-13 19:32:13 +02:00
* Usage: npm run chore:generate-openapi
2025-05-03 09:05:21 +02:00
* Output: ./apps/server/src/assets/openapi.json
2025-02-13 17:02:03 +01:00
*
* Inspect generated file by opening it in https://editor-next.swagger.io/
*
*/
2025-05-03 09:05:21 +02:00
const scriptDir = dirname ( fileURLToPath ( import . meta . url ) ) ;
const outputPath = join ( scriptDir , ".." , "apps" , "server" , "src" , "assets" , "openapi.json" ) ;
const packageJson = JSON . parse ( fs . readFileSync ( join ( scriptDir , ".." , "package.json" ) , 'utf8' ) ) ;
2025-02-13 17:02:03 +01:00
const options = {
2025-03-02 20:47:57 +01:00
definition : {
openapi : "3.1.1" ,
info : {
title : "Trilium Notes - Sync server API" ,
2025-05-03 09:05:21 +02:00
version : packageJson [ "version" ] ,
2025-03-02 20:47:57 +01:00
description :
2025-06-30 20:39:29 +03:00
"This is the internal sync server API used by Trilium Notes.\n\n_If you're looking for the officially supported External Trilium API, see [here](https://triliumnext.github.io/Docs/Wiki/etapi.html)._\n\nThis page does not yet list all routes. For a full list, see the [route controller](https://github.com/TriliumNext/Trilium/blob/v0.91.6/src/routes/routes.ts)." ,
2025-03-02 20:47:57 +01:00
contact : {
name : "TriliumNext issue tracker" ,
2025-06-30 20:39:29 +03:00
url : "https://github.com/TriliumNext/Trilium/issues"
2025-03-02 20:47:57 +01:00
} ,
license : {
name : "GNU Free Documentation License 1.3 (or later)" ,
url : "https://www.gnu.org/licenses/fdl-1.3"
}
}
2025-02-13 17:02:03 +01:00
} ,
2025-03-02 20:47:57 +01:00
apis : [
// Put individual files here to have them ordered first.
2025-05-03 09:05:21 +02:00
"./apps/server/src/routes/api/setup.ts" ,
2025-03-02 20:47:57 +01:00
// all other files
2025-05-03 09:05:21 +02:00
"./apps/server/src/routes/api/*.ts" ,
"./apps/server/src/routes/*.ts" ,
"./scripts/generate-openapi.ts"
2025-03-02 20:47:57 +01:00
]
2025-02-13 17:02:03 +01:00
} ;
const openapiSpecification = swaggerJsdoc ( options ) ;
2025-02-15 00:25:23 +02:00
fs . writeFileSync ( outputPath , JSON . stringify ( openapiSpecification ) ) ;
2025-05-03 09:05:21 +02:00
console . log ( "Saved to" , outputPath ) ;
2025-02-13 17:02:03 +01:00
2025-02-13 23:51:42 +01:00
/**
* @swagger
* tags:
* - name: auth
* description: Authentication
* - name: sync
* description: Synchronization
2025-02-14 09:40:38 +01:00
* - name: data
2025-02-13 23:51:42 +01:00
*/
2025-02-13 17:02:03 +01:00
/**
* @swagger
* components:
* schemas:
2025-02-14 09:40:38 +01:00
* Attribute:
* type: object
* properties:
* attributeId:
* type: string
* example: "4G1DPrI58PAb"
* noteId:
* $ref: "#/components/schemas/NoteId"
* type:
* type: string
2025-05-03 08:47:43 +02:00
* enum: ["label", "relation"]
2025-02-14 09:40:38 +01:00
* name:
* type: string
* example: "internalLink"
* value:
* type: string
* example: "hA8aHSpTRdZ6"
* description: "If type = \"relation\", a note ID. Otherwise, the attribute content."
* position:
* type: integer
* example: 20
* isInheritable:
* type: boolean
* Blob:
* type: object
* properties:
* blobId:
* type: string
* example: "8iqMIB8eiY1tPYmElfjm"
* content:
* type:
* - string
* - 'null'
* description: "`null` if not text."
* contentLength:
* type: integer
* dateModified:
* $ref: "#/components/schemas/DateTime"
* utcDateModified:
* $ref: "#/components/schemas/UtcDateTime"
* Branch:
* type: object
2025-05-03 08:47:43 +02:00
* required: ["branchId", "noteId", "parentNoteId", "notePosition"]
2025-02-14 09:40:38 +01:00
* properties:
* branchId:
* $ref: "#/components/schemas/BranchId"
* noteId:
* $ref: "#/components/schemas/NoteId"
* parentNoteId:
* $ref: "#/components/schemas/NoteId"
* notePosition:
* type: integer
* example: 20
* prefix:
* type:
* - string
* - 'null'
* isExpanded:
* type: boolean
* BranchId:
* type: string
* example: "WUjhaGp4EKah_ur11rSfHkzeV"
* description: Equal to `{parentNoteId}_{noteId}`
* DateTime:
* type: string
* example: "2025-02-14 08:19:59.203+0100"
2025-02-13 23:51:42 +01:00
* EntityChange:
* type: object
* properties:
* entityChange:
* type: object
* properties:
* entityName:
* type: string
* example: "notes"
* description: Database table for this entity.
* changeId:
* type: string
* example: "changeId9630"
* description: ID, referenced in `entity_changes` table.
* entity:
* type: object
* description: Encoded entity data. Object has one property for each database column.
2025-02-14 09:40:38 +01:00
* Note:
* type: object
2025-05-03 08:47:43 +02:00
* required: ["noteId", "title", "isProtected", "type", "mime", "blobId"]
2025-02-14 09:40:38 +01:00
* properties:
* noteId:
* $ref: "#/components/schemas/NoteId"
* title:
* type: string
* isProtected:
* type: boolean
* type:
* type: string
* example: "text"
2025-07-06 20:23:50 +03:00
* enum: ["text", "code", "render", "file", "image", "search", "relationMap", "book", "noteMap", "mermaid", "canvas", "webView", "launcher", "doc", "contentWidget", "mindMap"]
2025-06-30 20:39:29 +03:00
* description: "[Reference list](https://github.com/TriliumNext/Trilium/blob/v0.91.6/src/services/note_types.ts)"
2025-02-14 09:40:38 +01:00
* mime:
* type: string
* example: "text/html"
* blobId:
* type: string
* example: "z4PhNX7vuL3xVChQ1m2A"
* NoteId:
* type: string
* example: "ur11rSfHkzeV"
* description: "12-character note ID. Special values: \"none\"`, `\"root\"."
* Timestamps:
* type: object
* properties:
* dateCreated:
* $ref: "#/components/schemas/DateTime"
* dateModified:
* $ref: "#/components/schemas/DateTime"
* utcDateCreated:
* $ref: "#/components/schemas/UtcDateTime"
* utcDateModified:
* $ref: "#/components/schemas/UtcDateTime"
2025-02-13 17:02:03 +01:00
* UtcDateTime:
* type: string
* example: "2025-02-13T07:42:47.698Z"
2025-02-14 09:40:38 +01:00
* description: "Result of `new Date().toISOString().replace('T', ' ')`"
2025-02-13 17:02:03 +01:00
* securitySchemes:
* user-password:
* type: apiKey
* name: trilium-cred
* in: header
* description: "Username and password, formatted as `user:password`"
* session:
* type: apiKey
* in: cookie
* name: trilium.sid
*/