From 4e073765279db515ab1a32bd39a60f33931e3bb6 Mon Sep 17 00:00:00 2001 From: Josh Twist Date: Sun, 3 Nov 2024 19:14:12 +0000 Subject: [PATCH] updated to use a plain OAS json doc --- tests/mock-server.test.ts | 2 +- tests/pizza.oas.json | 390 ++++++++++++++++++++++++++++++++++++ tests/pizza.oas.ts | 403 -------------------------------------- www/pages/index.tsx | 6 +- 4 files changed, 396 insertions(+), 405 deletions(-) create mode 100644 tests/pizza.oas.json delete mode 100644 tests/pizza.oas.ts diff --git a/tests/mock-server.test.ts b/tests/mock-server.test.ts index 709be2b..e3121bc 100644 --- a/tests/mock-server.test.ts +++ b/tests/mock-server.test.ts @@ -1,6 +1,6 @@ import { MockServer } from "../modules/mock-server"; // Adjust the path as needed import { describe, test, assert } from "vitest"; -import { json } from "./pizza.oas"; +import json from "./pizza.oas.json"; // OpenAPI document const mockServer = new MockServer(json); diff --git a/tests/pizza.oas.json b/tests/pizza.oas.json new file mode 100644 index 0000000..1f8d742 --- /dev/null +++ b/tests/pizza.oas.json @@ -0,0 +1,390 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "Pizza Ordering API", + "description": "An API to order pizzas, check order status, and view menu options", + "version": "1.0.0" + }, + "paths": { + "/pizza/order": { + "post": { + "summary": "Place a pizza order", + "operationId": "placeOrder", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/OrderRequest" + }, + "examples": { + "orderExample": { + "summary": "Example order", + "value": { + "customerName": "John Doe", + "pizzaType": "Margherita", + "size": "Large", + "toppings": ["Mushrooms", "Olives"], + "deliveryAddress": "123 Pizza St, Pizzatown" + } + } + } + } + } + }, + "responses": { + "201": { + "description": "Order successfully placed", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/OrderResponse" + }, + "examples": { + "orderResponseExample": { + "summary": "Order Response Example", + "value": { + "orderId": "abc123", + "estimatedDeliveryTime": "45 minutes" + } + } + } + } + } + } + } + } + }, + "/pizza/order/{orderId}": { + "get": { + "summary": "Check the status of an existing order", + "operationId": "checkOrderStatus", + "parameters": [ + { + "name": "orderId", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "filter", + "in": "query", + "required": true, + "schema": { + "type": "integer" + } + }, + { + "name": "banana", + "in": "header", + "required": true, + "schema": { + "type": "number" + } + } + ], + "responses": { + "200": { + "description": "Order status retrieved", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/OrderStatusResponse" + }, + "examples": { + "statusExample": { + "summary": "Status example", + "value": { + "orderId": "abc123", + "status": "In the oven", + "estimatedDeliveryTime": "20 minutes" + } + } + } + } + } + }, + "404": { + "description": "Order not found" + } + } + } + }, + "/pizza/menu": { + "get": { + "summary": "Get the available pizza menu", + "operationId": "getMenu", + "responses": { + "200": { + "description": "Menu retrieved", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/MenuResponse" + }, + "examples": { + "menuExample": { + "summary": "Menu example", + "value": { + "pizzas": [ + { + "name": "Margherita", + "sizes": ["Small", "Medium", "Large"], + "toppings": ["Cheese", "Tomato"] + }, + { + "name": "Pepperoni", + "sizes": ["Small", "Medium", "Large"], + "toppings": ["Pepperoni", "Cheese", "Tomato"] + } + ] + } + } + } + } + } + } + } + } + }, + "/v1/todos": { + "x-zuplo-path": { + "pathMode": "open-api" + }, + "get": { + "summary": "Get all todos", + "description": "Lorem ipsum dolor sit amet, **consectetur adipiscing** elit, sed do `eiusmod tempor` incididunt ut labore et dolore magna aliqua.", + "x-zuplo-route": { + "corsPolicy": "none", + "handler": { + "export": "urlRewriteHandler", + "module": "$import(@zuplo/runtime)", + "options": { + "rewritePattern": "https://jsonplaceholder.typicode.com/todos" + } + }, + "policies": { + "inbound": ["api-key-inbound", "rate-limit-inbound"] + } + }, + "operationId": "e48db9e6-b0df-444a-9059-08a5c81cc8eb" + } + }, + "/no-mock-available": { + "get": { + "summary": "Information about the pizza API", + "operationId": "noMockAvailable", + "responses": { + "200": { + "description": "Information retrieved", + "content": {} + } + } + } + }, + "/foo": { + "get": { + "summary": "Retrieve the foo structure", + "operationId": "getFoo", + "responses": { + "200": { + "description": "Foo structure retrieved", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "bar": { + "type": "object", + "properties": { + "wibble": { + "type": "object", + "properties": { + "foo": { + "type": "array", + "items": { + "type": "integer" + } + } + } + } + } + } + } + } + } + } + } + } + } + }, + "/xml": { + "get": { + "summary": "Get XML response", + "operationId": "getXmlResponse", + "responses": { + "200": { + "description": "XML response", + "content": { + "application/xml": { + "examples": { + "xmlExample": { + "summary": "XML Example", + "value": "\n User\n API\n Reminder\n This is an XML response example.\n" + } + } + } + } + } + } + } + }, + "/text": { + "get": { + "summary": "Get plain text response", + "operationId": "getTextResponse", + "responses": { + "200": { + "description": "Plain text response", + "content": { + "text/plain": { + "examples": { + "textExample": { + "summary": "Text Example", + "value": "This is a plain text response example." + } + } + } + } + } + } + } + }, + "/multi-type": { + "get": { + "summary": "Get response with multiple content types", + "operationId": "getMultiTypeResponse", + "responses": { + "200": { + "description": "Response with multiple content types", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "message": { + "type": "string" + } + } + }, + "examples": { + "jsonExample": { + "summary": "JSON Example", + "value": { + "message": "This is a JSON response example." + } + } + } + }, + "application/xml": { + "examples": { + "xmlExample": { + "summary": "XML Example", + "value": "\n This is an XML response example.\n" + } + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "OrderRequest": { + "type": "object", + "properties": { + "customerName": { + "type": "string" + }, + "pizzaType": { + "type": "string" + }, + "size": { + "type": "string", + "enum": ["Small", "Medium", "Large"] + }, + "toppings": { + "type": "array", + "items": { + "type": "string" + } + }, + "deliveryAddress": { + "$ref": "#/components/schemas/Address" + } + }, + "required": ["customerName", "pizzaType", "size", "deliveryAddress"] + }, + "Address": { + "type": "string" + }, + "OrderResponse": { + "type": "object", + "properties": { + "orderId": { + "type": "string" + }, + "estimatedDeliveryTime": { + "type": "string" + } + } + }, + "OrderStatusResponse": { + "type": "object", + "properties": { + "orderId": { + "type": "string" + }, + "status": { + "type": "string" + }, + "estimatedDeliveryTime": { + "type": "string" + } + } + }, + "MenuResponse": { + "type": "object", + "properties": { + "pizzas": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "sizes": { + "type": "array", + "items": { + "type": "string" + } + }, + "toppings": { + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + } + } + } +} diff --git a/tests/pizza.oas.ts b/tests/pizza.oas.ts deleted file mode 100644 index 3fb4472..0000000 --- a/tests/pizza.oas.ts +++ /dev/null @@ -1,403 +0,0 @@ -export const json = { - openapi: "3.0.0", - info: { - title: "Pizza Ordering API", - description: - "An API to order pizzas, check order status, and view menu options", - version: "1.0.0", - }, - paths: { - "/pizza/order": { - post: { - summary: "Place a pizza order", - operationId: "placeOrder", - requestBody: { - required: true, - content: { - "application/json": { - schema: { - $ref: "#/components/schemas/OrderRequest", - }, - examples: { - orderExample: { - summary: "Example order", - value: { - customerName: "John Doe", - pizzaType: "Margherita", - size: "Large", - toppings: ["Mushrooms", "Olives"], - deliveryAddress: "123 Pizza St, Pizzatown", - }, - }, - }, - }, - }, - }, - responses: { - "201": { - description: "Order successfully placed", - content: { - "application/json": { - schema: { - $ref: "#/components/schemas/OrderResponse", - }, - examples: { - orderResponseExample: { - summary: "Order Response Example", - value: { - orderId: "abc123", - estimatedDeliveryTime: "45 minutes", - }, - }, - }, - }, - }, - }, - }, - }, - }, - "/pizza/order/{orderId}": { - get: { - summary: "Check the status of an existing order", - operationId: "checkOrderStatus", - parameters: [ - { - name: "orderId", - in: "path", - required: true, - schema: { - type: "string", - }, - }, - { - name: "filter", - in: "query", - required: true, - schema: { - type: "integer", - }, - }, - { - name: "banana", - in: "header", - required: true, - schema: { - type: "number", - }, - }, - ], - responses: { - "200": { - description: "Order status retrieved", - content: { - "application/json": { - schema: { - $ref: "#/components/schemas/OrderStatusResponse", - }, - examples: { - statusExample: { - summary: "Status example", - value: { - orderId: "abc123", - status: "In the oven", - estimatedDeliveryTime: "20 minutes", - }, - }, - }, - }, - }, - }, - "404": { - description: "Order not found", - }, - }, - }, - }, - "/pizza/menu": { - get: { - summary: "Get the available pizza menu", - operationId: "getMenu", - responses: { - "200": { - description: "Menu retrieved", - content: { - "application/json": { - schema: { - $ref: "#/components/schemas/MenuResponse", - }, - examples: { - menuExample: { - summary: "Menu example", - value: { - pizzas: [ - { - name: "Margherita", - sizes: ["Small", "Medium", "Large"], - toppings: ["Cheese", "Tomato"], - }, - { - name: "Pepperoni", - sizes: ["Small", "Medium", "Large"], - toppings: ["Pepperoni", "Cheese", "Tomato"], - }, - ], - }, - }, - }, - }, - }, - }, - }, - }, - }, - "/v1/todos": { - "x-zuplo-path": { - pathMode: "open-api", - }, - get: { - summary: "Get all todos", - description: - "Lorem ipsum dolor sit amet, **consectetur adipiscing** elit, sed do `eiusmod tempor` incididunt ut labore et dolore magna aliqua.", - "x-zuplo-route": { - corsPolicy: "none", - handler: { - export: "urlRewriteHandler", - module: "$import(@zuplo/runtime)", - options: { - rewritePattern: "https://jsonplaceholder.typicode.com/todos", - }, - }, - policies: { - inbound: ["api-key-inbound", "rate-limit-inbound"], - }, - }, - operationId: "e48db9e6-b0df-444a-9059-08a5c81cc8eb", - }, - }, - "/no-mock-available": { - get: { - summary: "Information about the pizza API", - operationId: "noMockAvailable", - responses: { - "200": { - description: "Information retrieved", - content: {}, - }, - }, - }, - }, - "/foo": { - get: { - summary: "Retrieve the foo structure", - operationId: "getFoo", - responses: { - "200": { - description: "Foo structure retrieved", - content: { - "application/json": { - schema: { - type: "object", - properties: { - bar: { - type: "object", - properties: { - wibble: { - type: "object", - properties: { - foo: { - type: "array", - items: { - type: "integer", - }, - }, - }, - }, - }, - }, - }, - }, - }, - }, - }, - }, - }, - }, - "/xml": { - get: { - summary: "Get XML response", - operationId: "getXmlResponse", - responses: { - "200": { - description: "XML response", - content: { - "application/xml": { - examples: { - xmlExample: { - summary: "XML Example", - value: ` - - User - API - Reminder - This is an XML response example. - - `, - }, - }, - }, - }, - }, - }, - }, - }, - "/text": { - get: { - summary: "Get plain text response", - operationId: "getTextResponse", - responses: { - "200": { - description: "Plain text response", - content: { - "text/plain": { - examples: { - textExample: { - summary: "Text Example", - value: "This is a plain text response example.", - }, - }, - }, - }, - }, - }, - }, - }, - "/multi-type": { - get: { - summary: "Get response with multiple content types", - operationId: "getMultiTypeResponse", - responses: { - "200": { - description: "Response with multiple content types", - content: { - "application/json": { - schema: { - type: "object", - properties: { - message: { - type: "string", - }, - }, - }, - examples: { - jsonExample: { - summary: "JSON Example", - value: { - message: "This is a JSON response example.", - }, - }, - }, - }, - "application/xml": { - examples: { - xmlExample: { - summary: "XML Example", - value: ` - - This is an XML response example. - - `, - }, - }, - }, - }, - }, - }, - }, - }, - }, - components: { - schemas: { - OrderRequest: { - type: "object", - properties: { - customerName: { - type: "string", - }, - pizzaType: { - type: "string", - }, - size: { - type: "string", - enum: ["Small", "Medium", "Large"], - }, - toppings: { - type: "array", - items: { - type: "string", - }, - }, - deliveryAddress: { - $ref: "#/components/schemas/Address", - }, - }, - required: ["customerName", "pizzaType", "size", "deliveryAddress"], - }, - Address: { - type: "string", - }, - OrderResponse: { - type: "object", - properties: { - orderId: { - type: "string", - }, - estimatedDeliveryTime: { - type: "string", - }, - }, - }, - OrderStatusResponse: { - type: "object", - properties: { - orderId: { - type: "string", - }, - status: { - type: "string", - }, - estimatedDeliveryTime: { - type: "string", - }, - }, - }, - MenuResponse: { - type: "object", - properties: { - pizzas: { - type: "array", - items: { - type: "object", - properties: { - name: { - type: "string", - }, - sizes: { - type: "array", - items: { - type: "string", - }, - }, - toppings: { - type: "array", - items: { - type: "string", - }, - }, - }, - }, - }, - }, - }, - }, - }, -}; diff --git a/www/pages/index.tsx b/www/pages/index.tsx index 7acfab8..b9a0690 100644 --- a/www/pages/index.tsx +++ b/www/pages/index.tsx @@ -141,6 +141,7 @@ const Index = () => { if (response.status !== 201) { alert(`Error ${response.status}\n\n ${await response.text()}`); + setIsCreating(false); return; } @@ -148,7 +149,10 @@ const Index = () => { setIsCreating(false); router.push(`/bins/${result.id}`); updateRecentBins(result, recentBins); - } catch (error) {} + } catch (error: any) { + alert(`Error - ${error.message}`); + setIsCreating(false); + } setIsCreating(false); };