Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Openapi #53

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
28 changes: 28 additions & 0 deletions bundle.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import esbuild from "esbuild";
import path from "path";

const toBundle = ["yaml"];

for (const dep of toBundle) {
const entry = import.meta.resolve(dep);
const outputPath = new URL(
path.resolve("./modules/third-party", dep),
import.meta.url,
).pathname;
const url = new URL(entry);
await esbuild.build({
entryPoints: [url.pathname],
bundle: true,
platform: "browser",
target: "es2022",
legalComments: "linked",
keepNames: true,
treeShaking: true,
minifyIdentifiers: false,
minifySyntax: false,
minifyWhitespace: false,
conditions: ["workerd", "worker", "browser"],
format: "esm",
outfile: path.join(outputPath, "index.js"),
});
}
15 changes: 13 additions & 2 deletions config/policies.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"export": "CachingInboundPolicy",
"module": "$import(@zuplo/runtime)",
"options": {
"expirationSecondsTtl": 60
"expirationSecondsTtl": 60,
"headers": [
"accept"
]
}
},
"name": "caching-inbound",
Expand Down Expand Up @@ -52,6 +55,14 @@
},
"name": "block-bad-bins",
"policyType": "custom-code-inbound"
},
{
"handler": {
"export": "default",
"module": "$import(./modules/add-server-to-openapi)"
},
"name": "custom-code-outbound",
"policyType": "custom-code-outbound"
}
]
}
}
52 changes: 45 additions & 7 deletions config/routes.oas.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"response": {
"type": "object",
"additionalProperties": false,
"required": ["status"],
"required": [
"status"
],
"properties": {
"status": {
"type": "number"
Expand Down Expand Up @@ -65,12 +67,36 @@
"options": {}
},
"policies": {
"inbound": ["request-validation-inbound", "rate-limit-inbound"]
"inbound": [
"request-validation-inbound",
"rate-limit-inbound"
]
}
},
"operationId": "2e21caf1-f4c5-4b8a-8358-b335b4fd4222"
}
},
"/v1/openapi/bins": {
"x-zuplo-path": {
"pathMode": "open-api"
},
"post": {
"summary": "Create OpenAPI bin",
"description": "Lorem ipsum dolor sit amet, **consectetur adipiscing** elit, sed do `eiusmod tempor` incididunt ut labore et dolore magna aliqua.",
"x-zuplo-route": {
"corsPolicy": "anything-goes",
"handler": {
"export": "createMockResponse",
"module": "$import(./modules/handlers)",
"options": {}
},
"policies": {
"inbound": []
}
},
"operationId": "6e4b7ac5-f1e4-48b4-82b5-336d48aaef24"
}
},
"/v1/bins/{binId}": {
"x-zuplo-path": {
"pathMode": "open-api"
Expand All @@ -86,7 +112,13 @@
"options": {}
},
"policies": {
"inbound": ["block-bad-bins", "rate-limit-inbound"]
"inbound": [
"block-bad-bins",
"rate-limit-inbound"
],
"outbound": [
"custom-code-outbound"
]
}
},
"operationId": "2ae802d2-c8e1-40ba-ac2e-f45a9b4eafab"
Expand All @@ -107,7 +139,10 @@
"options": {}
},
"policies": {
"inbound": ["block-bad-bins", "rate-limit-inbound"]
"inbound": [
"block-bad-bins",
"rate-limit-inbound"
]
}
},
"responses": {
Expand All @@ -129,7 +164,7 @@
}
}
},
"operationId": "460b0c3d-6267-4913-9e42-46f6882e2a42"
"operationId": "5fbfb141-50e3-4803-8553-084825e7ce81"
}
},
"/v1/bins/{binId}/requests/{requestId}": {
Expand All @@ -147,7 +182,10 @@
"options": {}
},
"policies": {
"inbound": ["block-bad-bins", "rate-limit-inbound"]
"inbound": [
"block-bad-bins",
"rate-limit-inbound"
]
}
},
"responses": {
Expand Down Expand Up @@ -198,4 +236,4 @@
}
}
}
}
}
37 changes: 37 additions & 0 deletions modules/add-server-to-openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ZuploContext, ZuploRequest } from "@zuplo/runtime";
import { getInvokeBinUrl, isOasBin } from "./utils";

// Module-level function to add invokeBinUrl to the OpenAPI document
function addInvokeBinUrlToOpenApiDoc(openApiDoc, invokeBinUrl) {
// Ensure the 'servers' section exists
if (!openApiDoc.servers) {
openApiDoc.servers = [];
}
// Add the invokeBinUrl as the first server
openApiDoc.servers.unshift({ url: invokeBinUrl });
return openApiDoc;
}

export default async function policy(
response: Response,
request: ZuploRequest,
context: ZuploContext,
options: never,
policyName: string
) {
const binId = request.params.binId;
const url = new URL(request.url);
const invokeBinUrl = getInvokeBinUrl(url, binId);

if (isOasBin(binId)) {
const openApiDoc = await response.json();

// Add the invokeBinUrl as the first server to the openApiDoc
const newOpenApiDoc = addInvokeBinUrlToOpenApiDoc(openApiDoc, invokeBinUrl);
return new Response(JSON.stringify(newOpenApiDoc, null, 2), {
headers: { "Content-Type": "application/json" },
});
}

return response;
}
Loading