Skip to content

Commit

Permalink
Added route convert script
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Dec 5, 2023
1 parent 08cb5bc commit a30c738
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions docs/articles/convert-urls-to-openapi.md
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");
}
```

1 comment on commit a30c738

@vercel
Copy link

@vercel vercel bot commented on a30c738 Dec 5, 2023

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

Please sign in to comment.