Skip to content

Commit

Permalink
Openapi (#53)
Browse files Browse the repository at this point in the history
* updated to have openapi support with Zudoku link

* Added OpenAPI support (dirty) (#52)

* added tests for the mockserver

* updated with cleaner types

* fixing tests

* updated to use a plain OAS json doc

* fixed routes (#54)

* Zup josh 11 3 gold haddock (#55)

* fixed routes

* fixes (that I lost before)

* Zup josh 11 3 amethyst wolverine (#56)

* fixed routes

* fixes (that I lost before)

* added accept to caching headers

* removed silly route

* removed docs links for non-oas bins

* fixed build error

* Added yaml bundl

* fixed domain using env

* added better layout and file input

* fixed error

* added yaml support

* fixed error

---------

Co-authored-by: Josh Twist <[email protected]>
Co-authored-by: Josh Twist <[email protected]>
  • Loading branch information
3 people authored Nov 19, 2024
1 parent 99b1def commit 1875598
Show file tree
Hide file tree
Showing 28 changed files with 15,671 additions and 2,981 deletions.
76 changes: 20 additions & 56 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,34 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug: Attach to Running Zup",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "node"
},
{
"type": "node",
"request": "launch",
"name": "Dev",
"env": {
"NODE_ENV": "development"
},
"smartStep": true,
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "dev"],
"skipFiles": ["<node_internals>/**"],
"name": "Launch Zuplo",
"runtimeExecutable": "npx",
"runtimeArgs": [
"@zuplo/cli",
"dev",
"--debug-port",
"9229",
"--port",
"9000"
],
"console": "integratedTerminal",
"sourceMaps": true,
"resolveSourceMapLocations": [
"${workspaceFolder}/dist/**",
"!**/node_modules/**"
]
},
{
"type": "node",
"request": "launch",
"name": "Start",
"env": {
"NODE_ENV": "development"
},
"smartStep": true,
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "serve"],
"skipFiles": ["<node_internals>/**"],
"console": "integratedTerminal"
"internalConsoleOptions": "neverOpen"
},
{
"name": "Zuplo Gateway",
"type": "node",
"request": "launch",
"name": "Build: Workers",
"env": {
"NODE_ENV": "development"
},
"smartStep": true,
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "build"],
"skipFiles": ["<node_internals>/**"],
"console": "integratedTerminal"
},
"request": "attach",
"restart": true,
"port": 9229
}
],
"compounds": [
{
"type": "node",
"request": "launch",
"name": "Run: Tests",
"env": {
"NODE_ENV": "development",
"LOG_LEVEL": "info"
},
"smartStep": true,
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "test"],
"skipFiles": ["<node_internals>/**"],
"console": "integratedTerminal"
"name": "Launch & Attach Zuplo",
"configurations": ["Launch Zuplo", "Zuplo Gateway"]
}
]
}
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

0 comments on commit 1875598

Please sign in to comment.