-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
title: Script to Convert URL Params to OpenAPI Format | ||
--- | ||
|
||
This is an example script that shows how to convert URLs in your Zuplo OpenAPI | ||
files to OpenAPI formatted parameters. | ||
|
||
```js | ||
import fs from "fs/promises"; | ||
import kabab from "kebab-case"; | ||
import path from "path"; | ||
import { pathToRegexp } from "path-to-regexp"; | ||
import prettier from "prettier"; | ||
|
||
const files = await fs.readdir(path.join(process.cwd(), "config")); | ||
await Promise.all( | ||
files.map(async (file) => { | ||
if (file.endsWith(".oas.json")) { | ||
const specPath = path.join(process.cwd(), "config", file); | ||
const content = await fs.readFile(specPath, "utf-8").then(JSON.parse); | ||
const newPaths = {}; | ||
Object.entries(content.paths).forEach(([path, entry]) => { | ||
delete entry["x-zuplo-path"]; | ||
const keys = []; | ||
pathToRegexp(path, keys); | ||
let newPath = path; | ||
keys.forEach((key) => { | ||
newPath = newPath.replace(`:${key.name}`, `{${kababCase(key.name)}}`); | ||
}); | ||
Object.entries(entry).forEach(([method, methodEntry]) => { | ||
if (entry[method].parameters) { | ||
entry[method].parameters.forEach((param) => { | ||
if (param.in === "path") { | ||
param.name = kababCase(param.name); | ||
} | ||
}); | ||
} | ||
}); | ||
newPaths[newPath] = entry; | ||
}); | ||
|
||
content.paths = newPaths; | ||
|
||
const json = JSON.stringify(content, null, 2); | ||
const output = prettier.format(json, { parser: "json" }); | ||
|
||
await fs.writeFile(specPath, output, "utf-8"); | ||
} | ||
}), | ||
); | ||
|
||
function kababCase(str) { | ||
return kabab(str).replaceAll("-u-r-l", "-url").replaceAll("-a-p-i", "-api"); | ||
} | ||
``` |
a30c738
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./
docs.zuplo.site
docs.zuplopreview.net
docs-git-main.zuplopreview.net