-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Swagger UI integration and API documentation support
- Loading branch information
Showing
7 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
"@std/jsonc": "jsr:/@std/[email protected]", | ||
"@std/path": "jsr:/@std/[email protected]", | ||
"@std/semver": "jsr:/@std/[email protected]", | ||
"@std/yaml/": "jsr:/@std/[email protected]/", | ||
"deno_dom/": "jsr:/@b-fuze/[email protected]/", | ||
"sqlite/": "https://deno.land/x/[email protected]/", | ||
"zipjs/": "https://deno.land/x/[email protected]/", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { join } from "@std/path"; | ||
import { configure, HttpReader, ZipReader } from "zipjs/index.js"; | ||
import { sure_dir } from "../utils.ts"; | ||
|
||
async function get_latest_version() { | ||
const re = await fetch( | ||
"https://api.github.com/repos/swagger-api/swagger-ui/releases/latest", | ||
); | ||
if (!re.ok) { | ||
throw new Error( | ||
`Failed to fetch latest version: ${re.status} ${re.statusText}`, | ||
); | ||
} | ||
const json = await re.json(); | ||
return json.tag_name; | ||
} | ||
|
||
async function get_download_url() { | ||
const version = await get_latest_version(); | ||
return `https://github.com/swagger-api/swagger-ui/archive/refs/tags/${version}.zip`; | ||
} | ||
|
||
const DIST = /swagger-ui-[1-9\.]+\/dist/; | ||
|
||
async function unzip(url: string) { | ||
const zip_reader = new ZipReader(new HttpReader(url)); | ||
const entries = await zip_reader.getEntries(); | ||
let swagger_base = import.meta.resolve("../static/swagger").slice(7); | ||
if (Deno.build.os === "windows") { | ||
swagger_base = swagger_base.slice(1); | ||
} | ||
await sure_dir(swagger_base); | ||
for (const entry of entries) { | ||
const m = entry.filename.match(DIST); | ||
if (!m) { | ||
continue; | ||
} | ||
const filename = entry.filename.replace(DIST, ""); | ||
if (filename.endsWith("/")) { | ||
const path = join(swagger_base, filename); | ||
await sure_dir(path); | ||
continue; | ||
} | ||
if (!entry.getData) { | ||
continue; | ||
} | ||
const path = join(swagger_base, filename); | ||
console.log("Extracting", entry.filename, "to", path); | ||
const file = await Deno.open(path, { write: true, create: true }); | ||
try { | ||
await entry.getData(file.writable); | ||
} finally { | ||
try { | ||
file.close(); | ||
} catch (_) { | ||
null; | ||
} | ||
} | ||
} | ||
} | ||
|
||
configure({ useWebWorkers: false }); | ||
const download_url = await get_download_url(); | ||
await unzip(download_url); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ flutter/ | |
sw.js | ||
sw.js.map | ||
sw.meta.json | ||
swagger/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
window.onload = function() { | ||
//<editor-fold desc="Changeable Configuration Block"> | ||
|
||
// the following lines will be replaced by docker/configurator, when it runs in a docker-container | ||
window.ui = SwaggerUIBundle({ | ||
url: "/api.json", | ||
dom_id: '#swagger-ui', | ||
deepLinking: true, | ||
presets: [ | ||
SwaggerUIBundle.presets.apis, | ||
SwaggerUIStandalonePreset | ||
], | ||
plugins: [ | ||
SwaggerUIBundle.plugins.DownloadUrl | ||
], | ||
layout: "StandaloneLayout", | ||
validatorUrl: null, | ||
}); | ||
|
||
//</editor-fold> | ||
}; |