Skip to content

Commit abb6308

Browse files
Add comprehensive tests for EndpointBuilder functionality
- Implement tests for tags functionality, including setting, adding, and chaining tags. - Add tests for callbacks and links, ensuring callbacks can be added and response links are correctly configured. - Create tests for examples in parameters, request bodies, and responses, verifying example setting and retrieval. - Introduce tests for extensions and parameter styling, validating vendor extensions and parameter styles. - Enhance method chaining tests to ensure all new methods support fluent API style. - Add security tests to verify the addition and configuration of security requirements. - Implement advanced feature tests for EndpointBuilder, covering various functionalities and ensuring correct behavior. - Add tests for OpenAPI JSON and YAML serialization, validating output structure and content. - Include validation rule description tests to ensure proper handling of custom validation rules.
1 parent 754510f commit abb6308

35 files changed

Lines changed: 5574 additions & 2 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/coverage

deno.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"tasks": {
3+
"lint": "deno lint",
4+
"test": "deno test --allow-all",
5+
"test:coverage-html": "deno test --allow-all --coverage=coverage && deno coverage coverage",
6+
"fmt": "deno fmt"
7+
},
8+
"imports": {
9+
"@eemeli/yaml": "jsr:@eemeli/yaml@^2.7.1"
10+
}
11+
}

deno.lock

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# OpenAPI Documentation
2+
3+
Welcome to the documentation for the OpenAPI library. This package provides a developer-friendly way to define OpenAPI specifications for your APIs.
4+
5+
## Table of Contents
6+
7+
- [Getting Started](./getting-started.md)
8+
- [EndpointBuilder](./endpoint-builder.md)
9+
- [OpenAPI Schema](./openapi-schema.md)
10+
- [Examples](./examples.md)
11+
12+
## Overview
13+
14+
The OpenAPI library helps you define your API specifications in a structured way following the OpenAPI 3.1 standard. It provides:
15+
16+
- A fluent API for creating endpoints and their documentation
17+
- Type-safe schema definitions for request and response objects
18+
- Tools for serialization to JSON and YAML formats
19+
- Validation rule handling and documentation
20+
21+
## Basic Usage
22+
23+
Here's a basic example of creating an OpenAPI document:
24+
25+
```typescript
26+
import { OpenAPI, EndpointBuilder } from "@murat/openapi";
27+
28+
// Create a new OpenAPI document
29+
const api = new OpenAPI({
30+
title: "My API",
31+
version: "1.0.0",
32+
description: "A sample API for demonstration purposes",
33+
});
34+
35+
// Create an endpoint
36+
const getUserEndpoint = new EndpointBuilder({
37+
method: "get",
38+
title: "Get User",
39+
})
40+
.setDescription("Retrieves user information by ID")
41+
.addPathParameter("id", { type: "string" }, "User ID")
42+
.addJsonResponse(200, "User retrieved successfully", {
43+
type: "object",
44+
properties: {
45+
id: { type: "string" },
46+
name: { type: "string" },
47+
email: { type: "string", format: "email" },
48+
},
49+
});
50+
51+
// Add the endpoint to the API document
52+
api.addNewEndpoint_("/users/{id}", getUserEndpoint);
53+
54+
// Export as JSON or YAML
55+
const jsonString = api.getJSONString();
56+
const yamlString = api.getYAMLString();
57+
```
58+
59+
For more detailed instructions, check out the [Getting Started](./getting-started.md) guide.

docs/components.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# OpenAPI Components
2+
3+
Components are a powerful feature in OpenAPI 3.1 that allow you to define reusable objects in your API specification. The Components Object holds various schemas that can be referenced throughout the specification.
4+
5+
## Available Component Types
6+
7+
Yelix OpenAPI supports all component types from the OpenAPI 3.1 specification:
8+
9+
- **Schemas**: Reusable schema definitions
10+
- **Responses**: Reusable response objects
11+
- **Parameters**: Reusable parameter objects
12+
- **Examples**: Reusable examples
13+
- **RequestBodies**: Reusable request body objects
14+
- **Headers**: Reusable header objects
15+
- **SecuritySchemes**: Authentication and authorization schemes
16+
- **Links**: Reusable links between operations
17+
- **Callbacks**: Reusable callback definitions
18+
- **PathItems**: Reusable path item objects
19+
20+
## Adding Components
21+
22+
All component methods return a reference object that can be used in other parts of your API specification.
23+
24+
### Adding Schemas
25+
26+
```typescript
27+
import { OpenAPI } from "yelix/openapi";
28+
29+
const openapi = new OpenAPI({
30+
title: "My API",
31+
version: "1.0.0"
32+
});
33+
34+
// Define a reusable schema
35+
const userSchemaRef = openapi.addSchema("User", {
36+
type: "object",
37+
properties: {
38+
id: { type: "integer" },
39+
name: { type: "string" },
40+
email: { type: "string", format: "email" }
41+
},
42+
required: ["id", "name", "email"]
43+
});
44+
45+
// Now you can reference this schema in your endpoints
46+
// using the returned reference object
47+
console.log(userSchemaRef); // { $ref: "#/components/schemas/User" }
48+
```
49+
50+
### Adding Responses
51+
52+
```typescript
53+
// Define a reusable error response
54+
const errorResponseRef = openapi.addResponse("Error", {
55+
description: "An error occurred",
56+
content: {
57+
"application/json": {
58+
schema: {
59+
type: "object",
60+
properties: {
61+
code: { type: "integer" },
62+
message: { type: "string" }
63+
},
64+
required: ["code", "message"]
65+
}
66+
}
67+
}
68+
});
69+
```
70+
71+
### Adding Parameters
72+
73+
```typescript
74+
// Define a reusable parameter
75+
const limitParamRef = openapi.addParameter("limit", {
76+
name: "limit",
77+
in: "query",
78+
description: "Maximum number of items to return",
79+
required: false,
80+
schema: {
81+
type: "integer",
82+
minimum: 1,
83+
maximum: 100,
84+
default: 20
85+
}
86+
});
87+
```
88+
89+
### Adding Security Schemes
90+
91+
```typescript
92+
// Define an API key security scheme
93+
const apiKeyRef = openapi.addSecurityScheme("ApiKey", {
94+
type: "apiKey",
95+
name: "X-API-KEY",
96+
in: "header",
97+
description: "API key authentication"
98+
});
99+
100+
// Define OAuth2 security scheme
101+
const oauth2Ref = openapi.addSecurityScheme("OAuth2", {
102+
type: "oauth2",
103+
flows: {
104+
implicit: {
105+
authorizationUrl: "https://example.com/oauth/authorize",
106+
tokenUrl: "https://example.com/oauth/token",
107+
scopes: {
108+
"read:items": "Read items",
109+
"write:items": "Write items"
110+
}
111+
}
112+
}
113+
});
114+
```
115+
116+
## Using Components with EndpointBuilder
117+
118+
You can use the returned reference objects directly in your operations with EndpointBuilder:
119+
120+
```typescript
121+
import { EndpointBuilder } from "yelix/openapi";
122+
123+
// Create an endpoint builder
124+
const getUserEndpoint = new EndpointBuilder()
125+
.setOperation("get")
126+
.setTags(["users"])
127+
.setSummary("Get user by ID")
128+
.setDescription("Returns a user based on the provided ID")
129+
.addParameter({
130+
name: "id",
131+
in: "path",
132+
required: true,
133+
schema: {
134+
type: "integer"
135+
}
136+
})
137+
.addResponse("200", {
138+
description: "User found",
139+
content: {
140+
"application/json": {
141+
schema: userSchemaRef // Using the reference from addSchema
142+
}
143+
}
144+
})
145+
.addResponse("404", errorResponseRef); // Using the reference from addResponse
146+
147+
openapi.addNewEndpoint_("/users/{id}", getUserEndpoint);
148+
```
149+
150+
## Looking Up Components by Reference
151+
152+
You can retrieve a component using its reference string:
153+
154+
```typescript
155+
const userSchema = openapi.getComponentByRef("#/components/schemas/User");
156+
console.log(userSchema);
157+
```
158+
159+
## Best Practices
160+
161+
1. **Use Components for Common Objects**: Define schemas, responses, and parameters that are used in multiple endpoints as components.
162+
2. **Consistent Naming**: Use a consistent naming convention for your components.
163+
3. **Schema Reuse**: Break down complex schemas into smaller, reusable components.
164+
```

0 commit comments

Comments
 (0)